| 1 | /* permutation/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_permutation.h" |
| 24 | |
| 25 | #define IN_FORMAT "%lu" |
| 26 | |
| 27 | int |
| 28 | gsl_permutation_fread (FILE * stream, gsl_permutation * p) |
| 29 | { |
| 30 | size_t n = p->size ; |
| 31 | |
| 32 | size_t * data = p->data ; |
| 33 | |
| 34 | size_t items = fread (data, sizeof (size_t), n, stream); |
| 35 | |
| 36 | if (items != n) |
| 37 | { |
| 38 | GSL_ERROR ("fread failed", GSL_EFAILED); |
| 39 | } |
| 40 | |
| 41 | return GSL_SUCCESS; |
| 42 | } |
| 43 | |
| 44 | int |
| 45 | gsl_permutation_fwrite (FILE * stream, const gsl_permutation * p) |
| 46 | { |
| 47 | size_t n = p->size ; |
| 48 | |
| 49 | size_t * data = p->data ; |
| 50 | |
| 51 | size_t items = fwrite (data, sizeof (size_t), n, stream); |
| 52 | |
| 53 | if (items != n) |
| 54 | { |
| 55 | GSL_ERROR ("fwrite failed", GSL_EFAILED); |
| 56 | } |
| 57 | |
| 58 | return GSL_SUCCESS; |
| 59 | } |
| 60 | |
| 61 | int |
| 62 | gsl_permutation_fprintf (FILE * stream, const gsl_permutation * p, const char *format) |
| 63 | { |
| 64 | size_t n = p->size ; |
| 65 | |
| 66 | size_t * data = p->data ; |
| 67 | |
| 68 | size_t i; |
| 69 | |
| 70 | for (i = 0; i < n; i++) |
| 71 | { |
| 72 | int status = fprintf (stream, format, data[i]); |
| 73 | |
| 74 | if (status < 0) |
| 75 | { |
| 76 | GSL_ERROR ("fprintf failed", GSL_EFAILED); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return GSL_SUCCESS; |
| 81 | } |
| 82 | |
| 83 | int |
| 84 | gsl_permutation_fscanf (FILE * stream, gsl_permutation * p) |
| 85 | { |
| 86 | size_t n = p->size ; |
| 87 | |
| 88 | size_t * data = p->data ; |
| 89 | |
| 90 | size_t i; |
| 91 | |
| 92 | for (i = 0; i < n; i++) |
| 93 | { |
| 94 | unsigned long j ; |
| 95 | |
| 96 | /* FIXME: what if size_t != unsigned long ??? |
| 97 | |
| 98 | want read in size_t but have to read in unsigned long to avoid |
| 99 | error from compiler */ |
| 100 | |
| 101 | int status = fscanf (stream, IN_FORMAT, &j); |
| 102 | |
| 103 | if (status != 1) |
| 104 | { |
| 105 | GSL_ERROR ("fscanf failed", GSL_EFAILED); |
| 106 | } |
| 107 | |
| 108 | data[i] = j; |
| 109 | } |
| 110 | |
| 111 | return GSL_SUCCESS; |
| 112 | } |
| 113 |