| 1 | /* combination/file.c |
| 2 | * based on permutation/file.c by Brian Gough |
| 3 | * |
| 4 | * Copyright (C) 2001 Szymon Jaroszewicz |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or (at |
| 9 | * your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | #include "gsl__config.h" |
| 22 | #include <stdio.h> |
| 23 | #include "gsl_errno.h" |
| 24 | #include "gsl_combination.h" |
| 25 | |
| 26 | #define IN_FORMAT "%lu" |
| 27 | |
| 28 | int |
| 29 | gsl_combination_fread (FILE * stream, gsl_combination * c) |
| 30 | { |
| 31 | size_t k = c->k ; |
| 32 | |
| 33 | size_t * data = c->data ; |
| 34 | |
| 35 | size_t items = fread (data, sizeof (size_t), k, stream); |
| 36 | |
| 37 | if (items != k) |
| 38 | { |
| 39 | GSL_ERROR ("fread failed", GSL_EFAILED); |
| 40 | } |
| 41 | |
| 42 | return GSL_SUCCESS; |
| 43 | } |
| 44 | |
| 45 | int |
| 46 | gsl_combination_fwrite (FILE * stream, const gsl_combination * c) |
| 47 | { |
| 48 | size_t k = c->k ; |
| 49 | |
| 50 | size_t * data = c->data ; |
| 51 | |
| 52 | size_t items = fwrite (data, sizeof (size_t), k, stream); |
| 53 | |
| 54 | if (items != k) |
| 55 | { |
| 56 | GSL_ERROR ("fwrite failed", GSL_EFAILED); |
| 57 | } |
| 58 | |
| 59 | return GSL_SUCCESS; |
| 60 | } |
| 61 | |
| 62 | int |
| 63 | gsl_combination_fprintf (FILE * stream, const gsl_combination * c, const char *format) |
| 64 | { |
| 65 | size_t k = c->k ; |
| 66 | |
| 67 | size_t * data = c->data ; |
| 68 | |
| 69 | size_t i; |
| 70 | |
| 71 | for (i = 0; i < k; i++) |
| 72 | { |
| 73 | int status = fprintf (stream, format, data[i]); |
| 74 | |
| 75 | if (status < 0) |
| 76 | { |
| 77 | GSL_ERROR ("fprintf failed", GSL_EFAILED); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return GSL_SUCCESS; |
| 82 | } |
| 83 | |
| 84 | int |
| 85 | gsl_combination_fscanf (FILE * stream, gsl_combination * c) |
| 86 | { |
| 87 | size_t k = c->k ; |
| 88 | |
| 89 | size_t * data = c->data ; |
| 90 | |
| 91 | size_t i; |
| 92 | |
| 93 | for (i = 0; i < k; i++) |
| 94 | { |
| 95 | unsigned long j ; |
| 96 | |
| 97 | /* FIXME: what if size_t != unsigned long ??? |
| 98 | |
| 99 | want read in size_t but have to read in unsigned long to avoid |
| 100 | error from compiler */ |
| 101 | |
| 102 | int status = fscanf (stream, IN_FORMAT, &j); |
| 103 | |
| 104 | if (status != 1) |
| 105 | { |
| 106 | GSL_ERROR ("fscanf failed", GSL_EFAILED); |
| 107 | } |
| 108 | |
| 109 | data[i] = j; |
| 110 | } |
| 111 | |
| 112 | return GSL_SUCCESS; |
| 113 | } |
| 114 |