gsl_permutation__file.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include "gsl__config.h"
  20. #include <stdio.h>
  21. #include "gsl_errno.h"
  22. #include "gsl_permutation.h"
  23. #define IN_FORMAT "%lu"
  24. int
  25. gsl_permutation_fread (FILE * stream, gsl_permutation * p)
  26. {
  27. size_t n = p->size ;
  28. size_t * data = p->data ;
  29. size_t items = fread (data, sizeof (size_t), n, stream);
  30. if (items != n)
  31. {
  32. GSL_ERROR ("fread failed", GSL_EFAILED);
  33. }
  34. return GSL_SUCCESS;
  35. }
  36. int
  37. gsl_permutation_fwrite (FILE * stream, const gsl_permutation * p)
  38. {
  39. size_t n = p->size ;
  40. size_t * data = p->data ;
  41. size_t items = fwrite (data, sizeof (size_t), n, stream);
  42. if (items != n)
  43. {
  44. GSL_ERROR ("fwrite failed", GSL_EFAILED);
  45. }
  46. return GSL_SUCCESS;
  47. }
  48. int
  49. gsl_permutation_fprintf (FILE * stream, const gsl_permutation * p, const char *format)
  50. {
  51. size_t n = p->size ;
  52. size_t * data = p->data ;
  53. size_t i;
  54. for (i = 0; i < n; i++)
  55. {
  56. int status = fprintf (stream, format, data[i]);
  57. if (status < 0)
  58. {
  59. GSL_ERROR ("fprintf failed", GSL_EFAILED);
  60. }
  61. }
  62. return GSL_SUCCESS;
  63. }
  64. int
  65. gsl_permutation_fscanf (FILE * stream, gsl_permutation * p)
  66. {
  67. size_t n = p->size ;
  68. size_t * data = p->data ;
  69. size_t i;
  70. for (i = 0; i < n; i++)
  71. {
  72. unsigned long j ;
  73. /* FIXME: what if size_t != unsigned long ???
  74. want read in size_t but have to read in unsigned long to avoid
  75. error from compiler */
  76. int status = fscanf (stream, IN_FORMAT, &j);
  77. if (status != 1)
  78. {
  79. GSL_ERROR ("fscanf failed", GSL_EFAILED);
  80. }
  81. data[i] = j;
  82. }
  83. return GSL_SUCCESS;
  84. }