schiff

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

schiff_streamline.h (2056B)


      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_STREAMLINE_H
     17 #define SHCIFF_STREAMLINE_H
     18 
     19 #include <rsys/stretchy_array.h>
     20 #include <string.h>
     21 
     22 struct schiff_streamline { char* buf; };
     23 
     24 static INLINE void
     25 schiff_streamline_init(struct schiff_streamline* sline)
     26 {
     27   ASSERT(sline);
     28   memset(sline, 0, sizeof(struct schiff_streamline));
     29   sline->buf = sa_add(sline->buf, 128);
     30 }
     31 
     32 static INLINE void
     33 schiff_streamline_release(struct schiff_streamline* sline)
     34 {
     35   ASSERT(sline);
     36   sa_release(sline->buf);
     37 }
     38 
     39 static INLINE res_T
     40 schiff_streamline_read
     41   (struct schiff_streamline* sline,
     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(sline && stream && out_line);
     50 
     51   if(!fgets(sline->buf, (int)sa_size(sline->buf), stream)) {
     52     res = RES_EOF;
     53     goto exit;
     54   }
     55 
     56   while(!strrchr(sline->buf, '\n')) { /* Ensure that the whole line is read */
     57     if(!fgets(sa_add(sline->buf, chunk), (int)chunk, stream)) /* EOF */
     58       break;
     59   }
     60 
     61   /* Remove leading spaces */
     62   line = sline->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 
     70 exit:
     71   *out_line = line;
     72   return res;
     73 error:
     74   goto error;
     75 }
     76 
     77 #endif /* SCHIFF_STREAMLINE_H */
     78