melder_tensorio.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* melder_tensorio.cpp
  2. *
  3. * Copyright (C) 1992-2018 Paul Boersma
  4. *
  5. * This code 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 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code 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.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "melder.h"
  19. /*** Typed I/O functions for vectors and matrices. ***/
  20. #define FUNCTION(type,storage) \
  21. void NUMvector_writeText_##storage (const type *v, integer lo, integer hi, MelderFile file, conststring32 name) { \
  22. texputintro (file, name, U" []: ", hi >= lo ? nullptr : U"(empty)", 0,0,0); \
  23. for (integer i = lo; i <= hi; i ++) \
  24. texput##storage (file, v [i], name, U" [", Melder_integer (i), U"]", 0,0); \
  25. texexdent (file); \
  26. if (feof (file -> filePointer) || ferror (file -> filePointer)) Melder_throw (U"Write error."); \
  27. } \
  28. void NUMvector_writeBinary_##storage (const type *v, integer lo, integer hi, FILE *f) { \
  29. for (integer i = lo; i <= hi; i ++) \
  30. binput##storage (v [i], f); \
  31. if (feof (f) || ferror (f)) Melder_throw (U"Write error."); \
  32. } \
  33. type * NUMvector_readText_##storage (integer lo, integer hi, MelderReadText text, const char *name) { \
  34. type *result = nullptr; \
  35. try { \
  36. result = NUMvector <type> (lo, hi); \
  37. for (integer i = lo; i <= hi; i ++) { \
  38. try { \
  39. result [i] = texget##storage (text); \
  40. } catch (MelderError) { \
  41. Melder_throw (U"Could not read ", Melder_peek8to32 (name), U" [", i, U"]."); \
  42. } \
  43. } \
  44. return result; \
  45. } catch (MelderError) { \
  46. NUMvector_free (result, lo); \
  47. throw; \
  48. } \
  49. } \
  50. type * NUMvector_readBinary_##storage (integer lo, integer hi, FILE *f) { \
  51. type *result = nullptr; \
  52. try { \
  53. result = NUMvector <type> (lo, hi); \
  54. for (integer i = lo; i <= hi; i ++) { \
  55. result [i] = binget##storage (f); \
  56. } \
  57. return result; \
  58. } catch (MelderError) { \
  59. NUMvector_free (result, lo); \
  60. throw; \
  61. } \
  62. } \
  63. void NUMmatrix_writeText_##storage (type **m, integer row1, integer row2, integer col1, integer col2, MelderFile file, conststring32 name) { \
  64. texputintro (file, name, U" [] []: ", row2 >= row1 ? nullptr : U"(empty)", 0,0,0); \
  65. if (row2 >= row1) { \
  66. for (integer irow = row1; irow <= row2; irow ++) { \
  67. texputintro (file, name, U" [", Melder_integer (irow), U"]:", 0,0); \
  68. for (integer icol = col1; icol <= col2; icol ++) { \
  69. texput##storage (file, m [irow] [icol], name, U" [", Melder_integer (irow), U"] [", Melder_integer (icol), U"]"); \
  70. } \
  71. texexdent (file); \
  72. } \
  73. } \
  74. texexdent (file); \
  75. if (feof (file -> filePointer) || ferror (file -> filePointer)) Melder_throw (U"Write error."); \
  76. } \
  77. void NUMmatrix_writeBinary_##storage (type **m, integer row1, integer row2, integer col1, integer col2, FILE *f) { \
  78. if (row2 >= row1) { \
  79. for (integer irow = row1; irow <= row2; irow ++) { \
  80. for (integer icol = col1; icol <= col2; icol ++) \
  81. binput##storage (m [irow] [icol], f); \
  82. } \
  83. } \
  84. if (feof (f) || ferror (f)) Melder_throw (U"Write error."); \
  85. } \
  86. type ** NUMmatrix_readText_##storage (integer row1, integer row2, integer col1, integer col2, MelderReadText text, const char *name) { \
  87. type **result = nullptr; \
  88. try { \
  89. result = NUMmatrix <type> (row1, row2, col1, col2); \
  90. for (integer irow = row1; irow <= row2; irow ++) for (integer icol = col1; icol <= col2; icol ++) { \
  91. try { \
  92. result [irow] [icol] = texget##storage (text); \
  93. } catch (MelderError) { \
  94. Melder_throw (U"Could not read ", Melder_peek8to32 (name), U" [", irow, U"] [", icol, U"]."); \
  95. } \
  96. } \
  97. return result; \
  98. } catch (MelderError) { \
  99. NUMmatrix_free (result, row1, col1); \
  100. throw; \
  101. } \
  102. } \
  103. type ** NUMmatrix_readBinary_##storage (integer row1, integer row2, integer col1, integer col2, FILE *f) { \
  104. type **result = nullptr; \
  105. try { \
  106. result = NUMmatrix <type> (row1, row2, col1, col2); \
  107. for (integer irow = row1; irow <= row2; irow ++) for (integer icol = col1; icol <= col2; icol ++) \
  108. result [irow] [icol] = binget##storage (f); \
  109. return result; \
  110. } catch (MelderError) { \
  111. NUMmatrix_free (result, row1, col1); \
  112. throw; \
  113. } \
  114. }
  115. FUNCTION (signed char, i8)
  116. FUNCTION (int, i16)
  117. FUNCTION (long, i32)
  118. FUNCTION (integer, integer32BE)
  119. FUNCTION (integer, integer16BE)
  120. FUNCTION (unsigned char, u8)
  121. FUNCTION (unsigned int, u16)
  122. FUNCTION (unsigned long, u32)
  123. FUNCTION (double, r32)
  124. FUNCTION (double, r64)
  125. FUNCTION (dcomplex, c64)
  126. FUNCTION (dcomplex, c128)
  127. #undef FUNCTION
  128. /* End of file melder_tensorio.cpp */