schiff_streamline.h (2374B)
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_STREAMLINE_H 22 #define SCHIFF_STREAMLINE_H 23 24 #include <rsys/stretchy_array.h> 25 #include <string.h> 26 27 struct schiff_streamline { char* buf; }; 28 29 static INLINE void 30 schiff_streamline_init(struct schiff_streamline* sline) 31 { 32 ASSERT(sline); 33 memset(sline, 0, sizeof(struct schiff_streamline)); 34 sline->buf = sa_add(sline->buf, 128); 35 } 36 37 static INLINE void 38 schiff_streamline_release(struct schiff_streamline* sline) 39 { 40 ASSERT(sline); 41 sa_release(sline->buf); 42 } 43 44 static INLINE res_T 45 schiff_streamline_read 46 (struct schiff_streamline* sline, 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(sline && stream && out_line); 55 56 if(!fgets(sline->buf, (int)sa_size(sline->buf), stream)) { 57 res = RES_EOF; 58 goto exit; 59 } 60 61 while(!strrchr(sline->buf, '\n')) { /* Ensure that the whole line is read */ 62 if(!fgets(sa_add(sline->buf, chunk), (int)chunk, stream)) /* EOF */ 63 break; 64 } 65 66 /* Remove leading spaces */ 67 line = sline->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 75 exit: 76 *out_line = line; 77 return res; 78 error: 79 goto error; 80 } 81 82 #endif /* SCHIFF_STREAMLINE_H */