IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 6, 2004, 12:51:31 PM (22 years ago)
Author:
harman
Message:

Added more error checks. Removed unnecessary code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/xml/psXML.c

    r1973 r1986  
    99*  @author Ross Harman, MHPCC
    1010*
    11 *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    12 *  @date $Date: 2004-10-06 01:17:39 $
     11*  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     12*  @date $Date: 2004-10-06 22:51:15 $
    1313*
    1414*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    101101
    102102
    103 /** Determines if a line is blank (whitespace only) or a comment, if so returns true. The input string must
    104  * be null terminated. */
     103/** Determines if a line is blank (whitespace only) or a commentline. It returns true if so. The input string
     104 *  must * be null terminated. */
    105105bool ignoreLine(char *inString)
    106106{
     
    116116
    117117
    118 /** Removes leading and trailing space from a line, as well as the leading comment # character. The cleaned
    119  * line is a new null terminated copy of the original input string. The input string must be null terminated.
     118/** Removes leading and trailing whitespace and # characters from a string. The cleaned string is a new null
     119 *  terminated copy of the original input string.
    120120 */
    121 char *cleanLine(char *inString)
     121char *cleanString(char *inString, int sLen)
    122122{
    123123    char *ptrB = NULL;
    124124    char *ptrE = NULL;
    125125    char *cleaned = NULL;
    126     int sLen = 0;
    127 
    128 
    129     sLen = strlen(inString);
     126
     127
    130128    ptrB = inString;
    131129
    132     /* Skip over # and leading whitespace */
     130    /* Skip over leading # or whitespace */
    133131    while (isspace(*ptrB) || *ptrB=='#') {
    134132        ptrB++;
    135133    }
    136134
    137     /* Skip over trailing whitespace */
     135    /* Skip over trailing whitespace, null terminators, and # characters */
    138136    ptrE = inString + sLen;
    139     while(isspace(*ptrE) || *ptrE=='\0') {
     137    while(isspace(*ptrE) || *ptrE=='\0' || *ptrE=='#') {
    140138        ptrE--;
    141139    }
     
    150148}
    151149
    152 /** Count repeated occurances of a single character within a line. The input string must be null terminated. */
     150/** Count repeat occurances of a single character within a line. The input string must be null terminated. */
    153151int repeatedChars(char *inString, char ch)
    154152{
     
    166164}
    167165
    168 /** Returns individual tokens within a line of text based on delimiter. Tokens are new null terminated copies
    169  *  of the original input string. */
    170 char* getToken(char **inString, char *delimiter, bool clean)
    171 {
    172     char *token = NULL;
     166/** Returns cleaned token based on delimiter, but not including delimiter. Also changes the pointer location
     167 * the beginning of the string. Tokens are newly allocated null terminated strings. */
     168char* getToken(char **inString, char *delimiter, int *status)
     169{
    173170    char *cleanToken = NULL;
    174171    int sLen = 0;
    175172
    176173
    177     // Skip over leading blanks
     174    // Skip over leading whitespace
    178175    while(isspace(**inString)) {
    179176        (*inString)++;
    180177    }
    181178
    182     // Length of next token
     179    // Length of token, not including delimiter
    183180    sLen = strcspn(*inString, delimiter);
    184181    if(sLen) {
    185182
    186         // Create null terminated token
    187         token = psStringNCopy(*inString, sLen);
    188         if(clean) {
    189 
    190             // Remove excess from token
    191             cleanToken = cleanLine(token);
    192             psFree(token);
    193             token = cleanToken;
    194         }
    195 
    196         // Skip to end of token
     183        // Create new, cleaned, and null terminated token
     184        cleanToken = cleanString(*inString, sLen);
     185
     186        // Move to end of token
    197187        (*inString) += sLen;
    198     }
    199 
    200     return token;
     188    } else if(**inString!='\0' && sLen==0) {
     189        *status = 1;
     190    }
     191
     192    return cleanToken;
    201193}
    202194
     
    212204    if(*end != '\0') {
    213205        *status = 1;
     206    } else if(inString==end) {
     207        *status = 1;
    214208    }
    215209
     
    217211}
    218212
    219 /** Returns true or false. 'T', 't', '1', 'F', 'f', and '0' are parsable variations. */
     213/** Returns true or false. 'T', 't', '1', 'F', 'f', and '0' are acceptable, parsable variations. */
    220214bool parseBool(char *inString, int *status)
    221215{
     
    268262        while(*inString!='\0') {
    269263            value = strtod(inString, &end);
     264            if(inString==end) {
     265                *status = 1;
     266                return vec;
     267            }
    270268            switch(elemType) {
    271269            case PS_TYPE_U8:
     
    493491    // While loop to parse the file
    494492    while(fgets(line, MAX_STRING_LENGTH, fp) != NULL) {
     493
     494        // Initialize variables for new line
    495495        linePtr = line;
    496496        lineCount++;
     
    509509                psError(__func__, "More than one @ charecter not allowed. Line %d: %s", lineCount, line);
    510510                continue;
    511             }
     511            } else if(repeatedChars(linePtr, '~') > 0) {
     512                failedLines++;
     513                psError(__func__, "Illegal character. Character:~ Line %d: %s", lineCount, line);
     514                continue;
     515            }
     516
    512517
    513518            // Get metadata item name
    514             strName = getToken(&linePtr, " ", true);
    515             if(strName==NULL) {
    516                 failedLines++;
     519            strName = getToken(&linePtr, " ", &status);
     520            if(strName==NULL || status) {
     521                failedLines++;
     522                status = 0;
    517523                psError(__func__, "Null name not allowed. Line %d: %s", lineCount, line);
    518524                continue;
     
    520526
    521527            // Get the metadata item type
    522             strType = getToken(&linePtr, " ", true);
     528            strType = getToken(&linePtr, " ", &status);
    523529            if(strType==NULL) {
    524530                failedLines++;
     531                status = 0;
    525532                psError(__func__, "Null type not allowed. Line %d: %s", lineCount, line);
    526533                continue;
     
    554561            // Get the metadata item value if there is one. Lines with * don't have values.
    555562            if(mdType != PS_META_ITEM_SET) {
    556                 strValue = getToken(&linePtr, "#", true);
     563                strValue = getToken(&linePtr, "#", &status);
    557564                if(strValue==NULL) {
    558565                    failedLines++;
     566                    status = 0;
    559567                    psError(__func__, "Null value not allowed. Line %d: %s", lineCount, line);
    560568                    continue;
     
    563571
    564572            // Get the metadata item value if there is one. Not all lines will have comments, so NULL is ok.
    565             strComment = getToken(&linePtr,"!!!!", true);
     573            strComment = getToken(&linePtr,"~", &status);
     574            if(status) {
     575                failedLines++;
     576                status = 0;
     577                psError(__func__, "Failed to parse comment. Line %d: %s", lineCount, line);
     578                continue;
     579            }
    566580
    567581            /* If metadata item is found, is not a folder node, and overwrite is allowed, then remove existing
Note: See TracChangeset for help on using the changeset viewer.