schiff

Estimate the radiative properties of soft particless
git clone git://git.meso-star.com/schiff.git
Log | Files | Refs | README | LICENSE

schiff_streambuf.h (1995B)


      1 /* Copyright (C) 2015-2016 CNRS
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #ifndef SCHIFF_STREAMBUF_H
     17 #define SHCIFF_STREAMBUF_H
     18 
     19 #include <rsys/stretchy_array.h>
     20 #include <string.h>
     21 
     22 struct schiff_streambuf { char* buf; };
     23 
     24 res_T
     25 schiff_streambuf_init(struct schiff_streambuf* sbuf)
     26 {
     27   ASSERT(sbuf);
     28   memset(sbuf, 0, sizeof(struct schiff_sbuf));
     29   sbuf->buf = sa_add(buf, 128);
     30 }
     31 
     32 res_T
     33 schiff_streambuf_release(struct schiff_streambuf* sbuf)
     34 {
     35   ASSERT(sbuf);
     36   sa_release(sbuf->buf);
     37 }
     38 
     39 res_T
     40 schiff_streambuf_read_line
     41   (struct schiff_streambuf* sbuf,
     42    FILE* stream,
     43    char** out_line)
     44 {
     45   char* line = NULL;
     46   size_t last_char;
     47   const size_t chunk = 128;
     48   res_T res = RES_OK;
     49   ASSERT(sbuf && stream && out_line);
     50 
     51   if(!fgets(sbuf->buf, (int)sa_size(sbuf->buf), stream)) {
     52     res = RES_EOF;
     53     goto exit;
     54   }
     55 
     56   while(!strrchr(buf, '\n')) { /* Ensure that the whole line is read */
     57     if(!fgets(sa_add(sbuf->buf, buf_chunk), buf_chunk, stream)) /* EOF */
     58       break;
     59   }
     60 
     61   /* Remove leading spaces */
     62   line = sbuf->buf;
     63   while((*line == ' ' || *line == '\t') && *line != '\0') ++line;
     64 
     65   /* Remove newline character(s) */
     66   last_char = strlen(line);
     67   while(last_char-- && (line[last_char]=='\n' || line[last_char]=='\r'));
     68   line[last_char + 1] = '\0';
     69 exit:
     70   *out_line = line;
     71   return res;
     72 error:
     73   line = NULL;
     74   goto error;
     75 }
     76 
     77 #endif /* SCHIFF_STREAMBUF_H */
     78