htgop

Optical properties of a gas mixture
git clone git://git.meso-star.fr/htgop.git
Log | Files | Refs | README | LICENSE

htgop_reader.h (1978B)


      1 /* Copyright (C) 2018-2021, 2023 |Méso|Star> (contact@meso-star.com)
      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 HTGOP_READER_H
     17 #define HTGOP_READER_H
     18 
     19 #include <rsys/rsys.h>
     20 #include <rsys/stretchy_array.h>
     21 #include <string.h>
     22 
     23 struct reader {
     24   FILE* stream;
     25   const char* name;
     26   size_t iline;
     27   char* line;
     28 };
     29 
     30 static INLINE void
     31 reader_init(struct reader* rdr, FILE* stream, const char* name)
     32 {
     33   ASSERT(rdr && stream && name);
     34   memset(rdr, 0, sizeof(struct reader));
     35   rdr->stream = stream;
     36   rdr->name = name;
     37   rdr->iline = 0;
     38   rdr->line = sa_add(rdr->line, 128);
     39   ASSERT(rdr->line);
     40 }
     41 
     42 static INLINE void
     43 reader_release(struct reader* rdr)
     44 {
     45   ASSERT(rdr);
     46   sa_release(rdr->line);
     47 }
     48 
     49 /* Read a non empty line */
     50 static INLINE char*
     51 read_line(struct reader* rdr)
     52 {
     53   const int chunk = 32;
     54 
     55   do {
     56     if(!fgets(rdr->line, (int)sa_size(rdr->line), rdr->stream)) return NULL;
     57     ++rdr->iline;
     58 
     59     /* Ensure that the whole line is read */
     60     while(!strrchr(rdr->line, '\n') && !feof(rdr->stream)) {
     61       char* more = sa_add(rdr->line, (size_t)chunk);
     62       CHK(fgets(more-1/*prev null char*/, chunk+1/*prev null char*/, rdr->stream));
     63     }
     64 
     65     rdr->line[strcspn(rdr->line, "#\n\r")] = '\0'; /* Rm new line & comments */
     66   } while(strspn(rdr->line, " \t") == strlen(rdr->line)); /* Empty line */
     67   return rdr->line;
     68 }
     69 
     70 #endif /* HTGOP_READER_H */
     71