| 1 | /* abcio.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2011,2015,2017-2020,2022,2024,2025 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 | #ifdef macintosh |
| 21 | #include <TargetConditionals.h> |
| 22 | #endif |
| 23 | |
| 24 | /********** text I/O **********/ |
| 25 | |
| 26 | static int64 getInteger (MelderReadText me) { |
| 27 | char buffer [41]; |
| 28 | char32 c; |
| 29 | /* |
| 30 | * Look for the first numeric character. |
| 31 | */ |
| 32 | for (c = MelderReadText_getChar (me); c != U'-' && ! Melder_isAsciiDecimalNumber (c) && c != U'+'; c = MelderReadText_getChar (me)) { |
| 33 | if (c == U'\0') |
| 34 | Melder_throw (U"Early end of text detected while looking for an integer (line ", MelderReadText_getLineNumber (me), U")."); |
| 35 | if (c == U'!') { // end-of-line comment? |
| 36 | while ((c = MelderReadText_getChar (me)) != U'\n' && c != U'\r') { |
| 37 | if (c == 0) |
| 38 | Melder_throw (U"Early end of text detected in comment while looking for an integer (line ", MelderReadText_getLineNumber (me), U")."); |
| 39 | } |
| 40 | } |
| 41 | if (c == U'\"') |
| 42 | Melder_throw (U"Found a string while looking for an integer in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 43 | if (c == U'<') |
| 44 | Melder_throw (U"Found an enumerated value while looking for an integer in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 45 | while (! Melder_isHorizontalOrVerticalSpace (c)) { |
| 46 | if (c == U'\0') |
| 47 | Melder_throw (U"Early end of text detected in comment (line ", MelderReadText_getLineNumber (me), U")."); |
| 48 | c = MelderReadText_getChar (me); |
| 49 | } |
| 50 | } |
| 51 | int i = 0; |
| 52 | for (; i < 40; i ++) { |
| 53 | if (c > 127) |
| 54 | Melder_throw (U"Found strange text while looking for an integer in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 55 | buffer [i] = (char) (char8) c; // guarded conversion down |
| 56 | c = MelderReadText_getChar (me); |
| 57 | if (c == U'\0') { break; } // this may well be OK here |
| 58 | if (Melder_isHorizontalOrVerticalSpace (c)) break; |
| 59 | } |
| 60 | if (i >= 40) |
| 61 | Melder_throw (U"Found long text while looking for an integer in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 62 | buffer [i + 1] = '\0'; |
| 63 | return strtoll (buffer, nullptr, 10); |
| 64 | } |
| 65 | |
| 66 | static uint64 getUnsigned (MelderReadText me) { |
| 67 | char buffer [41]; |
| 68 | char32 c; |
| 69 | for (c = MelderReadText_getChar (me); ! Melder_isAsciiDecimalNumber (c) && c != U'+'; c = MelderReadText_getChar (me)) { |
| 70 | if (c == U'\0') |
| 71 | Melder_throw (U"Early end of text detected while looking for an unsigned integer (line ", MelderReadText_getLineNumber (me), U")."); |
| 72 | if (c == U'!') { // end-of-line comment? |
| 73 | while ((c = MelderReadText_getChar (me)) != '\n' && c != '\r') { |
| 74 | if (c == U'\0') |
| 75 | Melder_throw (U"Early end of text detected in comment while looking for an unsigned integer (line ", MelderReadText_getLineNumber (me), U")."); |
| 76 | } |
| 77 | } |
| 78 | if (c == U'\"') |
| 79 | Melder_throw (U"Found a string while looking for an unsigned integer in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 80 | if (c == U'<') |
| 81 | Melder_throw (U"Found an enumerated value while looking for an unsigned integer in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 82 | if (c == U'-') |
| 83 | Melder_throw (U"Found a negative value while looking for an unsigned integer in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 84 | while (! Melder_isHorizontalOrVerticalSpace (c)) { |
| 85 | if (c == U'\0') |
| 86 | Melder_throw (U"Early end of text detected in comment (line ", MelderReadText_getLineNumber (me), U")."); |
| 87 | c = MelderReadText_getChar (me); |
| 88 | } |
| 89 | } |
| 90 | int i = 0; |
| 91 | for (i = 0; i < 40; i ++) { |
| 92 | if (c > 127) |
| 93 | Melder_throw (U"Found strange text while looking for an unsigned integer in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 94 | buffer [i] = (char) (char8) c; // guarded conversion down |
| 95 | c = MelderReadText_getChar (me); |
| 96 | if (c == U'\0') { break; } // this may well be OK here |
| 97 | if (Melder_isHorizontalOrVerticalSpace (c)) break; |
| 98 | } |
| 99 | if (i >= 40) |
| 100 | Melder_throw (U"Found long text while searching for an unsigned integer in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 101 | buffer [i + 1] = '\0'; |
| 102 | return strtoull (buffer, nullptr, 10); |
| 103 | } |
| 104 | |
| 105 | static double getReal (MelderReadText me) { |
| 106 | int i; |
| 107 | char buffer [41], *slash; |
| 108 | char32 c; |
| 109 | do { |
| 110 | for (c = MelderReadText_getChar (me); c != U'-' && ! Melder_isAsciiDecimalNumber (c) && c != U'+'; c = MelderReadText_getChar (me)) { |
| 111 | if (c == U'\0') |
| 112 | Melder_throw (U"Early end of text detected while looking for a real number (line ", MelderReadText_getLineNumber (me), U")."); |
| 113 | if (c == U'!') { // end-of-line comment? |
| 114 | while ((c = MelderReadText_getChar (me)) != U'\n' && c != U'\r') { |
| 115 | if (c == U'\0') |
| 116 | Melder_throw (U"Early end of text detected in comment while looking for a real number (line ", MelderReadText_getLineNumber (me), U")."); |
| 117 | } |
| 118 | } |
| 119 | if (c == U'\"') |
| 120 | Melder_throw (U"Found a string while looking for a real number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 121 | if (c == U'<') |
| 122 | Melder_throw (U"Found an enumerated value while looking for a real number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 123 | while (! Melder_isHorizontalOrVerticalSpace (c)) { |
| 124 | if (c == U'\0') |
| 125 | Melder_throw (U"Early end of text detected in comment while looking for a real number (line ", MelderReadText_getLineNumber (me), U")."); |
| 126 | c = MelderReadText_getChar (me); |
| 127 | } |
| 128 | } |
| 129 | for (i = 0; i < 40; i ++) { |
| 130 | if (c > 127) |
| 131 | Melder_throw (U"Found strange text while looking for a real number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 132 | buffer [i] = (char) (char8) c; // guarded conversion down |
| 133 | c = MelderReadText_getChar (me); |
| 134 | if (c == U'\0') { break; } // this may well be OK here |
| 135 | if (Melder_isHorizontalOrVerticalSpace (c)) break; |
| 136 | } |
| 137 | if (i >= 40) |
| 138 | Melder_throw (U"Found long text while searching for a real number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 139 | } while (i == 0 && buffer [0] == '+'); // guard against single '+' symbols, which occur in complex numbers |
| 140 | buffer [i + 1] = '\0'; |
| 141 | slash = strchr (buffer, '/'); |
| 142 | if (slash) { |
| 143 | *slash = '\0'; |
| 144 | double numerator = Melder_a8tof (buffer), denominator = Melder_a8tof (slash + 1); |
| 145 | if (isundef (numerator) || isundef (denominator) || denominator == 0.0) |
| 146 | return undefined; |
| 147 | return numerator / denominator; |
| 148 | } |
| 149 | return Melder_a8tof (buffer); |
| 150 | } |
| 151 | |
| 152 | static dcomplex getComplex (MelderReadText me) { |
| 153 | dcomplex result; |
| 154 | char realBuffer [41], imaginaryBuffer [41]; |
| 155 | integer ireal = 0, iimag = 0; |
| 156 | char32 c; |
| 157 | bool inExponent = false, inExponentNumber = false, separatorIsMinus = false; |
| 158 | for (c = MelderReadText_getChar (me); c != U'-' && ! Melder_isAsciiDecimalNumber (c) && c != U'+'; c = MelderReadText_getChar (me)) { |
| 159 | if (c == U'\0') |
| 160 | Melder_throw (U"Early end of text detected while looking for a complex number (line ", MelderReadText_getLineNumber (me), U")."); |
| 161 | if (c == U'!') { // end-of-line comment? |
| 162 | while ((c = MelderReadText_getChar (me)) != U'\n' && c != U'\r') { |
| 163 | if (c == U'\0') |
| 164 | Melder_throw (U"Early end of text detected in comment while looking for a complex number (line ", MelderReadText_getLineNumber (me), U")."); |
| 165 | } |
| 166 | } |
| 167 | if (c == U'\"') |
| 168 | Melder_throw (U"Found a string while looking for a complex number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 169 | if (c == U'<') |
| 170 | Melder_throw (U"Found an enumerated value while looking for a complex number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 171 | while (! Melder_isHorizontalOrVerticalSpace (c)) { |
| 172 | if (c == U'\0') |
| 173 | Melder_throw (U"Early end of text detected in comment while looking for a complex number (line ", MelderReadText_getLineNumber (me), U")."); |
| 174 | c = MelderReadText_getChar (me); |
| 175 | } |
| 176 | } |
| 177 | for (; ireal < 40; ireal ++) { |
| 178 | if (c > 127) |
| 179 | Melder_throw (U"Found strange text while looking for a complex number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 180 | if (inExponent) { |
| 181 | if (c == U'+' || c == U'-') { |
| 182 | if (inExponentNumber) { |
| 183 | /* |
| 184 | This must be the beginning of the imaginary part. |
| 185 | */ |
| 186 | separatorIsMinus = ( c == U'-' ); |
| 187 | realBuffer [ireal] = U'\0'; |
| 188 | break; |
| 189 | } else { |
| 190 | inExponentNumber = true; |
| 191 | } |
| 192 | } else if (Melder_isAsciiDecimalNumber (c)) { |
| 193 | inExponentNumber = true; |
| 194 | } else if (inExponentNumber) { // typically a space |
| 195 | realBuffer [ireal] = U'\0'; |
| 196 | break; |
| 197 | } else { |
| 198 | Melder_throw (U"Found unexpected symbol in the exponent of a complex number (line ", MelderReadText_getLineNumber (me), U")."); |
| 199 | } |
| 200 | } else if (ireal > 0 && (c == U'+' || c == U'-')) { // note: initial signs are not separators |
| 201 | separatorIsMinus = ( c == U'-' ); |
| 202 | realBuffer [ireal] = U'\0'; |
| 203 | break; |
| 204 | } |
| 205 | if (c == 'e' || c == 'E') |
| 206 | inExponent = true; |
| 207 | realBuffer [ireal] = (char) (char8) c; // guarded conversion down |
| 208 | c = MelderReadText_getChar (me); |
| 209 | if (c == U'\0') |
| 210 | Melder_throw (U"Missing imaginary part in complex number (line ", MelderReadText_getLineNumber (me), U")."); |
| 211 | if (Melder_isHorizontalOrVerticalSpace (c)) |
| 212 | Melder_throw (U"Found a space within a complex number (line ", MelderReadText_getLineNumber (me), U")."); |
| 213 | } |
| 214 | if (ireal >= 40) |
| 215 | Melder_throw (U"Found long text while searching for a complex number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 216 | realBuffer [ireal + 1] = '\0'; |
| 217 | result. real (Melder_a8tof (realBuffer)); |
| 218 | c = MelderReadText_getChar (me); |
| 219 | if (c != U'-' && ! Melder_isAsciiDecimalNumber (c) && c != U'+') |
| 220 | Melder_throw (U"Found strange text while looking for the imaginary part of a complex number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 221 | if (c == U'\0') |
| 222 | Melder_throw (U"Early end of text detected while looking for the imaginary part of a complex number (line ", MelderReadText_getLineNumber (me), U")."); |
| 223 | if (Melder_isHorizontalOrVerticalSpace (c)) |
| 224 | Melder_throw (U"Found a space within a complex number (line ", MelderReadText_getLineNumber (me), U")."); |
| 225 | if (c == U'!') { // end-of-line comment? |
| 226 | while ((c = MelderReadText_getChar (me)) != U'\n' && c != U'\r') { |
| 227 | if (c == U'\0') |
| 228 | Melder_throw (U"Early end of text detected in comment while looking for the imaginary part of a complex number (line ", MelderReadText_getLineNumber (me), U")."); |
| 229 | } |
| 230 | } |
| 231 | if (c == U'\"') |
| 232 | Melder_throw (U"Found a string while looking for the imaginary part of a complex number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 233 | if (c == U'<') |
| 234 | Melder_throw (U"Found an enumerated value while looking for the imaginary part of a complex number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 235 | for (; iimag < 40; iimag ++) { |
| 236 | if (c > 127) |
| 237 | Melder_throw (U"Found strange text while looking for the imaginary part of a complex number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 238 | imaginaryBuffer [iimag] = (char) (char8) c; // guarded conversion down |
| 239 | c = MelderReadText_getChar (me); |
| 240 | if (c == U'\0') |
| 241 | Melder_throw (U"Missing i in a complex number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 242 | if (Melder_isHorizontalOrVerticalSpace (c)) |
| 243 | Melder_throw (U"Missing i in a complex number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 244 | if (c == U'i') |
| 245 | break; |
| 246 | } |
| 247 | if (iimag >= 40) |
| 248 | Melder_throw (U"Found long text while searching for the imaginary part of a complex number in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 249 | imaginaryBuffer [iimag + 1] = '\0'; |
| 250 | result. imag (Melder_a8tof (imaginaryBuffer) * ( separatorIsMinus ? -1.0 : 1.0 )); |
| 251 | return result; |
| 252 | } |
| 253 | |
| 254 | static int getEnum (MelderReadText me, int (*getValue) (conststring32)) { |
| 255 | char32 buffer [41], c; |
| 256 | for (c = MelderReadText_getChar (me); c != U'<'; c = MelderReadText_getChar (me)) { |
| 257 | if (c == U'\0') |
| 258 | Melder_throw (U"Early end of text detected while looking for an enumerated value (line ", MelderReadText_getLineNumber (me), U")."); |
| 259 | if (c == U'!') { /* End-of-line comment? */ |
| 260 | while ((c = MelderReadText_getChar (me)) != U'\n' && c != U'\r') { |
| 261 | if (c == U'\0') |
| 262 | Melder_throw (U"Early end of text detected in comment while looking for an enumerated value (line ", MelderReadText_getLineNumber (me), U")."); |
| 263 | } |
| 264 | } |
| 265 | if (c == U'-' || Melder_isAsciiDecimalNumber (c) || c == U'+') |
| 266 | Melder_throw (U"Found a number while looking for an enumerated value in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 267 | if (c == U'\"') |
| 268 | Melder_throw (U"Found a string while looking for an enumerated value in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 269 | while (! Melder_isHorizontalOrVerticalSpace (c)) { |
| 270 | if (c == U'\0') |
| 271 | Melder_throw (U"Early end of text detected in comment while looking for an enumerated value (line ", MelderReadText_getLineNumber (me), U")."); |
| 272 | c = MelderReadText_getChar (me); |
| 273 | } |
| 274 | } |
| 275 | int i = 0; |
| 276 | for (; i < 40; i ++) { |
| 277 | c = MelderReadText_getChar (me); // read past first '<' |
| 278 | if (c == U'\0') |
| 279 | Melder_throw (U"Early end of text detected while reading an enumerated value (line ", MelderReadText_getLineNumber (me), U")."); |
| 280 | constexpr char32 theOnlySpaceAllowedInAnEnum = U' '; |
| 281 | if (Melder_isHorizontalOrVerticalSpace (c) && c != theOnlySpaceAllowedInAnEnum) |
| 282 | Melder_throw (U"No matching '>' while reading an enumerated value (line ", MelderReadText_getLineNumber (me), U")."); |
| 283 | if (c == U'>') |
| 284 | break; // the expected closing bracket; not added to the buffer |
| 285 | buffer [i] = c; |
| 286 | } |
| 287 | if (i >= 40) |
| 288 | Melder_throw (U"Found strange text while reading an enumerated value in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 289 | buffer [i] = U'\0'; |
| 290 | int value = getValue (buffer); |
| 291 | if (value < 0) |
| 292 | Melder_throw (U"\"", buffer, U"\" is not a value of the enumerated type."); |
| 293 | return value; |
| 294 | } |
| 295 | |
| 296 | static char32 * peekString (MelderReadText me) { |
| 297 | static MelderString buffer; |
| 298 | MelderString_empty (& buffer); |
| 299 | for (char32 c = MelderReadText_getChar (me); c != U'\"'; c = MelderReadText_getChar (me)) { |
| 300 | if (c == U'\0') |
| 301 | Melder_throw (U"Early end of text detected while looking for a string (line ", MelderReadText_getLineNumber (me), U")."); |
| 302 | if (c == U'!') { // end-of-line comment? |
| 303 | while ((c = MelderReadText_getChar (me)) != '\n' && c != '\r') { |
| 304 | if (c == U'\0') |
| 305 | Melder_throw (U"Early end of text detected in comment while looking for a string (line ", MelderReadText_getLineNumber (me), U")."); |
| 306 | } |
| 307 | } |
| 308 | if (c == U'-' || Melder_isAsciiDecimalNumber (c) || c == U'+') |
| 309 | Melder_throw (U"Found a number while looking for a string in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 310 | if (c == U'<') |
| 311 | Melder_throw (U"Found an enumerated value while looking for a string in text (line ", MelderReadText_getLineNumber (me), U")."); |
| 312 | while (! Melder_isHorizontalOrVerticalSpace (c)) { |
| 313 | if (c == U'\0') |
| 314 | Melder_throw (U"Early end of text detected while looking for a string (line ", MelderReadText_getLineNumber (me), U")."); |
| 315 | c = MelderReadText_getChar (me); |
| 316 | } |
| 317 | } |
| 318 | for (int i = 0; 1; i ++) { |
| 319 | char32 c = MelderReadText_getChar (me); // read past first '"' |
| 320 | if (c == U'\0') |
| 321 | Melder_throw (U"Early end of text detected while reading a string (line ", MelderReadText_getLineNumber (me), U")."); |
| 322 | if (c == U'\"') { |
| 323 | char32 next = MelderReadText_getChar (me); |
| 324 | if (next == U'\0') { break; } // closing quote is last character in file: OK |
| 325 | if (next != U'\"') { |
| 326 | if (Melder_isHorizontalOrVerticalSpace (next)) { |
| 327 | // closing quote is followed by whitespace: it is OK to skip this whitespace (no need to "ungetChar") |
| 328 | } else { |
| 329 | char32 kar2 [2] = { next, U'\0' }; |
| 330 | Melder_throw (U"Character ", kar2, U" following quote (line ", MelderReadText_getLineNumber (me), U"). End of string or undoubled quote?"); |
| 331 | } |
| 332 | break; // the expected closing double quote; not added to the buffer |
| 333 | } // else: add only one of the two quotes to the buffer |
| 334 | } |
| 335 | MelderString_appendCharacter (& buffer, c); |
| 336 | } |
| 337 | return buffer.string; |
| 338 | } |
| 339 | |
| 340 | #include "enums_getText.h" |
| 341 | #include "abcio_enums.h" |
| 342 | #include "enums_getValue.h" |
| 343 | #include "abcio_enums.h" |
| 344 | |
| 345 | int texgeti8 (MelderReadText text) { |
| 346 | try { |
| 347 | int64 externalValue = getInteger (text); |
| 348 | if (externalValue < INT8_MIN || externalValue > INT8_MAX) |
| 349 | Melder_throw (U"Value (", externalValue, U") out of range (-128 .. +127)."); |
| 350 | return (int) externalValue; |
| 351 | } catch (MelderError) { |
| 352 | Melder_throw (U"Signed small integer not read from text file."); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | int16 texgeti16 (MelderReadText text) { |
| 357 | try { |
| 358 | int64 externalValue = getInteger (text); |
| 359 | if (externalValue < INT16_MIN || externalValue > INT16_MAX) |
| 360 | Melder_throw (U"Value (", externalValue, U") out of range (-32768 .. +32767)."); |
| 361 | return (int16) externalValue; |
| 362 | } catch (MelderError) { |
| 363 | Melder_throw (U"Signed short integer not read from text file."); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | int32 texgeti32 (MelderReadText text) { |
| 368 | try { |
| 369 | int64 externalValue = getInteger (text); |
| 370 | if (externalValue < INT32_MIN || externalValue > INT32_MAX) |
| 371 | Melder_throw (U"Value (", externalValue, U") out of range (-2147483648 .. +2147483647)."); |
| 372 | return (int32) externalValue; |
| 373 | } catch (MelderError) { |
| 374 | Melder_throw (U"Signed integer not read from text file."); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | integer texgetinteger (MelderReadText text) { |
| 379 | try { |
| 380 | int64 externalValue = getInteger (text); |
| 381 | if (externalValue < INT32_MIN || externalValue > INT32_MAX) |
| 382 | Melder_throw (U"Value (", externalValue, U") out of range (-2147483648 .. +2147483647)."); // this will change |
| 383 | return (integer) externalValue; |
| 384 | } catch (MelderError) { |
| 385 | Melder_throw (U"Signed integer not read from text file."); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | unsigned int texgetu8 (MelderReadText text) { |
| 390 | try { |
| 391 | uint64 externalValue = getUnsigned (text); |
| 392 | if (externalValue > UINT8_MAX) |
| 393 | Melder_throw (U"Value (", externalValue, U") out of range (0 .. 255)."); |
| 394 | return (unsigned int) externalValue; |
| 395 | } catch (MelderError) { |
| 396 | Melder_throw (U"Unsigned small integer not read from text file."); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | uint16 texgetu16 (MelderReadText text) { |
| 401 | try { |
| 402 | uint64 externalValue = getUnsigned (text); |
| 403 | if (externalValue > UINT16_MAX) |
| 404 | Melder_throw (U"Value (", externalValue, U") out of range (0 .. 65535)."); |
| 405 | return (uint16) externalValue; |
| 406 | } catch (MelderError) { |
| 407 | Melder_throw (U"Unsigned short integer not read from text file."); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | uint32 texgetu32 (MelderReadText text) { |
| 412 | try { |
| 413 | uint64 externalValue = getUnsigned (text); |
| 414 | if (externalValue > UINT32_MAX) |
| 415 | Melder_throw (U"Value (", externalValue, U") out of range (0 .. 4294967295)."); |
| 416 | return (uint32) externalValue; |
| 417 | } catch (MelderError) { |
| 418 | Melder_throw (U"Unsigned integer not read from text file."); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | double texgetr32 (MelderReadText text) { return getReal (text); } |
| 423 | double texgetr64 (MelderReadText text) { return getReal (text); } |
| 424 | double texgetr80 (MelderReadText text) { return getReal (text); } |
| 425 | dcomplex texgetc64 (MelderReadText text) { return getComplex (text); } |
| 426 | dcomplex texgetc128 (MelderReadText text) { return getComplex (text); } |
| 427 | |
| 428 | int texgete8 (MelderReadText text, enum_generic_getValue getValue) { return getEnum (text, getValue); } |
| 429 | int texgete16 (MelderReadText text, enum_generic_getValue getValue) { return getEnum (text, getValue); } |
| 430 | bool texgeteb (MelderReadText text) { return getEnum (text, (enum_generic_getValue) kBoolean_getValue); } |
| 431 | bool texgeteq (MelderReadText text) { return getEnum (text, (enum_generic_getValue) kQuestion_getValue); } |
| 432 | bool texgetex (MelderReadText text) { return getEnum (text, (enum_generic_getValue) kExistence_getValue); } |
| 433 | autostring32 texgetw16 (MelderReadText text) { return Melder_dup (peekString (text)); } |
| 434 | autostring32 texgetw32 (MelderReadText text) { return Melder_dup (peekString (text)); } |
| 435 | |
| 436 | void texindent (MelderFile file) { file -> indent += 4; } |
| 437 | void texexdent (MelderFile file) { file -> indent -= 4; } |
| 438 | void texresetindent (MelderFile file) { file -> indent = 0; } |
| 439 | |
| 440 | #define texput_UP_TO_NINE_NULLABLE_STRINGS \ |
| 441 | conststring32 s1, conststring32 s2, conststring32 s3, \ |
| 442 | conststring32 s4, conststring32 s5, conststring32 s6, \ |
| 443 | conststring32 s7, conststring32 s8, conststring32 s9 |
| 444 | |
| 445 | void texputintro (MelderFile file, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 446 | if (file -> verbose) { |
| 447 | MelderFile_write (file, U"\n"); |
| 448 | for (int iindent = 1; iindent <= file -> indent; iindent ++) { |
| 449 | MelderFile_write (file, U" "); |
| 450 | } |
| 451 | MelderFile_write (file, |
| 452 | s1 && s1 [0] == U'd' && s1 [1] == U'_' ? & s1 [2] : & s1 [0], |
| 453 | s2 && s2 [0] == U'd' && s2 [1] == U'_' ? & s2 [2] : & s2 [0], |
| 454 | s3 && s3 [0] == U'd' && s3 [1] == U'_' ? & s3 [2] : & s3 [0], |
| 455 | s4 && s4 [0] == U'd' && s4 [1] == U'_' ? & s4 [2] : & s4 [0], |
| 456 | s5 && s5 [0] == U'd' && s5 [1] == U'_' ? & s5 [2] : & s5 [0], |
| 457 | s6 && s6 [0] == U'd' && s6 [1] == U'_' ? & s6 [2] : & s6 [0], |
| 458 | s7 && s7 [0] == U'd' && s7 [1] == U'_' ? & s7 [2] : & s7 [0], |
| 459 | s8 && s8 [0] == U'd' && s8 [1] == U'_' ? & s8 [2] : & s8 [0], |
| 460 | s9 && s9 [0] == U'd' && s9 [1] == U'_' ? & s9 [2] : & s9 [0]); |
| 461 | } |
| 462 | file -> indent += 4; |
| 463 | } |
| 464 | |
| 465 | #define PUTLEADER \ |
| 466 | MelderFile_write (file, U"\n"); \ |
| 467 | if (file -> verbose) { \ |
| 468 | for (int iindent = 1; iindent <= file -> indent; iindent ++) { \ |
| 469 | MelderFile_write (file, U" "); \ |
| 470 | } \ |
| 471 | MelderFile_write (file, \ |
| 472 | s1 && s1 [0] == U'd' && s1 [1] == U'_' ? & s1 [2] : & s1 [0], \ |
| 473 | s2 && s2 [0] == U'd' && s2 [1] == U'_' ? & s2 [2] : & s2 [0], \ |
| 474 | s3 && s3 [0] == U'd' && s3 [1] == U'_' ? & s3 [2] : & s3 [0], \ |
| 475 | s4 && s4 [0] == U'd' && s4 [1] == U'_' ? & s4 [2] : & s4 [0], \ |
| 476 | s5 && s5 [0] == U'd' && s5 [1] == U'_' ? & s5 [2] : & s5 [0], \ |
| 477 | s6 && s6 [0] == U'd' && s6 [1] == U'_' ? & s6 [2] : & s6 [0], \ |
| 478 | s7 && s7 [0] == U'd' && s7 [1] == U'_' ? & s7 [2] : & s7 [0], \ |
| 479 | s8 && s8 [0] == U'd' && s8 [1] == U'_' ? & s8 [2] : & s8 [0], \ |
| 480 | s9 && s9 [0] == U'd' && s9 [1] == U'_' ? & s9 [2] : & s9 [0]); \ |
| 481 | } |
| 482 | |
| 483 | void texputi8 (MelderFile file, int i, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 484 | PUTLEADER |
| 485 | MelderFile_write (file, file -> verbose ? U" = " : nullptr, i, file -> verbose ? U" " : nullptr); |
| 486 | } |
| 487 | void texputi16 (MelderFile file, int i, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 488 | PUTLEADER |
| 489 | MelderFile_write (file, file -> verbose ? U" = " : nullptr, i, file -> verbose ? U" " : nullptr); |
| 490 | } |
| 491 | void texputi32 (MelderFile file, long i, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 492 | PUTLEADER |
| 493 | MelderFile_write (file, file -> verbose ? U" = " : nullptr, i, file -> verbose ? U" " : nullptr); |
| 494 | } |
| 495 | void texputinteger (MelderFile file, integer number, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 496 | PUTLEADER |
| 497 | MelderFile_write (file, file -> verbose ? U" = " : nullptr, number, file -> verbose ? U" " : nullptr); |
| 498 | } |
| 499 | void texputu8 (MelderFile file, unsigned int u, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 500 | PUTLEADER |
| 501 | MelderFile_write (file, file -> verbose ? U" = " : nullptr, u, file -> verbose ? U" " : nullptr); |
| 502 | } |
| 503 | void texputu16 (MelderFile file, unsigned int u, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 504 | PUTLEADER |
| 505 | MelderFile_write (file, file -> verbose ? U" = " : nullptr, u, file -> verbose ? U" " : nullptr); |
| 506 | } |
| 507 | void texputu32 (MelderFile file, unsigned long u, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 508 | PUTLEADER |
| 509 | MelderFile_write (file, file -> verbose ? U" = " : nullptr, u, file -> verbose ? U" " : nullptr); |
| 510 | } |
| 511 | void texputr32 (MelderFile file, double x, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 512 | PUTLEADER |
| 513 | MelderFile_write (file, file -> verbose ? U" = " : nullptr, Melder_single (x), file -> verbose ? U" " : nullptr); |
| 514 | } |
| 515 | void texputr64 (MelderFile file, double x, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 516 | PUTLEADER |
| 517 | MelderFile_write (file, file -> verbose ? U" = " : nullptr, x, file -> verbose ? U" " : nullptr); |
| 518 | } |
| 519 | void texputc64 (MelderFile file, dcomplex z, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 520 | PUTLEADER |
| 521 | MelderFile_write (file, file -> verbose ? U" = " : nullptr, z, file -> verbose ? U" " : nullptr); |
| 522 | } |
| 523 | void texputc128 (MelderFile file, dcomplex z, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 524 | PUTLEADER |
| 525 | MelderFile_write (file, file -> verbose ? U" = " : nullptr, z, file -> verbose ? U" " : nullptr); |
| 526 | } |
| 527 | void texpute8 (MelderFile file, int i, conststring32 (*getText) (int), texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 528 | PUTLEADER |
| 529 | MelderFile_write (file, file -> verbose ? U" = <" : U"<", getText (i), file -> verbose ? U"> " : U">"); |
| 530 | } |
| 531 | void texpute16 (MelderFile file, int i, conststring32 (*getText) (int), texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 532 | PUTLEADER |
| 533 | MelderFile_write (file, file -> verbose ? U" = <" : U"<", getText (i), file -> verbose ? U"> " : U">"); |
| 534 | } |
| 535 | void texputeb (MelderFile file, bool i, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 536 | PUTLEADER |
| 537 | MelderFile_write (file, file -> verbose ? U" = " : nullptr, i ? U"<true>" : U"<false>", file -> verbose ? U" " : nullptr); |
| 538 | } |
| 539 | void texputeq (MelderFile file, bool i, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 540 | PUTLEADER |
| 541 | MelderFile_write (file, file -> verbose ? U"? " : nullptr, i ? U"<yes>" : U"<no>", file -> verbose ? U" " : nullptr); |
| 542 | } |
| 543 | void texputex (MelderFile file, bool i, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 544 | PUTLEADER |
| 545 | MelderFile_write (file, file -> verbose ? U"? " : nullptr, i ? U"<exists>" : U"<absent>", file -> verbose ? U" " : nullptr); |
| 546 | } |
| 547 | void texputs8 (MelderFile file, const char *s, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 548 | PUTLEADER |
| 549 | MelderFile_write (file, file -> verbose ? U" = \"" : U"\""); |
| 550 | if (s) { |
| 551 | char c; |
| 552 | while ((c = *s ++) != '\0') { |
| 553 | MelderFile_writeCharacter (file, (char32) (char8) c); |
| 554 | if (c == U'\"') MelderFile_writeCharacter (file, (char32) (char8) c); // double any internal quotes |
| 555 | } |
| 556 | } |
| 557 | MelderFile_write (file, file -> verbose ? U"\" " : U"\""); |
| 558 | } |
| 559 | void texputs16 (MelderFile file, const char *s, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 560 | PUTLEADER |
| 561 | MelderFile_write (file, file -> verbose ? U" = \"" : U"\""); |
| 562 | if (s) { |
| 563 | char c; |
| 564 | while ((c = *s ++) != '\0') { |
| 565 | MelderFile_writeCharacter (file, (char32) (char8) c); |
| 566 | if (c == '\"') MelderFile_writeCharacter (file, (char32) (char8) c); // double any internal quotes |
| 567 | } |
| 568 | } |
| 569 | MelderFile_write (file, file -> verbose ? U"\" " : U"\""); |
| 570 | } |
| 571 | void texputs32 (MelderFile file, const char *s, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 572 | PUTLEADER |
| 573 | MelderFile_write (file, file -> verbose ? U" = \"" : U"\""); |
| 574 | if (s) { |
| 575 | char c; |
| 576 | while ((c = *s ++) != '\0') { |
| 577 | MelderFile_writeCharacter (file, (char32) (char8) c); |
| 578 | if (c == '\"') MelderFile_writeCharacter (file, (char32) (char8) c); // double any internal quotes |
| 579 | } |
| 580 | } |
| 581 | MelderFile_write (file, file -> verbose ? U"\" " : U"\""); |
| 582 | } |
| 583 | void texputw16 (MelderFile file, conststring32 s, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 584 | PUTLEADER |
| 585 | MelderFile_write (file, file -> verbose ? U" = \"" : U"\""); |
| 586 | if (s) { |
| 587 | char32 c; |
| 588 | while ((c = *s ++) != U'\0') { |
| 589 | MelderFile_writeCharacter (file, c); |
| 590 | if (c == U'\"') MelderFile_writeCharacter (file, c); // double any internal quotes |
| 591 | } |
| 592 | } |
| 593 | MelderFile_write (file, file -> verbose ? U"\" " : U"\""); |
| 594 | } |
| 595 | void texputw32 (MelderFile file, conststring32 s, texput_UP_TO_NINE_NULLABLE_STRINGS) { |
| 596 | PUTLEADER |
| 597 | MelderFile_write (file, file -> verbose ? U" = \"" : U"\""); |
| 598 | if (s) { |
| 599 | char32 c; |
| 600 | while ((c = *s ++) != U'\0') { |
| 601 | MelderFile_writeCharacter (file, c); |
| 602 | if (c == U'\"') MelderFile_writeCharacter (file, c); // double any internal quotes |
| 603 | } |
| 604 | } |
| 605 | MelderFile_write (file, file -> verbose ? U"\" " : U"\""); |
| 606 | } |
| 607 | |
| 608 | /********** machine-independent binary I/O **********/ |
| 609 | |
| 610 | /* Optimizations for machines for which some of the formats are native. */ |
| 611 | |
| 612 | /* On which machines is "short" a two's complement Big-Endian (MSB-first) 2-byte word? */ |
| 613 | |
| 614 | #if defined (macintosh) && TARGET_RT_BIG_ENDIAN == 1 |
| 615 | #define binario_16bitBE 1 |
| 616 | #define binario_16bitLE 0 |
| 617 | #elif defined (_WIN32) || defined (macintosh) && TARGET_RT_LITTLE_ENDIAN == 1 |
| 618 | #define binario_16bitBE 0 |
| 619 | #define binario_16bitLE 1 |
| 620 | #else |
| 621 | #define binario_16bitBE 0 |
| 622 | #define binario_16bitLE 0 |
| 623 | #endif |
| 624 | |
| 625 | /* On which machines is "long" a two's complement Big-Endian (MSB-first) 4-byte word? */ |
| 626 | |
| 627 | #if defined (macintosh) && TARGET_RT_BIG_ENDIAN == 1 |
| 628 | #define binario_32bitBE 1 |
| 629 | #define binario_32bitLE 0 |
| 630 | #elif defined (_WIN32) || defined (macintosh) && TARGET_RT_LITTLE_ENDIAN == 1 |
| 631 | #define binario_32bitBE 0 |
| 632 | #define binario_32bitLE 1 |
| 633 | #else |
| 634 | #define binario_32bitBE 0 |
| 635 | #define binario_32bitLE 0 |
| 636 | #endif |
| 637 | |
| 638 | /* On which machines is "float" IEEE, four bytes, Most Significant Bit first? */ |
| 639 | |
| 640 | #if defined (macintosh) && TARGET_RT_BIG_ENDIAN == 1 |
| 641 | #define binario_floatIEEE4msb (sizeof (float) == 4) |
| 642 | #define binario_floatIEEE4lsb 0 |
| 643 | #elif defined (_WIN32) || defined (macintosh) && TARGET_RT_LITTLE_ENDIAN == 1 |
| 644 | #define binario_floatIEEE4msb 0 |
| 645 | #define binario_floatIEEE4lsb (sizeof (float) == 4) |
| 646 | #else |
| 647 | #define binario_floatIEEE4msb 0 |
| 648 | #define binario_floatIEEE4lsb 0 |
| 649 | #endif |
| 650 | |
| 651 | /* On which machines is "double" IEEE, eight bytes, Most Significant Bit first? */ |
| 652 | |
| 653 | #if defined (macintosh) && TARGET_RT_BIG_ENDIAN == 1 |
| 654 | #define binario_doubleIEEE8msb (sizeof (double) == 8) |
| 655 | #define binario_doubleIEEE8lsb 0 |
| 656 | #elif defined (_WIN32) || defined (macintosh) && TARGET_RT_LITTLE_ENDIAN == 1 |
| 657 | #define binario_doubleIEEE8msb 0 |
| 658 | #define binario_doubleIEEE8lsb (sizeof (double) == 8) |
| 659 | #else |
| 660 | #define binario_doubleIEEE8msb 0 |
| 661 | #define binario_doubleIEEE8lsb 0 |
| 662 | #endif |
| 663 | |
| 664 | /* |
| 665 | The routines bingetr32, bingetr64, binputr32, and binputr64, |
| 666 | were implemented by Paul Boersma from the descriptions of the IEEE floating-point formats, |
| 667 | as found in the MC68881/MC68882 User's Manual by Motorola (second edition, 1989). |
| 668 | The following copyright notice only refers to the code of bingetr10 and binputr10. |
| 669 | */ |
| 670 | |
| 671 | /* Copyright (C) 1988-1991 Apple Computer, Inc. |
| 672 | * All rights reserved. |
| 673 | * |
| 674 | * Warranty Information |
| 675 | * Even though Apple has reviewed this software, Apple makes no warranty |
| 676 | * or representation, either express or implied, with respect to this |
| 677 | * software, its quality, accuracy, merchantability, or fitness for a |
| 678 | * particular purpose. As a result, this software is provided "as is," |
| 679 | * and you, its user, are assuming the entire risk as to its quality |
| 680 | * and accuracy. |
| 681 | * |
| 682 | * This code may be used and freely distributed as long as it includes |
| 683 | * this copyright notice and the above warranty information. |
| 684 | * |
| 685 | * Machine-independent I/O routines for IEEE floating-point numbers. |
| 686 | * |
| 687 | * NaN's and infinities are converted to HUGE_VAL or HUGE, which |
| 688 | * happens to be infinity on IEEE machines. Unfortunately, it is |
| 689 | * impossible to preserve NaN's in a machine-independent way. |
| 690 | * Infinities are, however, preserved on IEEE machines. |
| 691 | * |
| 692 | * These routines have been tested on the following machines: |
| 693 | * Apple Macintosh, MPW 3.1 C compiler |
| 694 | * Apple Macintosh, THINK C compiler |
| 695 | * Silicon Graphics IRIS, MIPS compiler |
| 696 | * Cray X/MP and Y/MP |
| 697 | * Digital Equipment VAX |
| 698 | * |
| 699 | * Implemented by Malcolm Slaney and Ken Turkowski. |
| 700 | * |
| 701 | * Malcolm Slaney contributions during 1988-1990 include big- and little- |
| 702 | * endian file I/O, conversion to and from Motorola's extended 80-bit |
| 703 | * floating-point format, and conversions to and from IEEE single- |
| 704 | * precision floating-point format. |
| 705 | * |
| 706 | * In 1991, Ken Turkowski implemented the conversions to and from |
| 707 | * IEEE double-precision format, added more precision to the extended |
| 708 | * conversions, and accommodated conversions involving +/- infinity, |
| 709 | * NaN's, and denormalized numbers. |
| 710 | */ |
| 711 | |
| 712 | /**************************************************************** |
| 713 | * Extended precision IEEE floating-point conversion routines. |
| 714 | ****************************************************************/ |
| 715 | |
| 716 | /* |
| 717 | * C O N V E R T T O I E E E E X T E N D E D |
| 718 | */ |
| 719 | |
| 720 | /* |
| 721 | * C O N V E R T F R O M I E E E E X T E N D E D |
| 722 | */ |
| 723 | |
| 724 | /*************** End of Apple Computer intermezzo. ***************/ |
| 725 | |
| 726 | static void readError (FILE *f, conststring32 text) { |
| 727 | Melder_throw (feof (f) ? U"Reached end of file" : U"Error in file", U" while trying to read ", text); |
| 728 | } |
| 729 | |
| 730 | static void writeError (conststring32 text) { |
| 731 | Melder_throw (U"Error in file while trying to write ", text); |
| 732 | } |
| 733 | |
| 734 | uint32 bingetu8 (FILE *f) { |
| 735 | try { |
| 736 | int32 externalValue = getc (f); // either -1 (EOF) or in the range 0..255 |
| 737 | if (externalValue < 0) |
| 738 | readError (f, U"a byte."); |
| 739 | return (uint32) externalValue; |
| 740 | } catch (MelderError) { |
| 741 | Melder_throw (U"Unsigned integer not read from 1 byte in binary file."); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | void binputu8 (uint32 u, FILE *f) { |
| 746 | try { |
| 747 | if (putc ((int32) u, f) < 0) |
| 748 | writeError (U"a byte."); |
| 749 | } catch (MelderError) { |
| 750 | Melder_throw (U"Unsigned integer not written to 1 byte in binary file."); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | int32 bingeti8 (FILE *f) { |
| 755 | try { |
| 756 | int32 externalValue = getc (f); |
| 757 | if (externalValue < 0) |
| 758 | readError (f, U"a byte."); |
| 759 | return (signed char) externalValue; // this converts e.g. 200 to -56 |
| 760 | } catch (MelderError) { |
| 761 | Melder_throw (U"Signed integer not read from 1 byte in binary file."); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | void binputi8 (int32 u, FILE *f) { |
| 766 | try { |
| 767 | if (putc (u, f) < 0) |
| 768 | writeError (U"a byte."); |
| 769 | } catch (MelderError) { |
| 770 | Melder_throw (U"Signed integer not written to 1 byte in binary file."); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | bool bingetbool8 (FILE *f) { |
| 775 | try { |
| 776 | int32 externalValue = getc (f); |
| 777 | if (externalValue < 0) |
| 778 | readError (f, U"a byte."); |
| 779 | return (bool) externalValue; // this converts e.g. 200 to true |
| 780 | } catch (MelderError) { |
| 781 | Melder_throw (U"Boolean not read from 1 byte in binary file."); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | void binputbool8 (bool value, FILE *f) { |
| 786 | try { |
| 787 | if (putc (value, f) < 0) |
| 788 | writeError (U"a byte."); |
| 789 | } catch (MelderError) { |
| 790 | Melder_throw (U"Boolean not written to 1 byte in binary file."); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | int32 bingete8 (FILE *f, int min, int max, conststring32 type) { |
| 795 | try { |
| 796 | int32 externalValue = getc (f); |
| 797 | if (externalValue < 0) |
| 798 | readError (f, U"a byte."); |
| 799 | int32 result = (signed char) externalValue; // this converts e.g. 200 to -56, so the enumerated type is signed |
| 800 | if (result < min || result > max) |
| 801 | Melder_throw (result, U" is not a value of enumerated type <", type, U">."); |
| 802 | return result; |
| 803 | } catch (MelderError) { |
| 804 | Melder_throw (U"Enumerated type not read from 1 byte in binary file."); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | void binpute8 (int32 value, FILE *f) { |
| 809 | try { |
| 810 | if (putc (value, f) < 0) |
| 811 | writeError (U"a byte."); |
| 812 | } catch (MelderError) { |
| 813 | Melder_throw (U"Enumerated type not written to 1 byte in binary file."); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | int16 bingeti16 (FILE *f) { |
| 818 | try { |
| 819 | if (binario_16bitBE && ! (Melder_debug == 18)) { |
| 820 | int16 s; |
| 821 | if (fread (& s, sizeof (int16), 1, f) != 1) readError (f, U"a signed 16-bit integer."); |
| 822 | return s; |
| 823 | } else { |
| 824 | uint8 bytes [2]; |
| 825 | if (fread (bytes, sizeof (uint8), 2, f) != 2) readError (f, U"two bytes."); |
| 826 | return (int16) // reinterpret sign bit |
| 827 | ((uint16) ((uint16) bytes [0] << 8) | |
| 828 | (uint16) bytes [1]); |
| 829 | } |
| 830 | } catch (MelderError) { |
| 831 | Melder_throw (U"Signed integer not read from 2 bytes in binary file."); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | int16 bingeti16LE (FILE *f) { |
| 836 | try { |
| 837 | if (binario_16bitLE && ! (Melder_debug == 18)) { |
| 838 | int16 s; |
| 839 | if (fread (& s, sizeof (int16), 1, f) != 1) readError (f, U"a signed 16-bit integer."); |
| 840 | return s; |
| 841 | } else { |
| 842 | uint8 bytes [2]; |
| 843 | if (fread (bytes, sizeof (uint8), 2, f) != 2) readError (f, U"two bytes."); |
| 844 | return (int16) // reinterpret sign bit |
| 845 | ((uint16) ((uint16) bytes [1] << 8) | |
| 846 | (uint16) bytes [0]); |
| 847 | } |
| 848 | } catch (MelderError) { |
| 849 | Melder_throw (U"Signed integer not read from 2 bytes in binary file."); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | integer bingetinteger16BE (FILE *f) { |
| 854 | try { |
| 855 | if (binario_16bitBE && ! (Melder_debug == 18)) { |
| 856 | int16 s; |
| 857 | if (fread (& s, sizeof (int16), 1, f) != 1) readError (f, U"a signed 16-bit integer."); |
| 858 | return s; |
| 859 | } else { |
| 860 | uint8 bytes [2]; |
| 861 | if (fread (bytes, sizeof (uint8), 2, f) != 2) readError (f, U"two bytes."); |
| 862 | return (int16) // reinterpret sign bit |
| 863 | ((uint16) ((uint16) bytes [0] << 8) | |
| 864 | (uint16) bytes [1]); |
| 865 | } |
| 866 | } catch (MelderError) { |
| 867 | Melder_throw (U"Signed integer not read from 2 bytes in binary file."); |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | uint16 bingetu16 (FILE *f) { |
| 872 | try { |
| 873 | if (binario_16bitBE && ! (Melder_debug == 18)) { |
| 874 | uint16 s; |
| 875 | if (fread (& s, sizeof (uint16), 1, f) != 1) readError (f, U"an unsigned 16-bit integer."); |
| 876 | return s; // without sign extension |
| 877 | } else { |
| 878 | uint8 bytes [2]; |
| 879 | if (fread (bytes, sizeof (uint8), 2, f) != 2) readError (f, U"two bytes."); |
| 880 | return |
| 881 | (uint16) ((uint16) bytes [0] << 8) | |
| 882 | (uint16) bytes [1]; |
| 883 | } |
| 884 | } catch (MelderError) { |
| 885 | Melder_throw (U"Unsigned integer not read from 2 bytes in binary file."); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | uint16 bingetu16LE (FILE *f) { |
| 890 | try { |
| 891 | if (binario_16bitLE && ! (Melder_debug == 18)) { |
| 892 | uint16 s; |
| 893 | if (fread (& s, sizeof (uint16), 1, f) != 1) readError (f, U"an unsigned 16-bit integer."); |
| 894 | return s; // without sign extension |
| 895 | } else { |
| 896 | uint8 bytes [2]; |
| 897 | if (fread (bytes, sizeof (uint8), 2, f) != 2) readError (f, U"two bytes."); |
| 898 | return |
| 899 | (uint16) ((uint16) bytes [1] << 8) | |
| 900 | (uint16) bytes [0]; |
| 901 | } |
| 902 | } catch (MelderError) { |
| 903 | Melder_throw (U"Unsigned integer not read from 2 bytes in binary file."); |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | int32 bingete16 (FILE *f, int min, int max, conststring32 type) { |
| 908 | try { |
| 909 | int16 result; |
| 910 | if (binario_16bitBE && ! (Melder_debug == 18)) { |
| 911 | if (fread (& result, sizeof (int16), 1, f) != 1) readError (f, U"a signed 16-bit integer."); |
| 912 | } else { |
| 913 | uint8 bytes [2]; |
| 914 | if (fread (bytes, sizeof (uint8), 2, f) != 2) readError (f, U"two bytes."); |
| 915 | uint16 externalValue = |
| 916 | (uint16) ((uint16) bytes [0] << 8) | |
| 917 | (uint16) bytes [1]; |
| 918 | result = (int16) externalValue; |
| 919 | } |
| 920 | if (result < min || result > max) |
| 921 | Melder_throw (result, U" is not a value of enumerated type \"", type, U"\"."); |
| 922 | return (int32) result; |
| 923 | } catch (MelderError) { |
| 924 | Melder_throw (U"Enumerated value not read from 2 bytes in binary file."); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | int32 bingeti24 (FILE *f) { |
| 929 | try { |
| 930 | uint8 bytes [3]; |
| 931 | if (fread (bytes, sizeof (uint8), 3, f) != 3) readError (f, U"three bytes."); |
| 932 | uint32 externalValue = |
| 933 | (uint32) ((uint32) bytes [0] << 16) | |
| 934 | (uint32) ((uint32) bytes [1] << 8) | |
| 935 | (uint32) bytes [2]; |
| 936 | if ((bytes [0] & 128) != 0) // is the 24-bit sign bit on? |
| 937 | externalValue |= 0xFF00'0000; // extend negative sign to 32 bits |
| 938 | return (int32) externalValue; // reinterpret sign bit |
| 939 | } catch (MelderError) { |
| 940 | Melder_throw (U"Signed integer not read from 3 bytes in binary file."); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | int32 bingeti24LE (FILE *f) { |
| 945 | try { |
| 946 | uint8 bytes [3]; |
| 947 | if (fread (bytes, sizeof (uint8), 3, f) != 3) readError (f, U"three bytes."); |
| 948 | uint32 externalValue = |
| 949 | (uint32) ((uint32) bytes [2] << 16) | |
| 950 | (uint32) ((uint32) bytes [1] << 8) | |
| 951 | (uint32) bytes [0]; |
| 952 | if ((bytes [2] & 128) != 0) // is the 24-bit sign bit on? |
| 953 | externalValue |= 0xFF00'0000; // extend negative sign to 32 bits |
| 954 | return (int32) externalValue; // reinterpret sign bit |
| 955 | } catch (MelderError) { |
| 956 | Melder_throw (U"Signed integer not read from 3 bytes in binary file."); |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | int32 bingeti32 (FILE *f) { |
| 961 | try { |
| 962 | if (binario_32bitBE && ! (Melder_debug == 18)) { |
| 963 | int32 l; |
| 964 | if (fread (& l, sizeof (int32), 1, f) != 1) readError (f, U"a signed 32-bit integer."); |
| 965 | return l; |
| 966 | } else { |
| 967 | uint8 bytes [4]; |
| 968 | if (fread (bytes, sizeof (uint8), 4, f) != 4) readError (f, U"four bytes."); |
| 969 | return (int32) |
| 970 | ((uint32) ((uint32) bytes [0] << 24) | |
| 971 | (uint32) ((uint32) bytes [1] << 16) | |
| 972 | (uint32) ((uint32) bytes [2] << 8) | |
| 973 | (uint32) bytes [3]); |
| 974 | } |
| 975 | } catch (MelderError) { |
| 976 | Melder_throw (U"Signed integer not read from 4 bytes in binary file."); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | int32 bingeti32LE (FILE *f) { |
| 981 | try { |
| 982 | if (binario_32bitLE && ! (Melder_debug == 18)) { |
| 983 | int32 l; |
| 984 | if (fread (& l, sizeof (int32), 1, f) != 1) readError (f, U"a signed 32-bit integer."); |
| 985 | return l; |
| 986 | } else { |
| 987 | uint8 bytes [4]; |
| 988 | if (fread (bytes, sizeof (uint8), 4, f) != 4) readError (f, U"four bytes."); |
| 989 | return (int32) // reinterpret sign bit |
| 990 | ((uint32) ((uint32) bytes [3] << 24) | |
| 991 | (uint32) ((uint32) bytes [2] << 16) | |
| 992 | (uint32) ((uint32) bytes [1] << 8) | |
| 993 | (uint32) bytes [0]); |
| 994 | } |
| 995 | } catch (MelderError) { |
| 996 | Melder_throw (U"Signed integer not read from 4 bytes in binary file."); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | integer bingetinteger32BE (FILE *f) { |
| 1001 | try { |
| 1002 | if (binario_32bitBE && ! (Melder_debug == 18)) { |
| 1003 | int32 l; |
| 1004 | if (fread (& l, sizeof (int32), 1, f) != 1) readError (f, U"a signed 32-bit integer."); |
| 1005 | return (integer) l; |
| 1006 | } else { |
| 1007 | uint8 bytes [4]; |
| 1008 | if (fread (bytes, sizeof (uint8), 4, f) != 4) readError (f, U"four bytes."); |
| 1009 | return (integer) (int32) |
| 1010 | ((uint32) ((uint32) bytes [0] << 24) | |
| 1011 | (uint32) ((uint32) bytes [1] << 16) | |
| 1012 | (uint32) ((uint32) bytes [2] << 8) | |
| 1013 | (uint32) bytes [3]); |
| 1014 | } |
| 1015 | } catch (MelderError) { |
| 1016 | Melder_throw (U"Signed integer not read from 4 bytes in binary file."); |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | uint32 bingetu32 (FILE *f) { |
| 1021 | try { |
| 1022 | if (binario_32bitBE && ! (Melder_debug == 18)) { |
| 1023 | uint32 l; |
| 1024 | if (fread (& l, sizeof (uint32), 1, f) != 1) readError (f, U"an unsigned 32-bit integer."); |
| 1025 | return l; |
| 1026 | } else { |
| 1027 | uint8 bytes [4]; |
| 1028 | if (fread (bytes, sizeof (uint8), 4, f) != 4) readError (f, U"four bytes."); |
| 1029 | return |
| 1030 | (uint32) ((uint32) bytes [0] << 24) | |
| 1031 | (uint32) ((uint32) bytes [1] << 16) | |
| 1032 | (uint32) ((uint32) bytes [2] << 8) | |
| 1033 | (uint32) bytes [3]; |
| 1034 | } |
| 1035 | } catch (MelderError) { |
| 1036 | Melder_throw (U"Unsigned integer not read from 4 bytes in binary file."); |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | uint32 bingetu32LE (FILE *f) { |
| 1041 | try { |
| 1042 | if (binario_32bitLE && ! (Melder_debug == 18)) { |
| 1043 | uint32 l; |
| 1044 | if (fread (& l, sizeof (uint32), 1, f) != 1) readError (f, U"an unsigned 32-bit integer."); |
| 1045 | return l; |
| 1046 | } else { |
| 1047 | uint8 bytes [4]; |
| 1048 | if (fread (bytes, sizeof (uint8), 4, f) != 4) readError (f, U"four bytes."); |
| 1049 | return |
| 1050 | (uint32) ((uint32) bytes [3] << 24) | |
| 1051 | (uint32) ((uint32) bytes [2] << 16) | |
| 1052 | (uint32) ((uint32) bytes [1] << 8) | |
| 1053 | (uint32) bytes [0]; |
| 1054 | } |
| 1055 | } catch (MelderError) { |
| 1056 | Melder_throw (U"Unsigned integer not read from 4 bytes in binary file."); |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | double bingetr32 (FILE *f) { |
| 1061 | try { |
| 1062 | if (binario_floatIEEE4msb && ! (Melder_debug == 18)) { |
| 1063 | float x; |
| 1064 | if (fread (& x, sizeof (float), 1, f) != 1) readError (f, U"a 32-bit floating-point number."); |
| 1065 | return x; |
| 1066 | } else { |
| 1067 | uint8 bytes [4]; |
| 1068 | if (fread (bytes, sizeof (uint8), 4, f) != 4) readError (f, U"four bytes."); |
| 1069 | int32 exponent = (int32) |
| 1070 | ((uint32) ((uint32) ((uint32) bytes [0] & 0x0000'007F) << 1) | |
| 1071 | (uint32) ((uint32) ((uint32) bytes [1] & 0x0000'0080) >> 7)); // between 0 and 255 (it's signed because we're going to subtract something) |
| 1072 | uint32 mantissa = |
| 1073 | (uint32) ((uint32) ((uint32) bytes [1] & 0x0000'007F) << 16) | |
| 1074 | (uint32) ((uint32) bytes [2] << 8) | |
| 1075 | (uint32) bytes [3]; |
| 1076 | double x; |
| 1077 | if (exponent == 0) |
| 1078 | if (mantissa == 0) x = 0.0; |
| 1079 | else x = ldexp ((double) mantissa, exponent - 149); // denormalized |
| 1080 | else if (exponent == 0x0000'00FF) // Infinity or Not-a-Number |
| 1081 | return undefined; |
| 1082 | else // finite |
| 1083 | x = ldexp ((double) (mantissa | 0x0080'0000), exponent - 150); |
| 1084 | return bytes [0] & 0x80 ? - x : x; |
| 1085 | } |
| 1086 | } catch (MelderError) { |
| 1087 | Melder_throw (U"Floating-point number not read from 4 bytes in binary file."); |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | double bingetr32LE (FILE *f) { |
| 1092 | try { |
| 1093 | if (binario_floatIEEE4lsb && ! (Melder_debug == 18)) { |
| 1094 | float x; |
| 1095 | if (fread (& x, sizeof (float), 1, f) != 1) readError (f, U"a 32-bit floating-point number."); |
| 1096 | return x; |
| 1097 | } else { |
| 1098 | uint8 bytes [4]; |
| 1099 | if (fread (bytes, sizeof (uint8), 4, f) != 4) readError (f, U"four bytes."); |
| 1100 | int32 exponent = (int32) |
| 1101 | ((uint32) ((uint32) ((uint32) bytes [3] & 0x0000'007F) << 1) | |
| 1102 | (uint32) ((uint32) ((uint32) bytes [2] & 0x0000'0080) >> 7)); |
| 1103 | uint32 mantissa = |
| 1104 | (uint32) ((uint32) ((uint32) bytes [2] & 0x0000'007F) << 16) | |
| 1105 | (uint32) ((uint32) bytes [1] << 8) | |
| 1106 | (uint32) bytes [0]; |
| 1107 | double x; |
| 1108 | if (exponent == 0) |
| 1109 | if (mantissa == 0) x = 0.0; |
| 1110 | else x = ldexp ((double) mantissa, exponent - 149); // denormalized |
| 1111 | else if (exponent == 0x0000'00FF) // Infinity or Not-a-Number |
| 1112 | return undefined; |
| 1113 | else // finite |
| 1114 | x = ldexp ((double) (mantissa | 0x0080'0000), exponent - 150); |
| 1115 | return bytes [3] & 0x80 ? - x : x; |
| 1116 | } |
| 1117 | } catch (MelderError) { |
| 1118 | Melder_throw (U"Floating-point number not read from 4 bytes in binary file."); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | double bingetr64 (FILE *f) { |
| 1123 | try { |
| 1124 | if (binario_doubleIEEE8msb && ! (Melder_debug == 18) || Melder_debug == 181) { |
| 1125 | double x; |
| 1126 | if (fread (& x, sizeof (double), 1, f) != 1) readError (f, U"a 64-bit floating-point number."); |
| 1127 | return x; |
| 1128 | } else { |
| 1129 | uint8 bytes [8]; |
| 1130 | if (fread (bytes, sizeof (uint8), 8, f) != 8) readError (f, U"eight bytes."); |
| 1131 | int32 exponent = (int32) |
| 1132 | ((uint32) ((uint32) ((uint32) bytes [0] & 0x0000'007F) << 4) | |
| 1133 | (uint32) ((uint32) ((uint32) bytes [1] & 0x0000'00F0) >> 4)); |
| 1134 | uint32 highMantissa = |
| 1135 | (uint32) ((uint32) ((uint32) bytes [1] & 0x0000'000F) << 16) | |
| 1136 | (uint32) ((uint32) bytes [2] << 8) | |
| 1137 | (uint32) bytes [3]; |
| 1138 | uint32 lowMantissa = |
| 1139 | (uint32) ((uint32) bytes [4] << 24) | |
| 1140 | (uint32) ((uint32) bytes [5] << 16) | |
| 1141 | (uint32) ((uint32) bytes [6] << 8) | |
| 1142 | (uint32) bytes [7]; |
| 1143 | double x; |
| 1144 | if (exponent == 0) |
| 1145 | if (highMantissa == 0 && lowMantissa == 0) x = 0.0; |
| 1146 | else x = ldexp ((double) highMantissa, exponent - 1042) + |
| 1147 | ldexp ((double) lowMantissa, exponent - 1074); // denormalized |
| 1148 | else if (exponent == 0x0000'07FF) // Infinity or Not-a-Number |
| 1149 | return undefined; |
| 1150 | else |
| 1151 | x = ldexp ((double) (highMantissa | 0x0010'0000), exponent - 1043) + |
| 1152 | ldexp ((double) lowMantissa, exponent - 1075); |
| 1153 | return bytes [0] & 0x80 ? - x : x; |
| 1154 | } |
| 1155 | } catch (MelderError) { |
| 1156 | Melder_throw (U"Floating-point number not read from 8 bytes in binary file."); |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | double bingetr64LE (FILE *f) { |
| 1161 | try { |
| 1162 | if (binario_doubleIEEE8lsb && ! (Melder_debug == 18) || Melder_debug == 181) { |
| 1163 | double x; |
| 1164 | if (fread (& x, sizeof (double), 1, f) != 1) readError (f, U"a 64-bit floating-point number."); |
| 1165 | return x; |
| 1166 | } else { |
| 1167 | uint8 bytes [8]; |
| 1168 | if (fread (bytes, sizeof (uint8), 8, f) != 8) readError (f, U"eight bytes."); |
| 1169 | int32 exponent = (int32) |
| 1170 | ((uint32) ((uint32) ((uint32) bytes [7] & 0x0000'007F) << 4) | |
| 1171 | (uint32) ((uint32) ((uint32) bytes [6] & 0x0000'00F0) >> 4)); |
| 1172 | uint32 highMantissa = |
| 1173 | (uint32) ((uint32) ((uint32) bytes [6] & 0x0000'000F) << 16) | |
| 1174 | (uint32) ((uint32) bytes [5] << 8) | |
| 1175 | (uint32) bytes [4]; |
| 1176 | uint32 lowMantissa = |
| 1177 | (uint32) ((uint32) bytes [3] << 24) | |
| 1178 | (uint32) ((uint32) bytes [2] << 16) | |
| 1179 | (uint32) ((uint32) bytes [1] << 8) | |
| 1180 | (uint32) bytes [0]; |
| 1181 | double x; |
| 1182 | if (exponent == 0) |
| 1183 | if (highMantissa == 0 && lowMantissa == 0) x = 0.0; |
| 1184 | else x = ldexp ((double) highMantissa, exponent - 1042) + |
| 1185 | ldexp ((double) lowMantissa, exponent - 1074); // denormalized |
| 1186 | else if (exponent == 0x0000'07FF) // Infinity or Not-a-Number |
| 1187 | return undefined; |
| 1188 | else |
| 1189 | x = ldexp ((double) (highMantissa | 0x0010'0000), exponent - 1043) + |
| 1190 | ldexp ((double) lowMantissa, exponent - 1075); |
| 1191 | return bytes [7] & 0x80 ? - x : x; |
| 1192 | } |
| 1193 | } catch (MelderError) { |
| 1194 | Melder_throw (U"Floating-point number not read from 8 bytes in binary file."); |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | double bingetr80 (FILE *f) { |
| 1199 | try { |
| 1200 | uint8 bytes [10]; |
| 1201 | if (fread (bytes, sizeof (uint8), 10, f) != 10) readError (f, U"ten bytes."); |
| 1202 | int32 exponent = (int32) |
| 1203 | ((uint32) ((uint32) ((uint32) bytes [0] & 0x0000'007F) << 8) | |
| 1204 | (uint32) bytes [1]); // between 0 and 32767 |
| 1205 | uint32 highMantissa = |
| 1206 | (uint32) ((uint32) bytes [2] << 24) | |
| 1207 | (uint32) ((uint32) bytes [3] << 16) | |
| 1208 | (uint32) ((uint32) bytes [4] << 8) | |
| 1209 | (uint32) bytes [5]; |
| 1210 | uint32 lowMantissa = |
| 1211 | (uint32) ((uint32) bytes [6] << 24) | |
| 1212 | (uint32) ((uint32) bytes [7] << 16) | |
| 1213 | (uint32) ((uint32) bytes [8] << 8) | |
| 1214 | (uint32) bytes [9]; |
| 1215 | double x; |
| 1216 | if (exponent == 0 && highMantissa == 0 && lowMantissa == 0) x = 0.0; |
| 1217 | else if (exponent == 0x0000'7FFF) return undefined; // Infinity or NaN |
| 1218 | else { |
| 1219 | exponent -= 16'383; // between -16'382 and +16'383 |
| 1220 | x = ldexp ((double) highMantissa, exponent - 31); |
| 1221 | x += ldexp ((double) lowMantissa, exponent - 63); |
| 1222 | } |
| 1223 | return bytes [0] & 0x80 ? - x : x; |
| 1224 | } catch (MelderError) { |
| 1225 | Melder_throw (U"Floating-point number not read from 10 bytes in binary file."); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | void binputi16 (int16 i, FILE *f) { |
| 1230 | try { |
| 1231 | if (binario_16bitBE && ! (Melder_debug == 18)) { |
| 1232 | if (fwrite (& i, sizeof (short), 1, f) != 1) |
| 1233 | writeError (U"a signed 16-bit integer."); |
| 1234 | } else { |
| 1235 | uint8 bytes [2]; |
| 1236 | bytes [0] = (uint8) (i >> 8); // truncate |
| 1237 | bytes [1] = (uint8) i; // truncate |
| 1238 | if (fwrite (bytes, sizeof (uint8), 2, f) != 2) |
| 1239 | writeError (U"two bytes."); |
| 1240 | } |
| 1241 | } catch (MelderError) { |
| 1242 | Melder_throw (U"Signed integer not written to 2 bytes in binary file."); |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | void binputi16LE (int16 i, FILE *f) { |
| 1247 | try { |
| 1248 | if (binario_16bitLE && ! (Melder_debug == 18)) { |
| 1249 | if (fwrite (& i, sizeof (short), 1, f) != 1) |
| 1250 | writeError (U"a signed 16-bit integer."); |
| 1251 | } else { |
| 1252 | uint8 bytes [2]; |
| 1253 | bytes [1] = (uint8) (i >> 8); // truncate |
| 1254 | bytes [0] = (uint8) i; // truncate |
| 1255 | if (fwrite (bytes, sizeof (uint8), 2, f) != 2) |
| 1256 | writeError (U"two bytes."); |
| 1257 | } |
| 1258 | } catch (MelderError) { |
| 1259 | Melder_throw (U"Signed integer not written to 2 bytes in binary file."); |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | void binputinteger16BE (integer i, FILE *f) { |
| 1264 | try { |
| 1265 | if (i < INT16_MIN || i > INT16_MAX) |
| 1266 | Melder_throw (U"The number ", i, U" is too big to fit into 16 bits."); // this will change in the future |
| 1267 | uint8 bytes [2]; |
| 1268 | bytes [0] = (uint8) (i >> 8); // truncate |
| 1269 | bytes [1] = (uint8) i; // truncate |
| 1270 | if (fwrite (bytes, sizeof (uint8), 2, f) != 2) |
| 1271 | writeError (U"two bytes."); |
| 1272 | } catch (MelderError) { |
| 1273 | Melder_throw (U"Signed integer not written to 2 bytes in binary file."); |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | void binputu16 (uint16 u, FILE *f) { |
| 1278 | try { |
| 1279 | if (binario_16bitBE && ! (Melder_debug == 18)) { |
| 1280 | if (fwrite (& u, sizeof (uint16), 1, f) != 1) |
| 1281 | writeError (U"an unsigned 16-bit integer."); |
| 1282 | } else { |
| 1283 | uint8 bytes [2]; |
| 1284 | bytes [0] = (uint8) (u >> 8); // truncate |
| 1285 | bytes [1] = (uint8) u; // truncate |
| 1286 | if (fwrite (bytes, sizeof (uint8), 2, f) != 2) |
| 1287 | writeError (U"two bytes."); |
| 1288 | } |
| 1289 | } catch (MelderError) { |
| 1290 | Melder_throw (U"Unsigned integer not written to 2 bytes in binary file."); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | void binputu16LE (uint16 u, FILE *f) { |
| 1295 | try { |
| 1296 | if (binario_16bitLE && ! (Melder_debug == 18)) { |
| 1297 | if (fwrite (& u, sizeof (uint16), 1, f) != 1) |
| 1298 | writeError (U"an unsigned 16-bit integer."); |
| 1299 | } else { |
| 1300 | uint8 bytes [2]; |
| 1301 | bytes [1] = (uint8) (u >> 8); // truncate |
| 1302 | bytes [0] = (uint8) u; // truncate |
| 1303 | if (fwrite (bytes, sizeof (uint8), 2, f) != 2) |
| 1304 | writeError (U"two bytes."); |
| 1305 | } |
| 1306 | } catch (MelderError) { |
| 1307 | Melder_throw (U"Unsigned integer not written to 2 bytes in binary file."); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | void binpute16 (int32 value, FILE *f) { |
| 1312 | try { |
| 1313 | if (binario_16bitBE && ! (Melder_debug == 18)) { |
| 1314 | short s = value; |
| 1315 | if (fwrite (& s, sizeof (short), 1, f) != 1) |
| 1316 | writeError (U"a signed 16-bit integer"); |
| 1317 | } else { |
| 1318 | uint8 bytes [2]; |
| 1319 | bytes [0] = (uint8) (value >> 8); // truncate |
| 1320 | bytes [1] = (uint8) value; // truncate |
| 1321 | if (fwrite (bytes, sizeof (uint8), 2, f) != 2) |
| 1322 | writeError (U"two bytes."); |
| 1323 | } |
| 1324 | } catch (MelderError) { |
| 1325 | Melder_throw (U"Enumerated value not written to 2 bytes in binary file."); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | void binputi24 (int32 i, FILE *f) { |
| 1330 | try { |
| 1331 | uint8 bytes [3]; |
| 1332 | bytes [0] = (uint8) (i >> 16); // truncate |
| 1333 | bytes [1] = (uint8) (i >> 8); // truncate |
| 1334 | bytes [2] = (uint8) i; // truncate |
| 1335 | if (fwrite (bytes, sizeof (uint8), 3, f) != 3) |
| 1336 | writeError (U"three bytes"); |
| 1337 | } catch (MelderError) { |
| 1338 | Melder_throw (U"Signed integer not written to 3 bytes in binary file."); |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | void binputi24LE (int32 i, FILE *f) { |
| 1343 | try { |
| 1344 | uint8 bytes [3]; |
| 1345 | bytes [2] = (uint8) (i >> 16); // truncate |
| 1346 | bytes [1] = (uint8) (i >> 8); // truncate |
| 1347 | bytes [0] = (uint8) i; // truncate |
| 1348 | if (fwrite (bytes, sizeof (uint8), 3, f) != 3) |
| 1349 | writeError (U"three bytes"); |
| 1350 | } catch (MelderError) { |
| 1351 | Melder_throw (U"Signed integer not written to 3 bytes in binary file."); |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | void binputi32 (int32 i, FILE *f) { |
| 1356 | try { |
| 1357 | if (binario_32bitBE && ! (Melder_debug == 18)) { |
| 1358 | if (fwrite (& i, sizeof (int32), 1, f) != 1) |
| 1359 | writeError (U"a signed 32-bit integer."); |
| 1360 | } else { |
| 1361 | uint8 bytes [4]; |
| 1362 | bytes [0] = (uint8) (i >> 24); // truncate |
| 1363 | bytes [1] = (uint8) (i >> 16); // truncate |
| 1364 | bytes [2] = (uint8) (i >> 8); // truncate |
| 1365 | bytes [3] = (uint8) i; // truncate |
| 1366 | if (fwrite (bytes, sizeof (uint8), 4, f) != 4) |
| 1367 | writeError (U"four bytes."); |
| 1368 | } |
| 1369 | } catch (MelderError) { |
| 1370 | Melder_throw (U"Signed integer not written to 4 bytes in binary file."); |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | void binputi32LE (int32 i, FILE *f) { |
| 1375 | try { |
| 1376 | if (binario_32bitLE && ! (Melder_debug == 18)) { |
| 1377 | if (fwrite (& i, sizeof (int32), 1, f) != 1) |
| 1378 | writeError (U"a signed 32-bit integer."); |
| 1379 | } else { |
| 1380 | uint8 bytes [4]; |
| 1381 | bytes [3] = (uint8) (i >> 24); // truncate |
| 1382 | bytes [2] = (uint8) (i >> 16); // truncate |
| 1383 | bytes [1] = (uint8) (i >> 8); // truncate |
| 1384 | bytes [0] = (uint8) i; // truncate |
| 1385 | if (fwrite (bytes, sizeof (uint8), 4, f) != 4) |
| 1386 | writeError (U"four bytes."); |
| 1387 | } |
| 1388 | } catch (MelderError) { |
| 1389 | Melder_throw (U"Signed integer not written to 4 bytes in binary file."); |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | void binputinteger32BE (integer i, FILE *f) { |
| 1394 | try { |
| 1395 | if (i < INT32_MIN || i > INT32_MAX) |
| 1396 | Melder_throw (U"The number ", i, U" is too big to fit into 32 bits."); // this will change in the future |
| 1397 | uint8 bytes [4]; |
| 1398 | bytes [0] = (uint8) (i >> 24); // truncate |
| 1399 | bytes [1] = (uint8) (i >> 16); // truncate |
| 1400 | bytes [2] = (uint8) (i >> 8); // truncate |
| 1401 | bytes [3] = (uint8) i; // truncate |
| 1402 | if (fwrite (bytes, sizeof (uint8), 4, f) != 4) |
| 1403 | writeError (U"a signed 32-bit integer."); |
| 1404 | } catch (MelderError) { |
| 1405 | Melder_throw (U"Signed integer not written to 4 bytes in binary file."); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | void binputu32 (uint32 u, FILE *f) { |
| 1410 | try { |
| 1411 | if (binario_32bitBE && ! (Melder_debug == 18)) { |
| 1412 | if (fwrite (& u, sizeof (uint32), 1, f) != 1) |
| 1413 | writeError (U"an unsigned 32-bit integer."); |
| 1414 | } else { |
| 1415 | uint8 bytes [4]; |
| 1416 | bytes [0] = (uint8) (u >> 24); // truncate |
| 1417 | bytes [1] = (uint8) (u >> 16); // truncate |
| 1418 | bytes [2] = (uint8) (u >> 8); // truncate |
| 1419 | bytes [3] = (uint8) u; // truncate |
| 1420 | if (fwrite (bytes, sizeof (uint8), 4, f) != 4) |
| 1421 | writeError (U"four bytes."); |
| 1422 | } |
| 1423 | } catch (MelderError) { |
| 1424 | Melder_throw (U"Unsigned integer not written to 4 bytes in binary file."); |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | void binputu32LE (uint32 u, FILE *f) { |
| 1429 | try { |
| 1430 | if (binario_32bitLE && ! (Melder_debug == 18)) { |
| 1431 | if (fwrite (& u, sizeof (uint32), 1, f) != 1) |
| 1432 | writeError (U"an unsigned 32-bit integer."); |
| 1433 | } else { |
| 1434 | uint8 bytes [4]; |
| 1435 | bytes [3] = (uint8) (u >> 24); // truncate |
| 1436 | bytes [2] = (uint8) (u >> 16); // truncate |
| 1437 | bytes [1] = (uint8) (u >> 8); // truncate |
| 1438 | bytes [0] = (uint8) u; // truncate |
| 1439 | if (fwrite (bytes, sizeof (uint8), 4, f) != 4) |
| 1440 | writeError (U"four bytes."); |
| 1441 | } |
| 1442 | } catch (MelderError) { |
| 1443 | Melder_throw (U"Unsigned integer not written to 4 bytes in binary file."); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | void binputr32 (double x, FILE *f) { |
| 1448 | try { |
| 1449 | if (binario_floatIEEE4msb && ! (Melder_debug == 18)) { |
| 1450 | float x32 = (float) x; // convert down, with loss of precision |
| 1451 | if (fwrite (& x32, sizeof (float), 1, f) != 1) |
| 1452 | writeError (U"a 32-bit floating-point number."); |
| 1453 | } else { |
| 1454 | uint8 bytes [4]; |
| 1455 | int sign, exponent; |
| 1456 | double fMantissa, fsMantissa; |
| 1457 | uint32 mantissa; |
| 1458 | if (x < 0.0) { |
| 1459 | sign = 0x0100; |
| 1460 | x *= -1.0; |
| 1461 | } else |
| 1462 | sign = 0; |
| 1463 | if (x == 0.0) { |
| 1464 | exponent = 0; |
| 1465 | mantissa = 0; |
| 1466 | } else { |
| 1467 | fMantissa = frexp (x, & exponent); |
| 1468 | if ((exponent > 128) || ! (fMantissa < 1.0)) // Infinity or Not-a-Number |
| 1469 | { exponent = sign | 0x00FF; mantissa = 0; } // Infinity |
| 1470 | else { // finite |
| 1471 | exponent += 126; // add bias |
| 1472 | if (exponent <= 0) { // denormalized |
| 1473 | fMantissa = ldexp (fMantissa, exponent - 1); |
| 1474 | exponent = 0; |
| 1475 | } |
| 1476 | exponent |= sign; |
| 1477 | fMantissa = ldexp (fMantissa, 24); |
| 1478 | fsMantissa = floor (fMantissa); |
| 1479 | mantissa = (uint32) fsMantissa & 0x007FFFFF; |
| 1480 | } |
| 1481 | } |
| 1482 | bytes [0] = (uint8) (exponent >> 1); // truncate: bits 2 through 9 (bit 9 is the sign bit) |
| 1483 | bytes [1] = (uint8) ((exponent << 7) | (mantissa >> 16)); // truncate |
| 1484 | bytes [2] = (uint8) (mantissa >> 8); // truncate |
| 1485 | bytes [3] = (uint8) mantissa; // truncate |
| 1486 | if (fwrite (bytes, sizeof (uint8), 4, f) != 4) |
| 1487 | writeError (U"four bytes."); |
| 1488 | } |
| 1489 | } catch (MelderError) { |
| 1490 | Melder_throw (U"Floating-point number not written to 4 bytes in binary file."); |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | void binputr32LE (double x, FILE *f) { |
| 1495 | try { |
| 1496 | if (binario_floatIEEE4lsb && ! (Melder_debug == 18)) { |
| 1497 | float x32 = (float) x; // convert down, with loss of precision |
| 1498 | if (fwrite (& x32, sizeof (float), 1, f) != 1) |
| 1499 | writeError (U"a 32-bit floating-point number."); |
| 1500 | } else { |
| 1501 | uint8 bytes [4]; |
| 1502 | int sign, exponent; |
| 1503 | double fMantissa, fsMantissa; |
| 1504 | uint32 mantissa; |
| 1505 | if (x < 0.0) { |
| 1506 | sign = 0x0100; |
| 1507 | x *= -1.0; |
| 1508 | } else |
| 1509 | sign = 0; |
| 1510 | if (x == 0.0) { |
| 1511 | exponent = 0; |
| 1512 | mantissa = 0; |
| 1513 | } else { |
| 1514 | fMantissa = frexp (x, & exponent); |
| 1515 | if ((exponent > 128) || ! (fMantissa < 1.0)) // Infinity or Not-a-Number |
| 1516 | { exponent = sign | 0x00FF; mantissa = 0; } // Infinity |
| 1517 | else { // finite |
| 1518 | exponent += 126; // add bias |
| 1519 | if (exponent <= 0) { // denormalized |
| 1520 | fMantissa = ldexp (fMantissa, exponent - 1); |
| 1521 | exponent = 0; |
| 1522 | } |
| 1523 | exponent |= sign; |
| 1524 | fMantissa = ldexp (fMantissa, 24); |
| 1525 | fsMantissa = floor (fMantissa); |
| 1526 | mantissa = (uint32) fsMantissa & 0x007F'FFFF; |
| 1527 | } |
| 1528 | } |
| 1529 | bytes [3] = (uint8) (exponent >> 1); |
| 1530 | bytes [2] = (uint8) ((exponent << 7) | (mantissa >> 16)); |
| 1531 | bytes [1] = (uint8) (mantissa >> 8); |
| 1532 | bytes [0] = (uint8) mantissa; |
| 1533 | if (fwrite (bytes, sizeof (uint8), 4, f) != 4) |
| 1534 | writeError (U"four bytes."); |
| 1535 | } |
| 1536 | } catch (MelderError) { |
| 1537 | Melder_throw (U"Floating-point number not written to 4 bytes in binary file."); |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | void binputr64 (double x, FILE *f) { |
| 1542 | try { |
| 1543 | if (binario_doubleIEEE8msb && ! (Melder_debug == 18) || Melder_debug == 181) { |
| 1544 | if (fwrite (& x, sizeof (double), 1, f) != 1) |
| 1545 | writeError (U"a 64-bit floating-point number."); |
| 1546 | } else if (binario_doubleIEEE8lsb && ! (Melder_debug == 18)) { |
| 1547 | union { double xx; uint8 bytes [8]; }; |
| 1548 | xx = x; |
| 1549 | std::swap (bytes [0], bytes [7]); |
| 1550 | std::swap (bytes [1], bytes [6]); |
| 1551 | std::swap (bytes [2], bytes [5]); |
| 1552 | std::swap (bytes [3], bytes [4]); |
| 1553 | if (fwrite (& xx, sizeof (double), 1, f) != 1) |
| 1554 | writeError (U"a 64-bit floating-point number."); |
| 1555 | } else { |
| 1556 | uint8 bytes [8]; |
| 1557 | int sign, exponent; |
| 1558 | double fMantissa, fsMantissa; |
| 1559 | uint32 highMantissa, lowMantissa; |
| 1560 | if (x < 0.0) { |
| 1561 | sign = 0x0800; |
| 1562 | x *= -1.0; |
| 1563 | } else |
| 1564 | sign = 0; |
| 1565 | if (x == 0.0) { |
| 1566 | exponent = 0; |
| 1567 | highMantissa = 0; |
| 1568 | lowMantissa = 0; |
| 1569 | } else { |
| 1570 | fMantissa = frexp (x, & exponent); |
| 1571 | if (/*(exponent > 1024) ||*/ ! (fMantissa < 1.0)) // Infinity or Not-a-Number |
| 1572 | { exponent = sign | 0x07FF; highMantissa = 0; lowMantissa = 0; } // Infinity |
| 1573 | else { // finite |
| 1574 | exponent += 1022; // add bias |
| 1575 | if (exponent <= 0) { // denormalized |
| 1576 | fMantissa = ldexp (fMantissa, exponent - 1); |
| 1577 | exponent = 0; |
| 1578 | } |
| 1579 | exponent |= sign; |
| 1580 | fMantissa = ldexp (fMantissa, 21); |
| 1581 | fsMantissa = floor (fMantissa); |
| 1582 | highMantissa = (uint32) fsMantissa & 0x000F'FFFF; |
| 1583 | fMantissa = ldexp (fMantissa - fsMantissa, 32); |
| 1584 | fsMantissa = floor (fMantissa); |
| 1585 | lowMantissa = (uint32) fsMantissa; |
| 1586 | } |
| 1587 | } |
| 1588 | bytes [0] = (uint8) (exponent >> 4); |
| 1589 | bytes [1] = (uint8) ((exponent << 4) | (highMantissa >> 16)); |
| 1590 | bytes [2] = (uint8) (highMantissa >> 8); |
| 1591 | bytes [3] = (uint8) highMantissa; |
| 1592 | bytes [4] = (uint8) (lowMantissa >> 24); |
| 1593 | bytes [5] = (uint8) (lowMantissa >> 16); |
| 1594 | bytes [6] = (uint8) (lowMantissa >> 8); |
| 1595 | bytes [7] = (uint8) lowMantissa; |
| 1596 | if (fwrite (bytes, sizeof (uint8), 8, f) != 8) |
| 1597 | writeError (U"eight bytes."); |
| 1598 | } |
| 1599 | } catch (MelderError) { |
| 1600 | Melder_throw (U"Floating-point number not written to 8 bytes in binary file."); |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | void binputr64LE (double x, FILE *f) { |
| 1605 | try { |
| 1606 | if (binario_doubleIEEE8lsb && ! (Melder_debug == 18) || Melder_debug == 181) { |
| 1607 | if (fwrite (& x, sizeof (double), 1, f) != 1) |
| 1608 | writeError (U"a 64-bit floating-point number."); |
| 1609 | } else if (binario_doubleIEEE8msb && ! (Melder_debug == 18)) { |
| 1610 | union { double xx; uint8 bytes [8]; }; |
| 1611 | xx = x; |
| 1612 | std::swap (bytes [0], bytes [7]); |
| 1613 | std::swap (bytes [1], bytes [6]); |
| 1614 | std::swap (bytes [2], bytes [5]); |
| 1615 | std::swap (bytes [3], bytes [4]); |
| 1616 | if (fwrite (& xx, sizeof (double), 1, f) != 1) |
| 1617 | writeError (U"a 64-bit floating-point number."); |
| 1618 | } else { |
| 1619 | uint8 bytes [8]; |
| 1620 | int sign, exponent; |
| 1621 | double fMantissa, fsMantissa; |
| 1622 | uint32 highMantissa, lowMantissa; |
| 1623 | if (x < 0.0) { |
| 1624 | sign = 0x0800; |
| 1625 | x *= -1.0; |
| 1626 | } else |
| 1627 | sign = 0; |
| 1628 | if (x == 0.0) { |
| 1629 | exponent = 0; |
| 1630 | highMantissa = 0; |
| 1631 | lowMantissa = 0; |
| 1632 | } else { |
| 1633 | fMantissa = frexp (x, & exponent); |
| 1634 | if (/*(exponent > 1024) ||*/ ! (fMantissa < 1.0)) // Infinity or Not-a-Number |
| 1635 | { exponent = sign | 0x07FF; highMantissa = 0; lowMantissa = 0; } // Infinity |
| 1636 | else { // finite |
| 1637 | exponent += 1022; // add bias |
| 1638 | if (exponent <= 0) { // denormalized |
| 1639 | fMantissa = ldexp (fMantissa, exponent - 1); |
| 1640 | exponent = 0; |
| 1641 | } |
| 1642 | exponent |= sign; |
| 1643 | fMantissa = ldexp (fMantissa, 21); |
| 1644 | fsMantissa = floor (fMantissa); |
| 1645 | highMantissa = (uint32) fsMantissa & 0x000F'FFFF; |
| 1646 | fMantissa = ldexp (fMantissa - fsMantissa, 32); |
| 1647 | fsMantissa = floor (fMantissa); |
| 1648 | lowMantissa = (uint32) fsMantissa; |
| 1649 | } |
| 1650 | } |
| 1651 | bytes [7] = (uint8) (exponent >> 4); |
| 1652 | bytes [6] = (uint8) ((exponent << 4) | (highMantissa >> 16)); |
| 1653 | bytes [5] = (uint8) (highMantissa >> 8); |
| 1654 | bytes [4] = (uint8) highMantissa; |
| 1655 | bytes [3] = (uint8) (lowMantissa >> 24); |
| 1656 | bytes [2] = (uint8) (lowMantissa >> 16); |
| 1657 | bytes [1] = (uint8) (lowMantissa >> 8); |
| 1658 | bytes [0] = (uint8) lowMantissa; |
| 1659 | if (fwrite (bytes, sizeof (uint8), 8, f) != 8) |
| 1660 | writeError (U"eight bytes."); |
| 1661 | } |
| 1662 | } catch (MelderError) { |
| 1663 | Melder_throw (U"Floating-point number not written to 8 bytes in binary file."); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | void binputr80 (double x, FILE *f) { |
| 1668 | try { |
| 1669 | uint8 bytes [10]; |
| 1670 | Melder_assert (sizeof (int) > 2); |
| 1671 | int sign, exponent; // these should be uint16, but frexp() expects an int |
| 1672 | double fMantissa, fsMantissa; |
| 1673 | uint32 highMantissa, lowMantissa; |
| 1674 | if (x < 0.0) { |
| 1675 | sign = 0x8000; |
| 1676 | x *= -1.0; |
| 1677 | } else |
| 1678 | sign = 0; |
| 1679 | if (x == 0.0) { |
| 1680 | exponent = 0; |
| 1681 | highMantissa = 0; |
| 1682 | lowMantissa = 0; |
| 1683 | } else { |
| 1684 | fMantissa = frexp (x, & exponent); |
| 1685 | if ((exponent > 16384) || ! (fMantissa < 1.0)) // Infinity or Not-a-Number |
| 1686 | { exponent = sign | 0x7FFF; highMantissa = 0; lowMantissa = 0; } // Infinity |
| 1687 | else { // finite |
| 1688 | exponent += 16382; // add bias |
| 1689 | if (exponent < 0) { // denormalized |
| 1690 | fMantissa = ldexp (fMantissa, exponent); |
| 1691 | exponent = 0; |
| 1692 | } |
| 1693 | exponent |= sign; |
| 1694 | fMantissa = ldexp (fMantissa, 32); |
| 1695 | fsMantissa = floor (fMantissa); |
| 1696 | highMantissa = (uint32) fsMantissa; |
| 1697 | fMantissa = ldexp (fMantissa - fsMantissa, 32); |
| 1698 | fsMantissa = floor (fMantissa); |
| 1699 | lowMantissa = (uint32) fsMantissa; |
| 1700 | } |
| 1701 | } |
| 1702 | bytes [0] = (uint8) (exponent >> 8); |
| 1703 | bytes [1] = (uint8) exponent; |
| 1704 | bytes [2] = (uint8) (highMantissa >> 24); |
| 1705 | bytes [3] = (uint8) (highMantissa >> 16); |
| 1706 | bytes [4] = (uint8) (highMantissa >> 8); |
| 1707 | bytes [5] = (uint8) highMantissa; |
| 1708 | bytes [6] = (uint8) (lowMantissa >> 24); |
| 1709 | bytes [7] = (uint8) (lowMantissa >> 16); |
| 1710 | bytes [8] = (uint8) (lowMantissa >> 8); |
| 1711 | bytes [9] = (uint8) lowMantissa; |
| 1712 | if (fwrite (bytes, sizeof (uint8), 10, f) != 10) |
| 1713 | writeError (U"ten bytes."); |
| 1714 | } catch (MelderError) { |
| 1715 | Melder_throw (U"Floating-point number not written to 10 bytes in binary file."); |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | dcomplex bingetc64 (FILE *f) { |
| 1720 | try { |
| 1721 | dcomplex result; |
| 1722 | result. real (bingetr32 (f)); |
| 1723 | result. imag (bingetr32 (f)); |
| 1724 | return result; |
| 1725 | } catch (MelderError) { |
| 1726 | Melder_throw (U"Complex number not read from 8 bytes in binary file."); |
| 1727 | dcomplex result { }; |
| 1728 | return result; |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | dcomplex bingetc128 (FILE *f) { |
| 1733 | try { |
| 1734 | dcomplex result; |
| 1735 | result. real (bingetr64 (f)); |
| 1736 | result. imag (bingetr64 (f)); |
| 1737 | return result; |
| 1738 | } catch (MelderError) { |
| 1739 | Melder_throw (U"Complex number not read from 16 bytes in binary file."); |
| 1740 | dcomplex result { }; |
| 1741 | return result; |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | void binputc64 (dcomplex z, FILE *f) { |
| 1746 | try { |
| 1747 | binputr32 (z.real(), f); |
| 1748 | binputr32 (z.imag(), f); |
| 1749 | } catch (MelderError) { |
| 1750 | Melder_throw (U"Complex number not written to 8 bytes in binary file."); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | void binputc128 (dcomplex z, FILE *f) { |
| 1755 | try { |
| 1756 | binputr64 (z.real(), f); |
| 1757 | binputr64 (z.imag(), f); |
| 1758 | } catch (MelderError) { |
| 1759 | Melder_throw (U"Complex number not written to 16 bytes in binary file."); |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | autostring8 bingets8 (FILE *f) { |
| 1764 | try { |
| 1765 | uint32 length = bingetu8 (f); |
| 1766 | autostring8 result (length); |
| 1767 | if (fread (result.get(), sizeof (char), length, f) != length) |
| 1768 | Melder_throw (feof (f) ? U"Reached end of file" : U"Error in file", U" while trying to read ", length, U" one-byte characters."); |
| 1769 | result [length] = 0; // trailing null byte |
| 1770 | return result; |
| 1771 | } catch (MelderError) { |
| 1772 | Melder_throw (U"Text not read from a binary file."); |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | autostring8 bingets16 (FILE *f) { |
| 1777 | try { |
| 1778 | uint16 length = bingetu16 (f); |
| 1779 | autostring8 result (length); |
| 1780 | if (fread (result.get(), sizeof (char), length, f) != length) |
| 1781 | Melder_throw (feof (f) ? U"Reached end of file" : U"Error in file", U" while trying to read ", length, U" one-byte characters."); |
| 1782 | result [length] = 0; // trailing null byte |
| 1783 | return result; |
| 1784 | } catch (MelderError) { |
| 1785 | Melder_throw (U"Text not read from a binary file."); |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | autostring8 bingets32 (FILE *f) { |
| 1790 | try { |
| 1791 | uint32 length = bingetu32 (f); |
| 1792 | autostring8 result (length); |
| 1793 | if (fread (result.get(), sizeof (char), length, f) != length) |
| 1794 | Melder_throw (feof (f) ? U"Reached end of file" : U"Error in file", U" while trying to read ", length, U" one-byte characters."); |
| 1795 | result [length] = 0; // trailing null byte |
| 1796 | return result; |
| 1797 | } catch (MelderError) { |
| 1798 | Melder_throw (U"Text not read from a binary file."); |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | autostring32 bingetw8 (FILE *f) { |
| 1803 | try { |
| 1804 | autostring32 result; |
| 1805 | uint32 length = bingetu8 (f); |
| 1806 | if (length == 0xFF) { // an escape for encoding |
| 1807 | /* |
| 1808 | UTF-16 |
| 1809 | */ |
| 1810 | length = bingetu8 (f); |
| 1811 | result = autostring32 (length); |
| 1812 | for (unsigned int i = 0; i < length; i ++) { |
| 1813 | char32 kar = bingetu16 (f); |
| 1814 | if ((kar & 0x00'F800) == 0x00'D800) { |
| 1815 | if (kar > 0x00'DBFF) |
| 1816 | Melder_throw (U"Incorrect Unicode value (first surrogate member ", kar, U")."); |
| 1817 | char32 kar2 = bingetu16 (f); |
| 1818 | if (kar2 < 0x00'DC'00 || kar2 > 0x00'DF'FF) |
| 1819 | Melder_throw (U"Incorrect Unicode value (second surrogate member ", kar2, U")."); |
| 1820 | result [i] = (((kar & 0x00'03FF) << 10) | (kar2 & 0x00'03FF)) + 0x01'0000; |
| 1821 | } else { |
| 1822 | result [i] = kar; |
| 1823 | } |
| 1824 | } |
| 1825 | } else { |
| 1826 | /* |
| 1827 | ASCII |
| 1828 | */ |
| 1829 | result = autostring32 (length); |
| 1830 | for (uint32 i = 0; i < length; i ++) |
| 1831 | result [i] = bingetu8 (f); |
| 1832 | } |
| 1833 | result [length] = U'\0'; |
| 1834 | return result; |
| 1835 | } catch (MelderError) { |
| 1836 | Melder_throw (U"Text not read from a binary file."); |
| 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | autostring32 bingetw16 (FILE *f) { |
| 1841 | try { |
| 1842 | autostring32 result; |
| 1843 | uint16 length = bingetu16 (f); |
| 1844 | if (length == 0xFFFF) { // an escape for encoding |
| 1845 | /* |
| 1846 | UTF-16 |
| 1847 | */ |
| 1848 | length = bingetu16 (f); |
| 1849 | result = autostring32 (length); |
| 1850 | for (uint16 i = 0; i < length; i ++) { |
| 1851 | char32 kar = (char32) (char16) bingetu16 (f); |
| 1852 | if ((kar & 0x00'F800) == 0x00'D800) { |
| 1853 | if (kar > 0x00'DBFF) |
| 1854 | Melder_throw (U"Incorrect Unicode value (first surrogate member ", kar, U")."); |
| 1855 | char32 kar2 = (char32) (char16) bingetu16 (f); |
| 1856 | if (kar2 < 0x00'DC00 || kar2 > 0x00'DFFF) |
| 1857 | Melder_throw (U"Incorrect Unicode value (second surrogate member ", kar2, U")."); |
| 1858 | result [i] = (((kar & 0x00'03FF) << 10) | (kar2 & 0x00'03FF)) + 0x01'0000; |
| 1859 | } else { |
| 1860 | result [i] = kar; |
| 1861 | } |
| 1862 | } |
| 1863 | } else { |
| 1864 | /* |
| 1865 | ASCII |
| 1866 | */ |
| 1867 | result = autostring32 (length); |
| 1868 | for (uint16 i = 0; i < length; i ++) |
| 1869 | result [i] = (char32) (char8) bingetu8 (f); |
| 1870 | } |
| 1871 | result [length] = U'\0'; |
| 1872 | return result; |
| 1873 | } catch (MelderError) { |
| 1874 | Melder_throw (U"Text not read from a binary file."); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | autostring32 bingetw32 (FILE *f) { |
| 1879 | try { |
| 1880 | autostring32 result; |
| 1881 | uint32 length = bingetu32 (f); |
| 1882 | if (length == 0xFFFF'FFFF) { // an escape for encoding |
| 1883 | /* |
| 1884 | UTF-16 |
| 1885 | */ |
| 1886 | length = bingetu32 (f); |
| 1887 | result = autostring32 (length); |
| 1888 | for (uint32 i = 0; i < length; i ++) { |
| 1889 | char32 kar = bingetu16 (f); |
| 1890 | if ((kar & 0x00'F800) == 0x00'D800) { |
| 1891 | if (kar > 0x00'DBFF) |
| 1892 | Melder_throw (U"Incorrect Unicode value (first surrogate member ", kar, U")."); |
| 1893 | char32 kar2 = bingetu16 (f); |
| 1894 | if (kar2 < 0x00'DC00 || kar2 > 0x00'DFFF) |
| 1895 | Melder_throw (U"Incorrect Unicode value (second surrogate member ", kar2, U")."); |
| 1896 | result [i] = (((kar & 0x00'03FF) << 10) | (kar2 & 0x00'03FF)) + 0x01'0000; |
| 1897 | } else { |
| 1898 | result [i] = kar; |
| 1899 | } |
| 1900 | } |
| 1901 | } else { |
| 1902 | /* |
| 1903 | ASCII |
| 1904 | */ |
| 1905 | result = autostring32 (length); |
| 1906 | for (uint32 i = 0; i < length; i ++) |
| 1907 | result [i] = bingetu8 (f); |
| 1908 | } |
| 1909 | result [length] = U'\0'; |
| 1910 | return result; |
| 1911 | } catch (MelderError) { |
| 1912 | Melder_throw (U"Text not read from a binary file."); |
| 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | void binputs8 (const char *s, FILE *f) { |
| 1917 | try { |
| 1918 | if (! s) { |
| 1919 | binputu8 (0, f); |
| 1920 | } else { |
| 1921 | size_t length = strlen (s); |
| 1922 | if (length > UINT8_MAX) { |
| 1923 | Melder_warning (U"Text of ", length, U" characters truncated to 255 characters."); |
| 1924 | length = UINT8_MAX; |
| 1925 | } |
| 1926 | binputu8 ((uint32) length, f); // guarded conversion down (or not: `length` may be uint32 or uint64) |
| 1927 | if (fwrite (s, sizeof (char), length, f) != length) |
| 1928 | Melder_throw (U"Error in file while trying to write ", length, U" one-byte characters."); |
| 1929 | } |
| 1930 | } catch (MelderError) { |
| 1931 | Melder_throw (U"Text not written to a binary file."); |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | void binputs16 (const char *s, FILE *f) { |
| 1936 | try { |
| 1937 | if (! s) { |
| 1938 | binputu16 (0, f); |
| 1939 | } else { |
| 1940 | size_t length = strlen (s); |
| 1941 | if (length > UINT16_MAX) { |
| 1942 | Melder_warning (U"Text of ", length, U" characters truncated to 65535 characters."); |
| 1943 | length = UINT16_MAX; |
| 1944 | } |
| 1945 | binputu16 ((uint16) length, f); // guarded conversion down |
| 1946 | if (fwrite (s, sizeof (char), length, f) != length) |
| 1947 | Melder_throw (U"Error in file while trying to write ", length, U" one-byte characters."); |
| 1948 | } |
| 1949 | } catch (MelderError) { |
| 1950 | Melder_throw (U"Text not written to a binary file."); |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | void binputs32 (const char *s, FILE *f) { |
| 1955 | try { |
| 1956 | if (! s) { |
| 1957 | binputu32 (0, f); |
| 1958 | } else { |
| 1959 | size_t length = strlen (s); |
| 1960 | if (length > UINT32_MAX) { |
| 1961 | Melder_warning (U"Text of ", length, U" characters truncated to 4,294,967,295 characters."); |
| 1962 | length = UINT32_MAX; |
| 1963 | } |
| 1964 | binputu32 ((uint32) length, f); // guarded conversion down (unless size_t is already uint32) |
| 1965 | if (fwrite (s, sizeof (char), length, f) != length) |
| 1966 | Melder_throw (U"Error in file while trying to write ", length, U" one-byte characters."); |
| 1967 | } |
| 1968 | } catch (MelderError) { |
| 1969 | Melder_throw (U"Text not written to a binary file."); |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | static inline void binpututf16 (char32 kar, FILE *f) { |
| 1974 | if (kar <= 0x00FFFF) { |
| 1975 | binputu16 ((uint16) kar, f); // truncate to lower 16 bits |
| 1976 | } else if (kar <= 0x10'FFFF) { |
| 1977 | kar -= 0x01'0000; |
| 1978 | binputu16 ((uint16) (0x00'D800 | (kar >> 10)), f); |
| 1979 | binputu16 ((uint16) (0x00'DC00 | (kar & 0x00'03FF)), f); |
| 1980 | } else { |
| 1981 | Melder_crash (U"Impossible Unicode value."); |
| 1982 | } |
| 1983 | } |
| 1984 | |
| 1985 | void binputw8 (conststring32 s, FILE *f) { |
| 1986 | try { |
| 1987 | if (! s) { |
| 1988 | binputu8 (0, f); |
| 1989 | } else { |
| 1990 | integer length = Melder_length (s); |
| 1991 | if (length > UINT8_MAX - 1) { |
| 1992 | Melder_warning (U"Text of ", length, U" characters truncated to 254 characters."); |
| 1993 | length = UINT8_MAX - 1; |
| 1994 | } |
| 1995 | if (Melder_isValidAscii (s)) { |
| 1996 | /* |
| 1997 | ASCII |
| 1998 | */ |
| 1999 | binputu8 ((uint32) length, f); // guarded conversion down |
| 2000 | for (integer i = 0; i < length; i ++) |
| 2001 | binputu8 ((uint32) (uint8) s [i], f); // guarded conversion down from char32 to uint8 |
| 2002 | } else { |
| 2003 | /* |
| 2004 | UTF-16 |
| 2005 | */ |
| 2006 | binputu8 (0xFF, f); // an escape for multibyte encoding |
| 2007 | binputu8 ((uint32) length, f); |
| 2008 | for (integer i = 0; i < length; i ++) |
| 2009 | binpututf16 (s [i], f); |
| 2010 | } |
| 2011 | } |
| 2012 | } catch (MelderError) { |
| 2013 | Melder_throw (U"Text not written to a binary file."); |
| 2014 | } |
| 2015 | } |
| 2016 | |
| 2017 | void binputw16 (conststring32 s, FILE *f) { |
| 2018 | try { |
| 2019 | if (! s) { |
| 2020 | binputu16 (0, f); |
| 2021 | } else { |
| 2022 | integer length = Melder_length (s); |
| 2023 | if (length > UINT16_MAX - 1) { |
| 2024 | Melder_warning (U"Text of ", length, U" characters truncated to 65534 characters."); |
| 2025 | length = UINT16_MAX - 1; |
| 2026 | } |
| 2027 | if (Melder_isValidAscii (s)) { |
| 2028 | /* |
| 2029 | ASCII |
| 2030 | */ |
| 2031 | binputu16 ((uint16) length, f); // guarded conversion down |
| 2032 | for (int64 i = 0; i < length; i ++) |
| 2033 | binputu8 ((uint32) (uint8) s [i], f); // guarded conversion down from char32 to uint8 |
| 2034 | } else { |
| 2035 | /* |
| 2036 | UTF-16 |
| 2037 | */ |
| 2038 | binputu16 (0xFFFF, f); // an escape for multibyte encoding |
| 2039 | binputu16 ((uint16) length, f); // guarded conversion down |
| 2040 | for (int64 i = 0; i < length; i ++) |
| 2041 | binpututf16 (s [i], f); |
| 2042 | } |
| 2043 | } |
| 2044 | } catch (MelderError) { |
| 2045 | Melder_throw (U"Text not written to a binary file."); |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | void binputw32 (conststring32 s, FILE *f) { |
| 2050 | try { |
| 2051 | if (! s) { |
| 2052 | binputu32 (0, f); |
| 2053 | } else { |
| 2054 | integer length = Melder_length (s); // on 32-bit systems the length cannot exceed 2^30 char32s, i.e. `length` must be positive |
| 2055 | if (length > UINT32_MAX - 1) { |
| 2056 | Melder_warning (U"Text of ", length, U" characters truncated to 4,294,967,294 characters."); |
| 2057 | length = UINT32_MAX - 1; |
| 2058 | } |
| 2059 | if (Melder_isValidAscii (s)) { |
| 2060 | /* |
| 2061 | ASCII |
| 2062 | */ |
| 2063 | binputu32 ((uint32) length, f); // guarded conversion down on 64-bit systems, and valid even on 32-bit systems |
| 2064 | for (integer i = 0; i < length; i ++) |
| 2065 | binputu8 ((uint32) (uint8) s [i], f); // guarded conversion down from char32 to uint8 |
| 2066 | } else { |
| 2067 | /* |
| 2068 | UTF-16 |
| 2069 | */ |
| 2070 | binputu32 (0xFFFF'FFFF, f); // an escape for multibyte encoding |
| 2071 | binputu32 ((uint32) length, f); // guarded conversion down |
| 2072 | for (integer i = 0; i < length; i ++) |
| 2073 | binpututf16 (s [i], f); |
| 2074 | } |
| 2075 | } |
| 2076 | } catch (MelderError) { |
| 2077 | Melder_throw (U"Text not written to a binary file."); |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | /* End of file abcio.cpp */ |