| 1 |
/* MelderFile.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2018,2020,2022 Paul Boersma |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "melder.h" |
| 20 |
#include "../kar/UnicodeData.h" |
| 21 |
|
| 22 |
extern "C" int FLAC__stream_encoder_finish (FLAC__StreamEncoder *); |
| 23 |
extern "C" void FLAC__stream_encoder_delete (FLAC__StreamEncoder *); |
| 24 |
|
| 25 |
/* |
| 26 |
* Functions for wrapping the file pointers. |
| 27 |
*/ |
| 28 |
|
| 29 |
MelderFile MelderFile_open (MelderFile me) { |
| 30 |
my filePointer = Melder_fopen (me, "rb"); |
| 31 |
my openForReading = true; |
| 32 |
return me; |
| 33 |
} |
| 34 |
|
| 35 |
char * MelderFile_readLine8 (MelderFile me) { |
| 36 |
if (! my filePointer) |
| 37 |
return nullptr; |
| 38 |
if (feof (my filePointer)) |
| 39 |
return nullptr; |
| 40 |
static char *buffer; |
| 41 |
static integer capacity; |
| 42 |
if (! buffer) |
| 43 |
buffer = Melder_malloc (char, capacity = 100); |
| 44 |
integer i = 0; |
| 45 |
for (; true; i ++) { |
| 46 |
if (i >= capacity) |
| 47 |
buffer = (char *) Melder_realloc (buffer, capacity *= 2); |
| 48 |
int c = fgetc (my filePointer); |
| 49 |
if (feof (my filePointer)) |
| 50 |
break; |
| 51 |
if (c == '\n') { |
| 52 |
c = fgetc (my filePointer); |
| 53 |
if (feof (my filePointer)) |
| 54 |
break; // ignore last empty line (Unix) |
| 55 |
ungetc (c, my filePointer); |
| 56 |
break; // Unix line separator |
| 57 |
} |
| 58 |
if (c == '\r') { |
| 59 |
c = fgetc (my filePointer); |
| 60 |
if (feof (my filePointer)) |
| 61 |
break; // ignore last empty line (Macintosh) |
| 62 |
if (c == '\n') { |
| 63 |
c = fgetc (my filePointer); |
| 64 |
if (feof (my filePointer)) |
| 65 |
break; // ignore last empty line (Windows) |
| 66 |
ungetc (c, my filePointer); |
| 67 |
break; // Windows line separator |
| 68 |
} |
| 69 |
ungetc (c, my filePointer); |
| 70 |
break; // Macintosh line separator |
| 71 |
} |
| 72 |
buffer [i] = c; |
| 73 |
} |
| 74 |
buffer [i] = '\0'; |
| 75 |
return buffer; |
| 76 |
} |
| 77 |
|
| 78 |
MelderFile MelderFile_create (MelderFile me) { |
| 79 |
my filePointer = Melder_fopen (me, "wb"); |
| 80 |
my openForWriting = true; // a bit superfluous (will have been set by Melder_fopen) |
| 81 |
return me; |
| 82 |
} |
| 83 |
|
| 84 |
void MelderFile__writeOneStringPart (MelderFile file, conststring32 string) { |
| 85 |
if (! file -> filePointer) |
| 86 |
return; |
| 87 |
if (! string) |
| 88 |
return; |
| 89 |
const int64 length = Melder_length (string); |
| 90 |
FILE *f = file -> filePointer; |
| 91 |
if (file -> outputEncoding == kMelder_textOutputEncoding_ASCII || file -> outputEncoding == kMelder_textOutputEncoding_ISO_LATIN1) { |
| 92 |
for (int64 i = 0; i < length; i ++) { |
| 93 |
char kar = (char) (char8) string [i]; // truncate |
| 94 |
if (kar == '\n' && file -> requiresCRLF) |
| 95 |
putc (13, f); |
| 96 |
putc (kar, f); |
| 97 |
} |
| 98 |
} else if (file -> outputEncoding == (unsigned long) kMelder_textOutputEncoding::UTF8) { |
| 99 |
for (int64 i = 0; i < length; i ++) { |
| 100 |
char32 kar = string [i]; |
| 101 |
if (kar <= 0x00'007F) { |
| 102 |
if (kar == U'\n' && file -> requiresCRLF) |
| 103 |
putc (13, f); |
| 104 |
putc ((int) kar, f); // guarded conversion down |
| 105 |
} else if (kar <= 0x00'07FF) { |
| 106 |
putc (0xC0 | (kar >> 6), f); |
| 107 |
putc (0x80 | (kar & 0x00'003F), f); |
| 108 |
} else if (kar <= 0x00'FFFF) { |
| 109 |
putc (0xE0 | (kar >> 12), f); |
| 110 |
putc (0x80 | ((kar >> 6) & 0x00'003F), f); |
| 111 |
putc (0x80 | (kar & 0x00'003F), f); |
| 112 |
} else { |
| 113 |
putc (0xF0 | (kar >> 18), f); |
| 114 |
putc (0x80 | ((kar >> 12) & 0x00'003F), f); |
| 115 |
putc (0x80 | ((kar >> 6) & 0x00'003F), f); |
| 116 |
putc (0x80 | (kar & 0x00'003F), f); |
| 117 |
} |
| 118 |
} |
| 119 |
} else { |
| 120 |
for (int64 i = 0; i < length; i ++) { |
| 121 |
char32 kar = string [i]; |
| 122 |
if (kar == U'\n' && file -> requiresCRLF) |
| 123 |
binputu16 (13, f); |
| 124 |
if (kar <= 0x00'FFFF) { |
| 125 |
binputu16 ((char16) kar, f); |
| 126 |
} else if (kar <= 0x10'FFFF) { |
| 127 |
kar -= 0x01'0000; |
| 128 |
binputu16 (0xD800 | (char16) (kar >> 10), f); |
| 129 |
binputu16 (0xDC00 | (char16) ((char16) kar & 0x03ff), f); |
| 130 |
} else { |
| 131 |
binputu16 (UNICODE_REPLACEMENT_CHARACTER, f); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void MelderFile_writeCharacter (MelderFile file, char32 kar) { |
| 138 |
FILE *f = file -> filePointer; |
| 139 |
if (! f) |
| 140 |
return; |
| 141 |
if (file -> outputEncoding == kMelder_textOutputEncoding_ASCII || file -> outputEncoding == kMelder_textOutputEncoding_ISO_LATIN1) { |
| 142 |
if (kar == U'\n' && file -> requiresCRLF) |
| 143 |
putc (13, f); |
| 144 |
putc ((int) kar, f); |
| 145 |
} else if (file -> outputEncoding == (unsigned long) kMelder_textOutputEncoding::UTF8) { |
| 146 |
if (kar <= 0x00'007F) { |
| 147 |
if (kar == U'\n' && file -> requiresCRLF) |
| 148 |
putc (13, f); |
| 149 |
putc ((int) kar, f); // guarded conversion down |
| 150 |
} else if (kar <= 0x00'07FF) { |
| 151 |
putc (0xC0 | (kar >> 6), f); |
| 152 |
putc (0x80 | (kar & 0x00'003F), f); |
| 153 |
} else if (kar <= 0x00'FFFF) { |
| 154 |
putc (0xE0 | (kar >> 12), f); |
| 155 |
putc (0x80 | ((kar >> 6) & 0x00'003F), f); |
| 156 |
putc (0x80 | (kar & 0x00'003F), f); |
| 157 |
} else { |
| 158 |
putc (0xF0 | (kar >> 18), f); |
| 159 |
putc (0x80 | ((kar >> 12) & 0x00'003F), f); |
| 160 |
putc (0x80 | ((kar >> 6) & 0x00'003F), f); |
| 161 |
putc (0x80 | (kar & 0x00'003F), f); |
| 162 |
} |
| 163 |
} else { |
| 164 |
if (kar == U'\n' && file -> requiresCRLF) |
| 165 |
binputu16 (13, f); |
| 166 |
if (kar <= 0x00'FFFF) { |
| 167 |
binputu16 ((uint16) kar, f); |
| 168 |
} else if (kar <= 0x10'FFFF) { |
| 169 |
kar -= 0x01'0000; |
| 170 |
binputu16 (0xD800 | (uint16) (kar >> 10), f); |
| 171 |
binputu16 (0xDC00 | (uint16) ((uint16) kar & 0x00'03ff), f); |
| 172 |
} else { |
| 173 |
binputu16 (UNICODE_REPLACEMENT_CHARACTER, f); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
void MelderFile_seek (MelderFile me, integer position, int direction) { |
| 179 |
if (! my filePointer) |
| 180 |
return; |
| 181 |
if (fseek (my filePointer, position, direction)) { |
| 182 |
fclose (my filePointer); |
| 183 |
my filePointer = nullptr; |
| 184 |
Melder_throw (U"Cannot seek in file ", me, U"."); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
integer MelderFile_tell (MelderFile me) { |
| 189 |
if (! my filePointer) |
| 190 |
return 0; |
| 191 |
integer result = ftell (my filePointer); |
| 192 |
if (result == -1) { |
| 193 |
fclose (my filePointer); |
| 194 |
my filePointer = nullptr; |
| 195 |
Melder_throw (U"Cannot tell in file ", me, U"."); |
| 196 |
} |
| 197 |
return result; |
| 198 |
} |
| 199 |
|
| 200 |
void MelderFile_rewind (MelderFile me) { |
| 201 |
if (! my filePointer) |
| 202 |
return; |
| 203 |
rewind (my filePointer); |
| 204 |
} |
| 205 |
|
| 206 |
static void _MelderFile_close (MelderFile me, bool mayThrow) { |
| 207 |
if (my outputEncoding == kMelder_textOutputEncoding_FLAC) { |
| 208 |
if (my flacEncoder) { |
| 209 |
FLAC__stream_encoder_finish (my flacEncoder); // This already calls fclose! BUG: we cannot get any error messages out. |
| 210 |
FLAC__stream_encoder_delete (my flacEncoder); |
| 211 |
} |
| 212 |
} else if (my filePointer) { |
| 213 |
if (mayThrow) |
| 214 |
Melder_fclose (me, my filePointer); |
| 215 |
else |
| 216 |
fclose (my filePointer); |
| 217 |
} |
| 218 |
/* Set everything to zero, except paths (they stay around for error messages and the like). */ |
| 219 |
my filePointer = nullptr; |
| 220 |
my openForWriting = my openForReading = false; |
| 221 |
my indent = 0; |
| 222 |
my flacEncoder = nullptr; |
| 223 |
} |
| 224 |
void MelderFile_close (MelderFile me) { |
| 225 |
_MelderFile_close (me, true); |
| 226 |
} |
| 227 |
void MelderFile_close_nothrow (MelderFile me) { |
| 228 |
_MelderFile_close (me, false); |
| 229 |
} |
| 230 |
|
| 231 |
/* End of file MelderFile.cpp */ |