IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 30, 2008, 11:35:51 AM (18 years ago)
Author:
Paul Price
Message:

Adding pmConceptsInterpolate to interpolate the {CONCEPT} within a string.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/concepts/pmConcepts.c

    r16512 r17864  
    11981198    return true;
    11991199}
     1200
     1201
     1202// Interpolate the concept.  Generalises the FPA/Chip/Cell
     1203#define CONCEPT_INTERPOLATE(SOURCE, NAME) \
     1204    if (strncmp(concept, NAME, 4) == 0) { \
     1205        if (!(SOURCE)) { \
     1206            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Cannot interpolate %s because %s not provided", \
     1207                    concept, NAME); \
     1208            psFree(string); \
     1209            return NULL; \
     1210        } \
     1211        psMetadataItem *item = psMetadataLookup((SOURCE)->concepts, concept); /* Item with concept */ \
     1212        if (!item) { \
     1213            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Can't find concept %s in %s", concept, NAME); \
     1214            psFree(string); \
     1215            return NULL; \
     1216        } \
     1217        \
     1218        psString value = psMetadataItemParseString(item); /* Value of concept */ \
     1219        if (!value) { \
     1220            psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to parse concept %s", concept); \
     1221            psFree(string); \
     1222            return NULL; \
     1223        } \
     1224        \
     1225        char replace[length + 2];       /* String to replace with value */ \
     1226        replace[0] = '{'; \
     1227        strcpy(replace + 1, concept); \
     1228        strcpy(replace + length + 1, "}"); \
     1229        \
     1230        psTrace("psModules.concepts", 10, "Interpolating concept %s for %s", replace, value); \
     1231        \
     1232        if (!psStringSubstitute(&string, value, replace)) { \
     1233            psError(PS_ERR_UNKNOWN, false, "Unable to replace concept %s", concept); \
     1234            psFree(string); \
     1235            return NULL; \
     1236        } \
     1237        \
     1238        continue; \
     1239    }
     1240
     1241
     1242// XXX Could make the concept delimiters, currently '{' and '}', configurable
     1243psString pmConceptsInterpolate(const char *input,
     1244                               const pmFPA *fpa,
     1245                               const pmChip *chip,
     1246                               const pmCell *cell
     1247    )
     1248{
     1249    PS_ASSERT_STRING_NON_EMPTY(input, NULL);
     1250
     1251    psString string = psStringCopy(input); // Interpolated string, to return
     1252
     1253    char *start;                        // Start of a concept
     1254    while ((start = strchr(string, '{'))) {
     1255        char *stop = strchr(start, '}'); // End of a concept
     1256        int length = stop - start;      // Length of the concept name, including terminating \0
     1257        char concept[length];  // Name of concept
     1258        strncpy(concept, start + 1, length - 1);
     1259        concept[length] = '\0';
     1260
     1261        psTrace("psModules.concepts", 7, "Interpolating concept %s", concept);
     1262
     1263        CONCEPT_INTERPOLATE(fpa,  "FPA");
     1264        CONCEPT_INTERPOLATE(chip, "CHIP");
     1265        CONCEPT_INTERPOLATE(cell, "CELL");
     1266
     1267        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unrecognised concept: %s", concept);
     1268        psFree(string);
     1269        return NULL;
     1270    }
     1271
     1272    return string;
     1273}
Note: See TracChangeset for help on using the changeset viewer.