gsl_combination__file.c 2.4 KB

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