From 70a9e8a7a81465164b34e188c3ba0d1a88ec7d92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A5rd=20Skaflestad?= Date: Wed, 7 Sep 2011 23:14:47 +0200 Subject: [PATCH] Add companion output utilities based on already opened streams. Functions csrmatrix_write_stream() and vector_write_stream() subsume the output responsibilities of csrmatrix_write() and vector_write(), respectively, but do not deal with opening or closing streams. This allows, e.g., a call such as csrmatrix_write_stream(A, stderr); for debugging purposes. Re-implement csrmatrix_write() and vector_write() in terms of csrmatrix_write_stream() and vector_write_stream(), respectively. --- src/sparse_sys.c | 49 +++++++++++++++++++++++++++++++++--------------- src/sparse_sys.h | 7 +++++++ 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/sparse_sys.c b/src/sparse_sys.c index 44790448b..77637ee61 100644 --- a/src/sparse_sys.c +++ b/src/sparse_sys.c @@ -203,41 +203,60 @@ void csrmatrix_write(const struct CSRMatrix *A, const char *fn) /* ---------------------------------------------------------------------- */ { - size_t i, j; - FILE *fp; + FILE *fp; fp = fopen(fn, "wt"); if (fp != NULL) { - for (i = j = 0; i < A->m; i++) { - for (; j < (size_t) (A->ia[i + 1]); j++) { - fprintf(fp, "%lu %lu %26.18e\n", - (unsigned long) (i + 1), - (unsigned long) (A->ja[j] + 1), - A->sa[j]); - } - } + csrmatrix_write_stream(A, fp); } fclose(fp); } +/* ---------------------------------------------------------------------- */ +void +csrmatrix_write_stream(const struct CSRMatrix *A, FILE *fp) +/* ---------------------------------------------------------------------- */ +{ + size_t i, j; + for (i = j = 0; i < A->m; i++) { + for (; j < (size_t) (A->ia[i + 1]); j++) { + fprintf(fp, "%lu %lu %26.18e\n", + (unsigned long) (i + 1), + (unsigned long) (A->ja[j] + 1), + A->sa[j]); + } + } +} + + /* ---------------------------------------------------------------------- */ void vector_write(size_t n, const double *v, const char *fn) /* ---------------------------------------------------------------------- */ { - size_t i; - FILE *fp; + FILE *fp; fp = fopen(fn, "wt"); if (fp != NULL) { - for (i = 0; i < n; i++) { - fprintf(fp, "%26.18e\n", v[i]); - } + vector_write_stream(n, v, fp); } fclose(fp); } + + +/* ---------------------------------------------------------------------- */ +void +vector_write_stream(size_t n, const double *v, FILE *fp) +/* ---------------------------------------------------------------------- */ +{ + size_t i; + + for (i = 0; i < n; i++) { + fprintf(fp, "%26.18e\n", v[i]); + } +} diff --git a/src/sparse_sys.h b/src/sparse_sys.h index e48dcf24c..d1788ed52 100644 --- a/src/sparse_sys.h +++ b/src/sparse_sys.h @@ -21,6 +21,7 @@ #define OPM_SPARSE_SYS_HEADER_INCLUDED #include +#include #ifdef __cplusplus extern "C" { @@ -70,9 +71,15 @@ vector_zero(size_t n, double *v); void csrmatrix_write(const struct CSRMatrix *A, const char *fn); +void +csrmatrix_write_stream(const struct CSRMatrix *A, FILE *fp); + void vector_write(size_t n, const double *v, const char *fn); +void +vector_write_stream(size_t n, const double *v, FILE *fp); + #ifdef __cplusplus } #endif