| 1 | /* melder_tensorio.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2018,2020,2026 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 3 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 | |
| 19 | #include "melder.h" |
| 20 | |
| 21 | /*** Typed I/O functions for vectors and matrices. ***/ |
| 22 | |
| 23 | #define FUNCTION(T,storage) \ |
| 24 | void vector_writeText_##storage (const constvector<T>& vec, MelderFile file, conststring32 name) { \ |
| 25 | texputintro (file, name, U" []: ", vec.size >= 1 ? nullptr : U"(empty)", 0,0,0); \ |
| 26 | for (integer i = 1; i <= vec.size; i ++) \ |
| 27 | texput##storage (file, vec [i], name, U" [", Melder_integer (i), U"]", 0,0); \ |
| 28 | texexdent (file); \ |
| 29 | if (feof (file -> filePointer) || ferror (file -> filePointer)) Melder_throw (U"Write error."); \ |
| 30 | } \ |
| 31 | void vector_writeBinary_##storage (const constvector<T>& vec, FILE *f) { \ |
| 32 | for (integer i = 1; i <= vec.size; i ++) \ |
| 33 | binput##storage (vec [i], f); \ |
| 34 | if (feof (f) || ferror (f)) Melder_throw (U"Write error."); \ |
| 35 | } \ |
| 36 | autovector<T> vector_readText_##storage (integer size, MelderReadText text, const char *name) { \ |
| 37 | autovector<T> result = newvectorzero<T> (size); \ |
| 38 | for (integer i = 1; i <= size; i ++) { \ |
| 39 | try { \ |
| 40 | result [i] = texget##storage (text); \ |
| 41 | } catch (MelderError) { \ |
| 42 | Melder_throw (U"Could not read ", Melder_peek8to32_u (name), U" [", i, U"]."); \ |
| 43 | } \ |
| 44 | } \ |
| 45 | return result; \ |
| 46 | } \ |
| 47 | autovector<T> vector_readBinary_##storage (integer size, FILE *f) { \ |
| 48 | autovector<T> result = newvectorzero<T> (size); \ |
| 49 | for (integer i = 1; i <= size; i ++) { \ |
| 50 | result [i] = binget##storage (f); \ |
| 51 | } \ |
| 52 | return result; \ |
| 53 | } \ |
| 54 | void matrix_writeText_##storage (const constmatrix<T>& mat, MelderFile file, conststring32 name) { \ |
| 55 | texputintro (file, name, U" [] []: ", mat.nrow >= 1 ? nullptr : U"(empty)", 0,0,0); \ |
| 56 | for (integer irow = 1; irow <= mat.nrow; irow ++) { \ |
| 57 | texputintro (file, name, U" [", Melder_integer (irow), U"] []:", 0,0); \ |
| 58 | for (integer icol = 1; icol <= mat.ncol; icol ++) { \ |
| 59 | texput##storage (file, mat [irow] [icol], name, U" [", Melder_integer (irow), U"] [", Melder_integer (icol), U"]"); \ |
| 60 | } \ |
| 61 | texexdent (file); \ |
| 62 | } \ |
| 63 | texexdent (file); \ |
| 64 | if (feof (file -> filePointer) || ferror (file -> filePointer)) Melder_throw (U"Write error."); \ |
| 65 | } \ |
| 66 | void matrix_writeBinary_##storage (const constmatrix<T>& mat, FILE *f) { \ |
| 67 | for (integer irow = 1; irow <= mat.nrow; irow ++) { \ |
| 68 | for (integer icol = 1; icol <= mat.ncol; icol ++) \ |
| 69 | binput##storage (mat [irow] [icol], f); \ |
| 70 | } \ |
| 71 | if (feof (f) || ferror (f)) Melder_throw (U"Write error."); \ |
| 72 | } \ |
| 73 | automatrix<T> matrix_readText_##storage (integer nrow, integer ncol, MelderReadText text, const char *name) { \ |
| 74 | automatrix<T> result = newmatrixzero<T> (nrow, ncol); \ |
| 75 | for (integer irow = 1; irow <= nrow; irow ++) for (integer icol = 1; icol <= ncol; icol ++) { \ |
| 76 | try { \ |
| 77 | result [irow] [icol] = texget##storage (text); \ |
| 78 | } catch (MelderError) { \ |
| 79 | Melder_throw (U"Could not read ", Melder_peek8to32_u (name), U" [", irow, U"] [", icol, U"]."); \ |
| 80 | } \ |
| 81 | } \ |
| 82 | return result; \ |
| 83 | } \ |
| 84 | automatrix<T> matrix_readBinary_##storage (integer nrow, integer ncol, FILE *f) { \ |
| 85 | automatrix<T> result = newmatrixzero<T> (nrow, ncol); \ |
| 86 | for (integer irow = 1; irow <= nrow; irow ++) for (integer icol = 1; icol <= ncol; icol ++) \ |
| 87 | result [irow] [icol] = binget##storage (f); \ |
| 88 | return result; \ |
| 89 | } \ |
| 90 | void tensor3_writeText_##storage (const consttensor3<T>& ten3, MelderFile file, conststring32 name) { \ |
| 91 | texputintro (file, name, U" [] [] []: ", ten3.ndim1 >= 1 && ten3.ndim2 >= 1 && ten3.ndim3 >= 1 ? nullptr : U"(empty)", 0,0,0); \ |
| 92 | for (integer idim1 = 1; idim1 <= ten3.ndim1; idim1 ++) { \ |
| 93 | texputintro (file, name, U" [", Melder_integer (idim1), U"] [] []:", 0,0); \ |
| 94 | for (integer idim2 = 1; idim2 <= ten3.ndim2; idim2 ++) { \ |
| 95 | texputintro (file, name, U" [", Melder_integer (idim1), U"] [", Melder_integer (idim2), U"] []:", 0,0); \ |
| 96 | for (integer idim3 = 1; idim3 <= ten3.ndim3; idim3 ++) { \ |
| 97 | texput##storage (file, ten3 [idim1] [idim2] [idim3], name, U" [", Melder_integer (idim1), U"] [", Melder_integer (idim2), U"] [", Melder_integer (idim3), U"]"); \ |
| 98 | } \ |
| 99 | } \ |
| 100 | texexdent (file); \ |
| 101 | } \ |
| 102 | texexdent (file); \ |
| 103 | if (feof (file -> filePointer) || ferror (file -> filePointer)) Melder_throw (U"Write error."); \ |
| 104 | } \ |
| 105 | void tensor3_writeBinary_##storage (const consttensor3<T>& ten3, FILE *f) { \ |
| 106 | for (integer idim1 = 1; idim1 <= ten3.ndim1; idim1 ++) { \ |
| 107 | for (integer idim2 = 1; idim2 <= ten3.ndim2; idim2 ++) { \ |
| 108 | for (integer idim3 = 1; idim3 <= ten3.ndim3; idim3 ++) { \ |
| 109 | binput##storage (ten3 [idim1] [idim2] [idim3], f); \ |
| 110 | } \ |
| 111 | } \ |
| 112 | } \ |
| 113 | if (feof (f) || ferror (f)) Melder_throw (U"Write error."); \ |
| 114 | } \ |
| 115 | autotensor3<T> tensor3_readText_##storage (integer ndim1, integer ndim2, integer ndim3, MelderReadText text, const char *name) { \ |
| 116 | autotensor3<T> result = newtensor3zero<T> (ndim1, ndim2, ndim3); \ |
| 117 | for (integer idim1 = 1; idim1 <= result.ndim1; idim1 ++) { \ |
| 118 | for (integer idim2 = 1; idim2 <= result.ndim2; idim2 ++) { \ |
| 119 | for (integer idim3 = 1; idim3 <= result.ndim3; idim3 ++) { \ |
| 120 | try { \ |
| 121 | result [idim1] [idim2] [idim3] = texget##storage (text); \ |
| 122 | } catch (MelderError) { \ |
| 123 | Melder_throw (U"Could not read ", Melder_peek8to32_u (name), U" [", idim1, U"] [", idim2, U"] [", idim3, U"]."); \ |
| 124 | } \ |
| 125 | } \ |
| 126 | } \ |
| 127 | } \ |
| 128 | return result; \ |
| 129 | } \ |
| 130 | autotensor3<T> tensor3_readBinary_##storage (integer ndim1, integer ndim2, integer ndim3, FILE *f) { \ |
| 131 | autotensor3<T> result = newtensor3zero<T> (ndim1, ndim2, ndim3); \ |
| 132 | for (integer idim1 = 1; idim1 <= result.ndim1; idim1 ++) { \ |
| 133 | for (integer idim2 = 1; idim2 <= result.ndim2; idim2 ++) { \ |
| 134 | for (integer idim3 = 1; idim3 <= result.ndim3; idim3 ++) { \ |
| 135 | result [idim1] [idim2] [idim3] = binget##storage (f); \ |
| 136 | } \ |
| 137 | } \ |
| 138 | } \ |
| 139 | return result; \ |
| 140 | } |
| 141 | |
| 142 | FUNCTION (signed char, i8) |
| 143 | FUNCTION (int, i16) |
| 144 | FUNCTION (long, i32) |
| 145 | FUNCTION (integer, integer32BE) |
| 146 | FUNCTION (integer, integer16BE) |
| 147 | FUNCTION (unsigned char, u8) |
| 148 | FUNCTION (unsigned int, u16) |
| 149 | FUNCTION (unsigned long, u32) |
| 150 | FUNCTION (double, r32) |
| 151 | FUNCTION (double, r64) |
| 152 | FUNCTION (dcomplex, c64) |
| 153 | FUNCTION (dcomplex, c128) |
| 154 | FUNCTION (bool, eb) |
| 155 | #undef FUNCTION |
| 156 | |
| 157 | /* End of file melder_tensorio.cpp */ |