/* loader.c $Id: loader.c,v 1.2 2005/12/30 03:28:22 dmochiha Exp $ */ #include #include #include #include #include "loader.h" #include "dmatrix.h" static char ws[] = " \t\n"; static char buf[BUFSIZ]; static char line[LINEBUFSIZ]; static int ntokens (char *line); static int filelines (FILE *fp); static int filetokens (FILE *fp); static int isspaces (char *s); double * load_vector (char *file, int *dims) /* both column and row vectors ok */ { FILE *fp; double *vector; int i; /* open file */ if ((fp = fopen(file, "r")) == NULL) { fprintf(stderr, "load_vector:: can't open %s.\n", file); return NULL; } *dims = filetokens(fp); if (!(*dims > 0)) { fprintf(stderr, "load_vector:: dimension invalid.\n"); return NULL; } /* allocate vector */ if ((vector = (double *)calloc(*dims, sizeof(double))) == NULL) { fprintf(stderr, "load_vector:: can't allocate vector.\n"); return NULL; } /* read */ i = 0; while (fscanf(fp, "%s", buf) != EOF) { vector[i] = atof(buf); i++; } fclose(fp); return vector; } double ** load_matrix (char *file, int *rows, int *cols) { FILE *fp; int i, j; char *token; double **matrix; /* inspect dimensions */ matrix_dimensions (file, rows, cols); if (!((*rows > 0) && (*cols > 0))) { fprintf(stderr, "load_matrix:: dimensions invalid.\n"); return NULL; } /* open file */ if ((fp = fopen(file, "r")) == NULL) { fprintf(stderr, "load_matrix:: can't open %s.\n", file); return NULL; } if ((matrix = dmatrix(*rows, *cols)) == NULL) { fprintf(stderr, "load_matrix:: can't allocate matrix.\n"); return NULL; } i = 0; /* load data */ while (fgets(line, sizeof(line), fp)) { if (isspaces(line)) continue; for (token = strtok(line, ws), j = 0; token; token = strtok(NULL, ws), j++) { matrix[i][j] = atof(token); } i++; } fclose(fp); return matrix; } double ** load_matrix_transposed (char *file, int *rows, int *cols) { FILE *fp; int i, j; char *token; double **matrix; /* inspect dimensions */ matrix_dimensions (file, cols, rows); if (!((*rows > 0) && (*cols > 0))) { fprintf(stderr, "load_matrix:: dimensions invalid.\n"); return NULL; } /* open file */ if ((fp = fopen(file, "r")) == NULL) { fprintf(stderr, "load_matrix:: can't open %s.\n", file); return NULL; } if ((matrix = dmatrix(*rows, *cols)) == NULL) { fprintf(stderr, "load_matrix:: can't allocate matrix.\n"); return NULL; } i = 0; /* load data */ while (fgets(line, sizeof(line), fp)) { if (isspaces(line)) continue; for (token = strtok(line, ws), j = 0; token; token = strtok(NULL, ws), j++) { matrix[j][i] = atof(token); } i++; } fclose(fp); return matrix; } void matrix_dimensions (char *file, int *rows, int *cols) { FILE *fp; int n, np = 0; if ((fp = fopen(file, "r")) == NULL) { fprintf(stderr, "matrix_dimensions:: can't open matrix %s.\n", file); return; } *rows = filelines(fp); while (fgets(line, sizeof(line), fp)) { if (isspaces(line)) continue; n = ntokens(line); if ((np > 0) && (n != np)) { fprintf(stderr, "matrix_dimensions:: columns are not identical.\n"); return; } np = n; } *cols = n; fclose(fp); return; } static int ntokens (char *line) { char *token; int n = 0; if ((token = strtok(line, ws)) == NULL) return 0; else n++; while (token = strtok(NULL, ws)) n++; return n; } static int filetokens (FILE *fp) { int n = 0; while (fscanf(fp, "%s", buf) != EOF) n++; rewind(fp); return n; } static int filelines (FILE *fp) { int n = 0; while (fgets(line, sizeof(line), fp)) { if (!isspaces(line)) n++; } rewind(fp); return n; } static int isspaces (char *s) /* return 1 if s consists of only white spaces */ { char *c = s; while (*c) { if (!isspace(*c)) return 0; c++; } return 1; }