GKlib/io.c
Go to the documentation of this file.
1 
12 #ifdef HAVE_GETLINE
13 /* Get getline to be defined. */
14 #define _GNU_SOURCE
15 #include <stdio.h>
16 #undef _GNU_SOURCE
17 #endif
18 
19 #include <GKlib.h>
20 
21 /*************************************************************************
22 * This function opens a file
23 **************************************************************************/
24 FILE *gk_fopen(char *fname, char *mode, const char *msg)
25 {
26  FILE *fp;
27  char errmsg[8192];
28 
29  fp = fopen(fname, mode);
30  if (fp != NULL)
31  return fp;
32 
33  sprintf(errmsg,"file: %s, mode: %s, [%s]", fname, mode, msg);
34  perror(errmsg);
35  errexit("Failed on gk_fopen()\n");
36 
37  return NULL;
38 }
39 
40 
41 /*************************************************************************
42 * This function closes a file
43 **************************************************************************/
44 void gk_fclose(FILE *fp)
45 {
46  fclose(fp);
47 }
48 
49 
50 /*************************************************************************/
56 /*************************************************************************/
57 gk_idx_t gk_getline(char **lineptr, size_t *n, FILE *stream)
58 {
59 #ifdef HAVE_GETLINE
60  return getline(lineptr, n, stream);
61 #else
62  size_t i;
63  int ch;
64 
65  if (feof(stream))
66  return -1;
67 
68  /* Initial memory allocation if *lineptr is NULL */
69  if (*lineptr == NULL || *n == 0) {
70  *n = 1024;
71  *lineptr = gk_malloc((*n)*sizeof(char), "gk_getline: lineptr");
72  }
73 
74  /* get into the main loop */
75  i = 0;
76  while ((ch = getc(stream)) != EOF) {
77  (*lineptr)[i++] = (char)ch;
78 
79  /* reallocate memory if reached at the end of the buffer. The +1 is for '\0' */
80  if (i+1 == *n) {
81  *n = 2*(*n);
82  *lineptr = gk_realloc(*lineptr, (*n)*sizeof(char), "gk_getline: lineptr");
83  }
84 
85  if (ch == '\n')
86  break;
87  }
88  (*lineptr)[i] = '\0';
89 
90  return (i == 0 ? -1 : i);
91 #endif
92 }
93 
94 
95 /*************************************************************************/
102 /*************************************************************************/
103 char **gk_readfile(char *fname, gk_idx_t *r_nlines)
104 {
105  size_t lnlen, nlines;
106  char *line=NULL, **lines=NULL;
107  FILE *fpin;
108 
109  gk_getfilestats(fname, &nlines, NULL, NULL, NULL);
110  if (nlines > 0) {
111  lines = (char **)gk_malloc(nlines*sizeof(char *), "gk_readfile: lines");
112 
113  fpin = gk_fopen(fname, "r", "gk_readfile");
114  nlines = 0;
115  while (gk_getline(&line, &lnlen, fpin) != -1) {
116  gk_strtprune(line, "\n\r");
117  lines[nlines++] = gk_strdup(line);
118  }
119  gk_fclose(fpin);
120  }
121 
122  gk_free((void **)&line, LTERM);
123 
124  if (r_nlines != NULL)
125  *r_nlines = nlines;
126 
127  return lines;
128 }
129 
130 
131 /*************************************************************************/
138 /*************************************************************************/
139 int32_t *gk_i32readfile(char *fname, gk_idx_t *r_nlines)
140 {
141  size_t lnlen, nlines;
142  char *line=NULL;
143  int32_t *array=NULL;
144  FILE *fpin;
145 
146  gk_getfilestats(fname, &nlines, NULL, NULL, NULL);
147  if (nlines > 0) {
148  array = gk_i32malloc(nlines, "gk_i32readfile: array");
149 
150  fpin = gk_fopen(fname, "r", "gk_readfile");
151  nlines = 0;
152 
153  while (gk_getline(&line, &lnlen, fpin) != -1) {
154  sscanf(line, "%"SCNd32, &array[nlines++]);
155  }
156 
157  gk_fclose(fpin);
158  }
159 
160  gk_free((void **)&line, LTERM);
161 
162  if (r_nlines != NULL)
163  *r_nlines = nlines;
164 
165  return array;
166 }
167 
168 
169 /*************************************************************************/
176 /*************************************************************************/
177 int64_t *gk_i64readfile(char *fname, gk_idx_t *r_nlines)
178 {
179  size_t lnlen, nlines;
180  char *line=NULL;
181  int64_t *array=NULL;
182  FILE *fpin;
183 
184  gk_getfilestats(fname, &nlines, NULL, NULL, NULL);
185  if (nlines > 0) {
186  array = gk_i64malloc(nlines, "gk_i64readfile: array");
187 
188  fpin = gk_fopen(fname, "r", "gk_readfile");
189  nlines = 0;
190 
191  while (gk_getline(&line, &lnlen, fpin) != -1) {
192  sscanf(line, "%"SCNd64, &array[nlines++]);
193  }
194 
195  gk_fclose(fpin);
196  }
197 
198  gk_free((void **)&line, LTERM);
199 
200  if (r_nlines != NULL)
201  *r_nlines = nlines;
202 
203  return array;
204 }
205 
206 /*************************************************************************/
213 /*************************************************************************/
214 int32_t *gk_i32readfilebin(char *fname, ssize_t *r_nelmnts)
215 {
216  ssize_t fsize, nelmnts;
217  int32_t *array=NULL;
218  FILE *fpin;
219 
220  *r_nelmnts = -1;
221 
222  fsize = (ssize_t) gk_getfsize(fname);
223  if (fsize%sizeof(int32_t) != 0) {
224  gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(int32_t).\n");
225  return NULL;
226  }
227 
228  nelmnts = fsize/sizeof(int32_t);
229  array = gk_i32malloc(nelmnts, "gk_i32readfilebin: array");
230 
231  fpin = gk_fopen(fname, "rb", "gk_i32readfilebin");
232 
233  if (fread(array, sizeof(int32_t), nelmnts, fpin) != nelmnts) {
234  gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);
235  gk_free((void **)&array, LTERM);
236  return NULL;
237  }
238  gk_fclose(fpin);
239 
240  *r_nelmnts = nelmnts;
241 
242  return array;
243 }
244 
245 /*************************************************************************/
252 /*************************************************************************/
253 int64_t *gk_i64readfilebin(char *fname, ssize_t *r_nelmnts)
254 {
255  ssize_t fsize, nelmnts;
256  int64_t *array=NULL;
257  FILE *fpin;
258 
259  *r_nelmnts = -1;
260 
261  fsize = (ssize_t) gk_getfsize(fname);
262  if (fsize%sizeof(int64_t) != 0) {
263  gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(int64_t).\n");
264  return NULL;
265  }
266 
267  nelmnts = fsize/sizeof(int64_t);
268  array = gk_i64malloc(nelmnts, "gk_i64readfilebin: array");
269 
270  fpin = gk_fopen(fname, "rb", "gk_i64readfilebin");
271 
272  if (fread(array, sizeof(int64_t), nelmnts, fpin) != nelmnts) {
273  gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);
274  gk_free((void **)&array, LTERM);
275  return NULL;
276  }
277  gk_fclose(fpin);
278 
279  *r_nelmnts = nelmnts;
280 
281  return array;
282 }
283 
284 /*************************************************************************/
291 /*************************************************************************/
292 float *gk_freadfilebin(char *fname, ssize_t *r_nelmnts)
293 {
294  ssize_t fsize, nelmnts;
295  float *array=NULL;
296  FILE *fpin;
297 
298  *r_nelmnts = -1;
299 
300  fsize = (ssize_t) gk_getfsize(fname);
301  if (fsize%sizeof(float) != 0) {
302  gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(float).\n");
303  return NULL;
304  }
305 
306  nelmnts = fsize/sizeof(float);
307  array = gk_fmalloc(nelmnts, "gk_freadfilebin: array");
308 
309  fpin = gk_fopen(fname, "rb", "gk_freadfilebin");
310 
311  if (fread(array, sizeof(float), nelmnts, fpin) != nelmnts) {
312  gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);
313  gk_free((void **)&array, LTERM);
314  return NULL;
315  }
316  gk_fclose(fpin);
317 
318  *r_nelmnts = nelmnts;
319 
320  return array;
321 }
322 
323 
324 /*************************************************************************/
330 /*************************************************************************/
331 size_t gk_fwritefilebin(char *fname, size_t n, float *a)
332 {
333  size_t fsize;
334  FILE *fp;
335 
336  fp = gk_fopen(fname, "wb", "gk_fwritefilebin");
337 
338  fsize = fwrite(a, sizeof(float), n, fp);
339 
340  gk_fclose(fp);
341 
342  return fsize;
343 }
344 
345 
346 /*************************************************************************/
353 /*************************************************************************/
354 double *gk_dreadfilebin(char *fname, ssize_t *r_nelmnts)
355 {
356  ssize_t fsize, nelmnts;
357  double *array=NULL;
358  FILE *fpin;
359 
360  *r_nelmnts = -1;
361 
362  fsize = (ssize_t) gk_getfsize(fname);
363  if (fsize%sizeof(double) != 0) {
364  gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(double).\n");
365  return NULL;
366  }
367 
368  nelmnts = fsize/sizeof(double);
369  array = gk_dmalloc(nelmnts, "gk_dreadfilebin: array");
370 
371  fpin = gk_fopen(fname, "rb", "gk_dreadfilebin");
372 
373  if (fread(array, sizeof(double), nelmnts, fpin) != nelmnts) {
374  gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);
375  gk_free((void **)&array, LTERM);
376  return NULL;
377  }
378  gk_fclose(fpin);
379 
380  *r_nelmnts = nelmnts;
381 
382  return array;
383 }
384 
gk_fclose
void gk_fclose(FILE *fp)
Definition: GKlib/io.c:44
array
int array[24]
Definition: Map_general_stride.cpp:1
ssize_t
Py_ssize_t ssize_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:489
gk_strdup
char * gk_strdup(char *orgstr)
Duplicates a string.
Definition: string.c:372
gk_readfile
char ** gk_readfile(char *fname, gk_idx_t *r_nlines)
Definition: GKlib/io.c:103
gk_free
void gk_free(void **ptr1,...)
Definition: memory.c:202
gtsam.examples.SFMExample_bal.stream
stream
Definition: SFMExample_bal.py:24
gk_fopen
FILE * gk_fopen(char *fname, char *mode, const char *msg)
Definition: GKlib/io.c:24
errexit
void errexit(char *f_str,...)
Definition: error.c:53
gk_idx_t
ssize_t gk_idx_t
Definition: gk_types.h:22
gk_realloc
void * gk_realloc(void *oldptr, size_t nbytes, char *msg)
Definition: memory.c:172
gk_i64readfilebin
int64_t * gk_i64readfilebin(char *fname, ssize_t *r_nelmnts)
Definition: GKlib/io.c:253
n
int n
Definition: BiCGSTAB_simple.cpp:1
gk_getfsize
intmax_t gk_getfsize(char *filename)
Returns the size of the file in bytes.
Definition: fs.c:55
gk_i32readfile
int32_t * gk_i32readfile(char *fname, gk_idx_t *r_nlines)
Definition: GKlib/io.c:139
gk_i64readfile
int64_t * gk_i64readfile(char *fname, gk_idx_t *r_nlines)
Definition: GKlib/io.c:177
SCNd64
#define SCNd64
Definition: ms_inttypes.h:174
gk_freadfilebin
float * gk_freadfilebin(char *fname, ssize_t *r_nelmnts)
Definition: GKlib/io.c:292
int64_t
signed __int64 int64_t
Definition: ms_stdint.h:94
mode
static const DiscreteKey mode(modeKey, 2)
LTERM
#define LTERM
Definition: gk_defs.h:14
gk_getfilestats
void gk_getfilestats(char *fname, size_t *r_nlines, size_t *r_ntokens, size_t *r_max_nlntokens, size_t *r_nbytes)
Definition: fs.c:79
gk_i32readfilebin
int32_t * gk_i32readfilebin(char *fname, ssize_t *r_nelmnts)
Definition: GKlib/io.c:214
SIGERR
#define SIGERR
Definition: gk_defs.h:38
lines
void lines()
Definition: geo_hyperplane.cpp:88
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
array
Definition: numpy.h:821
gk_getline
gk_idx_t gk_getline(char **lineptr, size_t *n, FILE *stream)
Definition: GKlib/io.c:57
gk_fwritefilebin
size_t gk_fwritefilebin(char *fname, size_t n, float *a)
Definition: GKlib/io.c:331
GKlib.h
SCNd32
#define SCNd32
Definition: ms_inttypes.h:167
gk_strtprune
char * gk_strtprune(char *, char *)
Prunes characters from the end of the string.
Definition: string.c:254
gtsam.examples.DogLegOptimizerExample.float
float
Definition: DogLegOptimizerExample.py:113
int32_t
signed int int32_t
Definition: ms_stdint.h:82
NULL
#define NULL
Definition: ccolamd.c:609
gk_malloc
void * gk_malloc(size_t nbytes, char *msg)
Definition: memory.c:140
gk_dreadfilebin
double * gk_dreadfilebin(char *fname, ssize_t *r_nelmnts)
Definition: GKlib/io.c:354
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gk_errexit
void gk_errexit(int signum, char *f_str,...)
Definition: error.c:76
pybind11.msg
msg
Definition: wrap/pybind11/pybind11/__init__.py:6


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:31