parse_query_string.c

Go to the documentation of this file.
00001 
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <string.h>
00027 
00028 #include "parse_query_string.h"
00029 
00040 qs_missing_block_t* missing_block_exist(unsigned int sbn, qs_missing_block_t *block_list) {
00041 
00042   qs_missing_block_t *block;
00043 
00044   block = block_list;
00045 
00046   if(block != NULL) {
00047     for(;;) {
00048       if(block->sbn == sbn) {
00049         break;
00050       }           
00051       if(block->next == NULL) {
00052         block = NULL;
00053         break;
00054       }
00055       block = block->next;
00056     }
00057   }
00058 
00059   return block;
00060 }
00061 
00070 void insert_missing_block(qs_missing_block_t *block, query_str_t *qs) {
00071 
00072   qs_missing_block_t *mb;
00073 
00074   mb = qs->block_list;
00075 
00076   while(1) {
00077 
00078     if(mb == NULL) {               
00079       qs->block_list = block;
00080       break;                
00081     }
00082 
00083     if(mb->next == NULL) {
00084         mb->next = block;  
00085         break;   
00086     }  
00087     mb = mb->next;
00088   }
00089 }
00090 
00099 void insert_missing_symbol(qs_missing_symbol_t *symbol, qs_missing_block_t *block) {
00100 
00101   qs_missing_symbol_t *ms;
00102 
00103   ms = block->es_list;
00104 
00105   while(1) {
00106 
00107     if(ms == NULL) {
00108       block->es_list = symbol;
00109       break;
00110     }
00111 
00112     if(ms->next == NULL) {
00113         ms->next = symbol;
00114         break;
00115     }
00116     ms = ms->next;
00117   }
00118 }
00119 
00120 query_str_t* parse_query_string(char *query_str) { 
00121   query_str_t *qs = NULL;
00122 
00123   char *chr = NULL;
00124   char *part = NULL;
00125   char *fileURI = NULL;
00126   char *sbn_list[100] = {NULL};
00127   int nb_of_sbn_part = 0;
00128 
00129   int search_pos = 0;
00130   int i;
00131   int j;
00132 
00133   int first_block;
00134   int last_block;
00135 
00136   int first_symbol;
00137   int last_symbol;
00138 
00139   qs_missing_block_t *ms_blck = NULL;
00140   qs_missing_symbol_t *ms_symb = NULL;
00141 
00142   unsigned int sbn;
00143 
00144   if (!(qs = (query_str_t*)calloc(1, sizeof(query_str_t)))) {
00145     return NULL;
00146   }
00147 
00148   qs->fileURI = NULL;   
00149   qs->block_list = NULL;
00150 
00151   part = strtok(query_str, "&");
00152   fileURI = part;
00153 
00154   part = strtok(NULL, "&");
00155   
00156   while(part != NULL) {
00157     sbn_list[nb_of_sbn_part] = part;
00158     nb_of_sbn_part++;
00159     part = strtok(NULL, "&");
00160   }
00161 
00162   /* fileURI */
00163 
00164   part = strtok(fileURI, "=");
00165 
00166   if(strcmp(part, "fileURI") == 0) {
00167     part = strtok(NULL, "&");
00168     qs->fileURI = parse_uri(part, strlen(part));
00169   }
00170 
00171   if(qs->fileURI == NULL) {
00172     free_query_str(qs);
00173     return NULL;
00174   }
00175 
00176   /* missing SBN and ESI part */
00177 
00178   for(i = 0; i < nb_of_sbn_part; i++) {
00179 
00180     search_pos = 0;
00181     part = strtok(sbn_list[i], "=");
00182   
00183     if(strcmp(part, "SBN") == 0) {
00184 
00185       search_pos += strlen(part);
00186       search_pos++;
00187 
00188       chr = strchr((sbn_list[i] + search_pos), ';');
00189 
00190       if(chr != NULL) {
00191 
00192         part = strtok(NULL, ";");
00193 
00194         search_pos += strlen(part);
00195         search_pos++;
00196 
00197         sbn = atoi(part);
00198 
00199         ms_blck = missing_block_exist(sbn, qs->block_list);
00200         
00201         if(ms_blck == NULL) {
00202 
00203           if(!(ms_blck = (qs_missing_block_t*)calloc(1, sizeof(qs_missing_block_t)))) {
00204             free_query_str(qs);
00205             return NULL;
00206           }
00207           
00208           ms_blck->sbn = sbn;
00209           ms_blck->next = NULL;
00210           ms_blck->es_list = NULL;
00211 
00212           insert_missing_block(ms_blck, qs);
00213         }
00214 
00215         part = strtok(NULL, "=");
00216 
00217         search_pos += strlen(part);
00218         search_pos++;
00219 
00220         if(strcmp(part, "ESI") == 0) {
00221 
00222           chr = strchr((sbn_list[i] + search_pos), '-');
00223 
00224           if(chr != NULL) {
00225             
00226             part = strtok(NULL, "-");
00227             
00228             first_symbol = atoi(part);
00229             
00230             part = strtok(NULL, " ");
00231             
00232             last_symbol = atoi(part);
00233             
00234             for(j = first_symbol; j < (last_symbol + 1); j++) {
00235               
00236               if (!(ms_symb = (qs_missing_symbol_t*)calloc(1, sizeof(qs_missing_symbol_t)))) {
00237                 free_query_str(qs);
00238                 return NULL;
00239               }
00240               
00241               ms_symb->esi = j;
00242               ms_symb->next = NULL;
00243         
00244               insert_missing_symbol(ms_symb, ms_blck);
00245             }
00246           }
00247           else {
00248             
00249             part = strtok(NULL, ",");
00250 
00251             while(part != NULL) {
00252               
00253               if (!(ms_symb = (qs_missing_symbol_t*)calloc(1, sizeof(qs_missing_symbol_t)))) {
00254                 free_query_str(qs);
00255                 return NULL;
00256               }
00257               
00258               ms_symb->esi = atoi(part);
00259               ms_symb->next = NULL;
00260               
00261               insert_missing_symbol(ms_symb, ms_blck);   
00262 
00263               part = strtok(NULL, ",");
00264             }
00265           }
00266         }
00267       }
00268       else {
00269 
00270         chr = strchr(sbn_list[i] + search_pos, '-');
00271         
00272         if(chr != NULL) {
00273 
00274           part = strtok(NULL, "-");
00275         
00276           first_block = atoi(part);
00277 
00278           part = strtok(NULL, " ");
00279 
00280           last_block = atoi(part);
00281 
00282           for(j = first_block; j < (last_block + 1); j++) {
00283             ms_blck = missing_block_exist(j, qs->block_list);
00284 
00285             if(ms_blck == NULL) {   
00286 
00287               if(!(ms_blck = (qs_missing_block_t*)calloc(1, sizeof(qs_missing_block_t)))) {
00288                 free_query_str(qs);
00289                 return NULL;
00290               }
00291 
00292               ms_blck->sbn = j;
00293               ms_blck->next = NULL;
00294               ms_blck->es_list = NULL;
00295           
00296               insert_missing_block(ms_blck, qs);
00297             }
00298           }
00299         }
00300         else {
00301           
00302           part = strtok(NULL, " ");
00303           sbn = atoi(part);
00304           
00305           ms_blck = missing_block_exist(sbn, qs->block_list);
00306 
00307           if(ms_blck == NULL) {
00308 
00309             if(!(ms_blck = (qs_missing_block_t*)calloc(1, sizeof(qs_missing_block_t)))) {
00310               free_query_str(qs);
00311               return NULL;
00312             }
00313                     
00314             ms_blck->sbn = sbn;
00315             ms_blck->next = NULL;
00316             ms_blck->es_list = NULL;
00317 
00318             insert_missing_block(ms_blck, qs); 
00319           }
00320         }
00321       }
00322     }
00323   }
00324  
00325   return qs;
00326 }
00327 
00328 void free_query_str(query_str_t *qs) {
00329 
00330   qs_missing_block_t *mb;
00331   qs_missing_block_t *next_mb;
00332   qs_missing_symbol_t *ms;
00333   qs_missing_symbol_t *next_ms;
00334 
00335   if(qs->fileURI != NULL) {
00336     free_uri(qs->fileURI);
00337   }
00338 
00339   if(qs->block_list != NULL) {
00340 
00341     next_mb = qs->block_list;
00342     
00343     while(next_mb != NULL) {
00344       mb = next_mb;
00345       
00346       next_ms = mb->es_list;
00347       
00348       while(next_ms != NULL) {
00349         ms = next_ms;
00350         next_ms = ms->next;
00351         free(ms);
00352       }
00353       
00354       next_mb = mb->next;
00355       free(mb);
00356     }
00357   }
00358   
00359   if(qs != NULL) {
00360     free(qs);
00361   }
00362 }
00363 
00364 
00365 
00366 
00367 

Generated on Fri Mar 9 19:59:52 2007 for MAD-FCL by  doxygen 1.5.0