1 #include "cado.h" // IWYU pragma: keep
2 #include <stdio.h>      // printf
3 #include <stdlib.h>        // for abort exit
4 #include <string.h>
5 #include <errno.h>
6 #include <inttypes.h>
7 #include "purgedfile.h"
8 #include "gzip.h"
9 
10 /* Read all lines which begin with # in the input file, until we find one
11  * which matches the desided format.
12  */
13 
14 void
purgedfile_read_firstline(const char * fname,uint64_t * nrows,uint64_t * ncols)15 purgedfile_read_firstline (const char *fname, uint64_t *nrows, uint64_t *ncols)
16 {
17   FILE *f_tmp = fopen_maybe_compressed (fname, "rb");
18   if (!f_tmp)
19   {
20     fprintf(stderr, "%s: %s\n", fname, strerror(errno));
21     abort();
22   }
23   char buf[1024];
24   while (fgets(buf, sizeof(buf), f_tmp)) {
25       if (*buf != '#') {
26           break;
27       }
28       int ret = sscanf(buf, "# %" SCNu64 " %" SCNu64 "", nrows, ncols);
29       if (ret == 2) {
30           fclose_maybe_compressed(f_tmp, fname);
31           return;
32       }
33   }
34   fprintf(stderr,
35           "Parse error while reading %s: no header line with desired format\n", fname);
36   fclose_maybe_compressed(f_tmp, fname);
37   exit(EXIT_FAILURE);
38 }
39