SUCS

No Account? Don't worry, sign up today!

PasteBin

  • SUCS
  • > Tools
  • > PasteBin
  • > 791

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

https://beta.sucs.org/~gigosaurus/sucssite/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

  • grepwood - 556 weeks ago
  • imranh - 561 weeks ago
  • grepwood - 568 weeks ago
  • grepwood - 573 weeks ago
  • rjames93 - 601 weeks ago
  • rjames93 - 612 weeks ago
  • firefury - 620 weeks ago
  • rjames93 - 623 weeks ago
  • eclipse - 626 weeks ago
  • welshbyte - 643 weeks ago
  • tswsl1989 - 658 weeks ago
  • rjames93 - 683 weeks ago
  • rjames93 - 697 weeks ago
  • rjames93 - 699 weeks ago
  • firefury - 766 weeks ago
  • News
  • About
  • Community
  • Games
  • Knowledge
  • Tools
    • Mailing Lists
    • ShortURI
    • Web Mail
    • PasteBin
    • Desktop on Demand

Site designed and maintained by SUCS. All opinions expressed are those of the relevant individual and not of the society.