forked from KarypisLab/GKlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
621 lines (486 loc) · 16.3 KB
/
io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*!
\file io.c
\brief Various file I/O functions.
This file contains various functions that perform I/O.
\date Started 4/10/95
\author George
\version\verbatim $Id: io.c 18951 2015-08-08 20:10:46Z karypis $ \endverbatim
*/
#ifdef HAVE_GETLINE
/* Get getline to be defined. */
#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#endif
#include <GKlib.h>
/*************************************************************************
* This function opens a file
**************************************************************************/
FILE *gk_fopen(char *fname, char *mode, const char *msg)
{
FILE *fp;
char errmsg[8192];
fp = fopen(fname, mode);
if (fp != NULL)
return fp;
sprintf(errmsg,"file: %s, mode: %s, [%s]", fname, mode, msg);
perror(errmsg);
errexit("Failed on gk_fopen()\n");
return NULL;
}
/*************************************************************************
* This function closes a file
**************************************************************************/
void gk_fclose(FILE *fp)
{
fclose(fp);
}
/*************************************************************************/
/*! This function is a wrapper around the read() function that ensures
that all data is been read, by issuing multiple read requests.
The only time when not 'count' items are read is when the EOF has been
reached.
*/
/*************************************************************************/
ssize_t gk_read(int fd, void *vbuf, size_t count)
{
char *buf = (char *)vbuf;
ssize_t rsize, tsize=count;
do {
if ((rsize = read(fd, buf, tsize)) == -1)
return -1;
buf += rsize;
tsize -= rsize;
} while (tsize > 0 && rsize > 0);
return count-tsize;
}
/*************************************************************************/
/*! This function is a wrapper around the write() function that ensures
that all data is been written, by issueing multiple write requests.
*/
/*************************************************************************/
ssize_t gk_write(int fd, void *vbuf, size_t count)
{
char *buf = (char *)vbuf;
ssize_t size, tsize=count;
do {
if ((size = write(fd, buf, tsize)) == -1)
return -1;
buf += size;
tsize -= size;
} while (tsize > 0);
return count;
}
/*************************************************************************/
/*! This function is the GKlib implementation of glibc's getline()
function.
\returns -1 if the EOF has been reached, otherwise it returns the
number of bytes read.
*/
/*************************************************************************/
gk_idx_t gk_getline(char **lineptr, size_t *n, FILE *stream)
{
#ifdef HAVE_GETLINE
return getline(lineptr, n, stream);
#else
size_t i;
int ch;
if (feof(stream))
return -1;
/* Initial memory allocation if *lineptr is NULL */
if (*lineptr == NULL || *n == 0) {
*n = 1024;
*lineptr = gk_malloc((*n)*sizeof(char), "gk_getline: lineptr");
}
/* get into the main loop */
i = 0;
while ((ch = getc(stream)) != EOF) {
(*lineptr)[i++] = (char)ch;
/* reallocate memory if reached at the end of the buffer. The +1 is for '\0' */
if (i+1 == *n) {
*n = 2*(*n);
*lineptr = gk_realloc(*lineptr, (*n)*sizeof(char), "gk_getline: lineptr");
}
if (ch == '\n')
break;
}
(*lineptr)[i] = '\0';
return (i == 0 ? -1 : i);
#endif
}
/*************************************************************************/
/*! This function reads the contents of a text file and returns it in the
form of an array of strings.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
char **gk_readfile(char *fname, size_t *r_nlines)
{
size_t lnlen, nlines=0;
char *line=NULL, **lines=NULL;
FILE *fpin;
gk_getfilestats(fname, &nlines, NULL, NULL, NULL);
if (nlines > 0) {
lines = (char **)gk_malloc(nlines*sizeof(char *), "gk_readfile: lines");
fpin = gk_fopen(fname, "r", "gk_readfile");
nlines = 0;
while (gk_getline(&line, &lnlen, fpin) != -1) {
gk_strtprune(line, "\n\r");
lines[nlines++] = gk_strdup(line);
}
gk_fclose(fpin);
}
gk_free((void **)&line, LTERM);
if (r_nlines != NULL)
*r_nlines = nlines;
return lines;
}
/*************************************************************************/
/*! This function reads the contents of a file and returns it in the
form of an array of int32_t.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
int32_t *gk_i32readfile(char *fname, size_t *r_nlines)
{
size_t lnlen, nlines=0;
char *line=NULL;
int32_t *array=NULL;
FILE *fpin;
gk_getfilestats(fname, &nlines, NULL, NULL, NULL);
if (nlines > 0) {
array = gk_i32malloc(nlines, "gk_i32readfile: array");
fpin = gk_fopen(fname, "r", "gk_readfile");
nlines = 0;
while (gk_getline(&line, &lnlen, fpin) != -1) {
sscanf(line, "%"SCNd32, &array[nlines++]);
}
gk_fclose(fpin);
}
gk_free((void **)&line, LTERM);
if (r_nlines != NULL)
*r_nlines = nlines;
return array;
}
/*************************************************************************/
/*! This function reads the contents of a file and returns it in the
form of an array of int64_t.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
int64_t *gk_i64readfile(char *fname, size_t *r_nlines)
{
size_t lnlen, nlines=0;
char *line=NULL;
int64_t *array=NULL;
FILE *fpin;
gk_getfilestats(fname, &nlines, NULL, NULL, NULL);
if (nlines > 0) {
array = gk_i64malloc(nlines, "gk_i64readfile: array");
fpin = gk_fopen(fname, "r", "gk_readfile");
nlines = 0;
while (gk_getline(&line, &lnlen, fpin) != -1) {
sscanf(line, "%"SCNd64, &array[nlines++]);
}
gk_fclose(fpin);
}
gk_free((void **)&line, LTERM);
if (r_nlines != NULL)
*r_nlines = nlines;
return array;
}
/*************************************************************************/
/*! This function reads the contents of a file and returns it in the
form of an array of ssize_t.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
ssize_t *gk_zreadfile(char *fname, size_t *r_nlines)
{
size_t lnlen, nlines=0;
char *line=NULL;
ssize_t *array=NULL;
FILE *fpin;
gk_getfilestats(fname, &nlines, NULL, NULL, NULL);
if (nlines > 0) {
array = gk_zmalloc(nlines, "gk_zreadfile: array");
fpin = gk_fopen(fname, "r", "gk_readfile");
nlines = 0;
while (gk_getline(&line, &lnlen, fpin) != -1) {
sscanf(line, "%zd", &array[nlines++]);
}
gk_fclose(fpin);
}
gk_free((void **)&line, LTERM);
if (r_nlines != NULL)
*r_nlines = nlines;
return array;
}
/*************************************************************************/
/*! This function reads the contents of a binary file and returns it in the
form of an array of int32_t.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
int32_t *gk_i32readfilebin(char *fname, size_t *r_nelmnts)
{
size_t nelmnts;
ssize_t fsize;
int32_t *array=NULL;
FILE *fpin;
*r_nelmnts = 0;
fsize = gk_getfsize(fname);
if (fsize == -1) {
gk_errexit(SIGERR, "Failed to fstat(%s).\n", fname);
return NULL;
}
if (fsize%sizeof(int32_t) != 0) {
gk_errexit(SIGERR, "The size [%zd] of the file [%s] is not in multiples of sizeof(int32_t).\n", fsize, fname);
return NULL;
}
nelmnts = fsize/sizeof(int32_t);
array = gk_i32malloc(nelmnts, "gk_i32readfilebin: array");
fpin = gk_fopen(fname, "rb", "gk_i32readfilebin");
if (fread(array, sizeof(int32_t), nelmnts, fpin) != nelmnts) {
gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);
gk_free((void **)&array, LTERM);
return NULL;
}
gk_fclose(fpin);
*r_nelmnts = nelmnts;
return array;
}
/*************************************************************************/
/*! This function writes the contents of an array into a binary file.
\param fname is the name of the file
\param n the number of elements in the array.
\param a the array to be written out.
*/
/*************************************************************************/
size_t gk_i32writefilebin(char *fname, size_t n, int32_t *a)
{
size_t fsize;
FILE *fp;
fp = gk_fopen(fname, "wb", "gk_writefilebin");
fsize = fwrite(a, sizeof(int32_t), n, fp);
gk_fclose(fp);
return fsize;
}
/*************************************************************************/
/*! This function reads the contents of a binary file and returns it in the
form of an array of int64_t.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
int64_t *gk_i64readfilebin(char *fname, size_t *r_nelmnts)
{
size_t nelmnts;
ssize_t fsize;
int64_t *array=NULL;
FILE *fpin;
*r_nelmnts = 0;
fsize = gk_getfsize(fname);
if (fsize == -1) {
gk_errexit(SIGERR, "Failed to fstat(%s).\n", fname);
return NULL;
}
if (fsize%sizeof(int64_t) != 0) {
gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(int64_t).\n");
return NULL;
}
nelmnts = fsize/sizeof(int64_t);
array = gk_i64malloc(nelmnts, "gk_i64readfilebin: array");
fpin = gk_fopen(fname, "rb", "gk_i64readfilebin");
if (fread(array, sizeof(int64_t), nelmnts, fpin) != nelmnts) {
gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);
gk_free((void **)&array, LTERM);
return NULL;
}
gk_fclose(fpin);
*r_nelmnts = nelmnts;
return array;
}
/*************************************************************************/
/*! This function writes the contents of an array into a binary file.
\param fname is the name of the file
\param n the number of elements in the array.
\param a the array to be written out.
*/
/*************************************************************************/
size_t gk_i64writefilebin(char *fname, size_t n, int64_t *a)
{
size_t fsize;
FILE *fp;
fp = gk_fopen(fname, "wb", "gk_writefilebin");
fsize = fwrite(a, sizeof(int64_t), n, fp);
gk_fclose(fp);
return fsize;
}
/*************************************************************************/
/*! This function reads the contents of a binary file and returns it in the
form of an array of ssize_t.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
ssize_t *gk_zreadfilebin(char *fname, size_t *r_nelmnts)
{
size_t nelmnts;
ssize_t fsize;
ssize_t *array=NULL;
FILE *fpin;
*r_nelmnts = 0;
fsize = gk_getfsize(fname);
if (fsize == -1) {
gk_errexit(SIGERR, "Failed to fstat(%s).\n", fname);
return NULL;
}
if (fsize%sizeof(ssize_t) != 0) {
gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(ssize_t).\n");
return NULL;
}
nelmnts = fsize/sizeof(ssize_t);
array = gk_zmalloc(nelmnts, "gk_zreadfilebin: array");
fpin = gk_fopen(fname, "rb", "gk_zreadfilebin");
if (fread(array, sizeof(ssize_t), nelmnts, fpin) != nelmnts) {
gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);
gk_free((void **)&array, LTERM);
return NULL;
}
gk_fclose(fpin);
*r_nelmnts = nelmnts;
return array;
}
/*************************************************************************/
/*! This function writes the contents of an array into a binary file.
\param fname is the name of the file
\param n the number of elements in the array.
\param a the array to be written out.
*/
/*************************************************************************/
size_t gk_zwritefilebin(char *fname, size_t n, ssize_t *a)
{
size_t fsize;
FILE *fp;
fp = gk_fopen(fname, "wb", "gk_writefilebin");
fsize = fwrite(a, sizeof(ssize_t), n, fp);
gk_fclose(fp);
return fsize;
}
/*************************************************************************/
/*! This function reads the contents of a binary file and returns it in the
form of an array of float.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
float *gk_freadfilebin(char *fname, size_t *r_nelmnts)
{
size_t nelmnts;
ssize_t fsize;
float *array=NULL;
FILE *fpin;
*r_nelmnts = 0;
fsize = gk_getfsize(fname);
if (fsize == -1) {
gk_errexit(SIGERR, "Failed to fstat(%s).\n", fname);
return NULL;
}
if (fsize%sizeof(float) != 0) {
gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(float).\n");
return NULL;
}
nelmnts = fsize/sizeof(float);
array = gk_fmalloc(nelmnts, "gk_freadfilebin: array");
fpin = gk_fopen(fname, "rb", "gk_freadfilebin");
if (fread(array, sizeof(float), nelmnts, fpin) != nelmnts) {
gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);
gk_free((void **)&array, LTERM);
return NULL;
}
gk_fclose(fpin);
*r_nelmnts = nelmnts;
return array;
}
/*************************************************************************/
/*! This function writes the contents of an array into a binary file.
\param fname is the name of the file
\param n the number of elements in the array.
\param a the array to be written out.
*/
/*************************************************************************/
size_t gk_fwritefilebin(char *fname, size_t n, float *a)
{
size_t fsize;
FILE *fp;
fp = gk_fopen(fname, "wb", "gk_fwritefilebin");
fsize = fwrite(a, sizeof(float), n, fp);
gk_fclose(fp);
return fsize;
}
/*************************************************************************/
/*! This function reads the contents of a binary file and returns it in the
form of an array of double.
\param fname is the name of the file
\param r_nlines is the number of lines in the file. If it is NULL,
this information is not returned.
*/
/*************************************************************************/
double *gk_dreadfilebin(char *fname, size_t *r_nelmnts)
{
size_t nelmnts;
ssize_t fsize;
double *array=NULL;
FILE *fpin;
*r_nelmnts = 0;
fsize = gk_getfsize(fname);
if (fsize == -1) {
gk_errexit(SIGERR, "Failed to fstat(%s).\n", fname);
return NULL;
}
if (fsize%sizeof(double) != 0) {
gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(double).\n");
return NULL;
}
nelmnts = fsize/sizeof(double);
array = gk_dmalloc(nelmnts, "gk_dreadfilebin: array");
fpin = gk_fopen(fname, "rb", "gk_dreadfilebin");
if (fread(array, sizeof(double), nelmnts, fpin) != nelmnts) {
gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);
gk_free((void **)&array, LTERM);
return NULL;
}
gk_fclose(fpin);
*r_nelmnts = nelmnts;
return array;
}
/*************************************************************************/
/*! This function writes the contents of an array into a binary file.
\param fname is the name of the file
\param n the number of elements in the array.
\param a the array to be written out.
*/
/*************************************************************************/
size_t gk_dwritefilebin(char *fname, size_t n, double *a)
{
size_t fsize;
FILE *fp;
fp = gk_fopen(fname, "wb", "gk_writefilebin");
fsize = fwrite(a, sizeof(double), n, fp);
gk_fclose(fp);
return fsize;
}