helpers.c

Go to the documentation of this file.
00001 
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <sys/types.h>
00027 #include <sys/stat.h>
00028 #include <unistd.h>
00029 #include <string.h>
00030 
00031 #include "helpers.h"
00032 
00033 int parse_repair_conf_file(char *repair_conf_file, repair_arguments_t *ra) {
00034   
00035   char *buf = NULL;
00036   FILE *fp;
00037   struct stat file_stats;
00038   int nbytes;
00039   
00040   char *tmp = NULL;
00041   
00042   if(stat(repair_conf_file, &file_stats) == -1) {
00043     printf("Error: %s is not valid file name\n", repair_conf_file);
00044     fflush(stdout);
00045     return -1;
00046   }
00047   
00048   /* Allocate memory for buf */
00049   if(!(buf = (char*)calloc((file_stats.st_size + 1), sizeof(char)))) {
00050     printf("Could not alloc memory for buffer!\n");
00051     fflush(stdout);
00052     return -1;
00053   }
00054   
00055   if((fp = fopen(repair_conf_file, "rb")) == NULL) {
00056     printf("Error: unable to open file %s\n", repair_conf_file);
00057     fflush(stdout);
00058     free(buf);
00059     return -1;
00060   }
00061   
00062   nbytes = fread(buf, 1, file_stats.st_size, fp);
00063   
00064   if(nbytes <= 0) {
00065     free(buf);
00066     fclose(fp);
00067     return -1;
00068   }
00069   
00070   fclose(fp);
00071   
00072   tmp = strtok (buf, "=");
00073   
00074   while(tmp != NULL) {
00075     if(strcmp(tmp, "RepairMethod") == 0) {
00076       tmp = strtok(NULL, "\n");
00077       memset(ra->repair_method, 0, MAX_PATH_LENGTH);
00078       strcpy(ra->repair_method, tmp);
00079       
00080       if(ra->repair_method[strlen(ra->repair_method)-1] == '\r') {
00081         ra->repair_method[strlen(ra->repair_method)-1] = '\0';
00082       }
00083     }
00084     else if(strcmp(tmp, "RequestedBlocksFile") == 0) {
00085       tmp = strtok(NULL, "\n");
00086       memset(ra->requested_blocks_file, 0, MAX_PATH_LENGTH);
00087       
00088       strcpy(ra->requested_blocks_file, tmp);
00089       
00090       if(ra->requested_blocks_file[strlen(ra->requested_blocks_file)-1] == '\r') {
00091         ra->requested_blocks_file[strlen(ra->requested_blocks_file)-1] = '\0';
00092       }
00093     }
00094     else if(strcmp(tmp, "PTMRepairSDPFile") == 0) {
00095       tmp = strtok(NULL, "\n");
00096       memset(ra->ptm_repair_sdp_file, 0, MAX_PATH_LENGTH);
00097       strcpy(ra->ptm_repair_sdp_file, tmp);
00098       
00099       if(ra->ptm_repair_sdp_file[strlen(ra->ptm_repair_sdp_file)-1] == '\r') {
00100         ra->ptm_repair_sdp_file[strlen(ra->ptm_repair_sdp_file)-1] = '\0';
00101       }
00102     }
00103     else if(strcmp(tmp, "FluteConfFile") == 0) {
00104       tmp = strtok(NULL, "\n");
00105       memset(ra->flute_conf_file, 0, MAX_PATH_LENGTH);
00106       strcpy(ra->flute_conf_file, tmp);
00107       
00108       if(ra->flute_conf_file[strlen(ra->flute_conf_file)-1] == '\r') {
00109         ra->flute_conf_file[strlen(ra->flute_conf_file)-1] = '\0';
00110       }
00111     }
00112     
00113     tmp = strtok (NULL, "=");
00114   }
00115   
00116   free(buf);
00117   
00118   return 0;
00119 }
00120 
00121 int parse_flute_conf_file(repair_arguments_t *ra) {
00122   
00123   char *buf = NULL;
00124   FILE *fp;
00125   struct stat file_stats;
00126   int nbytes;
00127   char *tmp = NULL;
00128   
00129   if(stat(ra->flute_conf_file, &file_stats) == -1) {
00130     printf("Error: %s is not valid file name\n", ra->flute_conf_file);
00131     fflush(stdout);
00132     return -1;
00133   }
00134   
00135   if(!(buf = (char*)calloc((file_stats.st_size + 1), sizeof(char)))) {
00136     printf("Could not alloc memory for buffer!\n");
00137     fflush(stdout);
00138     return -1;
00139   }
00140   
00141   if((fp = fopen(ra->flute_conf_file, "rb")) == NULL) {
00142     printf("Error: unable to open file %s\n", ra->flute_conf_file);
00143     fflush(stdout);
00144     free(buf);
00145     return -1;
00146   }
00147   
00148   nbytes = fread(buf, 1, file_stats.st_size, fp); 
00149   
00150   if(nbytes <= 0) {
00151     free(buf);
00152     fclose(fp);
00153     return -1;
00154   }
00155   fclose(fp);
00156   
00157   tmp = strtok (buf, "=");
00158   
00159   while(tmp != NULL) {
00160     if(strcmp(tmp, "BaseDir") == 0) {
00161       tmp = strtok(NULL, "\n");
00162       
00163       memset(ra->base_dir, 0, MAX_PATH_LENGTH);
00164       strcpy(ra->base_dir, tmp);
00165       
00166       if(ra->base_dir[strlen(ra->base_dir)-1] == '\r') {
00167         ra->base_dir[strlen(ra->base_dir)-1] = '\0';
00168       }
00169     }
00170     else if(strcmp(tmp, "FECRatio") == 0) {
00171       tmp = strtok(NULL, "\n");
00172       ra->fec_ratio = (unsigned short)atoi(tmp);
00173     }
00174     else if(strcmp(tmp, "FDTFile") == 0) {
00175       tmp = strtok(NULL, "\n");
00176 
00177       memset(ra->fdt_file, 0, MAX_PATH_LENGTH);
00178       strcpy(ra->fdt_file, tmp);
00179       
00180       if(ra->fdt_file[strlen(ra->fdt_file)-1] == '\r') {
00181         ra->fdt_file[strlen(ra->fdt_file)-1] = '\0';
00182       }
00183     }
00184     
00185     tmp = strtok (NULL, "=");
00186   }
00187   
00188   free(buf);
00189   
00190   return 0;
00191 }

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