nova proba

Revision:
4:7abcf4543282
Parent:
2:45b351b4fc2a
--- a/dxml.cpp	Wed Apr 08 08:41:16 2020 +0000
+++ b/dxml.cpp	Sat Apr 11 17:11:38 2020 +0000
@@ -1,3 +1,6 @@
+
+// DXML.CPP  korigovan 11.04.2020  !!!
+
 #define dxml_NOMMAP
 
 #include <stdlib.h>
@@ -18,8 +21,8 @@
 
 char* strdup(const char* literal)
 {
-    auto l_len = strlen(literal);
-    auto n = new char[l_len + 1];
+    int l_len = strlen(literal);
+    char* n = (char*)malloc(l_len + 1);
     strncpy(n, literal, l_len + 1);
     return n;
 }
@@ -82,7 +85,7 @@
     int idx = -1;
 
     if (name && *name) {
-        idx = va_arg(ap, int);    
+        idx = va_arg(ap, int);
         xml = dxml_child(xml, name);
     }
     return (idx < 0) ? xml : dxml_vget(dxml_idx(xml, idx), ap);
@@ -90,7 +93,7 @@
 
 // Traverses the xml tree to retrieve a specific subtag. Takes a variable
 // length list of tag names and indexes. The argument list must be terminated
-// by either an index of -1 or an empty string tag name. Example: 
+// by either an index of -1 or an empty string tag name. Example:
 // title = dxml_get(library, "shelf", 0, "book", 2, "title", -1);
 // This retrieves the title of the 3rd book on the 1st shelf of library.
 // Returns NULL if not found.
@@ -124,7 +127,7 @@
     va_list ap;
     int line = 1;
     char *t, fmt[dxml_ERRL];
-    
+
     for (t = root->s; t < s; t++) if (*t == '\n') line++;
     snprintf(fmt, dxml_ERRL, "[error near line %d]: %s", line, err);
 
@@ -152,7 +155,7 @@
             if (*s == '\n') memmove(s, (s + 1), strlen(s));
         }
     }
