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 (2314B)


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