#include #include #include #include "drvMpsLinkNode.h" #define MPS_NODE_NUM_ATTRS (5) #define MPS_NODE_NUM_CHANS (96) #define MPS_NODE_NUM_RELS (2) #define MPS_CHAN_NUM_ATTRS (11) #define MPS_ATTR_LENGTH (100) char node_attributes[MPS_NODE_NUM_ATTRS][MPS_ATTR_LENGTH]={"zid","linknodeid","pvprefix","rackelevation","racknumber"}; char node_relationships[MPS_NODE_NUM_RELS][MPS_ATTR_LENGTH]={"inputchannels","outputchannels"}; char chan_attributes[MPS_CHAN_NUM_ATTRS][MPS_ATTR_LENGTH]={"zid","debouncetimemicroseconds","descript","inputcard","inputcardchannel","isautorecoverable","location","inputname","pv","state0","state1"}; /* Given a line from the xml file in the node object definition, extract which node attribute this is and get its value while we're at it */ int parseNodeAttrAndVal(const char* str, mpsNode_t* pNode) { int ii = 0; int jj = 0; int kk = 0; int index = -1; int fillVal = 0; char strVal[MPS_ATTR_LENGTH]; int found = 0; int done = 0; while (!found && ii < MPS_NODE_NUM_ATTRS) { /*printf("looking for %s in %s\n", node_attributes[ii], str);*/ if (strstr(str, node_attributes[ii]) != NULL) { /* match found. now we know which attr this is */ index = ii; found = 1; printf("match found for attribute[%d], '%s'\n", index, node_attributes[ii]); } ii++; } /* Cruise through to end of "attribute ...>", saving next chars into temp string until you find '<'. Don't write '>' into the temp string Add a null on the end of temp string */ jj=0; while (!done && jj') { /* next char is start of value */ /*printf ("found '>'\n");*/ fillVal = 1; kk=0; } } } jj++; } printf ("retrieved '%s'\n",strVal); switch(index) { /*case 0: is zid */ case 1: pNode->linknodeid = atoi(strVal); printf("mps node attribute linknode id set to %d\n", pNode->linknodeid); break; /*case 2: strcpy(pNode->pvprefix,strVal); printf("mps node attribute pvprefix set to '%s'\n", pNode->pvprefix); break;*/ case 3: pNode->rackelevation = atoi(strVal); printf("mps node attribute rackelevation set to %d\n", pNode->rackelevation); break; case 4: strcpy(pNode->racknumber,strVal); printf("mps node attribute racknumber set to '%s'\n", pNode->racknumber); break; default: printf("Unrecognized index = %d\n", index); } if (index != -1) { return 0; } else { return -1; } }; /* Given a line from the xml file in the node object definition, extract which channel attribute this is and get its value while we're at it */ int parseChanAttrAndVal(const char* str, mpsInputChannel_t* pChan) { int ii = 0; int jj = 0; int kk = 0; int index = -1; int fillVal = 0; char strVal[MPS_ATTR_LENGTH]; int found = 0; int done = 0; while (!found && ii < MPS_CHAN_NUM_ATTRS) { /* printf("looking for %s in %s\n", chan_attributes[ii], str);*/ if (strstr(str, chan_attributes[ii]) != NULL) { /* match found. now we know which attr this is */ index = ii; found = 1; printf("match found for attribute[%d], '%s'\n", index, chan_attributes[ii]); } ii++; } /* Cruise through to end of "attribute ...>", saving next chars into temp string until you find '<'. Don't write '>' into the temp string Add a null on the end of temp string */ jj=0; while (!done && jj') { /* next char is start of value */ /*printf ("found '>'\n");*/ fillVal = 1; kk=0; } } } jj++; } printf ("retrieved '%s'\n",strVal); switch(index) { /*case 0: is zid */ case 1: pChan->debouncetimemicroseconds = atoi(strVal); printf("mps chan attribute debouncetimemicroseconds set to %d\n", pChan->debouncetimemicroseconds); break; case 2: strcpy(pChan->descript,strVal); printf("mps chan attribute descript set to '%s'\n", pChan->descript); break; case 3: pChan->inputcard = atoi(strVal); printf("mps chan attribute inputcard set to %d\n", pChan->inputcard); break; case 4: pChan->inputcardchannel = atoi(strVal); printf("mps chan attribute inputcardchannel set to %d\n", pChan->inputcardchannel); break; case 5: pChan->isautorecoverable = atoi(strVal); printf("mps chan attribute isautorecoverable set to %d\n", pChan->isautorecoverable); break; case 6: strcpy(pChan->location,strVal); printf("mps chan attribute location set to '%s'\n", pChan->location); break; case 7: strcpy(pChan->inputname,strVal); printf("mps chan attribute inputname set to '%s'\n", pChan->inputname); break; case 8: strcpy(pChan->pv,strVal); printf("mps chan attribute pv set to '%s'\n", pChan->pv); break; case 9: strcpy(pChan->state0,strVal); printf("mps chan attribute state0 set to '%s'\n", pChan->state0); break; case 10: strcpy(pChan->state1,strVal); printf("mps chan attribute state1 set to '%s'\n", pChan->state1); break; default: printf("Unrecognized index = %d\n", index); } if (index != -1) { return 0; } else { return -1; } }; int fillZid(int* dest, const char* str) { char *token; char *search = "id"; token = strtok((char*)str, search); printf("token is %s\n",token); /*doesn't work strcpy(search, "\n"); token = strtok(str, search); printf("token is %s\n",token); *dest = atoi(token); printf("%d\n",*dest); */ *dest = 0; /* blah */ return 0; } int main(int argc, char **argv) { char filename[100]; FILE *fp; int ii = 0; /* holds number of current line read in from xml file, starting from 0 */ mpsNode_t thisNode; char data[MPS_LINE_LENGTH]; int rc = 0; if (argc == 2) { strcpy(filename, argv[1]); printf("%s parsing: '%s'\n", argv[0],filename); } else { printf("%s usage: filename\n", argv[0]); exit(1); } fp = fopen(filename, "r"); /* What do we know? - only one node, many input channels - sequential - every node has all attributes - every channel has all attributes - node attribute names differ from channel attribute names - node attribute names and channel attribute names differ from xml keywords (i.e. "name") - this works without goto - can live without knowing zid number of object */ thisNode.numChannels = 0; /* we increment this later as channels are found */ while (fgets(data, MPS_LINE_LENGTH, fp) != NULL) { /* echo back file contents */ fprintf(stderr,"%d: %s\n", ii, data); ii++; /* number of lines of xml read in */ /* Check if this is a node object */ if ((strstr(data,"