| 1 | /* melder_files.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2008,2010-2026 Paul Boersma, 2013 Tom Naughton |
| 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 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 20 | #include <unistd.h> |
| 21 | #include <sys/stat.h> |
| 22 | #endif |
| 23 | #ifdef _WIN32 |
| 24 | #include <windows.h> |
| 25 | #endif |
| 26 | #if defined (macintosh) |
| 27 | #include "macport_on.h" |
| 28 | #include <CoreFoundation/CoreFoundation.h> |
| 29 | #include "macport_off.h" |
| 30 | #endif |
| 31 | #include <errno.h> |
| 32 | #include "melder.h"cxx . . $ `<file>/melder/melder.h`/ |
| 33 | #include "../kar/UnicodeData.h"cxx . . $ `<file>/kar/UnicodeData.h`/ |
| 34 | |
| 35 | //#include "flac_FLAC_stream_encoder.h" |
| 36 | extern "C" int FLAC__stream_encoder_finish (FLAC__StreamEncodercxx . . $ FLAC__StreamEncoder# *); |
| 37 | extern "C" void FLAC__stream_encoder_delete (FLAC__StreamEncodercxx . . $ FLAC__StreamEncoder# *); |
| 38 | |
| 39 | #if defined (macintosh) |
| 40 | #include <sys/stat.h> |
| 41 | #define UNIX |
| 42 | #include <unistd.h> |
| 43 | #endif |
| 44 | |
| 45 | static char32cxx . . $ char32# theShellDirectorycxx . . $ theShellDirectory. [kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1]; |
| 46 | void Melder_rememberShellDirectorycxx . . $ Melder_rememberShellDirectory(49f6e7a06ebc5aa8). () { |
| 47 | structMelderFoldercxx . . $ structMelderFolder# shellFolder { }; |
| 48 | Melder_getCurrentFoldercxx . . $ Melder_getCurrentFolder(63f38d3b6b86d1dc). (& shellFolder); |
| 49 | str32cpycxx . . $ str32cpy(6ab934322573b179). (theShellDirectorycxx . . $ theShellDirectory., MelderFolder_peekPathcxx . . $ MelderFolder_peekPath(fbe3b4f4d15c56ee). (& shellFolder)); |
| 50 | } |
| 51 | conststring32cxx . . $ conststring32# Melder_getShellDirectorycxx . . $ Melder_getShellDirectory(1912f6684645c851). () { |
| 52 | return & theShellDirectorycxx . . $ theShellDirectory. [0]; |
| 53 | } |
| 54 | |
| 55 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 56 | void Melder_8bitFileRepresentationToStr32_inplacecxx . . $ Melder_8bitFileRepresentationToStr32_inplace(9a630526fa4dddfe). (const char *path8, char32cxx . . $ char32# *path32) { |
| 57 | #if defined (macintosh) |
| 58 | CFStringRef cfpath = CFStringCreateWithCString (nullptr, path8, kCFStringEncodingUTF8); |
| 59 | if (! cfpath) { |
| 60 | /* |
| 61 | Probably something wrong, like a disk was disconnected in the meantime. |
| 62 | */ |
| 63 | try { |
| 64 | Melder_8to32_inplace_e (path8, path32, kMelder_textInputEncoding::UTF8); |
| 65 | } catch (MelderError) { |
| 66 | Melder_8to32_inplace_e (path8, path32, kMelder_textInputEncoding::MACROMAN); // cannot fail |
| 67 | Melder_throw (U"Unusual error finding or creating file <<", path32, U">> (MacRoman)."); |
| 68 | } |
| 69 | Melder_throw (U"Unusual error finding or creating file ", path32, U"."); |
| 70 | } |
| 71 | CFMutableStringRef cfpath2 = CFStringCreateMutableCopy (nullptr, 0, cfpath); |
| 72 | CFRelease (cfpath); |
| 73 | CFStringNormalize (cfpath2, kCFStringNormalizationFormC); // Praat requires composed characters |
| 74 | integer n_utf16 = CFStringGetLength (cfpath2); |
| 75 | integer n_utf32 = 0; |
| 76 | for (integer i = 0; i < n_utf16; i ++) { |
| 77 | char32 kar1 = CFStringGetCharacterAtIndex (cfpath2, i); |
| 78 | if (kar1 >= 0x00'D800 && kar1 <= 0x00'DBFF) { |
| 79 | char32 kar2 = (char32) CFStringGetCharacterAtIndex (cfpath2, ++ i); // convert up |
| 80 | if (kar2 >= 0x00'DC00 && kar2 <= 0x00'DFFF) { |
| 81 | kar1 = (((kar1 & 0x3FF) << 10) | (kar2 & 0x3FF)) + 0x01'0000; |
| 82 | } else { |
| 83 | kar1 = UNICODE_REPLACEMENT_CHARACTER; |
| 84 | } |
| 85 | } |
| 86 | path32 [n_utf32 ++] = kar1; |
| 87 | } |
| 88 | path32 [n_utf32] = U'\0'; |
| 89 | CFRelease (cfpath2); |
| 90 | #else |
| 91 | Melder_8to32_inplace_ecxx . . $ Melder_8to32_inplace_e(b9d82cb7d165907c). (path8, path32, kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UTF8cxx . . $ kMelder_textInputEncoding#UTF8.); |
| 92 | #endif |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | conststring32cxx . . $ conststring32# MelderFile_namecxx . . $ MelderFile_name(d9f043369fa9b55b). (MelderFilecxx . . $ MelderFile# file) { |
| 97 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 98 | char32cxx . . $ char32# *slash = str32rchrcxx . . $ str32rchr(328a9ede0fba853a). (file -> pathcxx . . $ structMelderFile#path., U'/'); |
| 99 | return slash ? slash + 1 : file -> pathcxx . . $ structMelderFile#path.; |
| 100 | #elif defined (_WIN32) |
| 101 | char32 *backslash = str32rchr (file -> path, U'\\'); |
| 102 | return backslash ? backslash + 1 : file -> path; |
| 103 | #else |
| 104 | return nullptr; |
| 105 | #endif |
| 106 | } |
| 107 | |
| 108 | conststring32cxx . . $ conststring32# MelderFolder_namecxx . . $ MelderFolder_name(fbe3b4f4d15c56ee). (MelderFoldercxx . . $ MelderFolder# folder) { |
| 109 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 110 | char32cxx . . $ char32# *slash = str32rchrcxx . . $ str32rchr(328a9ede0fba853a). (folder -> pathcxx . . $ structMelderFolder#path., U'/'); |
| 111 | return slash ? slash + 1 : folder -> pathcxx . . $ structMelderFolder#path.; |
| 112 | #elif defined (_WIN32) |
| 113 | char32 *backslash = str32rchr (folder -> path, U'\\'); |
| 114 | return backslash ? backslash + 1 : folder -> path; |
| 115 | #else |
| 116 | return nullptr; |
| 117 | #endif |
| 118 | } |
| 119 | |
| 120 | void Melder_pathToFoldercxx . . $ Melder_pathToFolder(f9a4643b225086b8). (conststring32cxx . . $ conststring32# path, MelderFoldercxx . . $ MelderFolder# folder) { |
| 121 | /* |
| 122 | FIXME: document that this function can be used to keep folder names relative and generic |
| 123 | */ |
| 124 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (folder -> pathcxx . . $ structMelderFolder#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, path); |
| 125 | } |
| 126 | |
| 127 | void Melder_pathToFilecxx . . $ Melder_pathToFile(82691f2ee21fbb13). (conststring32cxx . . $ conststring32# path, MelderFilecxx . . $ MelderFile# file) { |
| 128 | /* |
| 129 | This handles complete path names only. |
| 130 | |
| 131 | Used if we know for sure that we have a complete path name, |
| 132 | i.e. if the program determined the name (fileselector, printing, prefs). |
| 133 | |
| 134 | FIXME: document that this function can also be used to keep file names relative and generic |
| 135 | */ |
| 136 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (file -> pathcxx . . $ structMelderFile#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, path); |
| 137 | } |
| 138 | |
| 139 | void Melder_relativePathToFilecxx . . $ Melder_relativePathToFile(82691f2ee21fbb13). (conststring32cxx . . $ conststring32# path, MelderFilecxx . . $ MelderFile# file) { |
| 140 | /* |
| 141 | This handles complete and partial path names, |
| 142 | and translates slashes to native folder separators. |
| 143 | |
| 144 | Used if we do not know for sure that we have a complete path name, |
| 145 | i.e. if the user determined the name (scripting). |
| 146 | */ |
| 147 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 148 | /* |
| 149 | We assume that Unix complete path names start with a slash. |
| 150 | */ |
| 151 | if (path [0] == U'~' && path [1] == U'/') { |
| 152 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (file -> pathcxx . . $ structMelderFile#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, Melder_peek8to32_u (getenvcxx . . $ getenv(c63359b659779399). ("HOME")), & path [1]); |
| 153 | } else if (path [0] == U'/' || str32equcxx . . $ `melder/melder_str32.h:122:9`! (path, U"<stdout>") || str32strcxx . . $ str32str(2de17cd8f7c584a6). (path, U"://")) { |
| 154 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (file -> pathcxx . . $ structMelderFile#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, path); |
| 155 | } else { |
| 156 | structMelderFoldercxx . . $ structMelderFolder# currentFolder { }; |
| 157 | Melder_getCurrentFoldercxx . . $ Melder_getCurrentFolder(63f38d3b6b86d1dc). (& currentFolder); // BUG |
| 158 | if (currentFolder. pathcxx . . $ structMelderFolder#path. [0] == U'/' && currentFolder. pathcxx . . $ structMelderFolder#path. [1] == U'\0') |
| 159 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (file -> pathcxx . . $ structMelderFile#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, U"/", path); |
| 160 | else |
| 161 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (file -> pathcxx . . $ structMelderFile#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, currentFolder. pathcxx . . $ structMelderFolder#path., U"/", path); |
| 162 | } |
| 163 | #elif defined (_WIN32) |
| 164 | /* |
| 165 | We assume that Win32 complete path names look like: |
| 166 | C:\WINDOWS\CTRL32.DLL |
| 167 | LPT1: |
| 168 | \\host\path |
| 169 | */ |
| 170 | structMelderFolder currentFolder { }; |
| 171 | if (path [0] == U'~' && path [1] == U'/') { |
| 172 | Melder_getHomeDir (& currentFolder); |
| 173 | Melder_sprint (file -> path,kMelder_MAXPATH+1, currentFolder. path, & path [1]); |
| 174 | for (;;) { |
| 175 | char32 *slash = str32chr (file -> path, U'/'); |
| 176 | if (! slash) |
| 177 | break; |
| 178 | *slash = U'\\'; |
| 179 | } |
| 180 | return; |
| 181 | } |
| 182 | if (str32chr (path, U'/') && ! str32str (path, U"://")) { |
| 183 | char32 winPath [kMelder_MAXPATH+1]; |
| 184 | Melder_sprint (winPath,kMelder_MAXPATH+1, path); |
| 185 | for (;;) { |
| 186 | char32 *slash = str32chr (winPath, U'/'); |
| 187 | if (! slash) |
| 188 | break; |
| 189 | *slash = U'\\'; |
| 190 | } |
| 191 | Melder_relativePathToFile (winPath, file); |
| 192 | return; |
| 193 | } |
| 194 | if (str32chr (path, U':') || path [0] == U'\\' && path [1] == U'\\' || str32equ (path, U"<stdout>")) { |
| 195 | Melder_sprint (file -> path,kMelder_MAXPATH+1, path); |
| 196 | } else { |
| 197 | Melder_getCurrentFolder (& currentFolder); // BUG |
| 198 | Melder_sprint (file -> path,kMelder_MAXPATH+1, |
| 199 | currentFolder. path, |
| 200 | currentFolder. path [0] != U'\0' && currentFolder. path [Melder_length (currentFolder. path) - 1] == U'\\' ? U"" : U"\\", |
| 201 | path |
| 202 | ); |
| 203 | } |
| 204 | #endif |
| 205 | } |
| 206 | |
| 207 | void Melder_relativePathToFoldercxx . . $ Melder_relativePathToFolder(f9a4643b225086b8). (conststring32cxx . . $ conststring32# path, MelderFoldercxx . . $ MelderFolder# folder) { |
| 208 | /* |
| 209 | This handles complete, partial and home-relative path names, |
| 210 | and translates slashes to native folder separators. |
| 211 | |
| 212 | Used if we do not know for sure that we have a complete path name, |
| 213 | i.e. if the user determined the name (scripting). |
| 214 | */ |
| 215 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 216 | if (const bool pathIsHomeRelative = ( path [0] == U'~' && (path [1] == U'/' || path [1] == U'\0') )) { |
| 217 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (folder -> pathcxx . . $ structMelderFolder#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, Melder_peek8to32_u (getenvcxx . . $ getenv(c63359b659779399). ("HOME")), & path [1]); |
| 218 | } else if (const bool pathIsAbsolute = ( |
| 219 | path [0] == U'/' // path is on local disk |
| 220 | || |
| 221 | str32strcxx . . $ str32str(2de17cd8f7c584a6). (path, U"://") // path is on a service through a URL; is this needed and/or safe? |
| 222 | )) |
| 223 | { |
| 224 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (folder -> pathcxx . . $ structMelderFolder#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, path); |
| 225 | } else { |
| 226 | /* |
| 227 | Remaining case: the path must be current-folder-relative. |
| 228 | */ |
| 229 | structMelderFoldercxx . . $ structMelderFolder# currentFolder { }; |
| 230 | Melder_getCurrentFoldercxx . . $ Melder_getCurrentFolder(63f38d3b6b86d1dc). (& currentFolder); // BUG if in library? check in Python, for instance |
| 231 | const bool weAreInTheRootFolder = ( currentFolder. pathcxx . . $ structMelderFolder#path. [0] == U'/' && currentFolder. pathcxx . . $ structMelderFolder#path. [1] == U'\0' ); |
| 232 | conststring32cxx . . $ conststring32# folderSeparator = ( weAreInTheRootFolder ? nullptr : U"/" ); // prevent a double slash |
| 233 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (folder -> pathcxx . . $ structMelderFolder#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, currentFolder. pathcxx . . $ structMelderFolder#path., folderSeparator, path); |
| 234 | } |
| 235 | #elif defined (_WIN32) |
| 236 | /* |
| 237 | We assume that Win32 complete path names look like: |
| 238 | C:\WINDOWS\CTRL32.DLL |
| 239 | LPT1: |
| 240 | \\host\path |
| 241 | */ |
| 242 | structMelderFolder currentFolder { }; |
| 243 | if (path [0] == U'~' && path [1] == U'\0') { |
| 244 | Melder_getHomeDir (& currentFolder); |
| 245 | Melder_sprint (folder -> path,kMelder_MAXPATH+1, currentFolder. path); |
| 246 | return; |
| 247 | } |
| 248 | if (path [0] == U'~' && path [1] == U'/') { |
| 249 | Melder_getHomeDir (& currentFolder); |
| 250 | Melder_sprint (folder -> path,kMelder_MAXPATH+1, currentFolder. path, & path [1]); |
| 251 | for (;;) { |
| 252 | char32 *slash = str32chr (folder -> path, U'/'); |
| 253 | if (! slash) |
| 254 | break; |
| 255 | *slash = U'\\'; |
| 256 | } |
| 257 | return; |
| 258 | } |
| 259 | if (str32chr (path, U'/') && ! str32str (path, U"://")) { |
| 260 | char32 winPath [kMelder_MAXPATH+1]; |
| 261 | Melder_sprint (winPath,kMelder_MAXPATH+1, path); |
| 262 | for (;;) { |
| 263 | char32 *slash = str32chr (winPath, U'/'); |
| 264 | if (! slash) |
| 265 | break; |
| 266 | *slash = U'\\'; |
| 267 | } |
| 268 | Melder_relativePathToFolder (winPath, folder); |
| 269 | return; |
| 270 | } |
| 271 | if (str32chr (path, U':') || path [0] == U'\\' && path [1] == U'\\') { |
| 272 | Melder_sprint (folder -> path,kMelder_MAXPATH+1, path); |
| 273 | } else { |
| 274 | Melder_getCurrentFolder (& currentFolder); // BUG |
| 275 | Melder_sprint (folder -> path,kMelder_MAXPATH+1, |
| 276 | currentFolder. path, |
| 277 | currentFolder. path [0] != U'\0' && currentFolder. path [Melder_length (currentFolder. path) - 1] == U'\\' ? U"" : U"\\", |
| 278 | path |
| 279 | ); |
| 280 | } |
| 281 | #endif |
| 282 | } |
| 283 | |
| 284 | conststring32cxx . . $ conststring32# MelderFile_peekPathcxx . . $ MelderFile_peekPath(d9f043369fa9b55b). (MelderFilecxx . . $ MelderFile# file) { |
| 285 | return & file -> pathcxx . . $ structMelderFile#path. [0]; |
| 286 | } |
| 287 | conststring32cxx . . $ conststring32# MelderFolder_peekPathcxx . . $ MelderFolder_peekPath(fbe3b4f4d15c56ee). (MelderFoldercxx . . $ MelderFolder# folder) { |
| 288 | return & folder -> pathcxx . . $ structMelderFolder#path. [0]; |
| 289 | } |
| 290 | |
| 291 | conststring8cxx . . $ conststring8# MelderFile_peekPath8cxx . . $ MelderFile_peekPath8(3a3888509ce82e67). (MelderFilecxx . . $ MelderFile# file) { |
| 292 | return Melder_peek32to8_fileSystemcxx . . $ Melder_peek32to8_fileSystem(c6a971952cbb8e31). (MelderFile_peekPathcxx . . $ MelderFile_peekPath(d9f043369fa9b55b). (file)); |
| 293 | } |
| 294 | conststring8cxx . . $ conststring8# MelderFolder_peekPath8cxx . . $ MelderFolder_peekPath8(31391b8863732c6e). (MelderFoldercxx . . $ MelderFolder# folder) { |
| 295 | return Melder_peek32to8_fileSystemcxx . . $ Melder_peek32to8_fileSystem(c6a971952cbb8e31). (MelderFolder_peekPathcxx . . $ MelderFolder_peekPath(fbe3b4f4d15c56ee). (folder)); |
| 296 | } |
| 297 | |
| 298 | #ifdef _WIN32 |
| 299 | conststringW MelderFile_peekPathW (MelderFile file) { |
| 300 | return Melder_peek32toW_fileSystem (MelderFile_peekPath (file)); |
| 301 | } |
| 302 | conststringW MelderFolder_peekPathW (MelderFolder folder) { |
| 303 | return Melder_peek32toW_fileSystem (MelderFolder_peekPath (folder)); |
| 304 | } |
| 305 | #endif |
| 306 | |
| 307 | #ifdef macintosh |
| 308 | const void * MelderFile_peekPathCfstring (MelderFile file) { |
| 309 | return Melder_peek32toCfstring_fileSystem (MelderFile_peekPath (file)); |
| 310 | } |
| 311 | const void * MelderFolder_peekPathCfstring (MelderFolder folder) { |
| 312 | return Melder_peek32toCfstring_fileSystem (MelderFolder_peekPath (folder)); |
| 313 | } |
| 314 | #endif |
| 315 | |
| 316 | void MelderFile_copycxx . . $ MelderFile_copy(b298201f6d539a65). (constMelderFilecxx . . $ constMelderFile# file, MelderFilecxx . . $ MelderFile# copy) { |
| 317 | str32cpycxx . . $ str32cpy(6ab934322573b179). (copy -> pathcxx . . $ structMelderFile#path., file -> pathcxx . . $ structMelderFile#path.); |
| 318 | } |
| 319 | |
| 320 | void MelderFolder_copycxx . . $ MelderFolder_copy(29f3fed5dc56c318). (constMelderFoldercxx . . $ constMelderFolder# folder, MelderFoldercxx . . $ MelderFolder# copy) { |
| 321 | str32cpycxx . . $ str32cpy(6ab934322573b179). (copy -> pathcxx . . $ structMelderFolder#path., folder -> pathcxx . . $ structMelderFolder#path.); |
| 322 | } |
| 323 | |
| 324 | bool MelderFile_equalcxx . . $ MelderFile_equal(570c9229a9b16092). (MelderFilecxx . . $ MelderFile# file1, MelderFilecxx . . $ MelderFile# file2) { |
| 325 | return str32equcxx . . $ `melder/melder_str32.h:122:9`! (file1 -> pathcxx . . $ structMelderFile#path., file2 -> pathcxx . . $ structMelderFile#path.); |
| 326 | } |
| 327 | |
| 328 | bool MelderFolder_equalcxx . . $ MelderFolder_equal(f2bbb8160b3e7b22). (MelderFoldercxx . . $ MelderFolder# folder1, MelderFoldercxx . . $ MelderFolder# folder2) { |
| 329 | return str32equcxx . . $ `melder/melder_str32.h:122:9`! (folder1 -> pathcxx . . $ structMelderFolder#path., folder2 -> pathcxx . . $ structMelderFolder#path.); |
| 330 | } |
| 331 | |
| 332 | void MelderFile_setToNullcxx . . $ MelderFile_setToNull(1145552d769d7b5d). (MelderFilecxx . . $ MelderFile# file) { |
| 333 | file -> pathcxx . . $ structMelderFile#path. [0] = U'\0'; |
| 334 | } |
| 335 | |
| 336 | bool MelderFile_isNullcxx . . $ MelderFile_isNull(8f92cdf95b27d908). (MelderFilecxx . . $ MelderFile# file) { |
| 337 | return ! file || file -> pathcxx . . $ structMelderFile#path. [0] == U'\0'; |
| 338 | } |
| 339 | |
| 340 | void MelderFolder_setToNullcxx . . $ MelderFolder_setToNull(63f38d3b6b86d1dc). (MelderFoldercxx . . $ MelderFolder# folder) { |
| 341 | folder -> pathcxx . . $ structMelderFolder#path. [0] = U'\0'; |
| 342 | } |
| 343 | |
| 344 | bool MelderFolder_isNullcxx . . $ MelderFolder_isNull(f2f3cbeb84a71ab3). (MelderFoldercxx . . $ MelderFolder# folder) { |
| 345 | return folder -> pathcxx . . $ structMelderFolder#path. [0] == U'\0'; |
| 346 | } |
| 347 | |
| 348 | void MelderFolder_getFilecxx . . $ MelderFolder_getFile(fcdc1f9d1818a0a1). (MelderFoldercxx . . $ MelderFolder# parent, conststring32cxx . . $ conststring32# fileName, MelderFilecxx . . $ MelderFile# file) { |
| 349 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 350 | if (parent -> pathcxx . . $ structMelderFolder#path. [0] == U'/' && parent -> pathcxx . . $ structMelderFolder#path. [1] == U'\0') |
| 351 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (file -> pathcxx . . $ structMelderFile#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, U"/", fileName); |
| 352 | else |
| 353 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (file -> pathcxx . . $ structMelderFile#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, parent -> pathcxx . . $ structMelderFolder#path., U"/", fileName); |
| 354 | #elif defined (_WIN32) |
| 355 | if (str32rchr (file -> path, U'\\') - file -> path == Melder_length (file -> path) - 1) |
| 356 | Melder_sprint (file -> path,kMelder_MAXPATH+1, parent -> path, fileName); |
| 357 | else |
| 358 | Melder_sprint (file -> path,kMelder_MAXPATH+1, parent -> path, U"\\", fileName); |
| 359 | #endif |
| 360 | } |
| 361 | |
| 362 | void MelderFolder_relativePathToFilecxx . . $ MelderFolder_relativePathToFile(fcdc1f9d1818a0a1). (MelderFoldercxx . . $ MelderFolder# folder, conststring32cxx . . $ conststring32# path, MelderFilecxx . . $ MelderFile# file) { |
| 363 | autoMelderSetCurrentFoldercxx . . $ autoMelderSetCurrentFolder# saveFoldercxx . . $ autoMelderSetCurrentFolder#autoMelderSetCurrentFolder(63f38d3b6b86d1dc). (folder); |
| 364 | Melder_relativePathToFilecxx . . $ Melder_relativePathToFile(82691f2ee21fbb13). (path, file); |
| 365 | } |
| 366 | |
| 367 | #ifndef UNIXcxx . . $ `<built-in>:479:9`! |
| 368 | static void Melder_getDesktop (MelderFolder folder) { |
| 369 | folder -> path [0] = U'\0'; |
| 370 | } |
| 371 | #endif |
| 372 | |
| 373 | void MelderFile_getParentFoldercxx . . $ MelderFile_getParentFolder(fd81a6dabee805b). (MelderFilecxx . . $ MelderFile# file, MelderFoldercxx . . $ MelderFolder# parent) { |
| 374 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 375 | /* |
| 376 | The parent of /usr/hello.txt is /usr. |
| 377 | The parent of /hello.txt is /. |
| 378 | */ |
| 379 | str32cpycxx . . $ str32cpy(6ab934322573b179). (parent -> pathcxx . . $ structMelderFolder#path., file -> pathcxx . . $ structMelderFile#path.); |
| 380 | char32cxx . . $ char32# *slash = str32rchrcxx . . $ str32rchr(328a9ede0fba853a). (parent -> pathcxx . . $ structMelderFolder#path., U'/'); |
| 381 | if (slash) |
| 382 | *slash = U'\0'; |
| 383 | if (parent -> pathcxx . . $ structMelderFolder#path. [0] == U'\0') |
| 384 | str32cpycxx . . $ str32cpy(6ab934322573b179). (parent -> pathcxx . . $ structMelderFolder#path., U"/"); |
| 385 | #elif defined (_WIN32) |
| 386 | /* |
| 387 | The parent of C:\WINDOWS\CTRL.DLL is C:\WINDOWS. |
| 388 | The parent of E:\Praat.exe is E:\. |
| 389 | The parent of \\Swine\Apps\init.txt is \\Swine\Apps. |
| 390 | The parent of \\Swine\init.txt is \\Swine\. (BUG ?) |
| 391 | */ |
| 392 | str32cpy (parent -> path, file -> path); |
| 393 | char32 *colon = str32chr (parent -> path, U':'); |
| 394 | if (colon) { |
| 395 | char32 *backslash = str32rchr (parent -> path, U'\\'); |
| 396 | if (backslash) { // C:\WINDOWS\CTRL.DLL or C:\AUTOEXEC.BAT |
| 397 | if (backslash - colon == 1) { // C:\AUTOEXEC.BAT |
| 398 | * (backslash + 1) = U'\0'; // C:\ - !!! aargh this was a bug after converting this line to line comments |
| 399 | } else { // C:\WINDOWS\CTRL.DLL |
| 400 | *backslash = U'\0'; // C:\WINDOWS |
| 401 | } |
| 402 | } else { /* ??? */ |
| 403 | Melder_getDesktop (parent); // empty string |
| 404 | } |
| 405 | } else if (parent -> path [0] == U'\\' && parent -> path [1] == U'\\') { |
| 406 | char32 *backslash = str32rchr (parent -> path + 2, U'\\'); |
| 407 | if (backslash) { // \\Swine\Apps\init.txt or \\Swine\init.txt |
| 408 | char32 *leftBackslash = str32chr (parent -> path + 2, U'\\'); |
| 409 | if (backslash - leftBackslash == 0) { // \\Swine\init.txt |
| 410 | * (backslash + 1) = U'\0'; // \\Swine\ - !!! dear developer, don't delete this hyphen, lest the line ends in a backslash |
| 411 | } else { // \\Swine\Apps\init.txt |
| 412 | *backslash = U'\0'; // \\Swine\Apps |
| 413 | } |
| 414 | } else { // \\init.txt ??? |
| 415 | Melder_getDesktop (parent); // empty string |
| 416 | } |
| 417 | } else { // unknown path type |
| 418 | Melder_getDesktop (parent); // empty string |
| 419 | } |
| 420 | #endif |
| 421 | } |
| 422 | |
| 423 | void MelderFolder_getParentFoldercxx . . $ MelderFolder_getParentFolder(b7d9aeb63809d1fe). (MelderFoldercxx . . $ MelderFolder# subfolder, MelderFoldercxx . . $ MelderFolder# parentFolder) { |
| 424 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 425 | /* |
| 426 | The parent of /usr/local is /usr. |
| 427 | The parent of /usr is /. |
| 428 | The parent of / is "". |
| 429 | */ |
| 430 | str32cpycxx . . $ str32cpy(6ab934322573b179). (parentFolder -> pathcxx . . $ structMelderFolder#path., subfolder -> pathcxx . . $ structMelderFolder#path.); |
| 431 | char32cxx . . $ char32# *slash = str32rchrcxx . . $ str32rchr(328a9ede0fba853a). (parentFolder -> pathcxx . . $ structMelderFolder#path., U'/'); |
| 432 | if (slash) { |
| 433 | if (slash - parentFolder -> pathcxx . . $ structMelderFolder#path. == 0) { |
| 434 | if (slash [1] == U'\0') { // child is "/" |
| 435 | parentFolder -> pathcxx . . $ structMelderFolder#path. [0] = U'\0'; // parent is "" |
| 436 | } else { // child is "/usr" |
| 437 | slash [1] = '\0'; // parent is "/" |
| 438 | } |
| 439 | } else { // child is "/usr/local" |
| 440 | *slash = U'\0'; // parent is "/usr" |
| 441 | } |
| 442 | } else { |
| 443 | parentFolder -> pathcxx . . $ structMelderFolder#path. [0] = U'\0'; // some failure; desktop |
| 444 | } |
| 445 | #elif defined (_WIN32) |
| 446 | /* |
| 447 | The parent of C:\WINDOWS is C:\. |
| 448 | The parent of E:\ is the desktop. |
| 449 | The parent of \\Swine\ is the desktop. (BUG ?) |
| 450 | */ |
| 451 | str32cpy (parentFolder -> path, subfolder -> path); |
| 452 | char32 *colon = str32chr (parentFolder -> path, U':'); |
| 453 | if (colon) { |
| 454 | const integer length = Melder_length (parentFolder -> path); |
| 455 | char32 *backslash = str32rchr (parentFolder -> path, U'\\'); |
| 456 | if (backslash) { // C:\WINDOWS\FONTS or C:\WINDOWS or C:\ - (cannot add a line comment with a backslash) |
| 457 | if (backslash - parentFolder -> path == length - 1) { // C:\ - |
| 458 | Melder_getDesktop (parentFolder); // empty string |
| 459 | } else if (backslash - colon == 1) { // C:\WINDOWS |
| 460 | * (backslash + 1) = U'\0'; // C:\ - |
| 461 | } else { // C:\WINDOWS\FONTS |
| 462 | *backslash = U'\0'; // C:\WINDOWS |
| 463 | } |
| 464 | } else { // LPT1: ??? |
| 465 | Melder_getDesktop (parentFolder); // empty string |
| 466 | } |
| 467 | } else if (parentFolder -> path [0] == U'\\' && parentFolder -> path [1] == U'\\') { |
| 468 | const integer length = Melder_length (parentFolder -> path); |
| 469 | char32 *backslash = str32rchr (parentFolder -> path + 2, U'\\'); |
| 470 | if (backslash) { // \\Swine\Apps\Praats or \\Swine\Apps or \\Swine\ - |
| 471 | if (backslash - parentFolder -> path == length - 1) { // \\Swine\ - |
| 472 | Melder_getDesktop (parentFolder); // empty string |
| 473 | } else { // \\Swine\Apps\Praats or \\Swine\Apps |
| 474 | char32 *leftBackslash = str32chr (parentFolder -> path + 2, U'\\'); |
| 475 | if (backslash - leftBackslash == 0) { // \\Swine\Apps |
| 476 | * (backslash + 1) = U'\0'; // \\Swine\ - |
| 477 | } else { // \\Swine\Apps\Praats |
| 478 | *backslash = U'\0'; // \\Swine\Apps |
| 479 | } |
| 480 | } |
| 481 | } else { // \\Swine ??? |
| 482 | Melder_getDesktop (parentFolder); // empty string |
| 483 | } |
| 484 | } else { // unknown path type. |
| 485 | Melder_getDesktop (parentFolder); // empty string |
| 486 | } |
| 487 | #endif |
| 488 | } |
| 489 | |
| 490 | bool MelderFolder_isDesktopcxx . . $ MelderFolder_isDesktop(f2f3cbeb84a71ab3). (MelderFoldercxx . . $ MelderFolder# folder) { |
| 491 | return folder -> pathcxx . . $ structMelderFolder#path. [0] == U'\0'; |
| 492 | } |
| 493 | |
| 494 | void MelderFolder_getSubfoldercxx . . $ MelderFolder_getSubfolder(a6a48bbd99ea6094). (MelderFoldercxx . . $ MelderFolder# parentFolder, conststring32cxx . . $ conststring32# subfolderName, MelderFoldercxx . . $ MelderFolder# subfolder) { |
| 495 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 496 | if (parentFolder -> pathcxx . . $ structMelderFolder#path. [0] == U'/' && parentFolder -> pathcxx . . $ structMelderFolder#path. [1] == U'\0') { |
| 497 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (subfolder -> pathcxx . . $ structMelderFolder#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, U"/", subfolderName); |
| 498 | } else { |
| 499 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (subfolder -> pathcxx . . $ structMelderFolder#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, parentFolder -> pathcxx . . $ structMelderFolder#path., U"/", subfolderName); |
| 500 | } |
| 501 | #elif defined (_WIN32) |
| 502 | const integer length = Melder_length (parentFolder -> path); |
| 503 | char32 *backslash = str32rchr (parentFolder -> path, U'\\'); |
| 504 | if (backslash && backslash - parentFolder -> path == length - 1) { // C:\ or \\Swine\ - |
| 505 | Melder_sprint (subfolder -> path, kMelder_MAXPATH+1, parentFolder -> path, subfolderName); |
| 506 | } else { // C:\WINDOWS or \\Swine\Apps or even C: |
| 507 | Melder_sprint (subfolder -> path,kMelder_MAXPATH+1, parentFolder -> path, U"\\", subfolderName); |
| 508 | } |
| 509 | #endif |
| 510 | } |
| 511 | |
| 512 | void Melder_getHomeDircxx . . $ Melder_getHomeDir(63f38d3b6b86d1dc). (MelderFoldercxx . . $ MelderFolder# homeDir) { |
| 513 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 514 | char *home = getenvcxx . . $ getenv(c63359b659779399). ("HOME"); |
| 515 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (homeDir -> pathcxx . . $ structMelderFolder#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, home ? Melder_peek8to32_u (home) : U"/"); |
| 516 | #elif defined (_WIN32) |
| 517 | WCHAR driveW [kMelder_MAXPATH+1], pathW [kMelder_MAXPATH+1]; |
| 518 | DWORD n = GetEnvironmentVariableW (L"USERPROFILE", pathW, kMelder_MAXPATH+1); |
| 519 | if (n > kMelder_MAXPATH) |
| 520 | Melder_throw (U"Home folder name too long."); |
| 521 | if (n > 0) { |
| 522 | Melder_sprint (homeDir -> path,kMelder_MAXPATH+1, Melder_peekWto32 (pathW)); |
| 523 | return; |
| 524 | } |
| 525 | n = GetEnvironmentVariableW (L"HOMEDRIVE", driveW, kMelder_MAXPATH+1); |
| 526 | if (n > kMelder_MAXPATH) |
| 527 | Melder_throw (U"Home drive name too long."); |
| 528 | if (n > 0) { |
| 529 | GetEnvironmentVariable (L"HOMEPATH", pathW, kMelder_MAXPATH+1); |
| 530 | Melder_sprint (homeDir -> path,kMelder_MAXPATH+1, Melder_peekWto32 (driveW), Melder_peekWto32 (pathW)); |
| 531 | return; |
| 532 | } |
| 533 | MelderFolder_setToNull (homeDir); // Windows 95 and 98: alas |
| 534 | #endif |
| 535 | } |
| 536 | |
| 537 | static structMelderFoldercxx . . $ structMelderFolder# thePreferencesFoldercxx . . $ thePreferencesFolder.; |
| 538 | void Melder_setPreferencesFoldercxx . . $ Melder_setPreferencesFolder(ff00d524ab0355ec). (conststring32cxx . . $ conststring32# path) { |
| 539 | Melder_pathToFoldercxx . . $ Melder_pathToFolder(f9a4643b225086b8). (path, & thePreferencesFoldercxx . . $ thePreferencesFolder.); |
| 540 | } |
| 541 | MelderFoldercxx . . $ MelderFolder# Melder_preferencesFoldercxx . . $ Melder_preferencesFolder(3f0bf42ba8f9aa29).() { |
| 542 | return & thePreferencesFoldercxx . . $ thePreferencesFolder.; |
| 543 | } |
| 544 | MelderFoldercxx . . $ MelderFolder# Melder_preferencesFolder5cxx . . $ Melder_preferencesFolder5(3f0bf42ba8f9aa29).() { |
| 545 | static structMelderFoldercxx . . $ structMelderFolder# thePreferencesFolder5; |
| 546 | if (MelderFolder_isNullcxx . . $ MelderFolder_isNull(f2f3cbeb84a71ab3). (& thePreferencesFolder5)) { |
| 547 | structMelderFoldercxx . . $ structMelderFolder# homeFolder { }; |
| 548 | Melder_getHomeDircxx . . $ Melder_getHomeDir(63f38d3b6b86d1dc). (& homeFolder); |
| 549 | #if defined (macintosh) |
| 550 | Melder_sprint (thePreferencesFolder5. path,kMelder_MAXPATH+1, homeFolder. path, |
| 551 | U"/Library/Preferences/", Melder_upperCaseAppName(), U" Prefs"); |
| 552 | #elif defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 553 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (thePreferencesFolder5. pathcxx . . $ structMelderFolder#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, homeFolder. pathcxx . . $ structMelderFolder#path., |
| 554 | U"/.", Melder_lowerCaseAppNamecxx . . $ Melder_lowerCaseAppName(1912f6684645c851).(), U"-dir"); |
| 555 | #elif defined (_WIN32) |
| 556 | Melder_sprint (thePreferencesFolder5. path,kMelder_MAXPATH+1, homeFolder. path, |
| 557 | U"\\", Melder_upperCaseAppName()); |
| 558 | #endif |
| 559 | } |
| 560 | return & thePreferencesFolder5; |
| 561 | } |
| 562 | MelderFoldercxx . . $ MelderFolder# Melder_preferencesFolder7cxx . . $ Melder_preferencesFolder7(3f0bf42ba8f9aa29).() { |
| 563 | static structMelderFoldercxx . . $ structMelderFolder# thePreferencesFolder7; |
| 564 | if (MelderFolder_isNullcxx . . $ MelderFolder_isNull(f2f3cbeb84a71ab3). (& thePreferencesFolder7)) { |
| 565 | structMelderFoldercxx . . $ structMelderFolder# homeFolder { }; |
| 566 | Melder_getHomeDircxx . . $ Melder_getHomeDir(63f38d3b6b86d1dc). (& homeFolder); |
| 567 | #if defined (macintosh) |
| 568 | Melder_sprint (thePreferencesFolder7. path,kMelder_MAXPATH+1, homeFolder. path, |
| 569 | U"/Library/Application Support/", Melder_upperCaseAppName()); |
| 570 | #elif defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 571 | const conststring32cxx . . $ conststring32# xdgConfigHome = Melder_getenvcxx . . $ Melder_getenv(5553299aa8c4fee9). (U"XDG_CONFIG_HOME"); |
| 572 | if (xdgConfigHome) |
| 573 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (thePreferencesFolder7. pathcxx . . $ structMelderFolder#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, xdgConfigHome, |
| 574 | U"/", Melder_lowerCaseAppNamecxx . . $ Melder_lowerCaseAppName(1912f6684645c851).()); |
| 575 | else |
| 576 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (thePreferencesFolder7. pathcxx . . $ structMelderFolder#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, homeFolder. pathcxx . . $ structMelderFolder#path., |
| 577 | U"/.config/", Melder_lowerCaseAppNamecxx . . $ Melder_lowerCaseAppName(1912f6684645c851).()); |
| 578 | #elif defined (_WIN32) |
| 579 | Melder_sprint (thePreferencesFolder7. path,kMelder_MAXPATH+1, homeFolder. path, |
| 580 | U"\\AppData\\Roaming\\", Melder_upperCaseAppName()); |
| 581 | #endif |
| 582 | } |
| 583 | return & thePreferencesFolder7; |
| 584 | } |
| 585 | |
| 586 | void Melder_getTempDircxx . . $ Melder_getTempDir(63f38d3b6b86d1dc). (MelderFoldercxx . . $ MelderFolder# temporaryFolder) { |
| 587 | #if defined (macintosh) |
| 588 | Melder_sprint (temporaryFolder -> path,kMelder_MAXPATH+1, Melder_peek8to32_u (getenv ("TMPDIR"))); // or append /TemporaryItems |
| 589 | // confstr with _CS_DARWIN_USER_TEMP_DIR |
| 590 | #else |
| 591 | (void) temporaryFolder; |
| 592 | #endif |
| 593 | } |
| 594 | |
| 595 | FILE * Melder_fopencxx . . $ Melder_fopen(feb98de266255aea). (MelderFilecxx . . $ MelderFile# file, const char *type) { |
| 596 | if (MelderFile_isNullcxx . . $ MelderFile_isNull(8f92cdf95b27d908). (file)) |
| 597 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Cannot open null file."); |
| 598 | if (! Melder_isTracingGloballycxx . . $ Melder_isTracingGlobally.) |
| 599 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (str32equcxx . . $ `melder/melder_str32.h:122:9`! (Melder_double (1.5), U"1.5")); // check locale settings; because of the required file portability Praat cannot stand "1,5" |
| 600 | /* |
| 601 | On the Unix-like systems (including MacOS), the path has to be converted to 8-bit characters in UTF-8 encoding. |
| 602 | On MacOS, the characters also have to be decomposed. |
| 603 | On Windows, the characters have to be precomposed. |
| 604 | */ |
| 605 | bool isFolder = false; |
| 606 | char utf8path [kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1]; |
| 607 | Melder_32to8_fileSystem_inplacecxx . . $ Melder_32to8_fileSystem_inplace(d86258d5722a82b2). (file -> pathcxx . . $ structMelderFile#path., utf8path); |
| 608 | FILE *f; |
| 609 | file -> openForWritingcxx . . $ structMelderFile#openForWriting. = ( type [0] == 'w' || type [0] == 'a' || strchr (type, '+') ); |
| 610 | if (str32equcxx . . $ `melder/melder_str32.h:122:9`! (file -> pathcxx . . $ structMelderFile#path., U"<stdout>") && file -> openForWritingcxx . . $ structMelderFile#openForWriting.) { |
| 611 | f = Melder_stdoutcxx . . $ Melder_stdout.; |
| 612 | } else { |
| 613 | //TRACE |
| 614 | #if defined (_WIN32) && ! defined (__CYGWIN__) |
| 615 | f = _wfopen (MelderFile_peekPathW (file), Melder_peek32toW (Melder_peek8to32_u (type))); |
| 616 | #else |
| 617 | struct stat statbuf; |
| 618 | int status = stat ((char *) utf8path, & statbuf); |
| 619 | if (status == -1) { |
| 620 | if (type [0] == 'r') |
| 621 | f = nullptr; // and wait for errno to tell us why |
| 622 | else |
| 623 | f = fopen ((char *) utf8path, type); |
| 624 | } else if (S_ISDIRcxx . . $ `<external>/531885356569766532/stat.h:125:9`! (statbuf. st_mode)) { |
| 625 | isFolder = true; |
| 626 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"A folder is not a file!"); |
| 627 | f = nullptr; |
| 628 | } else { |
| 629 | f = fopen ((char *) utf8path, type); |
| 630 | } |
| 631 | #endif |
| 632 | } |
| 633 | if (! f) { |
| 634 | char32cxx . . $ char32# *path = file -> pathcxx . . $ structMelderFile#path.; |
| 635 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). ( |
| 636 | ( errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EPERMcxx . . $ `<external>/8987895939074593727/errno-base.h:5:9`! || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EACCEScxx . . $ `<external>/8987895939074593727/errno-base.h:17:9`! ? U"No permission to " : U"Cannot " ), |
| 637 | ( type [0] == 'r' ? U"open" : type [0] == 'a' ? U"append to" : U"create" ), |
| 638 | U" file ", file, U"." |
| 639 | ); |
| 640 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EIOcxx . . $ `<external>/8987895939074593727/errno-base.h:9:9`!) |
| 641 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (U"Not-so-useful hint: an error occurred while reading from the file system."); |
| 642 | else if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ELOOPcxx . . $ `<external>/17922322188510340118/errno.h:21:9`!) |
| 643 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (U"Hint: the file path contains a loop of symbolic links."); |
| 644 | else if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ENAMETOOLONGcxx . . $ `<external>/17922322188510340118/errno.h:8:9`!) |
| 645 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (U"Not-so-useful hint: the file path may be too long. This should not occur."); |
| 646 | else if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) |
| 647 | if (file -> openForWritingcxx . . $ structMelderFile#openForWriting.) |
| 648 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (U"Hint: one of the folders in this file path does not exist."); |
| 649 | else |
| 650 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (U"Hint: one of the folders or files in this file path does not exist."); |
| 651 | else if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ENOTDIRcxx . . $ `<external>/8987895939074593727/errno-base.h:24:9`!) |
| 652 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (U"Hint: a component of the file path is not a folder."); |
| 653 | else if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EOVERFLOWcxx . . $ `<external>/17922322188510340118/errno.h:59:9`!) |
| 654 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (U"Not-so-useful hint: the file size is too big."); |
| 655 | else if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != 0) |
| 656 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (U"Not-so-useful hint: unexpected error ", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, U"."); |
| 657 | if (isFolder) |
| 658 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (U"Hint: this is a folder, not a file."); |
| 659 | else if (path [0] == U'\0') |
| 660 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (U"Hint: empty file name."); |
| 661 | else if (path [0] == U' ' || path [0] == U'\t') |
| 662 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (U"Hint: file name starts with a space or tab."); |
| 663 | else if (path [Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (path) - 1] == U' ' || path [Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (path) - 1] == U'\t') |
| 664 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (U"Hint: file name ends in a space or tab."); |
| 665 | else if (str32chrcxx . . $ str32chr(328a9ede0fba853a). (path, U'\n')) |
| 666 | Melder_appendErrorcxx . . $ Melder_appendError(6298c93241566110). (U"Hint: file name contains a newline symbol."); |
| 667 | throw MelderErrorcxx . . $ MelderError# (); |
| 668 | return nullptr; |
| 669 | } |
| 670 | return f; |
| 671 | } |
| 672 | |
| 673 | void Melder_fclosecxx . . $ Melder_fclose(b523c54458c97cb0). (MelderFilecxx . . $ MelderFile# file, FILE *f) { |
| 674 | if (! f) |
| 675 | return; |
| 676 | if (f != Melder_stdoutcxx . . $ Melder_stdout. && fclose (f) == EOFcxx . . $ `<external>/232824719582829767/stdio.h:105:9`!) |
| 677 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Error closing file ", file, U"."); |
| 678 | } |
| 679 | |
| 680 | bool MelderFile_existscxx . . $ MelderFile_exists(8f92cdf95b27d908). (MelderFilecxx . . $ MelderFile# file) { |
| 681 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 682 | struct stat fileOrFolderStatus; |
| 683 | const bool exists = ( stat (MelderFile_peekPath8cxx . . $ MelderFile_peekPath8(3a3888509ce82e67). (file), & fileOrFolderStatus) == 0 ); |
| 684 | if (! exists) |
| 685 | return false; |
| 686 | return ! S_ISDIRcxx . . $ `<external>/531885356569766532/stat.h:125:9`! (fileOrFolderStatus. st_mode); |
| 687 | #else |
| 688 | DWORD fileOrFolderAttributes = GetFileAttributesW (MelderFile_peekPathW (file)); |
| 689 | if (fileOrFolderAttributes == INVALID_FILE_ATTRIBUTES) |
| 690 | return false; |
| 691 | return (fileOrFolderAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0; |
| 692 | #endif |
| 693 | } |
| 694 | |
| 695 | bool MelderFolder_existscxx . . $ MelderFolder_exists(f2f3cbeb84a71ab3). (MelderFoldercxx . . $ MelderFolder# folder) { |
| 696 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 697 | struct stat fileOrFolderStatus; |
| 698 | const bool exists = ( stat (MelderFolder_peekPath8cxx . . $ MelderFolder_peekPath8(31391b8863732c6e). (folder), & fileOrFolderStatus) == 0 ); |
| 699 | if (! exists) |
| 700 | return false; |
| 701 | return S_ISDIRcxx . . $ `<external>/531885356569766532/stat.h:125:9`! (fileOrFolderStatus. st_mode); |
| 702 | #else |
| 703 | DWORD fileOrFolderAttributes = GetFileAttributesW (MelderFolder_peekPathW (folder)); |
| 704 | if (fileOrFolderAttributes == INVALID_FILE_ATTRIBUTES) |
| 705 | return false; |
| 706 | return (fileOrFolderAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 707 | #endif |
| 708 | } |
| 709 | |
| 710 | bool MelderFile_readablecxx . . $ MelderFile_readable(8f92cdf95b27d908). (MelderFilecxx . . $ MelderFile# file) { |
| 711 | try { |
| 712 | autofilecxx . . $ autofile# f = Melder_fopencxx . . $ autofile#autofile(8d4262d45fc644ab). (file, "rb"); |
| 713 | f.closecxx . . $ autofile#close(1145552d769d7b5d). (file); |
| 714 | return true; |
| 715 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 716 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 717 | return false; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | bool Melder_tryToWriteFilecxx . . $ Melder_tryToWriteFile(8f92cdf95b27d908). (MelderFilecxx . . $ MelderFile# file) { |
| 722 | try { |
| 723 | autofilecxx . . $ autofile# f = Melder_fopencxx . . $ autofile#autofile(8d4262d45fc644ab). (file, "wb"); |
| 724 | f.closecxx . . $ autofile#close(1145552d769d7b5d). (file); |
| 725 | return true; |
| 726 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 727 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 728 | return false; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | bool Melder_tryToAppendFilecxx . . $ Melder_tryToAppendFile(8f92cdf95b27d908). (MelderFilecxx . . $ MelderFile# file) { |
| 733 | try { |
| 734 | autofilecxx . . $ autofile# f = Melder_fopencxx . . $ autofile#autofile(8d4262d45fc644ab). (file, "ab"); |
| 735 | f.closecxx . . $ autofile#close(1145552d769d7b5d). (file); |
| 736 | return true; |
| 737 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 738 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 739 | return false; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | integercxx . . $ integer# MelderFile_lengthcxx . . $ MelderFile_length(4c086c6b3713bb2e). (MelderFilecxx . . $ MelderFile# file) { |
| 744 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 745 | struct stat statistics; |
| 746 | if (stat (MelderFile_peekPath8cxx . . $ MelderFile_peekPath8(3a3888509ce82e67). (file), & statistics) != 0) |
| 747 | return -1; |
| 748 | return statistics. st_size; |
| 749 | #else |
| 750 | try { |
| 751 | autofile f = Melder_fopen (file, "r"); |
| 752 | fseek (f, 0, SEEK_END); |
| 753 | integer length = ftell (f); |
| 754 | f.close (file); |
| 755 | return length; |
| 756 | } catch (MelderError) { |
| 757 | Melder_clearError (); |
| 758 | return -1; |
| 759 | } |
| 760 | #endif |
| 761 | } |
| 762 | |
| 763 | void MelderFile_deletecxx . . $ MelderFile_delete(1145552d769d7b5d). (MelderFilecxx . . $ MelderFile# file) { |
| 764 | if (! file) |
| 765 | return; |
| 766 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 767 | remove (MelderFile_peekPath8cxx . . $ MelderFile_peekPath8(3a3888509ce82e67). (file)); |
| 768 | #elif defined (_WIN32) |
| 769 | DeleteFile (MelderFile_peekPathW (file)); |
| 770 | #endif |
| 771 | } |
| 772 | |
| 773 | void MelderFile_moveAndOrRenamecxx . . $ MelderFile_moveAndOrRename(551642421266be1d). (MelderFilecxx . . $ MelderFile# fromFile, MelderFilecxx . . $ MelderFile# toFile) { |
| 774 | if (! fromFile || ! toFile) |
| 775 | return; |
| 776 | try { |
| 777 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 778 | static char fromPath8 [1 + kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!], toPath8 [1 + kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!]; |
| 779 | Melder_32to8_fileSystem_inplacecxx . . $ Melder_32to8_fileSystem_inplace(d86258d5722a82b2). (MelderFile_peekPathcxx . . $ MelderFile_peekPath(d9f043369fa9b55b). (fromFile), fromPath8); // not MelderFile_peekPath8, because of static |
| 780 | Melder_32to8_fileSystem_inplacecxx . . $ Melder_32to8_fileSystem_inplace(d86258d5722a82b2). (MelderFile_peekPathcxx . . $ MelderFile_peekPath(d9f043369fa9b55b). (toFile), toPath8); |
| 781 | const int result = rename (fromPath8, toPath8); |
| 782 | #elif defined (_WIN32) |
| 783 | autostringW fromPathW = Melder_32toW_fileSystem (MelderFile_peekPath (fromFile)); |
| 784 | autostringW toPathW = Melder_32toW_fileSystem (MelderFile_peekPath (toFile)); |
| 785 | const int result = _wrename (fromPathW.get(), toPathW.get()); |
| 786 | #endif |
| 787 | if (result == 0) |
| 788 | return; // success |
| 789 | switch (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) { |
| 790 | case EACCEScxx . . $ `<external>/8987895939074593727/errno-base.h:17:9`!: |
| 791 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"A folder in one of the paths doesn’t allow reading and/or writing access."); |
| 792 | case EBUSYcxx . . $ `<external>/8987895939074593727/errno-base.h:20:9`!: |
| 793 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"A folder in one of the paths is busy."); |
| 794 | case EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!: |
| 795 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Invalid path (recursive? dots?)."); |
| 796 | case EIOcxx . . $ `<external>/8987895939074593727/errno-base.h:9:9`!: |
| 797 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Physical I/O error."); |
| 798 | case EISDIRcxx . . $ `<external>/8987895939074593727/errno-base.h:25:9`!: |
| 799 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Cannot move an/or rename a file to a folder name."); |
| 800 | #if ! defined (UNIXcxx . . $ `<built-in>:479:9`!) && ! defined (_WIN32) |
| 801 | case EMLOOP: |
| 802 | Melder_throw (U"Loop in symbolic links."); |
| 803 | #endif |
| 804 | case EMLINKcxx . . $ `<external>/8987895939074593727/errno-base.h:35:9`!: |
| 805 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Too many links."); |
| 806 | case ENAMETOOLONGcxx . . $ `<external>/17922322188510340118/errno.h:8:9`!: |
| 807 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Name too long."); |
| 808 | case ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!: |
| 809 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"A folder or file in the path does not exist."); |
| 810 | case ENOSPCcxx . . $ `<external>/8987895939074593727/errno-base.h:32:9`!: |
| 811 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Folder cannot be extended."); |
| 812 | case ENOTDIRcxx . . $ `<external>/8987895939074593727/errno-base.h:24:9`!: |
| 813 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"A part of the path is not a folder (hint: look at the slahes)."); |
| 814 | case EPERMcxx . . $ `<external>/8987895939074593727/errno-base.h:5:9`!: |
| 815 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"No permission to read and/or write in one of the paths."); |
| 816 | case EROFScxx . . $ `<external>/8987895939074593727/errno-base.h:34:9`!: |
| 817 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Cannot do links between file systems."); |
| 818 | #if ! defined (UNIXcxx . . $ `<built-in>:479:9`!) && ! defined (_WIN32) |
| 819 | case ETXTBUSY: |
| 820 | Melder_throw (U"Busy pure procedure file."); |
| 821 | #endif |
| 822 | default: |
| 823 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Unknown error ", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, U"."); |
| 824 | } |
| 825 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 826 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Could not rename ", fromFile, U" to ", toFile, U"."); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | char32cxx . . $ char32# * Melder_peekExpandBackslashescxx . . $ Melder_peekExpandBackslashes(ff4c117d906607ef). (conststring32cxx . . $ conststring32# message) { |
| 831 | static char32cxx . . $ char32# names [11] [kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1]; |
| 832 | static int index = 0; |
| 833 | if (++ index == 11) |
| 834 | index = 0; |
| 835 | char32cxx . . $ char32# *to = & names [index] [0]; |
| 836 | for (const char32cxx . . $ char32# *from = & message [0]; *from != '\0'; from ++, to ++) { |
| 837 | *to = *from; |
| 838 | if (*from == U'\\') { |
| 839 | * ++ to = U'b'; |
| 840 | * ++ to = U's'; |
| 841 | } |
| 842 | } |
| 843 | *to = U'\0'; |
| 844 | return & names [index] [0]; |
| 845 | } |
| 846 | |
| 847 | conststring32cxx . . $ conststring32# MelderFile_messageNamecxx . . $ MelderFile_messageName(d9f043369fa9b55b). (MelderFilecxx . . $ MelderFile# file) { |
| 848 | return Melder_catcxx . . $ Melder_cat(7fbf27f4d367f442). (U"“", file -> pathcxx . . $ structMelderFile#path., U"”"); // BUG: is cat allowed here? |
| 849 | } |
| 850 | |
| 851 | conststring32cxx . . $ conststring32# MelderFolder_messageNamecxx . . $ MelderFolder_messageName(fbe3b4f4d15c56ee). (MelderFoldercxx . . $ MelderFolder# folder) { |
| 852 | return Melder_catcxx . . $ Melder_cat(7fbf27f4d367f442). (U"“", folder -> pathcxx . . $ structMelderFolder#path., U"”"); // BUG: is cat allowed here? |
| 853 | } |
| 854 | |
| 855 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 856 | /* |
| 857 | From macOS 10.15 Catalina on, getcwd() has failed if a part of the path |
| 858 | is inaccessible, such as when you open a script that is attached to an email message. |
| 859 | */ |
| 860 | static structMelderFoldercxx . . $ structMelderFolder# theDefaultDircxx . . $ theDefaultDir.; |
| 861 | #endif |
| 862 | |
| 863 | void Melder_getCurrentFoldercxx . . $ Melder_getCurrentFolder(63f38d3b6b86d1dc). (MelderFoldercxx . . $ MelderFolder# folder) { |
| 864 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 865 | char path [kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1]; |
| 866 | char *pathResult = getcwd (path, kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1); |
| 867 | if (pathResult) |
| 868 | Melder_8bitFileRepresentationToStr32_inplacecxx . . $ Melder_8bitFileRepresentationToStr32_inplace(9a630526fa4dddfe). (path, folder -> pathcxx . . $ structMelderFolder#path.); |
| 869 | else if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EPERMcxx . . $ `<external>/8987895939074593727/errno-base.h:5:9`!) |
| 870 | str32cpycxx . . $ str32cpy(6ab934322573b179). (folder -> pathcxx . . $ structMelderFolder#path., theDefaultDircxx . . $ theDefaultDir.. pathcxx . . $ structMelderFolder#path.); |
| 871 | else |
| 872 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (Melder_peek8to32_u (strerror (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!))); |
| 873 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (Melder_length (folder -> path) <= kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!); |
| 874 | #elif defined (_WIN32) |
| 875 | static WCHAR folderPathW [kMelder_MAXPATH+1]; |
| 876 | GetCurrentDirectory (kMelder_MAXPATH+1, folderPathW); |
| 877 | Melder_sprint (folder -> path,kMelder_MAXPATH+1, Melder_peekWto32 (folderPathW)); |
| 878 | #endif |
| 879 | } |
| 880 | |
| 881 | void Melder_setCurrentFoldercxx . . $ Melder_setCurrentFolder(63f38d3b6b86d1dc). (MelderFoldercxx . . $ MelderFolder# folder) { |
| 882 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 883 | chdir (MelderFolder_peekPath8cxx . . $ MelderFolder_peekPath8(31391b8863732c6e). (folder)); |
| 884 | str32cpycxx . . $ str32cpy(6ab934322573b179). (theDefaultDircxx . . $ theDefaultDir.. pathcxx . . $ structMelderFolder#path., folder -> pathcxx . . $ structMelderFolder#path.); |
| 885 | #elif defined (_WIN32) |
| 886 | SetCurrentDirectory (MelderFolder_peekPathW (folder)); |
| 887 | #endif |
| 888 | } |
| 889 | |
| 890 | void MelderFile_setDefaultDircxx . . $ MelderFile_setDefaultDir(1145552d769d7b5d). (MelderFilecxx . . $ MelderFile# file) { |
| 891 | structMelderFoldercxx . . $ structMelderFolder# folder { }; |
| 892 | MelderFile_getParentFoldercxx . . $ MelderFile_getParentFolder(fd81a6dabee805b). (file, & folder); |
| 893 | Melder_setCurrentFoldercxx . . $ Melder_setCurrentFolder(63f38d3b6b86d1dc). (& folder); |
| 894 | } |
| 895 | |
| 896 | MelderFoldercxx . . $ MelderFolder# Melder_peekWorkingDirectorycxx . . $ Melder_peekWorkingDirectory(3f0bf42ba8f9aa29). () { |
| 897 | static structMelderFoldercxx . . $ structMelderFolder# s_workingDirectory; |
| 898 | Melder_getCurrentFoldercxx . . $ Melder_getCurrentFolder(63f38d3b6b86d1dc). (& s_workingDirectory); |
| 899 | return & s_workingDirectory; |
| 900 | } |
| 901 | |
| 902 | void Melder_createDirectorycxx . . $ Melder_createDirectory(e602797c0bbbcba1). (MelderFoldercxx . . $ MelderFolder# parent, conststring32cxx . . $ conststring32# folderName, int mode) { |
| 903 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 904 | structMelderFilecxx . . $ structMelderFile# file { }; |
| 905 | if (folderName [0] == U'/') { |
| 906 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (file. pathcxx . . $ structMelderFile#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, folderName); // absolute path |
| 907 | } else if (parent -> pathcxx . . $ structMelderFolder#path. [0] == U'/' && parent -> pathcxx . . $ structMelderFolder#path. [1] == U'\0') { |
| 908 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (file. pathcxx . . $ structMelderFile#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, U"/", folderName); // relative path in root folder |
| 909 | } else { |
| 910 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (file. pathcxx . . $ structMelderFile#path.,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, parent -> pathcxx . . $ structMelderFolder#path., U"/", folderName); // relative path |
| 911 | } |
| 912 | if (mkdir (MelderFile_peekPath8cxx . . $ MelderFile_peekPath8(3a3888509ce82e67). (& file), mode) == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EEXISTcxx . . $ `<external>/8987895939074593727/errno-base.h:21:9`!) // ignore if folder already exists |
| 913 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Cannot create directory ", & file, U"."); |
| 914 | #elif defined (_WIN32) |
| 915 | structMelderFile file { }; |
| 916 | SECURITY_ATTRIBUTES sa; |
| 917 | (void) mode; |
| 918 | sa. nLength = sizeof (SECURITY_ATTRIBUTES); |
| 919 | sa. lpSecurityDescriptor = nullptr; |
| 920 | sa. bInheritHandle = false; |
| 921 | if (str32chr (folderName, U':') || folderName [0] == U'/' && folderName [1] == U'/') { |
| 922 | Melder_sprint (file. path,kMelder_MAXPATH+1, folderName); // absolute path |
| 923 | } else { |
| 924 | Melder_sprint (file. path,kMelder_MAXPATH+1, parent -> path, U"/", folderName); // relative path |
| 925 | } |
| 926 | if (! CreateDirectoryW (MelderFile_peekPathW (& file), & sa) && GetLastError () != ERROR_ALREADY_EXISTS) // ignore if folder already exists |
| 927 | Melder_throw (U"Cannot create directory ", & file, U"."); |
| 928 | #else |
| 929 | //#error Unsupported operating system. |
| 930 | #endif |
| 931 | } |
| 932 | |
| 933 | void MelderFolder_createcxx . . $ MelderFolder_create(63f38d3b6b86d1dc). (MelderFoldercxx . . $ MelderFolder# folder) { |
| 934 | #if defined (UNIXcxx . . $ `<built-in>:479:9`!) |
| 935 | const int status = mkdir (MelderFolder_peekPath8cxx . . $ MelderFolder_peekPath8(31391b8863732c6e). (folder), S_IRWXUcxx . . $ `<external>/531885356569766532/stat.h:172:9`! | S_IRWXGcxx . . $ `<external>/531885356569766532/stat.h:184:9`! | S_IROTHcxx . . $ `<external>/531885356569766532/stat.h:186:9`! | S_IXOTHcxx . . $ `<external>/531885356569766532/stat.h:188:9`!); |
| 936 | if (status == 0) |
| 937 | return; // successfully created a new folder |
| 938 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EEXISTcxx . . $ `<external>/8987895939074593727/errno-base.h:21:9`!) |
| 939 | return; // it is no failure if the folder already existed |
| 940 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Cannot create folder ", folder, U"."); |
| 941 | #elif defined (_WIN32) |
| 942 | SECURITY_ATTRIBUTES securityAttributes; |
| 943 | securityAttributes. nLength = sizeof (SECURITY_ATTRIBUTES); |
| 944 | securityAttributes. lpSecurityDescriptor = nullptr; |
| 945 | securityAttributes. bInheritHandle = false; |
| 946 | const int status = CreateDirectoryW (MelderFolder_peekPathW (folder), & securityAttributes); |
| 947 | if (status != 0) |
| 948 | return; // successfully created a new folder |
| 949 | if (GetLastError () == ERROR_ALREADY_EXISTS) |
| 950 | return; // it is no failure if the folder already existed |
| 951 | Melder_throw (U"Cannot create folder ", folder, U"."); |
| 952 | #else |
| 953 | #error Unsupported operating system. |
| 954 | #endif |
| 955 | } |
| 956 | void Melder_createFoldercxx . . $ Melder_createFolder(ff00d524ab0355ec). (conststring32cxx . . $ conststring32# path) { |
| 957 | structMelderFoldercxx . . $ structMelderFolder# folder { }; |
| 958 | Melder_relativePathToFoldercxx . . $ Melder_relativePathToFolder(f9a4643b225086b8). (path, & folder); |
| 959 | MelderFolder_createcxx . . $ MelderFolder_create(63f38d3b6b86d1dc). (& folder); |
| 960 | } |
| 961 | |
| 962 | static size_t fread_multicxx . . $ fread_multi(7a88a749847cd312). (char *buffer, size_t numberOfBytes, FILE *f) { |
| 963 | off_t offset = 0; |
| 964 | size_t numberOfBytesRead = 0; |
| 965 | const size_t chunkSize = 1'000'000'000; |
| 966 | while (numberOfBytes > chunkSize) { |
| 967 | size_t numberOfBytesReadInChunk = fread (buffer + offset, sizeof (char), chunkSize, f); |
| 968 | numberOfBytesRead += numberOfBytesReadInChunk; |
| 969 | if (numberOfBytesReadInChunk < chunkSize) |
| 970 | return numberOfBytesRead; |
| 971 | numberOfBytes -= chunkSize; |
| 972 | offset += chunkSize; |
| 973 | } |
| 974 | size_t numberOfBytesReadInLastChunk = fread (buffer + offset, sizeof (char), numberOfBytes, f); |
| 975 | numberOfBytesRead += numberOfBytesReadInLastChunk; |
| 976 | return numberOfBytesRead; |
| 977 | } |
| 978 | |
| 979 | autostring32cxx . . $ autostring32# MelderFile_readTextcxx . . $ MelderFile_readText(2312d39ae70aea26). (MelderFilecxx . . $ MelderFile# file, autostring8cxx . . $ autostring8# *string8) { |
| 980 | try { |
| 981 | int type = 0; // 8-bit |
| 982 | autostring32cxx . . $ autostring32# textcxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).; |
| 983 | autofilecxx . . $ autofile# f = Melder_fopencxx . . $ autofile#autofile(8d4262d45fc644ab). (file, "rb"); |
| 984 | if (fseeko (f, 0, SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!) < 0) |
| 985 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Cannot count the bytes in the file."); |
| 986 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (sizeof (off_t) >= 8); |
| 987 | int64cxx . . $ int64# length = ftello (f); |
| 988 | rewind (f); |
| 989 | if (length >= 2) { |
| 990 | int firstByte = fgetc (f), secondByte = fgetc (f); |
| 991 | if (firstByte == 0xFE && secondByte == 0xFF) { |
| 992 | type = 1; // big-endian 16-bit |
| 993 | } else if (firstByte == 0xFF && secondByte == 0xFE) { |
| 994 | type = 2; // little-endian 16-bit |
| 995 | } else if (firstByte == 0xEF && secondByte == 0xBB && length >= 3) { |
| 996 | int thirdByte = fgetc (f); |
| 997 | if (thirdByte == 0xBF) |
| 998 | type = -1; // UTF-8 with BOM |
| 999 | } |
| 1000 | } |
| 1001 | if (type <= 0) { |
| 1002 | if (type == -1) { |
| 1003 | length -= 3; |
| 1004 | fseeko (f, 3, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 1005 | } else { |
| 1006 | rewind (f); // length and type already set correctly |
| 1007 | } |
| 1008 | autostring8cxx . . $ autostring8# text8bitcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (length); |
| 1009 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (text8bit); |
| 1010 | size_t numberOfBytesRead = fread_multicxx . . $ fread_multi(7a88a749847cd312). (text8bit.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), (size_t) length, f); |
| 1011 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! ((int64) numberOfBytesRead == length, |
| 1012 | U"The file contains ", length, U" bytes", |
| 1013 | type == -1 ? U" after the byte-order mark" : U"", |
| 1014 | U", but we could read only ", numberOfBytesRead, U" of them." |
| 1015 | ); |
| 1016 | text8bit [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).length] = '\0'; |
| 1017 | /* |
| 1018 | Count and repair null bytes. |
| 1019 | */ |
| 1020 | if (length > 0) { |
| 1021 | int64cxx . . $ int64# numberOfNullBytes = 0; |
| 1022 | char *q = & text8bit [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; |
| 1023 | for (integercxx . . $ integer# i = 0; i < length; i ++) |
| 1024 | if (text8bit [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i] != '\0') |
| 1025 | * (q ++) = text8bit [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i]; |
| 1026 | else |
| 1027 | numberOfNullBytes ++; |
| 1028 | *q = '\0'; |
| 1029 | if (numberOfNullBytes > 0) |
| 1030 | Melder_warningcxx . . $ Melder_warning(6298c93241566110). (U"Ignored ", numberOfNullBytes, U" null bytes in text file ", file, U"."); |
| 1031 | } |
| 1032 | if (string8) { |
| 1033 | *string8 =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). text8bit.movecxx . . $ _autostring#move(bd43c759570f178f).(); |
| 1034 | (void) Melder_killReturns_inplacecxx . . $ Melder_killReturns_inplace(d2cd1729a8aa36c). (string8->getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 1035 | return autostring32cxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).(); // OK |
| 1036 | } else { |
| 1037 | text =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_8to32_ecxx . . $ Melder_8to32_e(aa783ea934c735af). (text8bit.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), kMelder_textInputEncodingcxx . . $ kMelder_textInputEncoding#::UNDEFINEDcxx . . $ kMelder_textInputEncoding#UNDEFINED.); |
| 1038 | } |
| 1039 | } else { |
| 1040 | length = length / 2 - 1; // Byte Order Mark subtracted. Length = number of UTF-16 codes |
| 1041 | text =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). autostring32cxx . . $ autostring32# (length + 1); |
| 1042 | if (type == 1) { |
| 1043 | for (int64cxx . . $ int64# i = 0; i < length; i ++) { |
| 1044 | const char16cxx . . $ char16# kar1 = bingetu16cxx . . $ bingetu16(38a7f6633dfbdb53). (f); |
| 1045 | if (kar1 < 0xD800) { |
| 1046 | text [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i] = (const char32cxx . . $ char32#) kar1; // convert up without sign extension |
| 1047 | } else if (kar1 < 0xDC00) { |
| 1048 | length --; |
| 1049 | const char16cxx . . $ char16# kar2 = bingetu16cxx . . $ bingetu16(38a7f6633dfbdb53). (f); |
| 1050 | if (kar2 >= 0xDC00 && kar2 <= 0xDFFF) { |
| 1051 | text [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i] = (const char32cxx . . $ char32#) (0x010000 + |
| 1052 | (const char32cxx . . $ char32#) (((const char32cxx . . $ char32#) kar1 & 0x0003FF) << 10) + |
| 1053 | (const char32cxx . . $ char32#) ((const char32cxx . . $ char32#) kar2 & 0x0003FF)); |
| 1054 | } else { |
| 1055 | text [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i] = UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!; |
| 1056 | } |
| 1057 | } else if (kar1 < 0xE000) { |
| 1058 | text [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i] = UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!; |
| 1059 | } else { |
| 1060 | text [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i] = (const char32cxx . . $ char32#) kar1; // convert up without sign extension |
| 1061 | } |
| 1062 | } |
| 1063 | } else { |
| 1064 | for (int64cxx . . $ int64# i = 0; i < length; i ++) { |
| 1065 | const char16cxx . . $ char16# kar1 = bingetu16LEcxx . . $ bingetu16LE(38a7f6633dfbdb53). (f); |
| 1066 | if (kar1 < 0xD800) { |
| 1067 | text [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i] = (const char32cxx . . $ char32#) kar1; // convert up without sign extension |
| 1068 | } else if (kar1 < 0xDC00) { |
| 1069 | length --; |
| 1070 | const char16cxx . . $ char16# kar2 = bingetu16LEcxx . . $ bingetu16LE(38a7f6633dfbdb53). (f); |
| 1071 | if (kar2 >= 0xDC00 && kar2 <= 0xDFFF) { |
| 1072 | text [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i] = (const char32cxx . . $ char32#) (0x01'0000 + |
| 1073 | (const char32cxx . . $ char32#) (((const char32cxx . . $ char32#) kar1 & 0x00'03FF) << 10) + |
| 1074 | (const char32cxx . . $ char32#) ((const char32cxx . . $ char32#) kar2 & 0x00'03FF)); |
| 1075 | } else { |
| 1076 | text [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i] = UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!; |
| 1077 | } |
| 1078 | } else if (kar1 < 0xE000) { |
| 1079 | text [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i] = UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!; |
| 1080 | } else if (kar1 <= 0xFFFF) { |
| 1081 | text [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i] = (const char32cxx . . $ char32#) kar1; // convert up without sign extension |
| 1082 | } else { |
| 1083 | Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"MelderFile_readText: unsigned short greater than 0xFFFF: should not occur."); |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | text [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).length] = U'\0'; |
| 1088 | (void) Melder_killReturns_inplacecxx . . $ Melder_killReturns_inplace(9c47b7d7d51fe2c1). (text.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 1089 | } |
| 1090 | f.closecxx . . $ autofile#close(1145552d769d7b5d). (file); |
| 1091 | return textcxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 1092 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 1093 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Error reading file ", file, U"."); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | void Melder_fwrite32to8cxx . . $ Melder_fwrite32to8(43ad66ee48fe33fd). (conststring32cxx . . $ conststring32# string, FILE *f) { |
| 1098 | /* |
| 1099 | * Precondition: |
| 1100 | * the string's encoding is UTF-32. |
| 1101 | * Failure: |
| 1102 | * if the precondition does not hold, we don't crash, |
| 1103 | * but the characters that are written may be incorrect. |
| 1104 | */ |
| 1105 | for (const char32cxx . . $ char32#* p = string; *p != U'\0'; p ++) { |
| 1106 | const char32cxx . . $ char32# kar = *p; |
| 1107 | if (kar <= 0x00'007F) { |
| 1108 | #ifdef _WIN32 |
| 1109 | if (kar == U'\n') |
| 1110 | fputc (13, f); |
| 1111 | #endif |
| 1112 | fputc ((int) kar, f); // because fputc wants an int instead of an uint8 (guarded conversion) |
| 1113 | } else if (kar <= 0x00'07FF) { |
| 1114 | fputc (0xC0 | (kar >> 6), f); |
| 1115 | fputc (0x80 | (kar & 0x00'003F), f); |
| 1116 | } else if (kar <= 0x00FFFF) { |
| 1117 | fputc (0xE0 | (kar >> 12), f); |
| 1118 | fputc (0x80 | ((kar >> 6) & 0x00'003F), f); |
| 1119 | fputc (0x80 | (kar & 0x00'003F), f); |
| 1120 | } else { |
| 1121 | fputc (0xF0 | (kar >> 18), f); |
| 1122 | fputc (0x80 | ((kar >> 12) & 0x00'003F), f); |
| 1123 | fputc (0x80 | ((kar >> 6) & 0x00'003F), f); |
| 1124 | fputc (0x80 | (kar & 0x00'003F), f); |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | void MelderFile_writeText_ecxx . . $ MelderFile_writeText_e(4f26204211a3b413). (MelderFilecxx . . $ MelderFile# file, conststring32cxx . . $ conststring32# text, kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding# outputEncoding) { |
| 1130 | if (! text) |
| 1131 | text = U""; |
| 1132 | autofilecxx . . $ autofile# f = Melder_fopencxx . . $ autofile#autofile(8d4262d45fc644ab). (file, "wb"); |
| 1133 | if (outputEncoding == kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::UTF8cxx . . $ kMelder_textOutputEncoding#UTF8.) { |
| 1134 | Melder_fwrite32to8cxx . . $ Melder_fwrite32to8(43ad66ee48fe33fd). (text, f); |
| 1135 | } else if ((outputEncoding == kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::ASCII_THEN_UTF16cxx . . $ kMelder_textOutputEncoding#ASCII_THEN_UTF16. && Melder_isValidAsciicxx . . $ Melder_isValidAscii(2df281243f91fe91). (text)) || |
| 1136 | (outputEncoding == kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::ISO_LATIN1_THEN_UTF16cxx . . $ kMelder_textOutputEncoding#ISO_LATIN1_THEN_UTF16. && Melder_isEncodablecxx . . $ Melder_isEncodable(8f54aa54c3f23562). (text, kMelder_textOutputEncoding_ISO_LATIN1cxx . . $ kMelder_textOutputEncoding_ISO_LATIN1.))) |
| 1137 | { |
| 1138 | #ifdef _WIN32 |
| 1139 | #define flockfile(f) (void) 0 |
| 1140 | #define funlockfile(f) (void) 0 |
| 1141 | #define putc_unlocked putc |
| 1142 | #endif |
| 1143 | flockfile (f); |
| 1144 | const integercxx . . $ integer# n = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (text); |
| 1145 | for (integercxx . . $ integer# i = 0; i < n; i ++) { |
| 1146 | char32cxx . . $ char32# kar = text [i]; |
| 1147 | #ifdef _WIN32 |
| 1148 | if (kar == U'\n') |
| 1149 | putc_unlocked (13, f); |
| 1150 | #endif |
| 1151 | putc_unlocked (kar, f); |
| 1152 | } |
| 1153 | funlockfile (f); |
| 1154 | } else { |
| 1155 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). (0xFEFF, f); // Byte Order Mark |
| 1156 | const integercxx . . $ integer# n = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (text); |
| 1157 | for (integercxx . . $ integer# i = 0; i < n; i ++) { |
| 1158 | char32cxx . . $ char32# kar = text [i]; |
| 1159 | #ifdef _WIN32 |
| 1160 | if (kar == U'\n') |
| 1161 | binputu16 (13, f); |
| 1162 | #endif |
| 1163 | if (kar <= 0x00'FFFF) { |
| 1164 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). ((char16cxx . . $ char16#) kar, f); // guarded conversion down |
| 1165 | } else if (kar <= 0x10'FFFF) { |
| 1166 | kar -= 0x010000; |
| 1167 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). (0xD800 | (uint16cxx . . $ uint16#) (kar >> 10), f); |
| 1168 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). (0xDC00 | (uint16cxx . . $ uint16#) ((char16cxx . . $ char16#) kar & 0x3ff), f); |
| 1169 | } else { |
| 1170 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). (UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!, f); |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | f.closecxx . . $ autofile#close(1145552d769d7b5d). (file); |
| 1175 | } |
| 1176 | void MelderFile_writeText_icxx . . $ MelderFile_writeText_i(4f26204211a3b413). (MelderFilecxx . . $ MelderFile# file, conststring32cxx . . $ conststring32# text, kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding# outputEncoding) { |
| 1177 | try { |
| 1178 | MelderFile_writeText_ecxx . . $ MelderFile_writeText_e(4f26204211a3b413). (file, text, outputEncoding); |
| 1179 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 1180 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | void MelderFile_appendText_ecxx . . $ MelderFile_appendText_e(8fa576db0692df65). (MelderFilecxx . . $ MelderFile# file, conststring32cxx . . $ conststring32# text) { |
| 1185 | if (! text) text = U""; |
| 1186 | autofilecxx . . $ autofile# f1cxx . . $ autofile#autofile(49f6e7a06ebc5aa8).; |
| 1187 | try { |
| 1188 | f1 =cxx . . $ autofile#`operator=`(8d4262d45fc644ab). Melder_fopencxx . . $ Melder_fopen(feb98de266255aea). (file, "rb"); |
| 1189 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 1190 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); // it's OK if the file didn't exist yet... |
| 1191 | MelderFile_writeText_ecxx . . $ MelderFile_writeText_e(4f26204211a3b413). (file, text, Melder_getOutputEncodingcxx . . $ Melder_getOutputEncoding(581d0d572cba43d7). ()); // because then we just "write" |
| 1192 | return; |
| 1193 | } |
| 1194 | /* |
| 1195 | The file already exists and is open. Determine its type. |
| 1196 | */ |
| 1197 | int firstByte = fgetc (f1), secondByte = fgetc (f1); |
| 1198 | f1.closecxx . . $ autofile#close(1145552d769d7b5d). (file); |
| 1199 | int type = 0; |
| 1200 | if (firstByte == 0xfe && secondByte == 0xff) { |
| 1201 | type = 1; // big-endian 16-bit |
| 1202 | } else if (firstByte == 0xff && secondByte == 0xfe) { |
| 1203 | type = 2; // little-endian 16-bit |
| 1204 | } |
| 1205 | if (type == 0) { |
| 1206 | kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding# outputEncoding = Melder_getOutputEncodingcxx . . $ Melder_getOutputEncoding(581d0d572cba43d7). (); |
| 1207 | if (outputEncoding == kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::UTF8cxx . . $ kMelder_textOutputEncoding#UTF8.) { // TODO: read as file's encoding |
| 1208 | autofilecxx . . $ autofile# f2 = Melder_fopencxx . . $ autofile#autofile(8d4262d45fc644ab). (file, "ab"); |
| 1209 | Melder_fwrite32to8cxx . . $ Melder_fwrite32to8(43ad66ee48fe33fd). (text, f2); |
| 1210 | f2.closecxx . . $ autofile#close(1145552d769d7b5d). (file); |
| 1211 | } else if ((outputEncoding == kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::ASCII_THEN_UTF16cxx . . $ kMelder_textOutputEncoding#ASCII_THEN_UTF16. && Melder_isEncodablecxx . . $ Melder_isEncodable(8f54aa54c3f23562). (text, kMelder_textOutputEncoding_ASCIIcxx . . $ kMelder_textOutputEncoding_ASCII.)) |
| 1212 | || (outputEncoding == kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::ISO_LATIN1_THEN_UTF16cxx . . $ kMelder_textOutputEncoding#ISO_LATIN1_THEN_UTF16. && Melder_isEncodablecxx . . $ Melder_isEncodable(8f54aa54c3f23562). (text, kMelder_textOutputEncoding_ISO_LATIN1cxx . . $ kMelder_textOutputEncoding_ISO_LATIN1.))) |
| 1213 | { |
| 1214 | /* |
| 1215 | Append ASCII or ISOLatin1 text to ASCII or ISOLatin1 file. |
| 1216 | */ |
| 1217 | autofilecxx . . $ autofile# f2 = Melder_fopencxx . . $ autofile#autofile(8d4262d45fc644ab). (file, "ab"); |
| 1218 | const integercxx . . $ integer# n = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (text); |
| 1219 | for (integercxx . . $ integer# i = 0; i < n; i ++) { |
| 1220 | const char32cxx . . $ char32# kar = text [i]; |
| 1221 | #ifdef _WIN32 |
| 1222 | if (kar == U'\n') |
| 1223 | fputc (13, f2); |
| 1224 | #endif |
| 1225 | fputc ((char8cxx . . $ char8#) kar, f2); |
| 1226 | } |
| 1227 | f2.closecxx . . $ autofile#close(1145552d769d7b5d). (file); |
| 1228 | } else { |
| 1229 | /* |
| 1230 | Convert to wide character file. |
| 1231 | */ |
| 1232 | autostring32cxx . . $ autostring32# oldText = MelderFile_readTextcxx . . $ MelderFile_readText(2312d39ae70aea26). (file); |
| 1233 | autofilecxx . . $ autofile# f2 = Melder_fopencxx . . $ autofile#autofile(8d4262d45fc644ab). (file, "wb"); |
| 1234 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). (0xfeff, f2); |
| 1235 | integercxx . . $ integer# n = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (oldText.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 1236 | for (int64cxx . . $ int64# i = 0; i < n; i ++) { |
| 1237 | char32cxx . . $ char32# kar = oldText [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).i]; |
| 1238 | #ifdef _WIN32 |
| 1239 | if (kar == U'\n') |
| 1240 | binputu16 (13, f2); |
| 1241 | #endif |
| 1242 | if (kar <= 0x00'FFFF) { |
| 1243 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). ((uint16cxx . . $ uint16#) kar, f2); // guarded conversion down |
| 1244 | } else if (kar <= 0x10'FFFF) { |
| 1245 | kar -= 0x01'0000; |
| 1246 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). ((uint16cxx . . $ uint16#) (0x00'D800 | (kar >> 10)), f2); |
| 1247 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). ((uint16cxx . . $ uint16#) (0x00'DC00 | (kar & 0x00'03ff)), f2); |
| 1248 | } else { |
| 1249 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). (UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!, f2); |
| 1250 | } |
| 1251 | } |
| 1252 | n = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (text); |
| 1253 | for (integercxx . . $ integer# i = 0; i < n; i ++) { |
| 1254 | char32cxx . . $ char32# kar = text [i]; |
| 1255 | #ifdef _WIN32 |
| 1256 | if (kar == U'\n') |
| 1257 | binputu16 (13, f2); |
| 1258 | #endif |
| 1259 | if (kar <= 0x00'FFFF) { |
| 1260 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). ((uint16cxx . . $ uint16#) kar, f2); // guarded conversion down |
| 1261 | } else if (kar <= 0x10'FFFF) { |
| 1262 | kar -= 0x01'0000; |
| 1263 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). ((uint16cxx . . $ uint16#) (0x00'D800 | (kar >> 10)), f2); |
| 1264 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). ((uint16cxx . . $ uint16#) (0x00'DC00 | (kar & 0x00'03ff)), f2); |
| 1265 | } else { |
| 1266 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). (UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!, f2); |
| 1267 | } |
| 1268 | } |
| 1269 | f2.closecxx . . $ autofile#close(1145552d769d7b5d). (file); |
| 1270 | } |
| 1271 | } else { |
| 1272 | autofilecxx . . $ autofile# f2 = Melder_fopencxx . . $ autofile#autofile(8d4262d45fc644ab). (file, "ab"); |
| 1273 | const integercxx . . $ integer# n = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (text); |
| 1274 | for (integercxx . . $ integer# i = 0; i < n; i ++) { |
| 1275 | if (type == 1) { |
| 1276 | char32cxx . . $ char32# kar = text [i]; |
| 1277 | #ifdef _WIN32 |
| 1278 | if (kar == U'\n') |
| 1279 | binputu16 (13, f2); |
| 1280 | #endif |
| 1281 | if (kar <= 0x00'FFFF) { |
| 1282 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). ((uint16cxx . . $ uint16#) kar, f2); // guarded conversion down |
| 1283 | } else if (kar <= 0x10'FFFF) { |
| 1284 | kar -= 0x01'0000; |
| 1285 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). ((uint16cxx . . $ uint16#) (0x00'D800 | (kar >> 10)), f2); |
| 1286 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). ((uint16cxx . . $ uint16#) (0x00'DC00 | (kar & 0x00'03ff)), f2); |
| 1287 | } else { |
| 1288 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). (UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!, f2); |
| 1289 | } |
| 1290 | } else { |
| 1291 | char32cxx . . $ char32# kar = text [i]; |
| 1292 | #ifdef _WIN32 |
| 1293 | if (kar == U'\n') |
| 1294 | binputu16LE (13, f2); |
| 1295 | #endif |
| 1296 | if (kar <= 0x00'FFFF) { |
| 1297 | binputu16LEcxx . . $ binputu16LE(d4e8792de0db7e67). ((uint16cxx . . $ uint16#) kar, f2); // guarded conversion down |
| 1298 | } else if (kar <= 0x10FFFF) { |
| 1299 | kar -= 0x01'0000; |
| 1300 | binputu16LEcxx . . $ binputu16LE(d4e8792de0db7e67). ((uint16cxx . . $ uint16#) (0x00'D800 | (kar >> 10)), f2); |
| 1301 | binputu16LEcxx . . $ binputu16LE(d4e8792de0db7e67). ((uint16cxx . . $ uint16#) (0x00'DC00 | (kar & 0x00'03ff)), f2); |
| 1302 | } else { |
| 1303 | binputu16LEcxx . . $ binputu16LE(d4e8792de0db7e67). (UNICODE_REPLACEMENT_CHARACTERcxx . . $ `kar/UnicodeData.h:33798:9`!, f2); |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | f2.closecxx . . $ autofile#close(1145552d769d7b5d). (file); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | /* End of file melder_files.cpp */ |