IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 23, 2011, 9:33:05 AM (15 years ago)
Author:
rhenders
Message:

New class to encapsulate initialization data

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippToPsps/src/Config.c

    r31011 r31015  
    1515
    1616/**
    17    Gets PS type from string
     17   Gets PS type from string TODO FITS types? should be in Fits class
    1818   */
    1919static int ippToPsps_GetDataType(char *typename) {
     
    199199            default:
    200200                break;
    201 
    202201        }
    203202    }
     
    230229}
    231230
    232 /**
    233   Searches through this table and finds attribute value for a provided key
    234   */
    235 static bool getRowAttribute(Config* this, xmlNode* tableNode,
    236         const char* keyName, const char* keyValue,
    237         const char* attName, char* attValue) {
    238 
    239     xmlNode* node = NULL;
    240 
    241     char buffer[100];
    242 
    243     // loop round all available rows for this table
    244     for (node = tableNode->children; node; node = node->next) {
    245         if (node->type == XML_ELEMENT_NODE) {
    246             //psLogMsg("ippToPsps", PS_LOG_INFO, "Node name '%s'",  (const char*)node->name );
    247 
    248             if (strcmp((const char*)node->name, "row")==0) {
    249                 //psLogMsg("ippToPsps", PS_LOG_INFO, " Looking for key '%s'",  keyName );
    250 
    251                 if (!getAttribute(node, keyName, buffer)) continue;
    252                 //psLogMsg("ippToPsps", PS_LOG_INFO, "Found key '%s' value '%s'",  keyName, buffer );
    253 
    254                 if (strcmp(buffer, keyValue) != 0) continue;
    255                 getAttribute(node, attName, attValue);
    256                 //printf("FOUND %s %s %s %s \n", keyName, buffer, attName, attValue );
    257                 return true;
    258 
    259             }
    260         }
    261     }
    262 
    263     psLogMsg("ippToPsps", PS_LOG_INFO, "Could not find value for '%s' for '%s' with value '%s'", attName, keyName, keyValue );
    264     return false;
    265 }
    266 
    267231/*
    268232   opens an XML file and returns the document
     
    283247    xmlCleanupParser();
    284248
    285     return true;
    286 }
    287 
    288 /**
    289   Gets a value from the 'init' data
    290   */
    291 static bool getInitValue(
    292         Config* this,
    293         const char* tableName,
    294         const char* keyName, const char* keyValue,
    295         const char* attName, char* attValue) {
    296 
    297     bool ret = true;
    298 
    299     psString path = NULL;
    300     psStringAppend(&path, "%s/../init/data.xml", this->configsPath); // TODO nasty
    301 
    302     xmlDoc* doc = openXmlFile(path);
    303 
    304     if (doc == NULL) {
    305         psFree(path);
    306         return false;
    307     }
    308     psFree(path);
    309 
    310     xmlNode* rootElement = xmlDocGetRootElement(doc);
    311 
    312     if (strcmp((const char*)rootElement->name, "tabledata")!=0) {
    313         psError(PS_ERR_IO, false, "Root node of XML is not 'tabledata', as it should be");
    314         return false;
    315     }
    316 
    317     xmlNode* node = NULL;
    318     char tempStr[100];
    319 
    320     // loop round all available tables
    321     for (node = rootElement->children; node; node = node->next) {
    322         if (node->type == XML_ELEMENT_NODE) {
    323 
    324             if (strcmp((const char*)node->name, "table")!=0) continue;
    325             if (!getAttribute(node, "name", tempStr)) continue;
    326             if (strcmp(tempStr, tableName)!=0) continue;
    327 
    328             ret = getRowAttribute(this, node, keyName, keyValue, attName, attValue);
    329             break;
    330         }
    331 
    332     }
    333 
    334     closeXmlFile(doc);
    335 
    336     return ret;
    337 }
    338 
    339 /*
    340    Gets survey ID from survey name
    341    */
    342 static bool getSurveyId(Config* this, const char* surveyType, int8_t* surveyId) {
    343 
    344     char buffer[10];
    345     if (!getInitValue(this, "Survey", "name", surveyType, "surveyID", buffer)) {
    346 
    347         *surveyId = -1;
    348         return false;
    349     }
    350 
    351     *surveyId = atoi(buffer);
    352     return true;
    353 }
    354 
    355 /*
    356    Gets filter ID from filter type
    357    */
    358 static bool getFilterId(Config* this, const char* filterType, int8_t* filterId) {
    359 
    360     char tmp[2];
    361     strncpy(tmp,filterType,1);
    362     tmp[1] = '\0';
    363     char buffer[10];
    364     if (!getInitValue(this, "Filter", "filterType", tmp, "filterID", buffer)) {
    365 
    366         *filterId = -1;
    367         return false;
    368     }
    369 
    370     *filterId = atoi(buffer);
    371249    return true;
    372250}
     
    859737static void destroy(Config* this) {
    860738
    861     if (this != NULL ) {
    862 
    863         // TODO check logic here
    864         for (int i=0;i<this->numOfTables;i++)
    865             free(this->tables[i].columns);
    866 
    867         free(this->tables);
    868         psFree(this->configsPath);
    869         free(this);
    870     }
     739    if (this == NULL) return;
     740
     741    // TODO check logic here
     742    for (int i=0;i<this->numOfTables;i++)
     743        free(this->tables[i].columns);
     744
     745    free(this->tables);
     746    psFree(this->configsPath);
     747    free(this);
    871748}
    872749
     
    882759
    883760    // method pointers
    884     this->getFilterId = getFilterId;
    885     this->getSurveyId = getSurveyId;
    886761    this->createAndPopulateTable = createAndPopulateTable;
    887762    this->populateFromFile = populateFromFile;
Note: See TracChangeset for help on using the changeset viewer.