gsl_histogram__file.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* histogram/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_block.h"
  23. #include "gsl_histogram.h"
  24. int
  25. gsl_histogram_fread (FILE * stream, gsl_histogram * h)
  26. {
  27. int status = gsl_block_raw_fread (stream, h->range, h->n + 1, 1);
  28. if (status)
  29. return status;
  30. status = gsl_block_raw_fread (stream, h->bin, h->n, 1);
  31. return status;
  32. }
  33. int
  34. gsl_histogram_fwrite (FILE * stream, const gsl_histogram * h)
  35. {
  36. int status = gsl_block_raw_fwrite (stream, h->range, h->n + 1, 1);
  37. if (status)
  38. return status;
  39. status = gsl_block_raw_fwrite (stream, h->bin, h->n, 1);
  40. return status;
  41. }
  42. int
  43. gsl_histogram_fprintf (FILE * stream, const gsl_histogram * h,
  44. const char *range_format, const char *bin_format)
  45. {
  46. size_t i;
  47. const size_t n = h->n;
  48. for (i = 0; i < n; i++)
  49. {
  50. int status = fprintf (stream, range_format, h->range[i]);
  51. if (status < 0)
  52. {
  53. GSL_ERROR ("fprintf failed", GSL_EFAILED);
  54. }
  55. status = putc (' ', stream);
  56. if (status == EOF)
  57. {
  58. GSL_ERROR ("putc failed", GSL_EFAILED);
  59. }
  60. status = fprintf (stream, range_format, h->range[i + 1]);
  61. if (status < 0)
  62. {
  63. GSL_ERROR ("fprintf failed", GSL_EFAILED);
  64. }
  65. status = putc (' ', stream);
  66. if (status == EOF)
  67. {
  68. GSL_ERROR ("putc failed", GSL_EFAILED);
  69. }
  70. status = fprintf (stream, bin_format, h->bin[i]);
  71. if (status < 0)
  72. {
  73. GSL_ERROR ("fprintf failed", GSL_EFAILED);
  74. }
  75. status = putc ('\n', stream);
  76. if (status == EOF)
  77. {
  78. GSL_ERROR ("putc failed", GSL_EFAILED);
  79. }
  80. }
  81. return GSL_SUCCESS;
  82. }
  83. int
  84. gsl_histogram_fscanf (FILE * stream, gsl_histogram * h)
  85. {
  86. size_t i;
  87. const size_t n = h->n;
  88. double upper;
  89. for (i = 0; i < n; i++)
  90. {
  91. int status = fscanf (stream,
  92. "%lg %lg %lg", h->range + i, &upper,
  93. h->bin + i);
  94. if (status != 3)
  95. {
  96. GSL_ERROR ("fscanf failed", GSL_EFAILED);
  97. }
  98. }
  99. h->range[n] = upper;
  100. return GSL_SUCCESS;
  101. }