Path: blob/devel/elmergrid/src/metis-5.1.0/GKlib/io.c
3206 views
/*!1\file io.c2\brief Various file I/O functions.34This file contains various functions that perform I/O.56\date Started 4/10/957\author George8\version\verbatim $Id: io.c 12591 2012-09-01 19:03:15Z karypis $ \endverbatim9*/1011#ifdef HAVE_GETLINE12/* Get getline to be defined. */13#define _GNU_SOURCE14#include <stdio.h>15#undef _GNU_SOURCE16#endif1718#include <GKlib.h>1920/*************************************************************************21* This function opens a file22**************************************************************************/23FILE *gk_fopen(char *fname, char *mode, const char *msg)24{25FILE *fp;26char errmsg[8192];2728fp = fopen(fname, mode);29if (fp != NULL)30return fp;3132sprintf(errmsg,"file: %s, mode: %s, [%s]", fname, mode, msg);33perror(errmsg);34errexit("Failed on gk_fopen()\n");3536return NULL;37}383940/*************************************************************************41* This function closes a file42**************************************************************************/43void gk_fclose(FILE *fp)44{45fclose(fp);46}474849/*************************************************************************/50/*! This function is the GKlib implementation of glibc's getline()51function.52\returns -1 if the EOF has been reached, otherwise it returns the53number of bytes read.54*/55/*************************************************************************/56gk_idx_t gk_getline(char **lineptr, size_t *n, FILE *stream)57{58#ifdef HAVE_GETLINE59return getline(lineptr, n, stream);60#else61size_t i;62int ch;6364if (feof(stream))65return -1;6667/* Initial memory allocation if *lineptr is NULL */68if (*lineptr == NULL || *n == 0) {69*n = 1024;70*lineptr = gk_malloc((*n)*sizeof(char), "gk_getline: lineptr");71}7273/* get into the main loop */74i = 0;75while ((ch = getc(stream)) != EOF) {76(*lineptr)[i++] = (char)ch;7778/* reallocate memory if reached at the end of the buffer. The +1 is for '\0' */79if (i+1 == *n) {80*n = 2*(*n);81*lineptr = gk_realloc(*lineptr, (*n)*sizeof(char), "gk_getline: lineptr");82}8384if (ch == '\n')85break;86}87(*lineptr)[i] = '\0';8889return (i == 0 ? -1 : i);90#endif91}929394/*************************************************************************/95/*! This function reads the contents of a text file and returns it in the96form of an array of strings.97\param fname is the name of the file98\param r_nlines is the number of lines in the file. If it is NULL,99this information is not returned.100*/101/*************************************************************************/102char **gk_readfile(char *fname, gk_idx_t *r_nlines)103{104size_t lnlen, nlines;105char *line=NULL, **lines=NULL;106FILE *fpin;107108gk_getfilestats(fname, &nlines, NULL, NULL, NULL);109if (nlines > 0) {110lines = (char **)gk_malloc(nlines*sizeof(char *), "gk_readfile: lines");111112fpin = gk_fopen(fname, "r", "gk_readfile");113nlines = 0;114while (gk_getline(&line, &lnlen, fpin) != -1) {115gk_strtprune(line, "\n\r");116lines[nlines++] = gk_strdup(line);117}118gk_fclose(fpin);119}120121gk_free((void **)&line, LTERM);122123if (r_nlines != NULL)124*r_nlines = nlines;125126return lines;127}128129130/*************************************************************************/131/*! This function reads the contents of a file and returns it in the132form of an array of int32_t.133\param fname is the name of the file134\param r_nlines is the number of lines in the file. If it is NULL,135this information is not returned.136*/137/*************************************************************************/138int32_t *gk_i32readfile(char *fname, gk_idx_t *r_nlines)139{140size_t lnlen, nlines;141char *line=NULL;142int32_t *array=NULL;143FILE *fpin;144145gk_getfilestats(fname, &nlines, NULL, NULL, NULL);146if (nlines > 0) {147array = gk_i32malloc(nlines, "gk_i32readfile: array");148149fpin = gk_fopen(fname, "r", "gk_readfile");150nlines = 0;151152while (gk_getline(&line, &lnlen, fpin) != -1) {153sscanf(line, "%"SCNd32, &array[nlines++]);154}155156gk_fclose(fpin);157}158159gk_free((void **)&line, LTERM);160161if (r_nlines != NULL)162*r_nlines = nlines;163164return array;165}166167168/*************************************************************************/169/*! This function reads the contents of a file and returns it in the170form of an array of int64_t.171\param fname is the name of the file172\param r_nlines is the number of lines in the file. If it is NULL,173this information is not returned.174*/175/*************************************************************************/176int64_t *gk_i64readfile(char *fname, gk_idx_t *r_nlines)177{178size_t lnlen, nlines;179char *line=NULL;180int64_t *array=NULL;181FILE *fpin;182183gk_getfilestats(fname, &nlines, NULL, NULL, NULL);184if (nlines > 0) {185array = gk_i64malloc(nlines, "gk_i64readfile: array");186187fpin = gk_fopen(fname, "r", "gk_readfile");188nlines = 0;189190while (gk_getline(&line, &lnlen, fpin) != -1) {191sscanf(line, "%"SCNd64, &array[nlines++]);192}193194gk_fclose(fpin);195}196197gk_free((void **)&line, LTERM);198199if (r_nlines != NULL)200*r_nlines = nlines;201202return array;203}204205/*************************************************************************/206/*! This function reads the contents of a binary file and returns it in the207form of an array of int32_t.208\param fname is the name of the file209\param r_nlines is the number of lines in the file. If it is NULL,210this information is not returned.211*/212/*************************************************************************/213int32_t *gk_i32readfilebin(char *fname, ssize_t *r_nelmnts)214{215ssize_t fsize, nelmnts;216int32_t *array=NULL;217FILE *fpin;218219*r_nelmnts = -1;220221fsize = (ssize_t) gk_getfsize(fname);222if (fsize%sizeof(int32_t) != 0) {223gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(int32_t).\n");224return NULL;225}226227nelmnts = fsize/sizeof(int32_t);228array = gk_i32malloc(nelmnts, "gk_i32readfilebin: array");229230fpin = gk_fopen(fname, "rb", "gk_i32readfilebin");231232if (fread(array, sizeof(int32_t), nelmnts, fpin) != nelmnts) {233gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);234gk_free((void **)&array, LTERM);235return NULL;236}237gk_fclose(fpin);238239*r_nelmnts = nelmnts;240241return array;242}243244/*************************************************************************/245/*! This function reads the contents of a binary file and returns it in the246form of an array of int64_t.247\param fname is the name of the file248\param r_nlines is the number of lines in the file. If it is NULL,249this information is not returned.250*/251/*************************************************************************/252int64_t *gk_i64readfilebin(char *fname, ssize_t *r_nelmnts)253{254ssize_t fsize, nelmnts;255int64_t *array=NULL;256FILE *fpin;257258*r_nelmnts = -1;259260fsize = (ssize_t) gk_getfsize(fname);261if (fsize%sizeof(int64_t) != 0) {262gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(int64_t).\n");263return NULL;264}265266nelmnts = fsize/sizeof(int64_t);267array = gk_i64malloc(nelmnts, "gk_i64readfilebin: array");268269fpin = gk_fopen(fname, "rb", "gk_i64readfilebin");270271if (fread(array, sizeof(int64_t), nelmnts, fpin) != nelmnts) {272gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);273gk_free((void **)&array, LTERM);274return NULL;275}276gk_fclose(fpin);277278*r_nelmnts = nelmnts;279280return array;281}282283/*************************************************************************/284/*! This function reads the contents of a binary file and returns it in the285form of an array of float.286\param fname is the name of the file287\param r_nlines is the number of lines in the file. If it is NULL,288this information is not returned.289*/290/*************************************************************************/291float *gk_freadfilebin(char *fname, ssize_t *r_nelmnts)292{293ssize_t fsize, nelmnts;294float *array=NULL;295FILE *fpin;296297*r_nelmnts = -1;298299fsize = (ssize_t) gk_getfsize(fname);300if (fsize%sizeof(float) != 0) {301gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(float).\n");302return NULL;303}304305nelmnts = fsize/sizeof(float);306array = gk_fmalloc(nelmnts, "gk_freadfilebin: array");307308fpin = gk_fopen(fname, "rb", "gk_freadfilebin");309310if (fread(array, sizeof(float), nelmnts, fpin) != nelmnts) {311gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);312gk_free((void **)&array, LTERM);313return NULL;314}315gk_fclose(fpin);316317*r_nelmnts = nelmnts;318319return array;320}321322323/*************************************************************************/324/*! This function writes the contents of an array into a binary file.325\param fname is the name of the file326\param n the number of elements in the array.327\param a the array to be written out.328*/329/*************************************************************************/330size_t gk_fwritefilebin(char *fname, size_t n, float *a)331{332size_t fsize;333FILE *fp;334335fp = gk_fopen(fname, "wb", "gk_fwritefilebin");336337fsize = fwrite(a, sizeof(float), n, fp);338339gk_fclose(fp);340341return fsize;342}343344345/*************************************************************************/346/*! This function reads the contents of a binary file and returns it in the347form of an array of double.348\param fname is the name of the file349\param r_nlines is the number of lines in the file. If it is NULL,350this information is not returned.351*/352/*************************************************************************/353double *gk_dreadfilebin(char *fname, ssize_t *r_nelmnts)354{355ssize_t fsize, nelmnts;356double *array=NULL;357FILE *fpin;358359*r_nelmnts = -1;360361fsize = (ssize_t) gk_getfsize(fname);362if (fsize%sizeof(double) != 0) {363gk_errexit(SIGERR, "The size of the file is not in multiples of sizeof(double).\n");364return NULL;365}366367nelmnts = fsize/sizeof(double);368array = gk_dmalloc(nelmnts, "gk_dreadfilebin: array");369370fpin = gk_fopen(fname, "rb", "gk_dreadfilebin");371372if (fread(array, sizeof(double), nelmnts, fpin) != nelmnts) {373gk_errexit(SIGERR, "Failed to read the number of words requested. %zd\n", nelmnts);374gk_free((void **)&array, LTERM);375return NULL;376}377gk_fclose(fpin);378379*r_nelmnts = nelmnts;380381return array;382}383384385386