gsl_block__fwrite_source.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* block/fwrite_source.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, 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. int
  20. FUNCTION (gsl_block, fread) (FILE * stream, TYPE(gsl_block) * b)
  21. {
  22. size_t n = b->size ;
  23. ATOMIC * data = b->data ;
  24. size_t items = fread (data, MULTIPLICITY * sizeof (ATOMIC), n, stream);
  25. if (items != n)
  26. {
  27. GSL_ERROR ("fread failed", GSL_EFAILED);
  28. }
  29. return 0;
  30. }
  31. int
  32. FUNCTION (gsl_block, fwrite) (FILE * stream, const TYPE(gsl_block) * b)
  33. {
  34. size_t n = b->size ;
  35. ATOMIC * data = b->data ;
  36. size_t items = fwrite (data, MULTIPLICITY * sizeof (ATOMIC), n, stream);
  37. if (items != n)
  38. {
  39. GSL_ERROR ("fwrite failed", GSL_EFAILED);
  40. }
  41. return 0;
  42. }
  43. int
  44. FUNCTION (gsl_block, raw_fread) (FILE * stream, ATOMIC * data,
  45. const size_t n, const size_t stride)
  46. {
  47. if (stride == 1)
  48. {
  49. size_t items = fread (data, MULTIPLICITY * sizeof (ATOMIC), n, stream);
  50. if (items != n)
  51. {
  52. GSL_ERROR ("fread failed", GSL_EFAILED);
  53. }
  54. }
  55. else
  56. {
  57. size_t i;
  58. for (i = 0; i < n; i++)
  59. {
  60. size_t item = fread (data + MULTIPLICITY * i * stride,
  61. MULTIPLICITY * sizeof (ATOMIC), 1, stream);
  62. if (item != 1)
  63. {
  64. GSL_ERROR ("fread failed", GSL_EFAILED);
  65. }
  66. }
  67. }
  68. return GSL_SUCCESS;
  69. }
  70. int
  71. FUNCTION (gsl_block, raw_fwrite) (FILE * stream, const ATOMIC * data,
  72. const size_t n, const size_t stride)
  73. {
  74. if (stride == 1)
  75. {
  76. size_t items = fwrite (data, MULTIPLICITY * sizeof (ATOMIC), n, stream);
  77. if (items != n)
  78. {
  79. GSL_ERROR ("fwrite failed", GSL_EFAILED);
  80. }
  81. }
  82. else
  83. {
  84. size_t i;
  85. for (i = 0; i < n; i++)
  86. {
  87. size_t item = fwrite (data + MULTIPLICITY * i * stride,
  88. MULTIPLICITY * sizeof (ATOMIC),
  89. 1, stream);
  90. if (item != 1)
  91. {
  92. GSL_ERROR ("fwrite failed", GSL_EFAILED);
  93. }
  94. }
  95. }
  96. return GSL_SUCCESS;
  97. }