IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 9912 for trunk/psLib


Ignore:
Timestamp:
Nov 8, 2006, 2:03:44 PM (20 years ago)
Author:
jhoblitt
Message:

improved error checking, coding style, & structure in parseLine()
preparing to split parseLine() into multiple functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/types/psMetadataConfig.c

    r9874 r9912  
    1010*  @author Eric Van Alst, MHPCC
    1111*
    12 *  @version $Revision: 1.111 $ $Name: not supported by cvs2svn $
    13 *  @date $Date: 2006-11-07 05:09:32 $
     12*  @version $Revision: 1.112 $ $Name: not supported by cvs2svn $
     13*  @date $Date: 2006-11-09 00:03:44 $
    1414*
    1515*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    11011101                 psBool overwrite)
    11021102{
    1103     psBool              returnValue    = true;
    1104     psMetadataFlags     flags          = PS_META_DEFAULT;
    1105     char*               keyName        = NULL;
    1106     psS32               status         = 0;
    1107     psS32               limit          = 0;
    1108     psMetadata*         tempTemplate   = NULL;
    1109     char*               strType        = NULL;
    1110     psArray*            typeArray      = NULL;
    1111     psArray*            templateArray  = NULL;
    1112     p_psParseLevelInfo* upperLevelInfo = NULL;
    1113     p_psParseLevelInfo* lowerLevelInfo = NULL;
    1114     psBool              addStatus      = 0;
     1103    psMetadataFlags     flags  = PS_META_DEFAULT;
     1104    psS32               status = 0;
    11151105
    11161106    // Set flags if overwrite specified
    1117     if(overwrite) {
     1107    if (overwrite) {
    11181108        flags = PS_META_REPLACE;
    11191109    }
    11201110
    1121     // If line is not a comment or blank, then extract data
    1122     if(!ignoreLine(linePtr)) {
    1123 
    1124         // Check for more than one '@' in a line
    1125         if(repeatedChars(linePtr, '@') > 1) {
     1111    // If line is a comment or blank, then extract data
     1112    if (ignoreLine(linePtr)) {
     1113        // do nothing and return
     1114        return true;
     1115    }
     1116
     1117    // Check for more than one '@' in a line
     1118    if (repeatedChars(linePtr, '@') > 1) {
     1119        psError(PS_ERR_IO, true,
     1120                _("More than one '@' character not allowed.  Found on line %u."), lineCount);
     1121        return false;
     1122    }
     1123
     1124    // Get metadata item name
     1125    char *keyName = getToken(&linePtr, " ", &status, true);
     1126    if (status) {
     1127        psError(PS_ERR_IO, true,
     1128                _("Failed to read item key name on line %u."), lineCount);
     1129        psFree(keyName);
     1130        return false;
     1131    }
     1132
     1133    // Check for special keyName values "TYPE", "END"
     1134    if (strcmp(keyName,"TYPE") == 0 ) {
     1135        // Get the type name
     1136        char *strType = getToken(&linePtr, " ", &status, true);
     1137        if(!strType) {
     1138            psError(PS_ERR_IO,true,
     1139                    _("Failed to read item type on line %u."), lineCount);
     1140            psFree(keyName);
     1141            return false;
     1142        }
     1143        if (status) {
    11261144            psError(PS_ERR_IO, true,
    1127                     _("More than one '%c' character not allowed.  Found on line %u."),
    1128                     '@', lineCount);
     1145                    _("Failed to read item type on line %u."), lineCount);
     1146            psFree(strType);
     1147            psFree(keyName);
    11291148            return false;
    11301149        }
    11311150
    1132         // Get metadata item name
    1133         keyName = getToken(&linePtr, " ", &status,true);
    1134         /*        if(status) {
    1135                     psError(PS_ERR_IO, true,
    1136                             _("Failed to read a metadata %s on line %u."), "keyName", lineCount);
    1137                     psFree(keyName);
    1138                     return false;
    1139                 }
    1140         */
    1141         // Check for special keyName values "TYPE", "END"
    1142         if(strcmp(keyName,"TYPE") == 0 ) {
    1143             // Get the type name
    1144             strType = getToken(&linePtr," ",&status,true);
    1145             if(strType == NULL) {
     1151        // attempt to parse the TYPE line into a template metadata
     1152        psMetadata *tempTemplate = getMetadataType(linePtr);
     1153        if (!tempTemplate) {
     1154            psError(PS_ERR_IO,true,
     1155                    _("Metadata type '%s', found on line %u, is invalid."),
     1156                    strType,lineCount);
     1157            psFree(keyName);
     1158            psFree(strType);
     1159            return false;
     1160        }
     1161
     1162        // Access type array
     1163        psArray *typeArray = ((p_psParseLevelInfo*)(levelArray->data[*level]))->typeArray;
     1164
     1165        // Check if type already exists in array
     1166        for (psS32 k = 0; k < psArrayLength(typeArray); k++) {
     1167            // Compare type name with the list of current types
     1168            if (strcmp(strType,(char*)typeArray->data[k]) == 0) {
    11461169                psError(PS_ERR_IO,true,
    1147                         _("Failed to read a metadata %s on line %u."), "type", lineCount);
    1148                 psFree(keyName);
    1149                 return false;
    1150             }
    1151             tempTemplate = getMetadataType(linePtr);
    1152             // Check if type was parsed succesfully
    1153             if(tempTemplate != NULL) {
    1154                 // Access type array
    1155                 typeArray = ((p_psParseLevelInfo*)(levelArray->data[*level]))->typeArray;
    1156                 // Check if type already exists in array
    1157                 for(psS32 k=0; k < typeArray->n; k++) {
    1158                     // Compare type name with the list of current types
    1159                     if(strcmp(strType,(char*)typeArray->data[k]) == 0) {
    1160                         psError(PS_ERR_IO,true,
    1161                                 _("Specified type, %s, on line %u is already defined."),
    1162                                 strType,lineCount);
    1163                         psFree(tempTemplate);
    1164                         psFree(keyName);
    1165                         psFree(strType);
    1166                         return false;
    1167                     }
    1168                 }
    1169                 // Add key name to array of type
    1170                 typeArray = psArrayAdd(typeArray,1,strType);
    1171                 // Add template to array of templates
    1172                 templateArray = ((p_psParseLevelInfo*)(levelArray->data[*level]))->templateArray;
    1173                 templateArray = psArrayAdd(templateArray,1,tempTemplate);
     1170                        _("Specified type, %s, on line %u is already defined."),
     1171                        strType, lineCount);
    11741172                psFree(tempTemplate);
    1175             } else {
    1176                 psError(PS_ERR_IO,true,
    1177                         _("Metadata type '%s', found on line %u, is invalid."),
    1178                         strType,lineCount);
    11791173                psFree(keyName);
    11801174                psFree(strType);
    11811175                return false;
    11821176            }
    1183             psFree(strType);
    1184         } else if (strcmp(keyName,"END") == 0 ) {
    1185             if(*level > 0) {
    1186                 upperLevelInfo = ((p_psParseLevelInfo*)(levelArray->data[*level-1]));
    1187                 lowerLevelInfo = ((p_psParseLevelInfo*)(levelArray->data[*level]));
    1188                 // Check name in nonunique list
    1189                 for(psS32 k=0; k < upperLevelInfo->nonUniqueKeyArray->n; k++) {
    1190                     if(strcmp(upperLevelInfo->nonUniqueKeyArray->data[k],lowerLevelInfo->name) == 0) {
    1191                         flags = PS_META_DUPLICATE_OK;
    1192                         break;
    1193                     }
    1194                 }
    1195 
    1196                 char *strValue = NULL;
    1197                 strValue = getToken(&linePtr, "#", &status,true);
    1198                 char *strComment = NULL;
    1199                 strComment = getToken(&linePtr,"~", &status,true);
    1200 
    1201                 // Add metadata to upper level metadata
    1202                 addStatus = psMetadataAdd(upperLevelInfo->metadata,
    1203                                           PS_LIST_TAIL,lowerLevelInfo->name,
    1204                                           PS_DATA_METADATA | flags,
    1205                                           strComment, //"",
    1206                                           lowerLevelInfo->metadata);
    1207                 psFree(strValue);
    1208                 psFree(strComment);
    1209                 if(!addStatus) {
    1210                     psFree(keyName);
    1211                     return false;
    1212                 } else {
    1213                     // Remove lower info level
    1214                     /*                    if(!psArrayRemoveIndex(levelArray, *level)) {
    1215                                             psFree(keyName);
    1216                                             return false;
    1217                                         }
    1218                                         */
    1219                     psArrayRemoveIndex(levelArray, *level);
    1220                     (*level)--;
    1221                 }
    1222             } else {
    1223                 psFree(keyName);
    1224                 return false;
    1225             }
    1226         } else {
    1227             // Check if key name present in array of non-unique key names
    1228             limit = ((p_psParseLevelInfo*)(levelArray->data[*level]))->nonUniqueKeyArray->n;
    1229             for(psS32 k=0; k < limit; k++) {
    1230                 char* name = (char*)((p_psParseLevelInfo*)
    1231                                      (levelArray->data[*level]))->nonUniqueKeyArray->data[k];
    1232                 if(strcmp(name,keyName) == 0) {
    1233                     flags = PS_META_DUPLICATE_OK;
    1234                 }
    1235             }
    1236             // Parse metadataItem
    1237             if(!parseMetadataItem(keyName,level, levelArray, linePtr, lineCount, flags)) {
    1238                 psFree(keyName);
    1239                 return false;
    1240             }
    1241         }
     1177        }
     1178
     1179        // Add key name to array of type
     1180        typeArray = psArrayAdd(typeArray, 1, strType);
     1181        psFree(strType);
     1182
     1183        // Add template to array of templates
     1184        psArray *templateArray = ((p_psParseLevelInfo*)(levelArray->data[*level]))->templateArray;
     1185        psArrayAdd(templateArray, 1, tempTemplate);
     1186        psFree(tempTemplate);
     1187
     1188        return true;
     1189    }
     1190
     1191    if (strcmp(keyName, "END") == 0) {
     1192        if ((*level) < 1) {
     1193            psFree(keyName);
     1194            return false;
     1195        }
     1196
     1197        # if 1
     1198        p_psParseLevelInfo *upperLevelInfo = ((p_psParseLevelInfo*)(levelArray->data[*level-1]));
     1199        p_psParseLevelInfo *lowerLevelInfo = ((p_psParseLevelInfo*)(levelArray->data[*level]));
     1200        // Check name in nonunique list
     1201        for (psS32 k = 0; k < psArrayLength(upperLevelInfo->nonUniqueKeyArray); k++) {
     1202            if (strcmp(upperLevelInfo->nonUniqueKeyArray->data[k], lowerLevelInfo->name) == 0) {
     1203                flags = PS_META_DUPLICATE_OK;
     1204                break;
     1205            }
     1206        }
     1207        #endif
     1208
     1209        char *strValue = NULL;
     1210        strValue = getToken(&linePtr, "#", &status, true);
     1211        char *strComment = NULL;
     1212        // XXX why is there a ~ here?
     1213        strComment = getToken(&linePtr, "~", &status, true);
     1214
     1215        // Add metadata to upper level metadata
     1216        if (!psMetadataAdd(upperLevelInfo->metadata,
     1217                           PS_LIST_TAIL, lowerLevelInfo->name,
     1218                           PS_DATA_METADATA | flags,
     1219                           strComment, //"",
     1220                           lowerLevelInfo->metadata)) {
     1221            // This is pretty much a non-recoverable error - perhaps there
     1222            // should be a way of signalling non-recoverable parse errors
     1223            psError(PS_ERR_UNKNOWN, false, "failed to add metadata item");
     1224            psFree(strValue);
     1225            psFree(strComment);
     1226            psFree(keyName);
     1227            return false;
     1228        }
     1229
     1230        // Remove lower info level
     1231        if(!psArrayRemoveIndex(levelArray, *level)) {
     1232            psError(PS_ERR_UNKNOWN, false, "failed to remove array item");
     1233            psFree(strValue);
     1234            psFree(strComment);
     1235            psFree(keyName);
     1236        }
     1237
     1238        // the END just moved us up one nesting level
     1239        (*level)--;
     1240
     1241        psFree(strValue);
     1242        psFree(strComment);
    12421243        psFree(keyName);
    1243     }
    1244     return returnValue;
     1244
     1245        return true;
     1246    }
     1247
     1248    // Check if key name present in array of non-unique key names
     1249    psS32 limit = ((p_psParseLevelInfo*)(levelArray->data[*level]))->nonUniqueKeyArray->n;
     1250    for (psS32 k = 0; k < limit; k++) {
     1251        char *name = (char*)((p_psParseLevelInfo*)
     1252                             (levelArray->data[*level]))->nonUniqueKeyArray->data[k];
     1253        if (strcmp(name, keyName) == 0) {
     1254            flags = PS_META_DUPLICATE_OK;
     1255        }
     1256    }
     1257
     1258    // Parse metadataItem
     1259    if (!parseMetadataItem(keyName, level, levelArray, linePtr, lineCount, flags)) {
     1260        psFree(keyName);
     1261        return false;
     1262    }
     1263    psFree(keyName);
     1264
     1265    return true;
    12451266}
    12461267
Note: See TracChangeset for help on using the changeset viewer.