OSDN Git Service

* updated copyright.
[modchxj/mod_chxj.git] / src / qs_parse_tag.c
index 04e8b97..ca97748 100644 (file)
@@ -1,6 +1,6 @@
 /*
+ * Copyright (C) 2005-2011 Atsushi Konno All rights reserved.
  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
- * Copyright (C) 2005 Atsushi Konno All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "qs_parse_attr.h"
 #include "qs_parse_tag.h"
 
-static char* s_get_tag_name(Doc* doc, const char* s, int len) ; 
+static char *s_get_tag_name(Doc *doc, const char *s, int len) ; 
 
 
 
-Node*
-qs_parse_tag(Doc* doc, const char* s, int len) 
+Node *
+qs_parse_tag(Doc *doc, const char *s, int len) 
 {
-  Node*  node;
-  char*  tag_name;
-  char*  sp;
+  Node   *node;
+  char   *tag_name;
+  char   *sp;
+  char   *sv_s;
   int    ll;
   int    next_point;
 
-  sp         = (char*)s;
+  if (! doc) {
+    QX_LOGGER_FATAL("runtime exception: qs_parse_tag(): doc is null");
+    return NULL;
+  }
+
+  sv_s = sp = (char *)s;
   ll         = len;
   next_point = 0;
 
@@ -41,12 +47,15 @@ qs_parse_tag(Doc* doc, const char* s, int len)
 
 
   /* 
-   * s[0] == '<' && s[len-1] == '>' 
+   * s[0] == '<' && s[len] == '>' 
    */
   tag_name = (char *)s_get_tag_name(doc, ++s, --ll);
-  QX_LOGGER_DEBUG_INT("ll",ll);
 
-  node = (Node*)qs_new_tag(doc);
+  node = (Node *)qs_new_tag(doc);
+  if (! node) {
+    QX_LOGGER_DEBUG("runtime exception: qs_parse_tag(): Out of memory.");
+    return NULL;
+  }
   node->name = tag_name;
   node->otext = apr_palloc(doc->pool,len+2);
   memset(node->otext, 0, len+2);
@@ -58,7 +67,7 @@ qs_parse_tag(Doc* doc, const char* s, int len)
   QX_LOGGER_DEBUG_INT("ll",ll);
   sp += (strlen(tag_name)+1);
   for (;;) {
-    Attrattr = qs_parse_attr(doc,sp, ll, &next_point);
+    Attr *attr = qs_parse_attr(doc,sp, ll, &next_point);
     if (attr == NULL) {
       QX_LOGGER_DEBUG("End of QS_PARSE_ATTR()");
       break;
@@ -68,23 +77,30 @@ qs_parse_tag(Doc* doc, const char* s, int len)
     sp += next_point;
     ll -= next_point;
     QX_LOGGER_DEBUG_INT(sp, ll);
-    node = (Node*)qs_add_attr(doc,node, attr);
+    node = (Node *)qs_add_attr(doc,node, attr);
   }
 
+  if (sv_s[len-1] == '/') {
+    node->closed_by_itself = 1;
+  }
+  else {
+    node->closed_by_itself = 0;
+  }
   QX_LOGGER_DEBUG("end parse_tag()");
+
   return node;
 }
 
 
 
 
-static char* 
-s_get_tag_name(Doc* doc, const char* s, int len)  
+static char 
+s_get_tag_name(Doc *doc, const char *s, int len)  
 {
   int ii;
   int sp;
   int size;
-  charreturn_value = NULL;
+  char *return_value = NULL;
 
   /* ignore space. */
   for (ii = 0; ii < len; ii++) {
@@ -101,7 +117,7 @@ s_get_tag_name(Doc* doc, const char* s, int len)
 
   size = ii-sp;
 
-  return_value = (char*)apr_palloc(doc->pool, size+1);
+  return_value = (char *)apr_palloc(doc->pool, size+1);
 
   memset(return_value, 0, size+1);
   memcpy(return_value, &s[sp], size);
@@ -112,10 +128,21 @@ s_get_tag_name(Doc* doc, const char* s, int len)
 
 
 
-Node*
-qs_new_tag(Docdoc) 
+Node *
+qs_new_tag(Doc *doc) 
 {
-  Node* node      = (Node*)apr_palloc(doc->pool, sizeof(Node));
+  Node *node;
+
+  if (! doc) {
+    QX_LOGGER_FATAL("runtime exception: qs_new_tag(): doc is NULL");
+    return NULL;
+  }
+  if (! doc->pool) {
+    QX_LOGGER_FATAL("runtime exception: qs_new_tag(): doc->pool is NULL");
+    return NULL;
+  }
+
+  node = (Node *)apr_palloc(doc->pool, sizeof(Node));
   node->next      = NULL;
   node->parent    = NULL;
   node->child     = NULL;
@@ -131,23 +158,30 @@ qs_new_tag(Doc* doc)
 
 
 
-Node*
-qs_add_attr(Doc* doc, Node* node, Attr* attr) 
+Node *
+qs_add_attr(Doc *doc, Node *node, Attr *attr) 
 {
   if (node == NULL) {
-    QX_LOGGER_FATAL("qs_add_attr() node is null");
+    QX_LOGGER_FATAL("runtime exception: qs_add_attr(): node is null");
+    return NULL;
+  }
+  if (! attr) {
+    return node;
   }
 
   attr->parent = node;
   attr->next   = NULL;
+
   if (node->attr == NULL) {
     node->attr      = attr;
     node->attr_tail = attr;
+
     return node;
   }
 
   node->attr_tail->next = attr;
   node->attr_tail       = attr;
+
   return node;
 }
 /*