| 1 | /* histogram/file.c |
| 2 | * |
| 3 | * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "gsl__config.h" |
| 21 | #include <stdio.h> |
| 22 | #include "gsl_errno.h" |
| 23 | #include "gsl_block.h" |
| 24 | #include "gsl_histogram.h" |
| 25 | |
| 26 | int |
| 27 | gsl_histogram_fread (FILE * stream, gsl_histogram * h) |
| 28 | { |
| 29 | int status = gsl_block_raw_fread (stream, h->range, h->n + 1, 1); |
| 30 | |
| 31 | if (status) |
| 32 | return status; |
| 33 | |
| 34 | status = gsl_block_raw_fread (stream, h->bin, h->n, 1); |
| 35 | return status; |
| 36 | } |
| 37 | |
| 38 | int |
| 39 | gsl_histogram_fwrite (FILE * stream, const gsl_histogram * h) |
| 40 | { |
| 41 | int status = gsl_block_raw_fwrite (stream, h->range, h->n + 1, 1); |
| 42 | |
| 43 | if (status) |
| 44 | return status; |
| 45 | |
| 46 | status = gsl_block_raw_fwrite (stream, h->bin, h->n, 1); |
| 47 | return status; |
| 48 | } |
| 49 | |
| 50 | int |
| 51 | gsl_histogram_fprintf (FILE * stream, const gsl_histogram * h, |
| 52 | const char *range_format, const char *bin_format) |
| 53 | { |
| 54 | size_t i; |
| 55 | const size_t n = h->n; |
| 56 | |
| 57 | for (i = 0; i < n; i++) |
| 58 | { |
| 59 | int status = fprintf (stream, range_format, h->range[i]); |
| 60 | |
| 61 | if (status < 0) |
| 62 | { |
| 63 | GSL_ERROR ("fprintf failed", GSL_EFAILED); |
| 64 | } |
| 65 | |
| 66 | status = putc (' ', stream); |
| 67 | |
| 68 | if (status == EOF) |
| 69 | { |
| 70 | GSL_ERROR ("putc failed", GSL_EFAILED); |
| 71 | } |
| 72 | |
| 73 | status = fprintf (stream, range_format, h->range[i + 1]); |
| 74 | |
| 75 | if (status < 0) |
| 76 | { |
| 77 | GSL_ERROR ("fprintf failed", GSL_EFAILED); |
| 78 | } |
| 79 | |
| 80 | status = putc (' ', stream); |
| 81 | |
| 82 | if (status == EOF) |
| 83 | { |
| 84 | GSL_ERROR ("putc failed", GSL_EFAILED); |
| 85 | } |
| 86 | |
| 87 | status = fprintf (stream, bin_format, h->bin[i]); |
| 88 | |
| 89 | if (status < 0) |
| 90 | { |
| 91 | GSL_ERROR ("fprintf failed", GSL_EFAILED); |
| 92 | } |
| 93 | |
| 94 | status = putc ('\n', stream); |
| 95 | |
| 96 | if (status == EOF) |
| 97 | { |
| 98 | GSL_ERROR ("putc failed", GSL_EFAILED); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return GSL_SUCCESS; |
| 103 | } |
| 104 | |
| 105 | int |
| 106 | gsl_histogram_fscanf (FILE * stream, gsl_histogram * h) |
| 107 | { |
| 108 | size_t i; |
| 109 | const size_t n = h->n; |
| 110 | double upper; |
| 111 | |
| 112 | for (i = 0; i < n; i++) |
| 113 | { |
| 114 | int status = fscanf (stream, |
| 115 | "%lg %lg %lg", h->range + i, &upper, |
| 116 | h->bin + i); |
| 117 | |
| 118 | if (status != 3) |
| 119 | { |
| 120 | GSL_ERROR ("fscanf failed", GSL_EFAILED); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | h->range[n] = upper; |
| 125 | |
| 126 | return GSL_SUCCESS; |
| 127 | } |