Created By grepwood at 2014/06/13 11:52

https://beta.sucs.org/pb/791 (plain)
  1.   size_t grepline(char **lineptr, size_t *n, FILE * stream)
  2.   {
  3.     size_t len = 0;
  4.     size_t offset = ftell(stream);
  5.     char trash = 0;
  6.     char * buf = NULL;
  7.     char sizeofnewline = 0;
  8.   /* Cleaning up after previous run */
  9.     if(lineptr != NULL){
  10.       free(*lineptr);
  11.     }
  12.   /* Reading the file until we suspect the line ends */
  13.     do{
  14.       trash = fgetc(stream);
  15.       ++len;
  16.     }while(!feof(stream) && trash != '\n' && trash != '\r');
  17.   /* Windows and OSX newline detection */
  18.     if(trash == '\r'){
  19.       trash = fgetc(stream);
  20.       if(trash == '\n'){
  21.       ++sizeofnewline; /* It's a Windows newline */
  22.       }
  23.     }
  24.   /* Going back and reading our line */
  25.     fseek(stream,offset,SEEK_SET);
  26.     buf = (char*)malloc(len+1);
  27.     fread(buf,len,1,stream);
  28.     if(sizeofnewline){
  29.       trash = fgetc(stream);
  30.     }
  31.   /* Fixing the line in memory into a null-terminated LF line */
  32.     buf[len-1] = '\n';
  33.     buf[len] = '\0';
  34.   /* Plugging our input pointers up the addresses we were using */
  35.     *n = len;
  36.     *lineptr = buf;
  37.     return len;
  38.   }

You must be logged in to paste new items to the PasteBin