| 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 (UNIX) |
| 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" |
| 33 | #include "../kar/UnicodeData.h" |
| 34 | |
| 35 | //#include "flac_FLAC_stream_encoder.h" |
| 36 | extern "C" int FLAC__stream_encoder_finish (FLAC__StreamEncoder *); |
| 37 | extern "C" void FLAC__stream_encoder_delete (FLAC__StreamEncoder *); |
| 38 | |
| 39 | #if defined (macintosh) |
| 40 | #include <sys/stat.h> |
| 41 | #define UNIX |
| 42 | #include <unistd.h> |
| 43 | #endif |
| 44 | |
| 45 | static char32 theShellDirectory [kMelder_MAXPATH+1]; |
| 46 | void Melder_rememberShellDirectory () { |
| 47 | structMelderFolder shellFolder { }; |
| 48 | Melder_getCurrentFolder (& shellFolder); |
| 49 | str32cpy (theShellDirectory, MelderFolder_peekPath (& shellFolder)); |
| 50 | } |
| 51 | conststring32 Melder_getShellDirectory () { |
| 52 | return & theShellDirectory [0]; |
| 53 | } |
| 54 | |
| 55 | #if defined (UNIX) |
| 56 | void Melder_8bitFileRepresentationToStr32_inplace (const char *path8, 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_e (path8, path32, kMelder_textInputEncoding::UTF8); |
| 92 | #endif |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | conststring32 MelderFile_name (MelderFile file) { |
| 97 | #if defined (UNIX) |
| 98 | char32 *slash = str32rchr (file -> path, U'/'); |
| 99 | return slash ? slash + 1 : file -> 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 | conststring32 MelderFolder_name (MelderFolder folder) { |
| 109 | #if defined (UNIX) |
| 110 | char32 *slash = str32rchr (folder -> path, U'/'); |
| 111 | return slash ? slash + 1 : folder -> 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_pathToFolder (conststring32 path, MelderFolder folder) { |
| 121 | /* |
| 122 | FIXME: document that this function can be used to keep folder names relative and generic |
| 123 | */ |
| 124 | Melder_sprint (folder -> path,kMelder_MAXPATH+1, path); |
| 125 | } |
| 126 | |
| 127 | void Melder_pathToFile (conststring32 path, 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_sprint (file -> path,kMelder_MAXPATH+1, path); |
| 137 | } |
| 138 | |
| 139 | void Melder_relativePathToFile (conststring32 path, 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 (UNIX) |
| 148 | /* |
| 149 | We assume that Unix complete path names start with a slash. |
| 150 | */ |
| 151 | if (path [0] == U'~' && path [1] == U'/') { |
| 152 | Melder_sprint (file -> path,kMelder_MAXPATH+1, Melder_peek8to32_u (getenv ("HOME")), & path [1]); |
| 153 | } else if (path [0] == U'/' || str32equ (path, U"<stdout>") || str32str (path, U"://")) { |
| 154 | Melder_sprint (file -> path,kMelder_MAXPATH+1, path); |
| 155 | } else { |
| 156 | structMelderFolder currentFolder { }; |
| 157 | Melder_getCurrentFolder (& currentFolder); // BUG |
| 158 | if (currentFolder. path [0] == U'/' && currentFolder. path [1] == U'\0') |
| 159 | Melder_sprint (file -> path,kMelder_MAXPATH+1, U"/", path); |
| 160 | else |
| 161 | Melder_sprint (file -> path,kMelder_MAXPATH+1, currentFolder. 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_relativePathToFolder (conststring32 path, 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 (UNIX) |
| 216 | if (const bool pathIsHomeRelative = ( path [0] == U'~' && (path [1] == U'/' || path [1] == U'\0') )) { |
| 217 | Melder_sprint (folder -> path,kMelder_MAXPATH+1, Melder_peek8to32_u (getenv ("HOME")), & path [1]); |
| 218 | } else if (const bool pathIsAbsolute = ( |
| 219 | path [0] == U'/' // path is on local disk |
| 220 | || |
| 221 | str32str (path, U"://") // path is on a service through a URL; is this needed and/or safe? |
| 222 | )) |
| 223 | { |
| 224 | Melder_sprint (folder -> path,kMelder_MAXPATH+1, path); |
| 225 | } else { |
| 226 | /* |
| 227 | Remaining case: the path must be current-folder-relative. |
| 228 | */ |
| 229 | structMelderFolder currentFolder { }; |
| 230 | Melder_getCurrentFolder (& currentFolder); // BUG if in library? check in Python, for instance |
| 231 | const bool weAreInTheRootFolder = ( currentFolder. path [0] == U'/' && currentFolder. path [1] == U'\0' ); |
| 232 | conststring32 folderSeparator = ( weAreInTheRootFolder ? nullptr : U"/" ); // prevent a double slash |
| 233 | Melder_sprint (folder -> path,kMelder_MAXPATH+1, currentFolder. 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 | conststring32 MelderFile_peekPath (MelderFile file) { |
| 285 | return & file -> path [0]; |
| 286 | } |
| 287 | conststring32 MelderFolder_peekPath (MelderFolder folder) { |
| 288 | return & folder -> path [0]; |
| 289 | } |
| 290 | |
| 291 | conststring8 MelderFile_peekPath8 (MelderFile file) { |
| 292 | return Melder_peek32to8_fileSystem (MelderFile_peekPath (file)); |
| 293 | } |
| 294 | conststring8 MelderFolder_peekPath8 (MelderFolder folder) { |
| 295 | return Melder_peek32to8_fileSystem (MelderFolder_peekPath (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_copy (constMelderFile file, MelderFile copy) { |
| 317 | str32cpy (copy -> path, file -> path); |
| 318 | } |
| 319 | |
| 320 | void MelderFolder_copy (constMelderFolder folder, MelderFolder copy) { |
| 321 | str32cpy (copy -> path, folder -> path); |
| 322 | } |
| 323 | |
| 324 | bool MelderFile_equal (MelderFile file1, MelderFile file2) { |
| 325 | return str32equ (file1 -> path, file2 -> path); |
| 326 | } |
| 327 | |
| 328 | bool MelderFolder_equal (MelderFolder folder1, MelderFolder folder2) { |
| 329 | return str32equ (folder1 -> path, folder2 -> path); |
| 330 | } |
| 331 | |
| 332 | void MelderFile_setToNull (MelderFile file) { |
| 333 | file -> path [0] = U'\0'; |
| 334 | } |
| 335 | |
| 336 | bool MelderFile_isNull (MelderFile file) { |
| 337 | return ! file || file -> path [0] == U'\0'; |
| 338 | } |
| 339 | |
| 340 | void MelderFolder_setToNull (MelderFolder folder) { |
| 341 | folder -> path [0] = U'\0'; |
| 342 | } |
| 343 | |
| 344 | bool MelderFolder_isNull (MelderFolder folder) { |
| 345 | return folder -> path [0] == U'\0'; |
| 346 | } |
| 347 | |
| 348 | void MelderFolder_getFile (MelderFolder parent, conststring32 fileName, MelderFile file) { |
| 349 | #if defined (UNIX) |
| 350 | if (parent -> path [0] == U'/' && parent -> path [1] == U'\0') |
| 351 | Melder_sprint (file -> path,kMelder_MAXPATH+1, U"/", fileName); |
| 352 | else |
| 353 | Melder_sprint (file -> path,kMelder_MAXPATH+1, parent -> 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_relativePathToFile (MelderFolder folder, conststring32 path, MelderFile file) { |
| 363 | autoMelderSetCurrentFolder saveFolder (folder); |
| 364 | Melder_relativePathToFile (path, file); |
| 365 | } |
| 366 | |
| 367 | #ifndef UNIX |
| 368 | static void Melder_getDesktop (MelderFolder folder) { |
| 369 | folder -> path [0] = U'\0'; |
| 370 | } |
| 371 | #endif |
| 372 | |
| 373 | void MelderFile_getParentFolder (MelderFile file, MelderFolder parent) { |
| 374 | #if defined (UNIX) |
| 375 | /* |
| 376 | The parent of /usr/hello.txt is /usr. |
| 377 | The parent of /hello.txt is /. |
| 378 | */ |
| 379 | str32cpy (parent -> path, file -> path); |
| 380 | char32 *slash = str32rchr (parent -> path, U'/'); |
| 381 | if (slash) |
| 382 | *slash = U'\0'; |
| 383 | if (parent -> path [0] == U'\0') |
| 384 | str32cpy (parent -> 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_getParentFolder (MelderFolder subfolder, MelderFolder parentFolder) { |
| 424 | #if defined (UNIX) |
| 425 | /* |
| 426 | The parent of /usr/local is /usr. |
| 427 | The parent of /usr is /. |
| 428 | The parent of / is "". |
| 429 | */ |
| 430 | str32cpy (parentFolder -> path, subfolder -> path); |
| 431 | char32 *slash = str32rchr (parentFolder -> path, U'/'); |
| 432 | if (slash) { |
| 433 | if (slash - parentFolder -> path == 0) { |
| 434 | if (slash [1] == U'\0') { // child is "/" |
| 435 | parentFolder -> 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 -> 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_isDesktop (MelderFolder folder) { |
| 491 | return folder -> path [0] == U'\0'; |
| 492 | } |
| 493 | |
| 494 | void MelderFolder_getSubfolder (MelderFolder parentFolder, conststring32 subfolderName, MelderFolder subfolder) { |
| 495 | #if defined (UNIX) |
| 496 | if (parentFolder -> path [0] == U'/' && parentFolder -> path [1] == U'\0') { |
| 497 | Melder_sprint (subfolder -> path,kMelder_MAXPATH+1, U"/", subfolderName); |
| 498 | } else { |
| 499 | Melder_sprint (subfolder -> path,kMelder_MAXPATH+1, parentFolder -> 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_getHomeDir (MelderFolder homeDir) { |
| 513 | #if defined (UNIX) |
| 514 | char *home = getenv ("HOME"); |
| 515 | Melder_sprint (homeDir -> path,kMelder_MAXPATH+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 structMelderFolder thePreferencesFolder; |
| 538 | void Melder_setPreferencesFolder (conststring32 path) { |
| 539 | Melder_pathToFolder (path, & thePreferencesFolder); |
| 540 | } |
| 541 | MelderFolder Melder_preferencesFolder() { |
| 542 | return & thePreferencesFolder; |
| 543 | } |
| 544 | MelderFolder Melder_preferencesFolder5() { |
| 545 | static structMelderFolder thePreferencesFolder5; |
| 546 | if (MelderFolder_isNull (& thePreferencesFolder5)) { |
| 547 | structMelderFolder homeFolder { }; |
| 548 | Melder_getHomeDir (& 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 (UNIX) |
| 553 | Melder_sprint (thePreferencesFolder5. path,kMelder_MAXPATH+1, homeFolder. path, |
| 554 | U"/.", Melder_lowerCaseAppName(), 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 | MelderFolder Melder_preferencesFolder7() { |
| 563 | static structMelderFolder thePreferencesFolder7; |
| 564 | if (MelderFolder_isNull (& thePreferencesFolder7)) { |
| 565 | structMelderFolder homeFolder { }; |
| 566 | Melder_getHomeDir (& 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 (UNIX) |
| 571 | const conststring32 xdgConfigHome = Melder_getenv (U"XDG_CONFIG_HOME"); |
| 572 | if (xdgConfigHome) |
| 573 | Melder_sprint (thePreferencesFolder7. path,kMelder_MAXPATH+1, xdgConfigHome, |
| 574 | U"/", Melder_lowerCaseAppName()); |
| 575 | else |
| 576 | Melder_sprint (thePreferencesFolder7. path,kMelder_MAXPATH+1, homeFolder. path, |
| 577 | U"/.config/", Melder_lowerCaseAppName()); |
| 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_getTempDir (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_fopen (MelderFile file, const char *type) { |
| 596 | if (MelderFile_isNull (file)) |
| 597 | Melder_throw (U"Cannot open null file."); |
| 598 | if (! Melder_isTracingGlobally) |
| 599 | Melder_assert (str32equ (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_MAXPATH+1]; |
| 607 | Melder_32to8_fileSystem_inplace (file -> path, utf8path); |
| 608 | FILE *f; |
| 609 | file -> openForWriting = ( type [0] == 'w' || type [0] == 'a' || strchr (type, '+') ); |
| 610 | if (str32equ (file -> path, U"<stdout>") && file -> openForWriting) { |
| 611 | f = 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_ISDIR (statbuf. st_mode)) { |
| 625 | isFolder = true; |
| 626 | trace (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 | char32 *path = file -> path; |
| 635 | Melder_appendError ( |
| 636 | ( errno == EPERM || errno == EACCES ? 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 (errno == EIO) |
| 641 | Melder_appendError (U"Not-so-useful hint: an error occurred while reading from the file system."); |
| 642 | else if (errno == ELOOP) |
| 643 | Melder_appendError (U"Hint: the file path contains a loop of symbolic links."); |
| 644 | else if (errno == ENAMETOOLONG) |
| 645 | Melder_appendError (U"Not-so-useful hint: the file path may be too long. This should not occur."); |
| 646 | else if (errno == ENOENT) |
| 647 | if (file -> openForWriting) |
| 648 | Melder_appendError (U"Hint: one of the folders in this file path does not exist."); |
| 649 | else |
| 650 | Melder_appendError (U"Hint: one of the folders or files in this file path does not exist."); |
| 651 | else if (errno == ENOTDIR) |
| 652 | Melder_appendError (U"Hint: a component of the file path is not a folder."); |
| 653 | else if (errno == EOVERFLOW) |
| 654 | Melder_appendError (U"Not-so-useful hint: the file size is too big."); |
| 655 | else if (errno != 0) |
| 656 | Melder_appendError (U"Not-so-useful hint: unexpected error ", errno, U"."); |
| 657 | if (isFolder) |
| 658 | Melder_appendError (U"Hint: this is a folder, not a file."); |
| 659 | else if (path [0] == U'\0') |
| 660 | Melder_appendError (U"Hint: empty file name."); |
| 661 | else if (path [0] == U' ' || path [0] == U'\t') |
| 662 | Melder_appendError (U"Hint: file name starts with a space or tab."); |
| 663 | else if (path [Melder_length (path) - 1] == U' ' || path [Melder_length (path) - 1] == U'\t') |
| 664 | Melder_appendError (U"Hint: file name ends in a space or tab."); |
| 665 | else if (str32chr (path, U'\n')) |
| 666 | Melder_appendError (U"Hint: file name contains a newline symbol."); |
| 667 | throw MelderError (); |
| 668 | return nullptr; |
| 669 | } |
| 670 | return f; |
| 671 | } |
| 672 | |
| 673 | void Melder_fclose (MelderFile file, FILE *f) { |
| 674 | if (! f) |
| 675 | return; |
| 676 | if (f != Melder_stdout && fclose (f) == EOF) |
| 677 | Melder_throw (U"Error closing file ", file, U"."); |
| 678 | } |
| 679 | |
| 680 | bool MelderFile_exists (MelderFile file) { |
| 681 | #if defined (UNIX) |
| 682 | struct stat fileOrFolderStatus; |
| 683 | const bool exists = ( stat (MelderFile_peekPath8 (file), & fileOrFolderStatus) == 0 ); |
| 684 | if (! exists) |
| 685 | return false; |
| 686 | return ! S_ISDIR (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_exists (MelderFolder folder) { |
| 696 | #if defined (UNIX) |
| 697 | struct stat fileOrFolderStatus; |
| 698 | const bool exists = ( stat (MelderFolder_peekPath8 (folder), & fileOrFolderStatus) == 0 ); |
| 699 | if (! exists) |
| 700 | return false; |
| 701 | return S_ISDIR (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_readable (MelderFile file) { |
| 711 | try { |
| 712 | autofile f = Melder_fopen (file, "rb"); |
| 713 | f.close (file); |
| 714 | return true; |
| 715 | } catch (MelderError) { |
| 716 | Melder_clearError (); |
| 717 | return false; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | bool Melder_tryToWriteFile (MelderFile file) { |
| 722 | try { |
| 723 | autofile f = Melder_fopen (file, "wb"); |
| 724 | f.close (file); |
| 725 | return true; |
| 726 | } catch (MelderError) { |
| 727 | Melder_clearError (); |
| 728 | return false; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | bool Melder_tryToAppendFile (MelderFile file) { |
| 733 | try { |
| 734 | autofile f = Melder_fopen (file, "ab"); |
| 735 | f.close (file); |
| 736 | return true; |
| 737 | } catch (MelderError) { |
| 738 | Melder_clearError (); |
| 739 | return false; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | integer MelderFile_length (MelderFile file) { |
| 744 | #if defined (UNIX) |
| 745 | struct stat statistics; |
| 746 | if (stat (MelderFile_peekPath8 (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_delete (MelderFile file) { |
| 764 | if (! file) |
| 765 | return; |
| 766 | #if defined (UNIX) |
| 767 | remove (MelderFile_peekPath8 (file)); |
| 768 | #elif defined (_WIN32) |
| 769 | DeleteFile (MelderFile_peekPathW (file)); |
| 770 | #endif |
| 771 | } |
| 772 | |
| 773 | void MelderFile_moveAndOrRename (MelderFile fromFile, MelderFile toFile) { |
| 774 | if (! fromFile || ! toFile) |
| 775 | return; |
| 776 | try { |
| 777 | #if defined (UNIX) |
| 778 | static char fromPath8 [1 + kMelder_MAXPATH], toPath8 [1 + kMelder_MAXPATH]; |
| 779 | Melder_32to8_fileSystem_inplace (MelderFile_peekPath (fromFile), fromPath8); // not MelderFile_peekPath8, because of static |
| 780 | Melder_32to8_fileSystem_inplace (MelderFile_peekPath (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 (errno) { |
| 790 | case EACCES: |
| 791 | Melder_throw (U"A folder in one of the paths doesn’t allow reading and/or writing access."); |
| 792 | case EBUSY: |
| 793 | Melder_throw (U"A folder in one of the paths is busy."); |
| 794 | case EINVAL: |
| 795 | Melder_throw (U"Invalid path (recursive? dots?)."); |
| 796 | case EIO: |
| 797 | Melder_throw (U"Physical I/O error."); |
| 798 | case EISDIR: |
| 799 | Melder_throw (U"Cannot move an/or rename a file to a folder name."); |
| 800 | #if ! defined (UNIX) && ! defined (_WIN32) |
| 801 | case EMLOOP: |
| 802 | Melder_throw (U"Loop in symbolic links."); |
| 803 | #endif |
| 804 | case EMLINK: |
| 805 | Melder_throw (U"Too many links."); |
| 806 | case ENAMETOOLONG: |
| 807 | Melder_throw (U"Name too long."); |
| 808 | case ENOENT: |
| 809 | Melder_throw (U"A folder or file in the path does not exist."); |
| 810 | case ENOSPC: |
| 811 | Melder_throw (U"Folder cannot be extended."); |
| 812 | case ENOTDIR: |
| 813 | Melder_throw (U"A part of the path is not a folder (hint: look at the slahes)."); |
| 814 | case EPERM: |
| 815 | Melder_throw (U"No permission to read and/or write in one of the paths."); |
| 816 | case EROFS: |
| 817 | Melder_throw (U"Cannot do links between file systems."); |
| 818 | #if ! defined (UNIX) && ! defined (_WIN32) |
| 819 | case ETXTBUSY: |
| 820 | Melder_throw (U"Busy pure procedure file."); |
| 821 | #endif |
| 822 | default: |
| 823 | Melder_throw (U"Unknown error ", errno, U"."); |
| 824 | } |
| 825 | } catch (MelderError) { |
| 826 | Melder_throw (U"Could not rename ", fromFile, U" to ", toFile, U"."); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | char32 * Melder_peekExpandBackslashes (conststring32 message) { |
| 831 | static char32 names [11] [kMelder_MAXPATH+1]; |
| 832 | static int index = 0; |
| 833 | if (++ index == 11) |
| 834 | index = 0; |
| 835 | char32 *to = & names [index] [0]; |
| 836 | for (const 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 | conststring32 MelderFile_messageName (MelderFile file) { |
| 848 | return Melder_cat (U"“", file -> path, U"”"); // BUG: is cat allowed here? |
| 849 | } |
| 850 | |
| 851 | conststring32 MelderFolder_messageName (MelderFolder folder) { |
| 852 | return Melder_cat (U"“", folder -> path, U"”"); // BUG: is cat allowed here? |
| 853 | } |
| 854 | |
| 855 | #if defined (UNIX) |
| 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 structMelderFolder theDefaultDir; |
| 861 | #endif |
| 862 | |
| 863 | void Melder_getCurrentFolder (MelderFolder folder) { |
| 864 | #if defined (UNIX) |
| 865 | char path [kMelder_MAXPATH+1]; |
| 866 | char *pathResult = getcwd (path, kMelder_MAXPATH+1); |
| 867 | if (pathResult) |
| 868 | Melder_8bitFileRepresentationToStr32_inplace (path, folder -> path); |
| 869 | else if (errno == EPERM) |
| 870 | str32cpy (folder -> path, theDefaultDir. path); |
| 871 | else |
| 872 | Melder_throw (Melder_peek8to32_u (strerror (errno))); |
| 873 | Melder_assert (Melder_length (folder -> path) <= kMelder_MAXPATH); |
| 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_setCurrentFolder (MelderFolder folder) { |
| 882 | #if defined (UNIX) |
| 883 | chdir (MelderFolder_peekPath8 (folder)); |
| 884 | str32cpy (theDefaultDir. path, folder -> path); |
| 885 | #elif defined (_WIN32) |
| 886 | SetCurrentDirectory (MelderFolder_peekPathW (folder)); |
| 887 | #endif |
| 888 | } |
| 889 | |
| 890 | void MelderFile_setDefaultDir (MelderFile file) { |
| 891 | structMelderFolder folder { }; |
| 892 | MelderFile_getParentFolder (file, & folder); |
| 893 | Melder_setCurrentFolder (& folder); |
| 894 | } |
| 895 | |
| 896 | MelderFolder Melder_peekWorkingDirectory () { |
| 897 | static structMelderFolder s_workingDirectory; |
| 898 | Melder_getCurrentFolder (& s_workingDirectory); |
| 899 | return & s_workingDirectory; |
| 900 | } |
| 901 | |
| 902 | void Melder_createDirectory (MelderFolder parent, conststring32 folderName, int mode) { |
| 903 | #if defined (UNIX) |
| 904 | structMelderFile file { }; |
| 905 | if (folderName [0] == U'/') { |
| 906 | Melder_sprint (file. path,kMelder_MAXPATH+1, folderName); // absolute path |
| 907 | } else if (parent -> path [0] == U'/' && parent -> path [1] == U'\0') { |
| 908 | Melder_sprint (file. path,kMelder_MAXPATH+1, U"/", folderName); // relative path in root folder |
| 909 | } else { |
| 910 | Melder_sprint (file. path,kMelder_MAXPATH+1, parent -> path, U"/", folderName); // relative path |
| 911 | } |
| 912 | if (mkdir (MelderFile_peekPath8 (& file), mode) == -1 && errno != EEXIST) // ignore if folder already exists |
| 913 | Melder_throw (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_create (MelderFolder folder) { |
| 934 | #if defined (UNIX) |
| 935 | const int status = mkdir (MelderFolder_peekPath8 (folder), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); |
| 936 | if (status == 0) |
| 937 | return; // successfully created a new folder |
| 938 | if (errno == EEXIST) |
| 939 | return; // it is no failure if the folder already existed |
| 940 | Melder_throw (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_createFolder (conststring32 path) { |
| 957 | structMelderFolder folder { }; |
| 958 | Melder_relativePathToFolder (path, & folder); |
| 959 | MelderFolder_create (& folder); |
| 960 | } |
| 961 | |
| 962 | static size_t fread_multi (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 | autostring32 MelderFile_readText (MelderFile file, autostring8 *string8) { |
| 980 | try { |
| 981 | int type = 0; // 8-bit |
| 982 | autostring32 text; |
| 983 | autofile f = Melder_fopen (file, "rb"); |
| 984 | if (fseeko (f, 0, SEEK_END) < 0) |
| 985 | Melder_throw (U"Cannot count the bytes in the file."); |
| 986 | Melder_assert (sizeof (off_t) >= 8); |
| 987 | 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_SET); |
| 1005 | } else { |
| 1006 | rewind (f); // length and type already set correctly |
| 1007 | } |
| 1008 | autostring8 text8bit (length); |
| 1009 | Melder_assert (text8bit); |
| 1010 | size_t numberOfBytesRead = fread_multi (text8bit.get(), (size_t) length, f); |
| 1011 | Melder_require ((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 [length] = '\0'; |
| 1017 | /* |
| 1018 | Count and repair null bytes. |
| 1019 | */ |
| 1020 | if (length > 0) { |
| 1021 | int64 numberOfNullBytes = 0; |
| 1022 | char *q = & text8bit [0]; |
| 1023 | for (integer i = 0; i < length; i ++) |
| 1024 | if (text8bit [i] != '\0') |
| 1025 | * (q ++) = text8bit [i]; |
| 1026 | else |
| 1027 | numberOfNullBytes ++; |
| 1028 | *q = '\0'; |
| 1029 | if (numberOfNullBytes > 0) |
| 1030 | Melder_warning (U"Ignored ", numberOfNullBytes, U" null bytes in text file ", file, U"."); |
| 1031 | } |
| 1032 | if (string8) { |
| 1033 | *string8 = text8bit.move(); |
| 1034 | (void) Melder_killReturns_inplace (string8->get()); |
| 1035 | return autostring32(); // OK |
| 1036 | } else { |
| 1037 | text = Melder_8to32_e (text8bit.get(), kMelder_textInputEncoding::UNDEFINED); |
| 1038 | } |
| 1039 | } else { |
| 1040 | length = length / 2 - 1; // Byte Order Mark subtracted. Length = number of UTF-16 codes |
| 1041 | text = autostring32 (length + 1); |
| 1042 | if (type == 1) { |
| 1043 | for (int64 i = 0; i < length; i ++) { |
| 1044 | const char16 kar1 = bingetu16 (f); |
| 1045 | if (kar1 < 0xD800) { |
| 1046 | text [i] = (const char32) kar1; // convert up without sign extension |
| 1047 | } else if (kar1 < 0xDC00) { |
| 1048 | length --; |
| 1049 | const char16 kar2 = bingetu16 (f); |
| 1050 | if (kar2 >= 0xDC00 && kar2 <= 0xDFFF) { |
| 1051 | text [i] = (const char32) (0x010000 + |
| 1052 | (const char32) (((const char32) kar1 & 0x0003FF) << 10) + |
| 1053 | (const char32) ((const char32) kar2 & 0x0003FF)); |
| 1054 | } else { |
| 1055 | text [i] = UNICODE_REPLACEMENT_CHARACTER; |
| 1056 | } |
| 1057 | } else if (kar1 < 0xE000) { |
| 1058 | text [i] = UNICODE_REPLACEMENT_CHARACTER; |
| 1059 | } else { |
| 1060 | text [i] = (const char32) kar1; // convert up without sign extension |
| 1061 | } |
| 1062 | } |
| 1063 | } else { |
| 1064 | for (int64 i = 0; i < length; i ++) { |
| 1065 | const char16 kar1 = bingetu16LE (f); |
| 1066 | if (kar1 < 0xD800) { |
| 1067 | text [i] = (const char32) kar1; // convert up without sign extension |
| 1068 | } else if (kar1 < 0xDC00) { |
| 1069 | length --; |
| 1070 | const char16 kar2 = bingetu16LE (f); |
| 1071 | if (kar2 >= 0xDC00 && kar2 <= 0xDFFF) { |
| 1072 | text [i] = (const char32) (0x01'0000 + |
| 1073 | (const char32) (((const char32) kar1 & 0x00'03FF) << 10) + |
| 1074 | (const char32) ((const char32) kar2 & 0x00'03FF)); |
| 1075 | } else { |
| 1076 | text [i] = UNICODE_REPLACEMENT_CHARACTER; |
| 1077 | } |
| 1078 | } else if (kar1 < 0xE000) { |
| 1079 | text [i] = UNICODE_REPLACEMENT_CHARACTER; |
| 1080 | } else if (kar1 <= 0xFFFF) { |
| 1081 | text [i] = (const char32) kar1; // convert up without sign extension |
| 1082 | } else { |
| 1083 | Melder_crash (U"MelderFile_readText: unsigned short greater than 0xFFFF: should not occur."); |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | text [length] = U'\0'; |
| 1088 | (void) Melder_killReturns_inplace (text.get()); |
| 1089 | } |
| 1090 | f.close (file); |
| 1091 | return text; |
| 1092 | } catch (MelderError) { |
| 1093 | Melder_throw (U"Error reading file ", file, U"."); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | void Melder_fwrite32to8 (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 char32* p = string; *p != U'\0'; p ++) { |
| 1106 | const 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_e (MelderFile file, conststring32 text, kMelder_textOutputEncoding outputEncoding) { |
| 1130 | if (! text) |
| 1131 | text = U""; |
| 1132 | autofile f = Melder_fopen (file, "wb"); |
| 1133 | if (outputEncoding == kMelder_textOutputEncoding::UTF8) { |
| 1134 | Melder_fwrite32to8 (text, f); |
| 1135 | } else if ((outputEncoding == kMelder_textOutputEncoding::ASCII_THEN_UTF16 && Melder_isValidAscii (text)) || |
| 1136 | (outputEncoding == kMelder_textOutputEncoding::ISO_LATIN1_THEN_UTF16 && Melder_isEncodable (text, 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 integer n = Melder_length (text); |
| 1145 | for (integer i = 0; i < n; i ++) { |
| 1146 | 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 | binputu16 (0xFEFF, f); // Byte Order Mark |
| 1156 | const integer n = Melder_length (text); |
| 1157 | for (integer i = 0; i < n; i ++) { |
| 1158 | char32 kar = text [i]; |
| 1159 | #ifdef _WIN32 |
| 1160 | if (kar == U'\n') |
| 1161 | binputu16 (13, f); |
| 1162 | #endif |
| 1163 | if (kar <= 0x00'FFFF) { |
| 1164 | binputu16 ((char16) kar, f); // guarded conversion down |
| 1165 | } else if (kar <= 0x10'FFFF) { |
| 1166 | kar -= 0x010000; |
| 1167 | binputu16 (0xD800 | (uint16) (kar >> 10), f); |
| 1168 | binputu16 (0xDC00 | (uint16) ((char16) kar & 0x3ff), f); |
| 1169 | } else { |
| 1170 | binputu16 (UNICODE_REPLACEMENT_CHARACTER, f); |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | f.close (file); |
| 1175 | } |
| 1176 | void MelderFile_writeText_i (MelderFile file, conststring32 text, kMelder_textOutputEncoding outputEncoding) { |
| 1177 | try { |
| 1178 | MelderFile_writeText_e (file, text, outputEncoding); |
| 1179 | } catch (MelderError) { |
| 1180 | Melder_clearError (); |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | void MelderFile_appendText_e (MelderFile file, conststring32 text) { |
| 1185 | if (! text) text = U""; |
| 1186 | autofile f1; |
| 1187 | try { |
| 1188 | f1 = Melder_fopen (file, "rb"); |
| 1189 | } catch (MelderError) { |
| 1190 | Melder_clearError (); // it's OK if the file didn't exist yet... |
| 1191 | MelderFile_writeText_e (file, text, Melder_getOutputEncoding ()); // 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.close (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_textOutputEncoding outputEncoding = Melder_getOutputEncoding (); |
| 1207 | if (outputEncoding == kMelder_textOutputEncoding::UTF8) { // TODO: read as file's encoding |
| 1208 | autofile f2 = Melder_fopen (file, "ab"); |
| 1209 | Melder_fwrite32to8 (text, f2); |
| 1210 | f2.close (file); |
| 1211 | } else if ((outputEncoding == kMelder_textOutputEncoding::ASCII_THEN_UTF16 && Melder_isEncodable (text, kMelder_textOutputEncoding_ASCII)) |
| 1212 | || (outputEncoding == kMelder_textOutputEncoding::ISO_LATIN1_THEN_UTF16 && Melder_isEncodable (text, kMelder_textOutputEncoding_ISO_LATIN1))) |
| 1213 | { |
| 1214 | /* |
| 1215 | Append ASCII or ISOLatin1 text to ASCII or ISOLatin1 file. |
| 1216 | */ |
| 1217 | autofile f2 = Melder_fopen (file, "ab"); |
| 1218 | const integer n = Melder_length (text); |
| 1219 | for (integer i = 0; i < n; i ++) { |
| 1220 | const char32 kar = text [i]; |
| 1221 | #ifdef _WIN32 |
| 1222 | if (kar == U'\n') |
| 1223 | fputc (13, f2); |
| 1224 | #endif |
| 1225 | fputc ((char8) kar, f2); |
| 1226 | } |
| 1227 | f2.close (file); |
| 1228 | } else { |
| 1229 | /* |
| 1230 | Convert to wide character file. |
| 1231 | */ |
| 1232 | autostring32 oldText = MelderFile_readText (file); |
| 1233 | autofile f2 = Melder_fopen (file, "wb"); |
| 1234 | binputu16 (0xfeff, f2); |
| 1235 | integer n = Melder_length (oldText.get()); |
| 1236 | for (int64 i = 0; i < n; i ++) { |
| 1237 | char32 kar = oldText [i]; |
| 1238 | #ifdef _WIN32 |
| 1239 | if (kar == U'\n') |
| 1240 | binputu16 (13, f2); |
| 1241 | #endif |
| 1242 | if (kar <= 0x00'FFFF) { |
| 1243 | binputu16 ((uint16) kar, f2); // guarded conversion down |
| 1244 | } else if (kar <= 0x10'FFFF) { |
| 1245 | kar -= 0x01'0000; |
| 1246 | binputu16 ((uint16) (0x00'D800 | (kar >> 10)), f2); |
| 1247 | binputu16 ((uint16) (0x00'DC00 | (kar & 0x00'03ff)), f2); |
| 1248 | } else { |
| 1249 | binputu16 (UNICODE_REPLACEMENT_CHARACTER, f2); |
| 1250 | } |
| 1251 | } |
| 1252 | n = Melder_length (text); |
| 1253 | for (integer i = 0; i < n; i ++) { |
| 1254 | char32 kar = text [i]; |
| 1255 | #ifdef _WIN32 |
| 1256 | if (kar == U'\n') |
| 1257 | binputu16 (13, f2); |
| 1258 | #endif |
| 1259 | if (kar <= 0x00'FFFF) { |
| 1260 | binputu16 ((uint16) kar, f2); // guarded conversion down |
| 1261 | } else if (kar <= 0x10'FFFF) { |
| 1262 | kar -= 0x01'0000; |
| 1263 | binputu16 ((uint16) (0x00'D800 | (kar >> 10)), f2); |
| 1264 | binputu16 ((uint16) (0x00'DC00 | (kar & 0x00'03ff)), f2); |
| 1265 | } else { |
| 1266 | binputu16 (UNICODE_REPLACEMENT_CHARACTER, f2); |
| 1267 | } |
| 1268 | } |
| 1269 | f2.close (file); |
| 1270 | } |
| 1271 | } else { |
| 1272 | autofile f2 = Melder_fopen (file, "ab"); |
| 1273 | const integer n = Melder_length (text); |
| 1274 | for (integer i = 0; i < n; i ++) { |
| 1275 | if (type == 1) { |
| 1276 | char32 kar = text [i]; |
| 1277 | #ifdef _WIN32 |
| 1278 | if (kar == U'\n') |
| 1279 | binputu16 (13, f2); |
| 1280 | #endif |
| 1281 | if (kar <= 0x00'FFFF) { |
| 1282 | binputu16 ((uint16) kar, f2); // guarded conversion down |
| 1283 | } else if (kar <= 0x10'FFFF) { |
| 1284 | kar -= 0x01'0000; |
| 1285 | binputu16 ((uint16) (0x00'D800 | (kar >> 10)), f2); |
| 1286 | binputu16 ((uint16) (0x00'DC00 | (kar & 0x00'03ff)), f2); |
| 1287 | } else { |
| 1288 | binputu16 (UNICODE_REPLACEMENT_CHARACTER, f2); |
| 1289 | } |
| 1290 | } else { |
| 1291 | char32 kar = text [i]; |
| 1292 | #ifdef _WIN32 |
| 1293 | if (kar == U'\n') |
| 1294 | binputu16LE (13, f2); |
| 1295 | #endif |
| 1296 | if (kar <= 0x00'FFFF) { |
| 1297 | binputu16LE ((uint16) kar, f2); // guarded conversion down |
| 1298 | } else if (kar <= 0x10FFFF) { |
| 1299 | kar -= 0x01'0000; |
| 1300 | binputu16LE ((uint16) (0x00'D800 | (kar >> 10)), f2); |
| 1301 | binputu16LE ((uint16) (0x00'DC00 | (kar & 0x00'03ff)), f2); |
| 1302 | } else { |
| 1303 | binputu16LE (UNICODE_REPLACEMENT_CHARACTER, f2); |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | f2.close (file); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | /* End of file melder_files.cpp */ |