Ignore:
Timestamp:
Feb 24, 2012, 12:37:36 PM (12 years ago)
Author:
frichard
Message:

-Alignement des antennes
-Version 0.0.9 de libindi

File:
1 edited

Legend:

Unmodified
Added
Removed
  • BAORadio/libindi/libindi/libs/lilxml.c

    r490 r642  
    1515    You should have received a copy of the GNU Lesser General Public
    1616    License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1818
    1919#endif
     
    2727 */
    2828
     29#include <stdio.h>
    2930#include <stdlib.h>
    3031#include <string.h>
     
    4142#define MINMEM  64                      /* starting string length */
    4243
    43 static int oneXMLchar (LilXML *lp, int c, char errmsg[]);
     44static int oneXMLchar (LilXML *lp, int c, char ynot[]);
    4445static void initParser(LilXML *lp);
    4546static void pushXMLEle(LilXML *lp);
     
    111112static char entities[] = "&<>'\"";
    112113
    113 /* default memory managers, override with indi_xmlMalloc() */
     114/* default memory managers, override with lilxmlMalloc() */
    114115static void *(*mymalloc)(size_t size) = malloc;
    115116static void *(*myrealloc)(void *ptr, size_t size) = realloc;
     
    120121 */
    121122void
    122 indi_xmlMalloc (void *(*newmalloc)(size_t size),
    123            void *(*newrealloc)(void *ptr, size_t size),
    124            void (*newfree)(void *ptr))
    125 {
    126         mymalloc = newmalloc;
    127         myrealloc = newrealloc;
    128         myfree = newfree;
     123lilxmlMalloc (void *(*newmalloc)(size_t size),
     124           void *(*newrealloc)(void *ptr, size_t size),
     125           void (*newfree)(void *ptr))
     126{
     127        mymalloc = newmalloc;
     128        myrealloc = newrealloc;
     129        myfree = newfree;
    129130}
    130131
     
    133134newLilXML ()
    134135{
    135         LilXML *lp = (LilXML *) moremem (NULL, sizeof(LilXML));
    136         memset (lp, 0, sizeof(LilXML));
    137         initParser(lp);
    138         return (lp);
     136        LilXML *lp = (LilXML *) moremem (NULL, sizeof(LilXML));
     137        memset (lp, 0, sizeof(LilXML));
     138        initParser(lp);
     139        return (lp);
    139140}
    140141
     
    143144delLilXML (LilXML *lp)
    144145{
    145         delXMLEle (lp->ce);
    146         freeString (&lp->endtag);
    147         (*myfree) (lp);
     146        delXMLEle (lp->ce);
     147        freeString (&lp->endtag);
     148        (*myfree) (lp);
    148149}
    149150
     
    152153delXMLEle (XMLEle *ep)
    153154{
    154         int i;
    155 
    156         /* benign if NULL */
    157         if (!ep)
    158             return;
    159 
    160         /* delete all parts of ep */
    161         freeString (&ep->tag);
    162         freeString (&ep->pcdata);
    163         if (ep->at) {
    164             for (i = 0; i < ep->nat; i++)
    165                 freeAtt (ep->at[i]);
    166             (*myfree) (ep->at);
    167         }
    168         if (ep->el) {
    169             for (i = 0; i < ep->nel; i++) {
    170                 /* forget parent so deleting doesn't modify _this_ el[] */
    171                 ep->el[i]->pe = NULL;
    172 
    173                 delXMLEle (ep->el[i]);
    174             }
    175             (*myfree) (ep->el);
    176         }
    177 
    178         /* remove from parent's list if known */
    179         if (ep->pe) {
    180             XMLEle *pe = ep->pe;
    181             for (i = 0; i < pe->nel; i++) {
    182                 if (pe->el[i] == ep) {
    183                     memmove (&pe->el[i], &pe->el[i+1],
    184                                               (--pe->nel-i)*sizeof(XMLEle*));
    185                     break;
    186                 }
    187             }
    188         }
    189 
    190         /* delete ep itself */
    191         (*myfree) (ep);
     155        int i;
     156
     157        /* benign if NULL */
     158        if (!ep)
     159            return;
     160
     161        /* delete all parts of ep */
     162        freeString (&ep->tag);
     163        freeString (&ep->pcdata);
     164        if (ep->at) {
     165            for (i = 0; i < ep->nat; i++)
     166                freeAtt (ep->at[i]);
     167            (*myfree) (ep->at);
     168        }
     169        if (ep->el) {
     170            for (i = 0; i < ep->nel; i++) {
     171                /* forget parent so deleting doesn't modify _this_ el[] */
     172                ep->el[i]->pe = NULL;
     173
     174                delXMLEle (ep->el[i]);
     175            }
     176            (*myfree) (ep->el);
     177        }
     178
     179        /* remove from parent's list if known */
     180        if (ep->pe) {
     181            XMLEle *pe = ep->pe;
     182            for (i = 0; i < pe->nel; i++) {
     183                if (pe->el[i] == ep) {
     184                    memmove (&pe->el[i], &pe->el[i+1],
     185                                              (--pe->nel-i)*sizeof(XMLEle*));
     186                    break;
     187                }
     188            }
     189        }
     190
     191        /* delete ep itself */
     192        (*myfree) (ep);
    192193}
    193194
    194195/* process one more character of an XML file.
    195196 * when find closure with outter element return root of complete tree.
    196  * when find error return NULL with reason in errmsg[].
    197  * when need more return NULL with errmsg[0] = '\0'.
     197 * when find error return NULL with reason in ynot[].
     198 * when need more return NULL with ynot[0] = '\0'.
    198199 * N.B. it is up to the caller to delete any tree returned with delXMLEle().
    199200 */
    200201XMLEle *
    201 readXMLEle (LilXML *lp, int newc, char errmsg[])
    202 {
    203         XMLEle *root;
    204         int s;
    205 
    206         /* start optimistic */
    207         errmsg[0] = '\0';
    208 
    209         /* EOF? */
    210         if (newc == 0) {
    211             sprintf (errmsg, "Line %d: early XML EOF", lp->ln);
    212             initParser(lp);
    213             return (NULL);
    214         }
    215 
    216         /* new line? */
    217         if (newc == '\n')
    218             lp->ln++;
    219 
    220         /* skip comments and declarations. requires 1 char history */
    221         if (!lp->skipping && lp->lastc == '<' && (newc == '?' || newc == '!')) {
    222             lp->skipping = 1;
    223             lp->lastc = newc;
    224             return (NULL);
    225         }
    226         if (lp->skipping) {
    227             if (newc == '>')
    228                 lp->skipping = 0;
    229             lp->lastc = newc;
    230             return (NULL);
    231         }
    232         if (newc == '<') {
    233             lp->lastc = '<';
    234             return (NULL);
    235         }
    236 
    237         /* do a pending '<' first then newc */
    238         if (lp->lastc == '<') {
    239             if (oneXMLchar (lp, '<', errmsg) < 0) {
    240                 initParser(lp);
    241                 return (NULL);
    242             }
    243             /* N.B. we assume '<' will never result in closure */
    244         }
    245 
    246         /* process newc (at last!) */
    247         s = oneXMLchar (lp, newc, errmsg);
    248         if (s == 0) {
    249             lp->lastc = newc;
    250             return (NULL);
    251         }
    252         if (s < 0) {
    253             initParser(lp);
    254             return (NULL);
    255         }
    256 
    257         /* Ok! return ce and we start over.
    258          * N.B. up to caller to call delXMLEle with what we return.
    259          */
    260         root = lp->ce;
    261         lp->ce = NULL;
    262         initParser(lp);
    263         return (root);
     202readXMLEle (LilXML *lp, int newc, char ynot[])
     203{
     204        XMLEle *root;
     205        int s;
     206
     207        /* start optimistic */
     208        ynot[0] = '\0';
     209
     210        /* EOF? */
     211        if (newc == 0) {
     212            sprintf (ynot, "Line %d: early XML EOF", lp->ln);
     213            initParser(lp);
     214            return (NULL);
     215        }
     216
     217        /* new line? */
     218        if (newc == '\n')
     219            lp->ln++;
     220
     221        /* skip comments and declarations. requires 1 char history */
     222        if (!lp->skipping && lp->lastc == '<' && (newc == '?' || newc == '!')) {
     223            lp->skipping = 1;
     224            lp->lastc = newc;
     225            return (NULL);
     226        }
     227        if (lp->skipping) {
     228            if (newc == '>')
     229                lp->skipping = 0;
     230            lp->lastc = newc;
     231            return (NULL);
     232        }
     233        if (newc == '<') {
     234            lp->lastc = '<';
     235            return (NULL);
     236        }
     237
     238        /* do a pending '<' first then newc */
     239        if (lp->lastc == '<') {
     240            if (oneXMLchar (lp, '<', ynot) < 0) {
     241                initParser(lp);
     242                return (NULL);
     243            }
     244            /* N.B. we assume '<' will never result in closure */
     245        }
     246
     247        /* process newc (at last!) */
     248        s = oneXMLchar (lp, newc, ynot);
     249        if (s == 0) {
     250            lp->lastc = newc;
     251            return (NULL);
     252        }
     253        if (s < 0) {
     254            initParser(lp);
     255            return (NULL);
     256        }
     257
     258        /* Ok! return ce and we start over.
     259         * N.B. up to caller to call delXMLEle with what we return.
     260         */
     261        root = lp->ce;
     262        lp->ce = NULL;
     263        initParser(lp);
     264        return (root);
     265}
     266
     267/* parse the given XML string.
     268 * return XMLEle* else NULL with reason why in ynot[]
     269 */
     270XMLEle *
     271parseXML (char buf[], char ynot[])
     272{
     273        LilXML *lp = newLilXML();
     274        XMLEle *root;
     275
     276        do {
     277            root = readXMLEle (lp, *buf++, ynot);
     278        } while (!root && !ynot[0]);
     279
     280        delLilXML (lp);
     281
     282        return (root);
     283}
     284
     285/* return a deep copy of the given XMLEle *
     286 */
     287XMLEle *
     288cloneXMLEle (XMLEle *ep)
     289{
     290        char *buf;
     291        char ynot[1024];
     292        XMLEle *newep;
     293
     294        buf = (*mymalloc) (sprlXMLEle (ep, 0) + 1);
     295        sprXMLEle (buf, ep, 0);
     296        newep = parseXML (buf, ynot);
     297        (*myfree) (buf);
     298
     299        return (newep);
    264300}
    265301
     
    270306findXMLAtt (XMLEle *ep, const char *name)
    271307{
    272         int i;
    273 
    274         for (i = 0; i < ep->nat; i++)
    275             if (!strcmp (ep->at[i]->name.s, name))
    276                 return (ep->at[i]);
    277         return (NULL);
     308        int i;
     309
     310        for (i = 0; i < ep->nat; i++)
     311            if (!strcmp (ep->at[i]->name.s, name))
     312                return (ep->at[i]);
     313        return (NULL);
    278314}
    279315
     
    284320findXMLEle (XMLEle *ep, const char *tag)
    285321{
    286         int tl = strlen (tag);
    287         int i;
    288 
    289         for (i = 0; i < ep->nel; i++) {
    290             String *sp = &ep->el[i]->tag;
    291             if (sp->sl == tl && !strcmp (sp->s, tag))
    292                 return (ep->el[i]);
    293         }
    294         return (NULL);
     322        int tl = strlen (tag);
     323        int i;
     324
     325        for (i = 0; i < ep->nel; i++) {
     326            String *sp = &ep->el[i]->tag;
     327            if (sp->sl == tl && !strcmp (sp->s, tag))
     328                return (ep->el[i]);
     329        }
     330        return (NULL);
    295331}
    296332
     
    302338nextXMLEle (XMLEle *ep, int init)
    303339{
    304         int eit;
    305        
    306         if (init)
    307             ep->eit = 0;
    308 
    309         eit = ep->eit++;
    310         if (eit < 0 || eit >= ep->nel)
    311             return (NULL);
    312         return (ep->el[eit]);
     340        int eit;
     341
     342        if (init)
     343            ep->eit = 0;
     344
     345        eit = ep->eit++;
     346        if (eit < 0 || eit >= ep->nel)
     347            return (NULL);
     348        return (ep->el[eit]);
    313349}
    314350
     
    320356nextXMLAtt (XMLEle *ep, int init)
    321357{
    322         int ait;
    323 
    324         if (init)
    325             ep->ait = 0;
    326 
    327         ait = ep->ait++;
    328         if (ait < 0 || ait >= ep->nat)
    329             return (NULL);
    330         return (ep->at[ait]);
     358        int ait;
     359
     360        if (init)
     361            ep->ait = 0;
     362
     363        ait = ep->ait++;
     364        if (ait < 0 || ait >= ep->nat)
     365            return (NULL);
     366        return (ep->at[ait]);
    331367}
    332368
     
    335371parentXMLEle (XMLEle *ep)
    336372{
    337         return (ep->pe);
     373        return (ep->pe);
    338374}
    339375
     
    342378parentXMLAtt (XMLAtt *ap)
    343379{
    344         return (ap->ce);
     380        return (ap->ce);
    345381}
    346382
     
    351387tagXMLEle (XMLEle *ep)
    352388{
    353         return (ep->tag.s);
     389        return (ep->tag.s);
    354390}
    355391
     
    358394pcdataXMLEle (XMLEle *ep)
    359395{
    360         return (ep->pcdata.s);
     396        return (ep->pcdata.s);
    361397}
    362398
    363399/* return the number of characters in the pcdata portion of the given element */
    364 int 
     400int
    365401pcdatalenXMLEle (XMLEle *ep)
    366402{
    367         return (ep->pcdata.sl);
     403        return (ep->pcdata.sl);
    368404}
    369405
     
    372408nameXMLAtt (XMLAtt *ap)
    373409{
    374         return (ap->name.s);
     410        return (ap->name.s);
    375411}
    376412
     
    379415valuXMLAtt (XMLAtt *ap)
    380416{
    381         return (ap->valu.s);
     417        return (ap->valu.s);
    382418}
    383419
     
    386422nXMLEle (XMLEle *ep)
    387423{
    388         return (ep->nel);
     424        return (ep->nel);
    389425}
    390426
     
    393429nXMLAtt (XMLEle *ep)
    394430{
    395         return (ep->nat);
     431        return (ep->nat);
    396432}
    397433
     
    403439findXMLAttValu (XMLEle *ep, const char *name)
    404440{
    405         XMLAtt *a = findXMLAtt (ep, name);
    406         return (a ? a->valu.s : "");
     441        XMLAtt *a = findXMLAtt (ep, name);
     442        return (a ? a->valu.s : "");
    407443}
    408444
    409445/* handy wrapper to read one xml file.
    410  * return root element else NULL with report in errmsg[]
     446 * return root element else NULL with report in ynot[]
    411447 */
    412448XMLEle *
    413 readXMLFile (FILE *fp, LilXML *lp, char errmsg[])
    414 {
    415         int c;
    416 
    417         while ((c = fgetc(fp)) != EOF) {
    418             XMLEle *root = readXMLEle (lp, c, errmsg);
    419             if (root || errmsg[0])
    420                 return (root);
    421         }
    422 
    423         return (NULL);
     449readXMLFile (FILE *fp, LilXML *lp, char ynot[])
     450{
     451        int c;
     452
     453        while ((c = fgetc(fp)) != EOF) {
     454            XMLEle *root = readXMLEle (lp, c, ynot);
     455            if (root || ynot[0])
     456                return (root);
     457        }
     458
     459        return (NULL);
    424460}
    425461
     
    430466addXMLEle (XMLEle *parent, const char *tag)
    431467{
    432         XMLEle *ep = growEle (parent);
    433         appendString (&ep->tag, tag);
    434         return (ep);
     468        XMLEle *ep = growEle (parent);
     469        appendString (&ep->tag, tag);
     470        return (ep);
     471}
     472
     473/* append an existing element to the given element.
     474 * N.B. be mindful of when these are deleted, this is not a deep copy.
     475 */
     476void
     477appXMLEle (XMLEle *ep, XMLEle *newep)
     478{
     479        ep->el = (XMLEle **) moremem (ep->el, (ep->nel+1)*sizeof(XMLEle *));
     480        ep->el[ep->nel++] = newep;
    435481}
    436482
     
    439485editXMLEle (XMLEle *ep, const char *pcdata)
    440486{
    441         freeString (&ep->pcdata);
    442         appendString (&ep->pcdata, pcdata);
    443         ep->pcdata_hasent = (strpbrk (pcdata, entities) != NULL);
     487        freeString (&ep->pcdata);
     488        appendString (&ep->pcdata, pcdata);
     489        ep->pcdata_hasent = (strpbrk (pcdata, entities) != NULL);
    444490}
    445491
     
    448494addXMLAtt (XMLEle *ep, const char *name, const char *valu)
    449495{
    450         XMLAtt *ap = growAtt (ep);
    451         appendString (&ap->name, name);
    452         appendString (&ap->valu, valu);
    453         return (ap);
     496        XMLAtt *ap = growAtt (ep);
     497        appendString (&ap->name, name);
     498        appendString (&ap->valu, valu);
     499        return (ap);
    454500}
    455501
     
    458504rmXMLAtt (XMLEle *ep, const char *name)
    459505{
    460         int i;
    461 
    462         for (i = 0; i < ep->nat; i++) {
    463             if (strcmp (ep->at[i]->name.s, name) == 0) {
    464                 freeAtt (ep->at[i]);
    465                 memmove (&ep->at[i],&ep->at[i+1],(--ep->nat-i)*sizeof(XMLAtt*));
    466                 return;
    467             }
    468         }
     506        int i;
     507
     508        for (i = 0; i < ep->nat; i++) {
     509            if (strcmp (ep->at[i]->name.s, name) == 0) {
     510                freeAtt (ep->at[i]);
     511                memmove (&ep->at[i],&ep->at[i+1],(--ep->nat-i)*sizeof(XMLAtt*));
     512                return;
     513            }
     514        }
    469515}
    470516
     
    473519editXMLAtt (XMLAtt *ap, const char *str)
    474520{
    475         freeString (&ap->valu);
    476         appendString (&ap->valu, str);
     521        freeString (&ap->valu);
     522        appendString (&ap->valu, str);
    477523}
    478524
     
    484530prXMLEle (FILE *fp, XMLEle *ep, int level)
    485531{
    486         int indent = level*PRINDENT;
    487         int i;
    488 
    489         fprintf (fp, "%*s<%s", indent, "", ep->tag.s);
    490         for (i = 0; i < ep->nat; i++)
    491             fprintf (fp, " %s=\"%s\"", ep->at[i]->name.s,
    492                                                 entityXML(ep->at[i]->valu.s));
    493         if (ep->nel > 0) {
    494             fprintf (fp, ">\n");
    495             for (i = 0; i < ep->nel; i++)
    496                 prXMLEle (fp, ep->el[i], level+1);
    497         }
    498         if (ep->pcdata.sl > 0) {
    499             if (ep->nel == 0)
    500                 fprintf (fp, ">\n");
    501             if (ep->pcdata_hasent)
    502                 fprintf (fp, "%s", entityXML(ep->pcdata.s));
    503             else
    504                 fprintf (fp, "%s", ep->pcdata.s);
    505             if (ep->pcdata.s[ep->pcdata.sl-1] != '\n')
    506                 fprintf (fp, "\n");
    507         }
    508         if (ep->nel > 0 || ep->pcdata.sl > 0)
    509             fprintf (fp, "%*s</%s>\n", indent, "", ep->tag.s);
    510         else
    511             fprintf (fp, "/>\n");
     532        int indent = level*PRINDENT;
     533        int i;
     534
     535        fprintf (fp, "%*s<%s", indent, "", ep->tag.s);
     536        for (i = 0; i < ep->nat; i++)
     537            fprintf (fp, " %s=\"%s\"", ep->at[i]->name.s,
     538                                                entityXML(ep->at[i]->valu.s));
     539        if (ep->nel > 0) {
     540            fprintf (fp, ">\n");
     541            for (i = 0; i < ep->nel; i++)
     542                prXMLEle (fp, ep->el[i], level+1);
     543        }
     544        if (ep->pcdata.sl > 0) {
     545            if (ep->nel == 0)
     546                fprintf (fp, ">\n");
     547            if (ep->pcdata_hasent)
     548                fprintf (fp, "%s", entityXML(ep->pcdata.s));
     549            else
     550                fprintf (fp, "%s", ep->pcdata.s);
     551            if (ep->pcdata.s[ep->pcdata.sl-1] != '\n')
     552                fprintf (fp, "\n");
     553        }
     554        if (ep->nel > 0 || ep->pcdata.sl > 0)
     555            fprintf (fp, "%*s</%s>\n", indent, "", ep->tag.s);
     556        else
     557            fprintf (fp, "/>\n");
    512558}
    513559
     
    520566sprXMLEle (char *s, XMLEle *ep, int level)
    521567{
    522         int indent = level*PRINDENT;
    523         int sl = 0;
    524         int i;
    525 
    526         sl += sprintf (s+sl, "%*s<%s", indent, "", ep->tag.s);
    527         for (i = 0; i < ep->nat; i++)
    528             sl += sprintf (s+sl, " %s=\"%s\"", ep->at[i]->name.s,
    529                                                 entityXML(ep->at[i]->valu.s));
    530         if (ep->nel > 0) {
    531             sl += sprintf (s+sl, ">\n");
    532             for (i = 0; i < ep->nel; i++)
    533                 sl += sprXMLEle (s+sl, ep->el[i], level+1);
    534         }
    535         if (ep->pcdata.sl > 0) {
    536             if (ep->nel == 0)
    537                 sl += sprintf (s+sl, ">\n");
    538             if (ep->pcdata_hasent)
    539                 sl += sprintf (s+sl, "%s", entityXML(ep->pcdata.s));
    540             else {
    541                 strcpy (s+sl, ep->pcdata.s);
    542                 sl += ep->pcdata.sl;
    543             }
    544             if (ep->pcdata.s[ep->pcdata.sl-1] != '\n')
    545                 sl += sprintf (s+sl, "\n");
    546         }
    547         if (ep->nel > 0 || ep->pcdata.sl > 0)
    548             sl += sprintf (s+sl, "%*s</%s>\n", indent, "", ep->tag.s);
    549         else
    550             sl += sprintf (s+sl, "/>\n");
    551 
    552         return (sl);
     568        int indent = level*PRINDENT;
     569        int sl = 0;
     570        int i;
     571
     572        sl += sprintf (s+sl, "%*s<%s", indent, "", ep->tag.s);
     573        for (i = 0; i < ep->nat; i++)
     574            sl += sprintf (s+sl, " %s=\"%s\"", ep->at[i]->name.s,
     575                                                entityXML(ep->at[i]->valu.s));
     576        if (ep->nel > 0) {
     577            sl += sprintf (s+sl, ">\n");
     578            for (i = 0; i < ep->nel; i++)
     579                sl += sprXMLEle (s+sl, ep->el[i], level+1);
     580        }
     581        if (ep->pcdata.sl > 0) {
     582            if (ep->nel == 0)
     583                sl += sprintf (s+sl, ">\n");
     584            if (ep->pcdata_hasent)
     585                sl += sprintf (s+sl, "%s", entityXML(ep->pcdata.s));
     586            else {
     587                strcpy (s+sl, ep->pcdata.s);
     588                sl += ep->pcdata.sl;
     589            }
     590            if (ep->pcdata.s[ep->pcdata.sl-1] != '\n')
     591                sl += sprintf (s+sl, "\n");
     592        }
     593        if (ep->nel > 0 || ep->pcdata.sl > 0)
     594            sl += sprintf (s+sl, "%*s</%s>\n", indent, "", ep->tag.s);
     595        else
     596            sl += sprintf (s+sl, "/>\n");
     597
     598        return (sl);
    553599}
    554600
     
    560606sprlXMLEle (XMLEle *ep, int level)
    561607{
    562         int indent = level*PRINDENT;
    563         int l = 0;
    564         int i;
    565 
    566         l += indent + 1 + ep->tag.sl;
    567         for (i = 0; i < ep->nat; i++)
    568             l += ep->at[i]->name.sl + 4 + strlen(entityXML(ep->at[i]->valu.s));
    569 
    570         if (ep->nel > 0) {
    571             l += 2;
    572             for (i = 0; i < ep->nel; i++)
    573                 l += sprlXMLEle (ep->el[i], level+1);
    574         }
    575         if (ep->pcdata.sl > 0) {
    576             if (ep->nel == 0)
    577                 l += 2;
    578             if (ep->pcdata_hasent)
    579                 l += strlen (entityXML(ep->pcdata.s));
    580             else
    581                 l += ep->pcdata.sl;
    582             if (ep->pcdata.s[ep->pcdata.sl-1] != '\n')
    583                 l += 1;
    584         }
    585         if (ep->nel > 0 || ep->pcdata.sl > 0)
    586             l += indent + 4 + ep->tag.sl;
    587         else
    588             l += 3;
    589 
    590         return (l);
     608        int indent = level*PRINDENT;
     609        int l = 0;
     610        int i;
     611
     612        l += indent + 1 + ep->tag.sl;
     613        for (i = 0; i < ep->nat; i++)
     614            l += ep->at[i]->name.sl + 4 + strlen(entityXML(ep->at[i]->valu.s));
     615
     616        if (ep->nel > 0) {
     617            l += 2;
     618            for (i = 0; i < ep->nel; i++)
     619                l += sprlXMLEle (ep->el[i], level+1);
     620        }
     621        if (ep->pcdata.sl > 0) {
     622            if (ep->nel == 0)
     623                l += 2;
     624            if (ep->pcdata_hasent)
     625                l += strlen (entityXML(ep->pcdata.s));
     626            else
     627                l += ep->pcdata.sl;
     628            if (ep->pcdata.s[ep->pcdata.sl-1] != '\n')
     629                l += 1;
     630        }
     631        if (ep->nel > 0 || ep->pcdata.sl > 0)
     632            l += indent + 4 + ep->tag.sl;
     633        else
     634            l += 3;
     635
     636        return (l);
    591637}
    592638
     
    598644entityXML (char *s)
    599645{
    600         static char *malbuf;
    601         int nmalbuf = 0;
    602         char *sret;
    603         char *ep;
    604 
    605         /* scan for each entity, if any */
    606         for (sret = s; (ep = strpbrk (s, entities)) != NULL; s = ep+1) {
    607 
    608             /* found another entity, copy preceding to malloced buffer */
    609             int nnew = ep - s;                  /* all but entity itself */
    610             sret = malbuf = moremem (malbuf, nmalbuf + nnew + 10);
    611             memcpy (malbuf+nmalbuf, s, nnew);
    612             nmalbuf += nnew;
    613 
    614             /* replace with entity encoding */
    615             switch (*ep) {
    616             case '&':
    617                 nmalbuf += sprintf (malbuf+nmalbuf, "&amp;");
    618                 break;
    619             case '<':
    620                 nmalbuf += sprintf (malbuf+nmalbuf, "&lt;");
    621                 break;
    622             case '>':
    623                 nmalbuf += sprintf (malbuf+nmalbuf, "&gt;");
    624                 break;
    625             case '\'':
    626                 nmalbuf += sprintf (malbuf+nmalbuf, "&apos;");
    627                 break;
    628             case '"':
    629                 nmalbuf += sprintf (malbuf+nmalbuf, "&quot;");
    630                 break;
    631 
    632             }
    633 
    634         }
    635 
    636         /* return s if no entities, else malloc cleaned-up copy */
    637         if (sret == s) {
    638             /* using s, so free any malloced memory from last time */
    639             if (malbuf) {
    640                 free (malbuf);
    641                 malbuf = NULL;
    642             }
    643         } else {
    644             /* put remaining part of s into malbuf */
    645             int nleft = strlen (s) + 1;         /* include \0 */
    646             sret = malbuf = moremem (malbuf, nmalbuf + nleft);
    647             memcpy (malbuf+nmalbuf, s, nleft);
    648         }
    649 
    650         return (sret);
    651 }
     646        static char *malbuf;
     647        int nmalbuf = 0;
     648        char *sret;
     649        char *ep;
     650
     651        /* scan for each entity, if any */
     652        for (sret = s; (ep = strpbrk (s, entities)) != NULL; s = ep+1) {
     653
     654            /* found another entity, copy preceding to malloced buffer */
     655            int nnew = ep - s;                  /* all but entity itself */
     656            sret = malbuf = moremem (malbuf, nmalbuf + nnew + 10);
     657            memcpy (malbuf+nmalbuf, s, nnew);
     658            nmalbuf += nnew;
     659
     660            /* replace with entity encoding */
     661            switch (*ep) {
     662            case '&':
     663                nmalbuf += sprintf (malbuf+nmalbuf, "&amp;");
     664                break;
     665            case '<':
     666                nmalbuf += sprintf (malbuf+nmalbuf, "&lt;");
     667                break;
     668            case '>':
     669                nmalbuf += sprintf (malbuf+nmalbuf, "&gt;");
     670                break;
     671            case '\'':
     672                nmalbuf += sprintf (malbuf+nmalbuf, "&apos;");
     673                break;
     674            case '"':
     675                nmalbuf += sprintf (malbuf+nmalbuf, "&quot;");
     676                break;
     677
     678            }
     679
     680        }
     681
     682        /* return s if no entities, else malloc cleaned-up copy */
     683        if (sret == s) {
     684            /* using s, so free any malloced memory from last time */
     685            if (malbuf) {
     686                free (malbuf);
     687                malbuf = NULL;
     688            }
     689        } else {
     690            /* put remaining part of s into malbuf */
     691            int nleft = strlen (s) + 1;         /* include \0 */
     692            sret = malbuf = moremem (malbuf, nmalbuf + nleft);
     693            memcpy (malbuf+nmalbuf, s, nleft);
     694        }
     695
     696        return (sret);
     697}
     698
     699
     700
    652701
    653702/* if ent is a recognized xml entity sequence, set *cp to char and return 1
     
    657706decodeEntity (char *ent, int *cp)
    658707{
    659         static struct {
    660             const char *ent;
    661             char c;
    662         } enttable[] = {
    663             {"&amp;",  '&'},
    664             {"&apos;", '\''},
    665             {"&lt;",   '<'},
    666             {"&gt;",   '>'},
    667             {"&quot;", '"'},
    668         };
    669         unsigned int i;
    670 
    671         for (i = 0; i < sizeof(enttable)/sizeof(enttable[0]); i++) {
    672             if (strcmp (ent, enttable[i].ent) == 0) {
    673                 *cp = enttable[i].c;
    674                 return (1);
    675             }
    676         }
    677        
    678         return (0);
     708        static struct {
     709            char *ent;
     710            char c;
     711        } enttable[] = {
     712            {"&amp;",  '&'},
     713            {"&apos;", '\''},
     714            {"&lt;",   '<'},
     715            {"&gt;",   '>'},
     716            {"&quot;", '"'},
     717        };
     718        int i;
     719
     720        for (i = 0; i < sizeof(enttable)/sizeof(enttable[0]); i++) {
     721            if (strcmp (ent, enttable[i].ent) == 0) {
     722                *cp = enttable[i].c;
     723                return (1);
     724            }
     725        }
     726
     727        return (0);
    679728}
    680729
     
    682731 * if find final closure, return 1 and tree is in ce.
    683732 * if need more, return 0.
    684  * if real trouble, return -1 and put reason in errmsg.
     733 * if real trouble, return -1 and put reason in ynot.
    685734 */
    686735static int
    687 oneXMLchar (LilXML *lp, int c, char errmsg[])
    688 {
    689         switch (lp->cs) {
    690         case LOOK4START:                /* looking for first element start */
    691             if (c == '<') {
    692                 pushXMLEle(lp);
    693                 lp->cs = LOOK4TAG;
    694             }
    695             /* silently ignore until resync */
    696             break;
    697 
    698         case LOOK4TAG:                  /* looking for element tag */
    699             if (isTokenChar (1, c)) {
    700                 growString (&lp->ce->tag, c);
    701                 lp->cs = INTAG;
    702             } else if (!isspace(c)) {
    703                 sprintf (errmsg, "Line %d: Bogus tag char %c", lp->ln, c);
    704                 return (-1);
    705             }
    706             break;
    707                
    708         case INTAG:                     /* reading tag */
    709             if (isTokenChar (0, c))
    710                 growString (&lp->ce->tag, c);
    711             else if (c == '>')
    712                 lp->cs = LOOK4CON;
    713             else if (c == '/')
    714                 lp->cs = SAWSLASH;
    715             else
    716                 lp->cs = LOOK4ATTRN;
    717             break;
    718 
    719         case LOOK4ATTRN:                /* looking for attr name, > or / */
    720             if (c == '>')
    721                 lp->cs = LOOK4CON;
    722             else if (c == '/')
    723                 lp->cs = SAWSLASH;
    724             else if (isTokenChar (1, c)) {
    725                 XMLAtt *ap = growAtt(lp->ce);
    726                 growString (&ap->name, c);
    727                 lp->cs = INATTRN;
    728             } else if (!isspace(c)) {
    729                 sprintf (errmsg, "Line %d: Bogus leading attr name char: %c",
    730                                                                     lp->ln, c);
    731                 return (-1);
    732             }
    733             break;
    734 
    735         case SAWSLASH:                  /* saw / in element opening */
    736             if (c == '>') {
    737                 if (!lp->ce->pe)
    738                     return(1);          /* root has no content */
    739                 popXMLEle(lp);
    740                 lp->cs = LOOK4CON;
    741             } else {
    742                 sprintf (errmsg, "Line %d: Bogus char %c before >", lp->ln, c);
    743                 return (-1);
    744             }
    745             break;
    746                
    747         case INATTRN:                   /* reading attr name */
    748             if (isTokenChar (0, c))
    749                 growString (&lp->ce->at[lp->ce->nat-1]->name, c);
    750             else if (isspace(c) || c == '=')
    751                 lp->cs = LOOK4ATTRV;
    752             else {
    753                 sprintf (errmsg, "Line %d: Bogus attr name char: %c", lp->ln,c);
    754                 return (-1);
    755             }
    756             break;
    757 
    758         case LOOK4ATTRV:                /* looking for attr value */
    759             if (c == '\'' || c == '"') {
    760                 lp->delim = c;
    761                 lp->cs = INATTRV;
    762             } else if (!(isspace(c) || c == '=')) {
    763                 sprintf (errmsg, "Line %d: No value for attribute %s", lp->ln,
    764                                         lp->ce->at[lp->ce->nat-1]->name.s);
    765                 return (-1);
    766             }
    767             break;
    768 
    769         case INATTRV:                   /* in attr value */
    770             if (c == '&') {
    771                 newString (&lp->entity);
    772                 growString (&lp->entity, c);
    773                 lp->cs = ENTINATTRV;
    774             } else if (c == lp->delim)
    775                 lp->cs = LOOK4ATTRN;
    776             else if (!iscntrl(c))
    777                 growString (&lp->ce->at[lp->ce->nat-1]->valu, c);
    778             break;
    779 
    780         case ENTINATTRV:                /* working on entity in attr valu */
    781             if (c == ';') {
    782                 /* if find a recongized esp seq, add equiv char else raw seq */
    783                 growString (&lp->entity, c);
    784                 if (decodeEntity (lp->entity.s, &c))
    785                     growString (&lp->ce->at[lp->ce->nat-1]->valu, c);
    786                 else
    787                     appendString(&lp->ce->at[lp->ce->nat-1]->valu,lp->entity.s);
    788                 freeString (&lp->entity);
    789                 lp->cs = INATTRV;
    790             } else
    791                 growString (&lp->entity, c);
    792             break;
    793 
    794         case LOOK4CON:                  /* skipping leading content whitespace*/
    795             if (c == '<')
    796                 lp->cs = SAWLTINCON;
    797             else if (!isspace(c)) {
    798                 growString (&lp->ce->pcdata, c);
    799                 lp->cs = INCON;
    800             }
    801             break;
    802 
    803         case INCON:                     /* reading content */
    804             if (c == '&') {
    805                 newString (&lp->entity);
    806                 growString (&lp->entity, c);
    807                 lp->cs = ENTINCON;
    808             } else if (c == '<') {
    809                 /* chomp trailing whitespace */
    810                 while (lp->ce->pcdata.sl > 0 &&
    811                             isspace(lp->ce->pcdata.s[lp->ce->pcdata.sl-1]))
    812                     lp->ce->pcdata.s[--(lp->ce->pcdata.sl)] = '\0';
    813                 lp->cs = SAWLTINCON;
    814             } else {
    815                 growString (&lp->ce->pcdata, c);
    816             }
    817             break;
    818 
    819         case ENTINCON:                  /* working on entity in content */
    820             if (c == ';') {
    821                 /* if find a recognized esc seq, add equiv char else raw seq */
    822                 growString (&lp->entity, c);
    823                 if (decodeEntity (lp->entity.s, &c))
    824                     growString (&lp->ce->pcdata, c);
    825                 else {
    826                     appendString(&lp->ce->pcdata, lp->entity.s);
    827                     lp->ce->pcdata_hasent = 1;
    828                 }
    829                 freeString (&lp->entity);
    830                 lp->cs = INCON;
    831             } else
    832                 growString (&lp->entity, c);
    833             break;
    834 
    835         case SAWLTINCON:                /* saw < in content */
    836             if (c == '/') {
    837                 resetEndTag(lp);
    838                 lp->cs = LOOK4CLOSETAG;
    839             } else {
    840                 pushXMLEle(lp);
    841                 if (isTokenChar(1,c)) {
    842                     growString (&lp->ce->tag, c);
    843                     lp->cs = INTAG;
    844                 } else
    845                     lp->cs = LOOK4TAG;
    846             }
    847             break;
    848 
    849         case LOOK4CLOSETAG:             /* looking for closing tag after < */
    850             if (isTokenChar (1, c)) {
    851                 growString (&lp->endtag, c);
    852                 lp->cs = INCLOSETAG;
    853             } else if (!isspace(c)) {
    854                 sprintf (errmsg, "Line %d: Bogus preend tag char %c", lp->ln,c);
    855                 return (-1);
    856             }
    857             break;
    858 
    859         case INCLOSETAG:                /* reading closing tag */
    860             if (isTokenChar(0, c))
    861                 growString (&lp->endtag, c);
    862             else if (c == '>') {
    863                 if (strcmp (lp->ce->tag.s, lp->endtag.s)) {
    864                     sprintf (errmsg,"Line %d: closing tag %s does not match %s",
    865                                     lp->ln, lp->endtag.s, lp->ce->tag.s);
    866                     return (-1);
    867                 } else if (lp->ce->pe) {
    868                     popXMLEle(lp);
    869                     lp->cs = LOOK4CON;  /* back to content after nested elem */
    870                 } else
    871                     return (1);         /* yes! */
    872             } else if (!isspace(c)) {
    873                 sprintf (errmsg, "Line %d: Bogus end tag char %c", lp->ln, c);
    874                 return (-1);
    875             }
    876             break;
    877         }
    878 
    879         return (0);
     736oneXMLchar (LilXML *lp, int c, char ynot[])
     737{
     738        switch (lp->cs) {
     739        case LOOK4START:                /* looking for first element start */
     740            if (c == '<') {
     741                pushXMLEle(lp);
     742                lp->cs = LOOK4TAG;
     743            }
     744            /* silently ignore until resync */
     745            break;
     746
     747        case LOOK4TAG:                  /* looking for element tag */
     748            if (isTokenChar (1, c)) {
     749                growString (&lp->ce->tag, c);
     750                lp->cs = INTAG;
     751            } else if (!isspace(c)) {
     752                sprintf (ynot, "Line %d: Bogus tag char %c", lp->ln, c);
     753                return (-1);
     754            }
     755            break;
     756
     757        case INTAG:                     /* reading tag */
     758            if (isTokenChar (0, c))
     759                growString (&lp->ce->tag, c);
     760            else if (c == '>')
     761                lp->cs = LOOK4CON;
     762            else if (c == '/')
     763                lp->cs = SAWSLASH;
     764            else
     765                lp->cs = LOOK4ATTRN;
     766            break;
     767
     768        case LOOK4ATTRN:                /* looking for attr name, > or / */
     769            if (c == '>')
     770                lp->cs = LOOK4CON;
     771            else if (c == '/')
     772                lp->cs = SAWSLASH;
     773            else if (isTokenChar (1, c)) {
     774                XMLAtt *ap = growAtt(lp->ce);
     775                growString (&ap->name, c);
     776                lp->cs = INATTRN;
     777            } else if (!isspace(c)) {
     778                sprintf (ynot, "Line %d: Bogus leading attr name char: %c",
     779                                                                    lp->ln, c);
     780                return (-1);
     781            }
     782            break;
     783
     784        case SAWSLASH:                  /* saw / in element opening */
     785            if (c == '>') {
     786                if (!lp->ce->pe)
     787                    return(1);          /* root has no content */
     788                popXMLEle(lp);
     789                lp->cs = LOOK4CON;
     790            } else {
     791                sprintf (ynot, "Line %d: Bogus char %c before >", lp->ln, c);
     792                return (-1);
     793            }
     794            break;
     795
     796        case INATTRN:                   /* reading attr name */
     797            if (isTokenChar (0, c))
     798                growString (&lp->ce->at[lp->ce->nat-1]->name, c);
     799            else if (isspace(c) || c == '=')
     800                lp->cs = LOOK4ATTRV;
     801            else {
     802                sprintf (ynot, "Line %d: Bogus attr name char: %c", lp->ln,c);
     803                return (-1);
     804            }
     805            break;
     806
     807        case LOOK4ATTRV:                /* looking for attr value */
     808            if (c == '\'' || c == '"') {
     809                lp->delim = c;
     810                lp->cs = INATTRV;
     811            } else if (!(isspace(c) || c == '=')) {
     812                sprintf (ynot, "Line %d: No value for attribute %s", lp->ln,
     813                                        lp->ce->at[lp->ce->nat-1]->name.s);
     814                return (-1);
     815            }
     816            break;
     817
     818        case INATTRV:                   /* in attr value */
     819            if (c == '&') {
     820                newString (&lp->entity);
     821                growString (&lp->entity, c);
     822                lp->cs = ENTINATTRV;
     823            } else if (c == lp->delim)
     824                lp->cs = LOOK4ATTRN;
     825            else if (!iscntrl(c))
     826                growString (&lp->ce->at[lp->ce->nat-1]->valu, c);
     827            break;
     828
     829        case ENTINATTRV:                /* working on entity in attr valu */
     830            if (c == ';') {
     831                /* if find a recongized esp seq, add equiv char else raw seq */
     832                growString (&lp->entity, c);
     833                if (decodeEntity (lp->entity.s, &c))
     834                    growString (&lp->ce->at[lp->ce->nat-1]->valu, c);
     835                else
     836                    appendString(&lp->ce->at[lp->ce->nat-1]->valu,lp->entity.s);
     837                freeString (&lp->entity);
     838                lp->cs = INATTRV;
     839            } else
     840                growString (&lp->entity, c);
     841            break;
     842
     843        case LOOK4CON:                  /* skipping leading content whitespace*/
     844            if (c == '<')
     845                lp->cs = SAWLTINCON;
     846            else if (!isspace(c)) {
     847                growString (&lp->ce->pcdata, c);
     848                lp->cs = INCON;
     849            }
     850            break;
     851
     852        case INCON:                     /* reading content */
     853            if (c == '&') {
     854                newString (&lp->entity);
     855                growString (&lp->entity, c);
     856                lp->cs = ENTINCON;
     857            } else if (c == '<') {
     858                /* chomp trailing whitespace */
     859                while (lp->ce->pcdata.sl > 0 &&
     860                            isspace(lp->ce->pcdata.s[lp->ce->pcdata.sl-1]))
     861                    lp->ce->pcdata.s[--(lp->ce->pcdata.sl)] = '\0';
     862                lp->cs = SAWLTINCON;
     863            } else {
     864                growString (&lp->ce->pcdata, c);
     865            }
     866            break;
     867
     868        case ENTINCON:                  /* working on entity in content */
     869            if (c == ';') {
     870                /* if find a recognized esc seq, add equiv char else raw seq */
     871                growString (&lp->entity, c);
     872                if (decodeEntity (lp->entity.s, &c))
     873                    growString (&lp->ce->pcdata, c);
     874                else {
     875                    appendString(&lp->ce->pcdata, lp->entity.s);
     876                    lp->ce->pcdata_hasent = 1;
     877                }
     878                freeString (&lp->entity);
     879                lp->cs = INCON;
     880            } else
     881                growString (&lp->entity, c);
     882            break;
     883
     884        case SAWLTINCON:                /* saw < in content */
     885            if (c == '/') {
     886                resetEndTag(lp);
     887                lp->cs = LOOK4CLOSETAG;
     888            } else {
     889                pushXMLEle(lp);
     890                if (isTokenChar(1,c)) {
     891                    growString (&lp->ce->tag, c);
     892                    lp->cs = INTAG;
     893                } else
     894                    lp->cs = LOOK4TAG;
     895            }
     896            break;
     897
     898        case LOOK4CLOSETAG:             /* looking for closing tag after < */
     899            if (isTokenChar (1, c)) {
     900                growString (&lp->endtag, c);
     901                lp->cs = INCLOSETAG;
     902            } else if (!isspace(c)) {
     903                sprintf (ynot, "Line %d: Bogus preend tag char %c", lp->ln,c);
     904                return (-1);
     905            }
     906            break;
     907
     908        case INCLOSETAG:                /* reading closing tag */
     909            if (isTokenChar(0, c))
     910                growString (&lp->endtag, c);
     911            else if (c == '>') {
     912                if (strcmp (lp->ce->tag.s, lp->endtag.s)) {
     913                    sprintf (ynot,"Line %d: closing tag %s does not match %s",
     914                                    lp->ln, lp->endtag.s, lp->ce->tag.s);
     915                    return (-1);
     916                } else if (lp->ce->pe) {
     917                    popXMLEle(lp);
     918                    lp->cs = LOOK4CON;  /* back to content after nested elem */
     919                } else
     920                    return (1);         /* yes! */
     921            } else if (!isspace(c)) {
     922                sprintf (ynot, "Line %d: Bogus end tag char %c", lp->ln, c);
     923                return (-1);
     924            }
     925            break;
     926        }
     927
     928        return (0);
    880929}
    881930
     
    884933initParser(LilXML *lp)
    885934{
    886         delXMLEle (lp->ce);
    887         freeString (&lp->endtag);
    888         memset (lp, 0, sizeof(*lp));
    889         newString (&lp->endtag);
    890         lp->cs = LOOK4START;
    891         lp->ln = 1;
     935        delXMLEle (lp->ce);
     936        freeString (&lp->endtag);
     937        memset (lp, 0, sizeof(*lp));
     938        newString (&lp->endtag);
     939        lp->cs = LOOK4START;
     940        lp->ln = 1;
    892941}
    893942
     
    900949pushXMLEle(LilXML *lp)
    901950{
    902         lp->ce = growEle (lp->ce);
    903         resetEndTag(lp);
     951        lp->ce = growEle (lp->ce);
     952        resetEndTag(lp);
    904953}
    905954
     
    910959popXMLEle(LilXML *lp)
    911960{
    912         lp->ce = lp->ce->pe;
    913         resetEndTag(lp);
     961        lp->ce = lp->ce->pe;
     962        resetEndTag(lp);
    914963}
    915964
     
    918967growEle (XMLEle *pe)
    919968{
    920         XMLEle *newe = (XMLEle *) moremem (NULL, sizeof(XMLEle));
    921 
    922         memset (newe, 0, sizeof(XMLEle));
    923         newString (&newe->tag);
    924         newString (&newe->pcdata);
    925         newe->pe = pe;
    926 
    927         if (pe) {
    928             pe->el = (XMLEle **) moremem (pe->el, (pe->nel+1)*sizeof(XMLEle *));
    929             pe->el[pe->nel++] = newe;
    930         }
    931 
    932         return (newe);
     969        XMLEle *newe = (XMLEle *) moremem (NULL, sizeof(XMLEle));
     970
     971        memset (newe, 0, sizeof(XMLEle));
     972        newString (&newe->tag);
     973        newString (&newe->pcdata);
     974        newe->pe = pe;
     975
     976        if (pe) {
     977            pe->el = (XMLEle **) moremem (pe->el, (pe->nel+1)*sizeof(XMLEle *));
     978            pe->el[pe->nel++] = newe;
     979        }
     980
     981        return (newe);
    933982}
    934983
     
    937986growAtt(XMLEle *ep)
    938987{
    939         XMLAtt *newa = (XMLAtt *) moremem (NULL, sizeof(XMLAtt));
    940 
    941         memset (newa, 0, sizeof(*newa));
    942         newString(&newa->name);
    943         newString(&newa->valu);
    944         newa->ce = ep;
    945 
    946         ep->at = (XMLAtt **) moremem (ep->at, (ep->nat+1)*sizeof(XMLAtt *));
    947         ep->at[ep->nat++] = newa;
    948 
    949         return (newa);
     988        XMLAtt *newa = (XMLAtt *) moremem (NULL, sizeof(XMLAtt));
     989
     990        memset (newa, 0, sizeof(*newa));
     991        newString(&newa->name);
     992        newString(&newa->valu);
     993        newa->ce = ep;
     994
     995        ep->at = (XMLAtt **) moremem (ep->at, (ep->nat+1)*sizeof(XMLAtt *));
     996        ep->at[ep->nat++] = newa;
     997
     998        return (newa);
    950999}
    9511000
     
    9541003freeAtt (XMLAtt *a)
    9551004{
    956         if (!a)
    957             return;
    958         freeString (&a->name);
    959         freeString (&a->valu);
    960         (*myfree)(a);
     1005        if (!a)
     1006            return;
     1007        freeString (&a->name);
     1008        freeString (&a->valu);
     1009        (*myfree)(a);
    9611010}
    9621011
     
    9651014resetEndTag(LilXML *lp)
    9661015{
    967         freeString (&lp->endtag);
    968         newString (&lp->endtag);
     1016        freeString (&lp->endtag);
     1017        newString (&lp->endtag);
    9691018}
    9701019
     
    9751024isTokenChar (int start, int c)
    9761025{
    977         return (isalpha(c) || c == '_' || (!start && isdigit(c)));
     1026        return (isalpha(c) || c == '_' || (!start && isdigit(c)));
    9781027}
    9791028
     
    9821031growString (String *sp, int c)
    9831032{
    984         int l = sp->sl + 2;             /* need room for '\0' plus c */
    985 
    986         if (l > sp->sm) {
    987             if (!sp->s)
    988                 newString (sp);
    989             else
    990                 sp->s = (char *) moremem (sp->s, sp->sm *= 2);
    991         }
    992         sp->s[--l] = '\0';
    993         sp->s[--l] = (char)c;
    994         sp->sl++;
     1033        int l = sp->sl + 2;             /* need room for '\0' plus c */
     1034
     1035        if (l > sp->sm) {
     1036            if (!sp->s)
     1037                newString (sp);
     1038            else
     1039                sp->s = (char *) moremem (sp->s, sp->sm *= 2);
     1040        }
     1041        sp->s[--l] = '\0';
     1042        sp->s[--l] = (char)c;
     1043        sp->sl++;
    9951044}
    9961045
     
    9991048appendString (String *sp, const char *str)
    10001049{
    1001         int strl = strlen (str);
    1002         int l = sp->sl + strl + 1;      /* need room for '\0' */
    1003 
    1004         if (l > sp->sm) {
    1005             if (!sp->s)
    1006                 newString (sp);
    1007             if (l > sp->sm)
    1008                 sp->s = (char *) moremem (sp->s, (sp->sm = l));
    1009         }
    1010         strcpy (&sp->s[sp->sl], str);
    1011         sp->sl += strl;         
     1050        int strl = strlen (str);
     1051        int l = sp->sl + strl + 1;      /* need room for '\0' */
     1052
     1053        if (l > sp->sm) {
     1054            if (!sp->s)
     1055                newString (sp);
     1056            if (l > sp->sm)
     1057                sp->s = (char *) moremem (sp->s, (sp->sm = l));
     1058        }
     1059        strcpy (&sp->s[sp->sl], str);
     1060        sp->sl += strl;
    10121061}
    10131062
     
    10161065newString(String *sp)
    10171066{
    1018         sp->s = (char *)moremem(NULL, MINMEM);
    1019         sp->sm = MINMEM;
    1020         *sp->s = '\0';
    1021         sp->sl = 0;
     1067        sp->s = (char *)moremem(NULL, MINMEM);
     1068        sp->sm = MINMEM;
     1069        *sp->s = '\0';
     1070        sp->sl = 0;
    10221071}
    10231072
     
    10261075freeString (String *sp)
    10271076{
    1028         if (sp->s)
    1029             (*myfree) (sp->s);
    1030         sp->s = NULL;
    1031         sp->sl = 0;
    1032         sp->sm = 0;
     1077        if (sp->s)
     1078            (*myfree) (sp->s);
     1079        sp->s = NULL;
     1080        sp->sl = 0;
     1081        sp->sm = 0;
    10331082}
    10341083
     
    10371086moremem (void *old, int n)
    10381087{
    1039         return (old ? (*myrealloc)(old, n) : (*mymalloc)(n));
     1088        return (old ? (*myrealloc)(old, n) : (*mymalloc)(n));
    10401089}
    10411090
     
    10441093main (int ac, char *av[])
    10451094{
    1046         LilXML *lp = newLilXML();
    1047         char errmsg[1024];
    1048         XMLEle *root;
    1049 
    1050         root = readXMLFile (stdin, lp, errmsg);
    1051         if (root) {
    1052             char *str;
    1053             int l;
    1054 
    1055             if (ac > 1) {
    1056                 XMLEle *theend = addXMLEle (root, "theend");
    1057                 editXMLEle (theend, "Added to test editing");
    1058                 addXMLAtt (theend, "hello", "world");
    1059             }
    1060 
    1061             fprintf (stderr, "::::::::::::: %s\n", tagXMLEle(root));
    1062             prXMLEle (stdout, root, 0);
    1063 
    1064             l = sprlXMLEle (root, 0);
    1065             str = malloc (l+1);
    1066             fprintf (stderr, "::::::::::::: %s : %d : %d",
    1067                                 tagXMLEle(root), l, sprXMLEle (str, root, 0));
    1068             fprintf (stderr, ": %d\n", printf ("%s", str));
    1069 
    1070             delXMLEle (root);
    1071         } else if (errmsg[0]) {
    1072             fprintf (stderr, "Error: %s\n", errmsg);
    1073         }
    1074 
    1075         delLilXML (lp);
    1076 
    1077         return (0);
     1095        LilXML *lp = newLilXML();
     1096        char ynot[1024];
     1097        XMLEle *root;
     1098
     1099        root = readXMLFile (stdin, lp, ynot);
     1100        if (root) {
     1101            char *str;
     1102            int l;
     1103
     1104            if (ac > 1) {
     1105                XMLEle *theend = addXMLEle (root, "theend");
     1106                editXMLEle (theend, "Added to test editing");
     1107                addXMLAtt (theend, "hello", "world");
     1108            }
     1109
     1110            fprintf (stderr, "::::::::::::: %s\n", tagXMLEle(root));
     1111            prXMLEle (stdout, root, 0);
     1112
     1113            l = sprlXMLEle (root, 0);
     1114            str = malloc (l+1);
     1115            fprintf (stderr, "::::::::::::: %s : %d : %d",
     1116                                tagXMLEle(root), l, sprXMLEle (str, root, 0));
     1117            fprintf (stderr, ": %d\n", printf ("%s", str));
     1118
     1119            delXMLEle (root);
     1120        } else if (ynot[0]) {
     1121            fprintf (stderr, "Error: %s\n", ynot);
     1122        }
     1123
     1124        delLilXML (lp);
     1125
     1126        return (0);
    10781127}
    10791128#endif
    1080 
Note: See TracChangeset for help on using the changeset viewer.