GLYLIB  0.3.0b
fileslurp.c
Go to the documentation of this file.
00001 /* \file fileslurp.c 
00002 \addtogroup FILE_UTILS
00003 \brief  Read an entire file into a structure to be processed
00004         later within the calling function.
00005 
00006         Argument: A fileset structure.
00007 
00008         Returns: Fileslurp structure containing each line in
00009                 the file (with newline intact).
00010 
00011         Begun on 20080706 by BLFoley.
00012 */
00013 
00014 #include <mylib.h>
00015 #include <general.h>
00016 //fileslurp slurp_file(fileset F);
00017 
00018 
00019 fileslurp slurp_file(fileset F){
00020 fileslurp S;
00021 char templine[2001],*line;
00022 int doneflag=0;
00023 
00024 // fgets each line in the file, place in temporary hold
00025 // issue warning if any line is longer than 2000 chars, but
00026 // finish reading the thing in anyhow.
00027 
00028 // open the file
00029 F.F=myfopen(F.N,"r");
00030 // read the file
00031 S.n=0;
00032 S.L=(char**)calloc(1,sizeof(char*));
00033 while(fgets(templine,2000,F.F)!=NULL){
00034         line=strdup(templine);
00035         if(strchr(templine,'\n')==NULL){ // got a line over 2000 chars
00036                 fprintf(stderr,"found line over 2000 chars in file ");
00037                 fprintf(stderr,"%s at line %d. reading anyway.\n",F.N,(S.n+1));
00038                 doneflag=1;
00039                 while(doneflag==1){
00040                         fgets(templine,2000,F.F);
00041         line=(char*)realloc(line,(strlen(line)+strlen(templine)+1)*sizeof(char));
00042                         strcat(line,templine);
00043                         if(strchr(templine,'\n')!=NULL){doneflag=0;}
00044                         }
00045                 }
00046         S.n++;
00047         S.L=(char**)realloc(S.L,S.n*sizeof(char*));
00048         S.L[S.n-1]=strdup(line);
00049         } // close the while loop
00050 
00051 fclose(F.F);
00052 
00053 return S;
00054 } 
 All Classes Files Functions Variables Typedefs Defines