apd.c

Go to the documentation of this file.
00001 
00033 #include <stdlib.h>
00034 #include <stdio.h>
00035 #include <string.h>
00036 
00037 #include <expat.h>
00038 
00039 #include "apd.h"    
00040 
00041 #ifdef USE_FILE_REPAIR
00042 
00047 apd_t *apd;
00048 
00058 static void startElementHandler_APD(void *userData, const char *name, const char **atts) {
00059 
00060   strcpy((char*)userData, name);
00061 
00062   while(*atts != NULL) {
00063 
00064     if(!strcmp(name, "postFileRepair")) {
00065 
00066       if(apd->postFileRepair == NULL) {
00067 
00068         if(!(apd->postFileRepair = (postFileRepair_t*)calloc(1, sizeof(postFileRepair_t)))) {
00069           printf("Could not alloc memory for postFileRepair!\n");
00070           return;          
00071         }
00072 
00073         /* initialise */
00074 
00075         apd->postFileRepair->offsetTime = 0;
00076         apd->postFileRepair->randomTimePeriod = 0;
00077         apd->postFileRepair->serviceURI_List = NULL;
00078       }
00079 
00080       if(!strcmp(*atts, "offsetTime")) {
00081 
00082         apd->postFileRepair->offsetTime = atoi(*(++atts));
00083       }
00084       else if(!strcmp(*atts, "randomTimePeriod")) {
00085 
00086         apd->postFileRepair->randomTimePeriod = atoi(*(++atts));
00087       }
00088     }
00089 
00090     else if(!strcmp(name, "bmFileRepair")) {
00091 
00092       if(apd->bmFileRepair == NULL) {
00093 
00094         if(!(apd->bmFileRepair = (bmFileRepair_t*)calloc(1, sizeof(bmFileRepair_t)))) {
00095           printf("Could not alloc memory for bmFileRepair!\n");
00096           return;
00097         }
00098 
00099         /* initialise */
00100 
00101         strcpy(apd->bmFileRepair->sessionDescriptionURI, "");
00102 
00103       }
00104 
00105       if(!strcmp(*atts, "sessionDescriptionURI")) {
00106 
00107         strcpy(apd->bmFileRepair->sessionDescriptionURI, *(++atts));
00108       }
00109     }
00110 
00111     /* attribute in unknown element */
00112 
00113     else {
00114       atts ++;
00115     }
00116 
00117     atts++;
00118 
00119   }
00120 }
00121 
00130 static void endElementHandler_APD(void *userData, const char *name) {
00131 
00132 /* Let's remove the last element name from the userData */
00133   strcpy((char*)userData, "");
00134 }
00135 
00145 static void characterDataHandler_APD(void *userData, const XML_Char *s, int len) {
00146 
00147   serviceURI_t *tmp_serviceURI;
00148   serviceURI_t *serviceURI;
00149   char *value;
00150   
00151   value = (char*)calloc(len+1, sizeof(char));
00152   memcpy(value, s, len);
00153 
00154   /* userData includes the element (tag) name if we are inside some element */
00155 
00156   if(strcmp((const char*)userData, "") == 0) {
00157 
00158   }
00159   else if(strcmp((const char*)userData, "serviceURI") == 0) {
00160 
00161     if(!(serviceURI = (serviceURI_t*)calloc(1, sizeof(serviceURI_t)))) {
00162       printf("Could not alloc memory for serviceURI!\n");
00163       return;
00164     }
00165 
00166     strcpy(serviceURI->URI, value);
00167     serviceURI->prev = NULL;
00168     serviceURI->next = NULL;
00169 
00170     tmp_serviceURI = apd->postFileRepair->serviceURI_List;
00171 
00172     while(1) {
00173 
00174       if(tmp_serviceURI == NULL) {
00175         apd->postFileRepair->serviceURI_List = serviceURI;
00176         break;
00177       }
00178 
00179       if(tmp_serviceURI->next == NULL) {
00180         serviceURI->prev = tmp_serviceURI;
00181         tmp_serviceURI->next = serviceURI;
00182         break;
00183       }
00184 
00185       tmp_serviceURI = tmp_serviceURI->next;
00186     }
00187   }
00188 
00189   free(value);
00190 }
00191 
00192 apd_t* decode_apd_config(char *apd_config) {
00193 
00194         XML_Parser parser = XML_ParserCreate(NULL);
00195         size_t len;
00196         char tag[100];
00197 
00198         len = strlen(apd_config);
00199 
00200         if(!(apd = (apd_t*)calloc(1, sizeof(apd_t)))) {
00201                 printf("Could not alloc memory for apd!\n");
00202                 XML_ParserFree(parser);
00203                 return NULL;
00204         }
00205 
00206         XML_SetUserData(parser, tag);
00207         XML_SetStartElementHandler(parser, startElementHandler_APD);
00208         XML_SetEndElementHandler(parser, endElementHandler_APD);
00209         XML_SetCharacterDataHandler(parser, characterDataHandler_APD);
00210 
00211         if(XML_Parse(parser, apd_config, len, 1) == XML_STATUS_ERROR) {
00212                 fprintf(stderr, "%s at line %d\n",
00213                                 XML_ErrorString(XML_GetErrorCode(parser)),
00214                                 XML_GetCurrentLineNumber(parser));
00215                 XML_ParserFree(parser);
00216                 return NULL;
00217         }
00218 
00219         XML_ParserFree(parser);
00220 
00221         return apd;
00222 }
00223 
00224 void FreeAPD(apd_t *apd) {              
00225 
00226         serviceURI_t *tmp_serviceURI;
00227 
00228         if(apd->postFileRepair != NULL) {
00229 
00230                 while(1) {
00231 
00232                         tmp_serviceURI = apd->postFileRepair->serviceURI_List;
00233 
00234                         if(tmp_serviceURI != NULL) {
00235                                 apd->postFileRepair->serviceURI_List = tmp_serviceURI->next;
00236                                 free(tmp_serviceURI);
00237                         }
00238                         else {
00239                                 break;
00240                         }
00241                 }
00242 
00243                 free(apd->postFileRepair);
00244         }
00245 
00246         if(apd->bmFileRepair != NULL) {
00247                 free(apd->bmFileRepair);
00248         }
00249 
00250         free(apd);
00251 }
00252 
00253 #endif
00254 

Generated on Fri Mar 9 20:08:51 2007 for MAD-FCL by  doxygen 1.5.0