00001
00012 #ifdef HAVE_GETLINE
00013
00014 #define _GNU_SOURCE
00015 #include <stdio.h>
00016 #undef _GNU_SOURCE
00017 #endif
00018
00019 #include <GKlib.h>
00020
00021
00022
00023
00024 FILE *gk_fopen(char *fname, char *mode, const char *msg)
00025 {
00026 FILE *fp;
00027 char errmsg[8192];
00028
00029 fp = fopen(fname, mode);
00030 if (fp != NULL)
00031 return fp;
00032
00033 sprintf(errmsg,"file: %s, mode: %s, [%s]", fname, mode, msg);
00034 perror(errmsg);
00035 errexit("Failed on gk_fopen()\n");
00036
00037 return NULL;
00038 }
00039
00040
00041
00042
00043
00044 void gk_fclose(FILE *fp)
00045 {
00046 fclose(fp);
00047 }
00048
00049
00050
00056
00057 gk_idx_t gk_getline(char **lineptr, size_t *n, FILE *stream)
00058 {
00059 #ifdef HAVE_GETLINE
00060 return getline(lineptr, n, stream);
00061 #else
00062 size_t i;
00063 int ch;
00064
00065 if (feof(stream))
00066 return -1;
00067
00068
00069 if (*lineptr == NULL || *n == 0) {
00070 *n = 1024;
00071 *lineptr = gk_malloc((*n)*sizeof(char), "gk_getline: lineptr");
00072 }
00073
00074
00075 i = 0;
00076 while ((ch = getc(stream)) != EOF) {
00077 (*lineptr)[i++] = (char)ch;
00078
00079
00080 if (i+1 == *n) {
00081 *n = 2*(*n);
00082 *lineptr = gk_realloc(*lineptr, (*n)*sizeof(char), "gk_getline: lineptr");
00083 }
00084
00085 if (ch == '\n')
00086 break;
00087 }
00088 (*lineptr)[i] = '\0';
00089
00090 return (i == 0 ? -1 : i);
00091 #endif
00092 }
00093
00094
00095
00102
00103 char **gk_readfile(char *fname, gk_idx_t *r_nlines)
00104 {
00105 size_t lnlen, nlines;
00106 char *line=NULL, **lines=NULL;
00107 FILE *fpin;
00108
00109 gk_getfilestats(fname, &nlines, NULL, NULL, NULL);
00110 if (nlines > 0) {
00111 lines = (char **)gk_malloc(nlines*sizeof(char *), "gk_readfile: lines");
00112
00113 fpin = gk_fopen(fname, "r", "gk_readfile");
00114 nlines = 0;
00115 while (gk_getline(&line, &lnlen, fpin) != -1) {
00116 gk_strtprune(line, "\n\r");
00117 lines[nlines++] = gk_strdup(line);
00118 }
00119 gk_fclose(fpin);
00120 }
00121
00122 gk_free((void **)&line, LTERM);
00123
00124 if (r_nlines != NULL)
00125 *r_nlines = nlines;
00126
00127 return lines;
00128 }
00129
00130
00131
00138
00139 int32_t *gk_i32readfile(char *fname, gk_idx_t *r_nlines)
00140 {
00141 size_t lnlen, nlines;
00142 char *line=NULL;
00143 int32_t *array=NULL;
00144 FILE *fpin;
00145
00146 gk_getfilestats(fname, &nlines, NULL, NULL, NULL);
00147 if (nlines > 0) {
00148 array = gk_i32malloc(nlines, "gk_i32readfile: array");
00149
00150 fpin = gk_fopen(fname, "r", "gk_readfile");
00151 nlines = 0;
00152
00153 while (gk_getline(&line, &lnlen, fpin) != -1) {
00154 sscanf(line, "%"SCNd32, &array[nlines++]);
00155 }
00156
00157 gk_fclose(fpin);
00158 }
00159
00160 gk_free((void **)&line, LTERM);
00161
00162 if (r_nlines != NULL)
00163 *r_nlines = nlines;
00164
00165 return array;
00166 }
00167
00168
00169
00176
00177 int64_t *gk_i64readfile(char *fname, gk_idx_t *r_nlines)
00178 {
00179 size_t lnlen, nlines;
00180 char *line=NULL;
00181 int64_t *array=NULL;
00182 FILE *fpin;
00183
00184 gk_getfilestats(fname, &nlines, NULL, NULL, NULL);
00185 if (nlines > 0) {
00186 array = gk_i64malloc(nlines, "gk_i64readfile: array");
00187
00188 fpin = gk_fopen(fname, "r", "gk_readfile");
00189 nlines = 0;
00190
00191 while (gk_getline(&line, &lnlen, fpin) != -1) {
00192 sscanf(line, "%"SCNd64, &array[nlines++]);
00193 }
00194
00195 gk_fclose(fpin);
00196 }
00197
00198 gk_free((void **)&line, LTERM);
00199
00200 if (r_nlines != NULL)
00201 *r_nlines = nlines;
00202
00203 return array;
00204 }
00205
00206
00213
00214 int32_t *gk_i32readfilebin(char *fname, ssize_t *r_nelmnts)
00215 {
00216 ssize_t fsize, nelmnts;
00217 int32_t *array=NULL;
00218 FILE *fpin;
00219
00220 *r_nelmnts = -1;
00221
00222 fsize = (ssize_t) gk_getfsize(fname);
00223 if (fsize%sizeof(int32_t) != 0) {
00224 gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(int32_t).\n");
00225 return NULL;
00226 }
00227
00228 nelmnts = fsize/sizeof(int32_t);
00229 array = gk_i32malloc(nelmnts, "gk_i32readfilebin: array");
00230
00231 fpin = gk_fopen(fname, "rb", "gk_i32readfilebin");
00232
00233 if (fread(array, sizeof(int32_t), nelmnts, fpin) != nelmnts) {
00234 gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);
00235 gk_free((void **)&array, LTERM);
00236 return NULL;
00237 }
00238 gk_fclose(fpin);
00239
00240 *r_nelmnts = nelmnts;
00241
00242 return array;
00243 }
00244
00245
00252
00253 int64_t *gk_i64readfilebin(char *fname, ssize_t *r_nelmnts)
00254 {
00255 ssize_t fsize, nelmnts;
00256 int64_t *array=NULL;
00257 FILE *fpin;
00258
00259 *r_nelmnts = -1;
00260
00261 fsize = (ssize_t) gk_getfsize(fname);
00262 if (fsize%sizeof(int64_t) != 0) {
00263 gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(int64_t).\n");
00264 return NULL;
00265 }
00266
00267 nelmnts = fsize/sizeof(int64_t);
00268 array = gk_i64malloc(nelmnts, "gk_i64readfilebin: array");
00269
00270 fpin = gk_fopen(fname, "rb", "gk_i64readfilebin");
00271
00272 if (fread(array, sizeof(int64_t), nelmnts, fpin) != nelmnts) {
00273 gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);
00274 gk_free((void **)&array, LTERM);
00275 return NULL;
00276 }
00277 gk_fclose(fpin);
00278
00279 *r_nelmnts = nelmnts;
00280
00281 return array;
00282 }
00283
00284
00291
00292 float *gk_freadfilebin(char *fname, ssize_t *r_nelmnts)
00293 {
00294 ssize_t fsize, nelmnts;
00295 float *array=NULL;
00296 FILE *fpin;
00297
00298 *r_nelmnts = -1;
00299
00300 fsize = (ssize_t) gk_getfsize(fname);
00301 if (fsize%sizeof(float) != 0) {
00302 gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(float).\n");
00303 return NULL;
00304 }
00305
00306 nelmnts = fsize/sizeof(float);
00307 array = gk_fmalloc(nelmnts, "gk_freadfilebin: array");
00308
00309 fpin = gk_fopen(fname, "rb", "gk_freadfilebin");
00310
00311 if (fread(array, sizeof(float), nelmnts, fpin) != nelmnts) {
00312 gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);
00313 gk_free((void **)&array, LTERM);
00314 return NULL;
00315 }
00316 gk_fclose(fpin);
00317
00318 *r_nelmnts = nelmnts;
00319
00320 return array;
00321 }
00322
00323
00324
00330
00331 size_t gk_fwritefilebin(char *fname, size_t n, float *a)
00332 {
00333 size_t fsize;
00334 FILE *fp;
00335
00336 fp = gk_fopen(fname, "wb", "gk_fwritefilebin");
00337
00338 fsize = fwrite(a, sizeof(float), n, fp);
00339
00340 gk_fclose(fp);
00341
00342 return fsize;
00343 }
00344
00345
00346
00353
00354 double *gk_dreadfilebin(char *fname, ssize_t *r_nelmnts)
00355 {
00356 ssize_t fsize, nelmnts;
00357 double *array=NULL;
00358 FILE *fpin;
00359
00360 *r_nelmnts = -1;
00361
00362 fsize = (ssize_t) gk_getfsize(fname);
00363 if (fsize%sizeof(double) != 0) {
00364 gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(double).\n");
00365 return NULL;
00366 }
00367
00368 nelmnts = fsize/sizeof(double);
00369 array = gk_dmalloc(nelmnts, "gk_dreadfilebin: array");
00370
00371 fpin = gk_fopen(fname, "rb", "gk_dreadfilebin");
00372
00373 if (fread(array, sizeof(double), nelmnts, fpin) != nelmnts) {
00374 gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);
00375 gk_free((void **)&array, LTERM);
00376 return NULL;
00377 }
00378 gk_fclose(fpin);
00379
00380 *r_nelmnts = nelmnts;
00381
00382 return array;
00383 }
00384