Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/elmergrid/src/metis-5.1.0/GKlib/io.c
3206 views
1
/*!
2
\file io.c
3
\brief Various file I/O functions.
4
5
This file contains various functions that perform I/O.
6
7
\date Started 4/10/95
8
\author George
9
\version\verbatim $Id: io.c 12591 2012-09-01 19:03:15Z karypis $ \endverbatim
10
*/
11
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
/*************************************************************************/
51
/*! This function is the GKlib implementation of glibc's getline()
52
function.
53
\returns -1 if the EOF has been reached, otherwise it returns the
54
number of bytes read.
55
*/
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
/*************************************************************************/
96
/*! This function reads the contents of a text file and returns it in the
97
form of an array of strings.
98
\param fname is the name of the file
99
\param r_nlines is the number of lines in the file. If it is NULL,
100
this information is not returned.
101
*/
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
/*************************************************************************/
132
/*! This function reads the contents of a file and returns it in the
133
form of an array of int32_t.
134
\param fname is the name of the file
135
\param r_nlines is the number of lines in the file. If it is NULL,
136
this information is not returned.
137
*/
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
/*************************************************************************/
170
/*! This function reads the contents of a file and returns it in the
171
form of an array of int64_t.
172
\param fname is the name of the file
173
\param r_nlines is the number of lines in the file. If it is NULL,
174
this information is not returned.
175
*/
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
/*************************************************************************/
207
/*! This function reads the contents of a binary file and returns it in the
208
form of an array of int32_t.
209
\param fname is the name of the file
210
\param r_nlines is the number of lines in the file. If it is NULL,
211
this information is not returned.
212
*/
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
/*************************************************************************/
246
/*! This function reads the contents of a binary file and returns it in the
247
form of an array of int64_t.
248
\param fname is the name of the file
249
\param r_nlines is the number of lines in the file. If it is NULL,
250
this information is not returned.
251
*/
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
/*************************************************************************/
285
/*! This function reads the contents of a binary file and returns it in the
286
form of an array of float.
287
\param fname is the name of the file
288
\param r_nlines is the number of lines in the file. If it is NULL,
289
this information is not returned.
290
*/
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
/*************************************************************************/
325
/*! This function writes the contents of an array into a binary file.
326
\param fname is the name of the file
327
\param n the number of elements in the array.
328
\param a the array to be written out.
329
*/
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
/*************************************************************************/
347
/*! This function reads the contents of a binary file and returns it in the
348
form of an array of double.
349
\param fname is the name of the file
350
\param r_nlines is the number of lines in the file. If it is NULL,
351
this information is not returned.
352
*/
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
385
386