-    
+
     for (s = r; ; ) {
         while (*s && *s != '&' && (*s != '%' || t != '%') && !isspace(*s)) s++;
 
@@ -207,7 +210,7 @@
 void dxml_open_tag(dxml_root_t root, char *name, char **attr)
 {
     dxml_t xml = root->cur;
-    
+
     if (xml->name) xml = dxml_add_child(xml, name, strlen(xml->txt));
     else xml->name = name; // first open tag
 
@@ -306,7 +309,7 @@
 {
     char q, *c, *t, *n = NULL, *v, **ent, **pe;
     int i, j;
-    
+
     pe = (char**)memcpy(malloc(sizeof(dxml_NIL)), dxml_NIL, sizeof(dxml_NIL));
 
     for (s[len] = '\0'; s; ) {
@@ -387,7 +390,7 @@
                 root->attr[i][j + 2] = c; // is it cdata?
                 root->attr[i][j + 1] = (v) ? dxml_decode(v, root->ent, *c)
                                            : NULL;
-                root->attr[i][j] = n; // attribute name 
+                root->attr[i][j] = n; // attribute name
             }
         }
         else if (! strncmp(s, "<!--", 4)) s = strstr(s + 4, "-->"); // comments
@@ -440,7 +443,7 @@
 void dxml_free_attr(char **attr) {
     int i = 0;
     char *m;
-    
+
     if (! attr || attr == dxml_NIL) return; // nothing to free
     while (attr[i]) i += 2; // find end of attribute list
     m = attr[i + 1]; // list of which names and values are malloced
@@ -463,7 +466,7 @@
     if (! len) return dxml_err(root, NULL, "root tag missing");
     root->u = dxml_str2utf8(&s, &len); // convert utf-16 to utf-8
     root->e = (root->s = s) + len; // record start and end of work area
-    
+
     e = s[len - 1]; // save end char
     s[len - 1] = '\0'; // turn end char into null terminator
 
@@ -473,14 +476,14 @@
     for (; ; ) {
         attr = (char **)dxml_NIL;
         d = ++s;
-        
+
         if (isalpha(*s) || *s == '_' || *s == ':' || *s < '\0') { // new tag
             if (! root->cur)
                 return dxml_err(root, d, "markup outside of root element");
 
             s += strcspn(s, dxml_WS "/>");
             while (isspace(*s)) *(s++) = '\0'; // null terminate tag name
-  
+
             if (*s && *s != '/' && *s != '>') // find tag in default attr list
                 for (i = 0; (a = root->attr[i]) && strcmp(a[0], d); i++);
 
@@ -495,7 +498,7 @@
                 attr[l] = s; // set attribute name
 
                 s += strcspn(s, dxml_WS "=/>");
-                if (*s == '=' || isspace(*s)) { 
+                if (*s == '=' || isspace(*s)) {
                     *(s++) = '\0'; // null terminate tag attribute name
                     q = *(s += strspn(s, dxml_WS "="));
                     if (q == '"' || q == '\'') { // attribute value
@@ -533,7 +536,7 @@
             }
             else {
                 if (l) dxml_free_attr(attr);
-                return dxml_err(root, d, "missing >"); 
+                return dxml_err(root, d, "missing >");
             }
         }
         else if (*s == '/') { // close tag
@@ -553,7 +556,7 @@
             else return dxml_err(root, d, "unclosed <![CDATA[");
         }
         else if (! strncmp(s, "!DOCTYPE", 8)) { // dtd
-            for (l = 0; *s && ((! l && *s != '>') || (l && (*s != ']' || 
+            for (l = 0; *s && ((! l && *s != '>') || (l && (*s != ']' ||
                  *(s + strspn(s + 1, dxml_WS) + 1) != '>')));
                  l = (*s == '[') ? 1 : l) s += strcspn(s + 1, "[]>") + 1;
             if (! *s && e != '>')
@@ -563,12 +566,12 @@
         }
         else if (*s == '?') { // <?...?> processing instructions
             do { s = strchr(s, '?'); } while (s && *(++s) && *s != '>');
-            if (! s || (! *s && e != '>')) 
+            if (! s || (! *s && e != '>'))
                 return dxml_err(root, d, "unclosed <?");
             else dxml_proc_inst(root, d + 1, s - d - 2);
         }
         else return dxml_err(root, d, "unexpected <");
-        
+
         if (! s || ! *s) break;
         *s = '\0';
         d = ++s;
@@ -644,7 +647,7 @@
                       size_t *max, short a)
 {
     const char *e;
-    
+
     for (e = s + len; s != e; s++) {
         while (*dlen + 10 > *max) *dst = (char*)realloc(*dst, *max += dxml_BUFSIZE);
 
@@ -705,7 +708,7 @@
 
     *s = (xml->child) ? dxml_toxml_r(xml->child, s, len, max, 0, attr) //child
                       : dxml_ampencode(xml->txt, -1, s, len, max, 0);  //data
-    
+
     while (*len + strlen(xml->name) + 4 > *max) // reallocate s
         *s = (char*)realloc(*s, *max += dxml_BUFSIZE);
 
@@ -783,7 +786,7 @@
             for (j = 1; root->pi[i][j]; j++);
             free(root->pi[i][j + 1]);
             free(root->pi[i]);
-        }            
+        }
         if (root->pi[0]) free(root->pi); // free processing instructions
 
         if (root->len == -1) free(root->m); // malloced xml data
@@ -811,7 +814,7 @@
 {
     static char *ent[] = { "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
                            "apos;", "&#39;", "amp;", "&#38;", NULL };
-    dxml_root_t root = (dxml_root_t)memset(malloc(sizeof(struct dxml_root)), 
+    dxml_root_t root = (dxml_root_t)memset(malloc(sizeof(struct dxml_root)),
                                              '\0', sizeof(struct dxml_root));
     root->xml.name = (char *)name;
     root->cur = &root->xml;
@@ -922,10 +925,12 @@
     if (value) xml->attr[l + 1] = (char *)value; // set attribute value
     else { // remove attribute
         if (xml->attr[c + 1][l / 2] & dxml_NAMEM) free(xml->attr[l]);
-        memmove(xml->attr + l, xml->attr + l + 2, (c - l + 2) * sizeof(char*));
+   //     memmove(xml->attr + l, xml->attr + l + 2, (c - l + 2) * sizeof(char*));
+        memmove(xml->attr + l, xml->attr + l + 2, (c - l) * sizeof(char*));  // promenio BL!!!
+        c -= 2; // dodao BL!!!
         xml->attr = (char**)realloc(xml->attr, (c + 2) * sizeof(char *));
         memmove(xml->attr[c + 1] + (l / 2), xml->attr[c + 1] + (l / 2) + 1,
-                (c / 2) - (l / 2)); // fix list of which name/vals are malloced
+         /*promenio BL!!! */     ((c + 2)/2) - (l / 2) /*(c / 2) - (l / 2)*/); // fix list of which name/vals are malloced
     }
     xml->flags &= ~dxml_DUP; // clear strdup() flag
     return xml;
@@ -966,7 +971,7 @@
 
             while (cur->next && cur->next != xml) cur = cur->next;
             if (cur->next) cur->next = cur->next->next; // patch next list
-        }        
+        }
     }
     xml->ordered = xml->sibling = xml->next = NULL;
     return xml;
@@ -989,3 +994,4 @@
     return (i) ? 1 : 0;
 }
 #endif // dxml_TEST
+