29 fp = fopen(fname, mode);
33 sprintf(errmsg,
"file: %s, mode: %s, [%s]", fname, mode, msg);
35 errexit(
"Failed on gk_fopen()\n");
60 return getline(lineptr, n, stream);
69 if (*lineptr ==
NULL || *n == 0) {
71 *lineptr =
gk_malloc((*n)*
sizeof(
char),
"gk_getline: lineptr");
76 while ((ch = getc(stream)) != EOF) {
77 (*lineptr)[i++] = (char)ch;
82 *lineptr =
gk_realloc(*lineptr, (*n)*
sizeof(
char),
"gk_getline: lineptr");
90 return (i == 0 ? -1 : i);
105 size_t lnlen, nlines;
111 lines = (
char **)
gk_malloc(nlines*
sizeof(
char *),
"gk_readfile: lines");
113 fpin =
gk_fopen(fname,
"r",
"gk_readfile");
115 while (
gk_getline(&line, &lnlen, fpin) != -1) {
124 if (r_nlines !=
NULL)
141 size_t lnlen, nlines;
148 array = gk_i32malloc(nlines,
"gk_i32readfile: array");
150 fpin =
gk_fopen(fname,
"r",
"gk_readfile");
153 while (
gk_getline(&line, &lnlen, fpin) != -1) {
154 sscanf(line,
"%"SCNd32, &array[nlines++]);
162 if (r_nlines !=
NULL)
179 size_t lnlen, nlines;
186 array = gk_i64malloc(nlines,
"gk_i64readfile: array");
188 fpin =
gk_fopen(fname,
"r",
"gk_readfile");
191 while (
gk_getline(&line, &lnlen, fpin) != -1) {
192 sscanf(line,
"%"SCNd64, &array[nlines++]);
200 if (r_nlines !=
NULL)
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");
228 nelmnts = fsize/
sizeof(
int32_t);
229 array = gk_i32malloc(nelmnts,
"gk_i32readfilebin: array");
231 fpin =
gk_fopen(fname,
"rb",
"gk_i32readfilebin");
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);
240 *r_nelmnts = nelmnts;
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");
267 nelmnts = fsize/
sizeof(
int64_t);
268 array = gk_i64malloc(nelmnts,
"gk_i64readfilebin: array");
270 fpin =
gk_fopen(fname,
"rb",
"gk_i64readfilebin");
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);
279 *r_nelmnts = nelmnts;
301 if (fsize%
sizeof(
float) != 0) {
302 gk_errexit(
SIGERR,
"The size of the file is not in multiples of sizeof(float).\n");
306 nelmnts = fsize/
sizeof(
float);
307 array = gk_fmalloc(nelmnts,
"gk_freadfilebin: array");
309 fpin =
gk_fopen(fname,
"rb",
"gk_freadfilebin");
311 if (fread(array,
sizeof(
float), nelmnts, fpin) != nelmnts) {
312 gk_errexit(
SIGERR,
"Failed to read the number of words requested. %zd\n", nelmnts);
318 *r_nelmnts = nelmnts;
336 fp =
gk_fopen(fname,
"wb",
"gk_fwritefilebin");
338 fsize = fwrite(a,
sizeof(
float), n, fp);
363 if (fsize%
sizeof(
double) != 0) {
364 gk_errexit(
SIGERR,
"The size of the file is not in multiples of sizeof(double).\n");
368 nelmnts = fsize/
sizeof(double);
369 array = gk_dmalloc(nelmnts,
"gk_dreadfilebin: array");
371 fpin =
gk_fopen(fname,
"rb",
"gk_dreadfilebin");
373 if (fread(array,
sizeof(
double), nelmnts, fpin) != nelmnts) {
374 gk_errexit(
SIGERR,
"Failed to read the number of words requested. %zd\n", nelmnts);
380 *r_nelmnts = nelmnts;
void gk_getfilestats(char *fname, size_t *r_nlines, size_t *r_ntokens, size_t *r_max_nlntokens, size_t *r_nbytes)
void errexit(char *f_str,...)
char ** gk_readfile(char *fname, gk_idx_t *r_nlines)
char * gk_strtprune(char *, char *)
Prunes characters from the end of the string.
double * gk_dreadfilebin(char *fname, ssize_t *r_nelmnts)
gk_idx_t gk_getline(char **lineptr, size_t *n, FILE *stream)
void gk_errexit(int signum, char *f_str,...)
int32_t * gk_i32readfile(char *fname, gk_idx_t *r_nlines)
void * gk_realloc(void *oldptr, size_t nbytes, char *msg)
int64_t * gk_i64readfilebin(char *fname, ssize_t *r_nelmnts)
size_t gk_fwritefilebin(char *fname, size_t n, float *a)
char * gk_strdup(char *orgstr)
Duplicates a string.
void * gk_malloc(size_t nbytes, char *msg)
void gk_free(void **ptr1,...)
int32_t * gk_i32readfilebin(char *fname, ssize_t *r_nelmnts)
FILE * gk_fopen(char *fname, char *mode, const char *msg)
float * gk_freadfilebin(char *fname, ssize_t *r_nelmnts)
int64_t * gk_i64readfile(char *fname, gk_idx_t *r_nlines)
intmax_t gk_getfsize(char *filename)
Returns the size of the file in bytes.