Changeset 10627


Ignore:
Timestamp:
11/14/11 10:12:13 (13 years ago)
Author:
jschierm
Message:

KML: Handle comments in KML files.

Location:
issm/trunk/src/c/objects/KML
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk/src/c/objects/KML/KMLFileReadUtils.cpp

    r10274 r10627  
    2323char* KMLFileToken(FILE* fid){
    2424
     25/*  get the next token (tag or field) in the file  */
     26
    2527        bool    inew=1,itag=0,ifield=0;
    2628        int     c;
    2729        int     ibuf=0,buflen=1024,bufblk=1024;
    28         char*   buffer=NULL;
     30        char    *buffer=NULL,*bufferc=NULL;
    2931
    3032        buffer=(char *) xmalloc(buflen*sizeof(char));
    3133        buffer[0]='\0';
    3234
    33 /*  read kml file */
     35/*  read kml file character-by-character */
    3436
    3537//  note that fgets includes newline
     
    3840        while ((c=getc(fid)) != EOF) {
    3941                /*  ignore leading blanks  */
     42//              printf("point 0 c=%c\n",c);
    4043                if (inew && isspace(c))
    4144                        continue;
     
    4346                /*  distinguish between tag or field  */
    4447                if (!itag && !ifield) {
    45                         if (c == '<')
    46                                 itag=1;
     48
     49                        /*  distinguish between tag or comment  */
     50                        if (c == '<') {
     51//                              printf("point 1 c=%c\n",c);
     52                                ungetc(c,fid);
     53                                if (!(bufferc=KMLFileTokenComment(fid))) {
     54                                        c=getc(fid);
     55//                                      printf("point 2 c=%c\n",c);
     56                                        itag=1;
     57                                }
     58                                else {
     59//                                      printf("point 3 buffer=%s\n",buffer);
     60                                        xfree((void**)&bufferc);
     61                                        inew=1;
     62                                        continue;
     63                                }
     64                        }
    4765                        else
    4866                                ifield=1;
     
    6987                /*  accumulate field, including newlines  */
    7088                else if (ifield) {
     89                        /*  distinguish between closing tag or comment  */
    7190                        if (c == '<') {
    7291                                ungetc(c,fid);
    73                                 break;
     92                                if (!(bufferc=KMLFileTokenComment(fid)))
     93                                        break;
     94                                else
     95                                        xfree((void**)&bufferc);
    7496                        }
    7597                        else {
     
    95117                }
    96118
    97 //      if      (itag)
    98 //              _printf_(true,"tag buffer (length=%d):\n",ibuf);
    99 //      else if (ifield)
    100 //              _printf_(true,"field buffer (length=%d):\n",ibuf);
    101 //      _printf_(true,"%s\n",buffer);
     119        if      (itag)
     120                _printf_(true,"tag buffer (length=%d):\n",ibuf);
     121        else if (ifield)
     122                _printf_(true,"field buffer (length=%d):\n",ibuf);
     123        _printf_(true,"%s\n",buffer);
     124
     125        if (!ibuf)
     126                xfree((void**)&buffer);
     127
     128        return(buffer);
     129}
     130/*}}}*/
     131
     132/*FUNCTION  KMLFileTokenComment(FILE* fid) {{{1*/
     133char* KMLFileTokenComment(FILE* fid){
     134
     135/*  check for comment in the file and read it  */
     136
     137        bool    inew=1;
     138        int     i;
     139        int     c;
     140        int     ibuf=0,buflen=1024,bufblk=1024;
     141        char*   buffer=NULL;
     142
     143        buffer=(char *) xmalloc(buflen*sizeof(char));
     144        buffer[0]='\0';
     145
     146/*  read kml file character-by-character  */
     147
     148        while ((c=getc(fid)) != EOF) {
     149//              printf("point 10 c=%c\n",c);
     150                /*  ignore leading blanks  */
     151                if (inew && isspace(c))
     152                        continue;
     153
     154                inew=0;
     155                KMLFileTokenBuffer(&buffer,&ibuf,&buflen,
     156                                                   c,
     157                                                   bufblk);
     158
     159                /*  check for comment  */
     160                if (ibuf <= 4) {
     161                        if ((ibuf == 1 && buffer[0] != '<') ||
     162                                (ibuf == 2 && buffer[1] != '!') ||
     163                                (ibuf == 3 && buffer[2] != '-') ||
     164                                (ibuf == 4 && buffer[3] != '-')) {
     165                                for (i=ibuf-1; i>=0; i--)
     166                                        ungetc(buffer[i],fid);
     167//                              printf("point 11 buffer=%p\n",buffer);
     168                                xfree((void**)&buffer);
     169//                              printf("point 12 buffer=%p\n",buffer);
     170                                return(buffer);
     171                        }
     172                }
     173
     174                /*  accumulate comment, including newlines  */
     175                else
     176                        if (buffer[ibuf-3]=='-' && buffer[ibuf-2]=='-' && buffer[ibuf-1]=='>')
     177                                break;
     178        }
     179
     180/*  remove trailing blanks or newline  */
     181
     182        while (ibuf > 0)
     183                if (isspace(buffer[ibuf-1]))
     184                        ibuf--;
     185                else {
     186                        buffer[ibuf]='\0';
     187                        break;
     188                }
     189
     190        _printf_(true,"comment buffer (length=%d):\n",ibuf);
     191        _printf_(true,"%s\n",buffer);
    102192
    103193        if (!ibuf)
     
    113203                                                int bufblk){
    114204
     205/*  add the specified character to the token buffer  */
     206
    115207        char*   buffer=NULL;
    116208
     
    134226void KMLFileTagAttrib(KML_Object* kobj,
    135227                                          char* ktag){
     228
     229/*  for the given tag buffer, read and store the attributes  */
    136230
    137231        char*   ktagi;
     
    144238        ktagi=(char *) xmalloc((strlen(ktag)+1)*sizeof(char));
    145239        memcpy(ktagi,ktag,(strlen(ktag)+1)*sizeof(char));
    146 
    147240
    148241/*  loop through tag to find all attributes  */
  • issm/trunk/src/c/objects/KML/KMLFileReadUtils.h

    r10256 r10627  
    1616/* local prototypes: */
    1717char* KMLFileToken(FILE* fid);
     18char* KMLFileTokenComment(FILE* fid);
    1819void KMLFileTokenBuffer(char** pbuffer,int* pibuf,int* pbuflen,
    1920                                                int c,
Note: See TracChangeset for help on using the changeset viewer.