diff --git a/src/client.c b/src/client.c index dbe0d52b3f99ee45d89a6181305a7305daa2e9ad..51a360d5427aaa2e4e458e7d9146104926d3e3b1 100644 --- a/src/client.c +++ b/src/client.c @@ -244,3 +244,57 @@ void availability(struct soap *soap, char *url, char *inep, char *macAddr) exit(6); } } + +/*---------------------------------------------------------------------------- + * Summary: Read XML data file and return the xmlInventory string. + * Parameters: + * basedir String with directory path + * Return: String with xml data. + */ +char *read_xml_file(char *basedir) +{ + FILE *xmlFile; + int aux; + int i = 0; + int tam = 1; + char filePath[strlen(basedir) + strlen(COLLECTDATAFILE) + 1]; + char *xmlInventory = NULL; + + /* Construct filePath */ + strcpy(filePath, basedir); + strcat(filePath, COLLECTDATAFILE); + if (DEBUG) + { + printf("%s %s %s\n", basedir, COLLECTDATAFILE, filePath); + } + + /* Open XML collect data file and check for errors */ + xmlFile = fopen(filePath, "r"); + if ( xmlFile == NULL ) + { + fprintf(stderr, "Problem while trying to open the "); + fprintf(stderr, "collect_data.xml file.\n"); + return NULL; + } + + /* Read and copy from data file to string */ + xmlInventory = (char *) malloc(sizeof(char)); + while ( (aux = fgetc(xmlFile)) != EOF ) + { + if ( tam <= i ) + { + tam *= 2; + xmlInventory = (char *) realloc(xmlInventory, tam * sizeof(char)); + } + if (xmlInventory == NULL) + { + fprintf(stderr, "Memory allocation error for xmlInventory.\n"); + return NULL; + } + xmlInventory[i++] = aux; + } + + /* Close file and return a pointer to "xmlInventory" */ + fclose(xmlFile); + return xmlInventory; +}