melder_files.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #ifndef _melder_files_h_
  2. #define _melder_files_h_
  3. /* melder_files.h
  4. *
  5. * Copyright (C) 1992-2018 Paul Boersma
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This code is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #if defined (_WIN32)
  21. #define Melder_DIRECTORY_SEPARATOR '\\'
  22. #else
  23. #define Melder_DIRECTORY_SEPARATOR '/'
  24. #endif
  25. struct FLAC__StreamDecoder;
  26. struct FLAC__StreamEncoder;
  27. #define kMelder_MAXPATH 1023 /* excluding the null byte */
  28. struct structMelderFile {
  29. FILE *filePointer;
  30. char32 path [kMelder_MAXPATH+1];
  31. enum class Format { none = 0, binary = 1, text = 2 } format;
  32. bool openForReading, openForWriting, verbose, requiresCRLF;
  33. uint32 outputEncoding;
  34. int indent;
  35. struct FLAC__StreamEncoder *flacEncoder;
  36. };
  37. typedef struct structMelderFile *MelderFile;
  38. struct structMelderDir {
  39. char32 path [kMelder_MAXPATH+1];
  40. };
  41. typedef struct structMelderDir *MelderDir;
  42. conststring32 MelderFile_name (MelderFile file);
  43. conststring32 MelderDir_name (MelderDir dir);
  44. void Melder_pathToDir (conststring32 path, MelderDir dir);
  45. void Melder_pathToFile (conststring32 path, MelderFile file);
  46. void Melder_relativePathToFile (conststring32 path, MelderFile file);
  47. conststring32 Melder_dirToPath (MelderDir dir);
  48. /* Returns a pointer internal to 'dir', like "/u/paul/praats" or "D:\Paul\Praats" */
  49. conststring32 Melder_fileToPath (MelderFile file);
  50. void MelderFile_copy (MelderFile file, MelderFile copy);
  51. void MelderDir_copy (MelderDir dir, MelderDir copy);
  52. bool MelderFile_equal (MelderFile file1, MelderFile file2);
  53. bool MelderDir_equal (MelderDir dir1, MelderDir dir2);
  54. void MelderFile_setToNull (MelderFile file);
  55. bool MelderFile_isNull (MelderFile file);
  56. void MelderDir_setToNull (MelderDir dir);
  57. bool MelderDir_isNull (MelderDir dir);
  58. void MelderDir_getFile (MelderDir parent, conststring32 fileName, MelderFile file);
  59. void MelderDir_relativePathToFile (MelderDir dir, conststring32 path, MelderFile file);
  60. void MelderFile_getParentDir (MelderFile file, MelderDir parent);
  61. void MelderDir_getParentDir (MelderDir file, MelderDir parent);
  62. bool MelderDir_isDesktop (MelderDir dir);
  63. void MelderDir_getSubdir (MelderDir parent, conststring32 subdirName, MelderDir subdir);
  64. void Melder_rememberShellDirectory ();
  65. conststring32 Melder_getShellDirectory ();
  66. void Melder_getHomeDir (MelderDir homeDir);
  67. void Melder_getPrefDir (MelderDir prefDir);
  68. void Melder_getTempDir (MelderDir tempDir);
  69. bool MelderFile_exists (MelderFile file);
  70. bool MelderFile_readable (MelderFile file);
  71. integer MelderFile_length (MelderFile file);
  72. void MelderFile_delete (MelderFile file);
  73. /* The following two should be combined with each other and with Windows extension setting: */
  74. FILE * Melder_fopen (MelderFile file, const char *type);
  75. void Melder_fclose (MelderFile file, FILE *stream);
  76. void Melder_files_cleanUp ();
  77. /* Use the following functions to pass unchanged text or file names to Melder_* functions. */
  78. /* Backslashes are replaced by "\bs". */
  79. /* The trick is that they return one of 11 cyclically used static strings, */
  80. /* so you can use up to 11 strings in a single Melder_* call. */
  81. char32 * Melder_peekExpandBackslashes (conststring32 message);
  82. conststring32 MelderFile_messageName (MelderFile file); // calls Melder_peekExpandBackslashes ()
  83. void Melder_createDirectory (MelderDir parent, conststring32 subdirName, int mode);
  84. void Melder_getDefaultDir (MelderDir dir);
  85. void Melder_setDefaultDir (MelderDir dir);
  86. void MelderFile_setDefaultDir (MelderFile file);
  87. class autofile {
  88. FILE *ptr;
  89. public:
  90. autofile (FILE *f) : ptr (f) {
  91. }
  92. autofile () : ptr (nullptr) {
  93. }
  94. ~autofile () {
  95. if (ptr)
  96. fclose (ptr); // no error checking, because this is a destructor, only called after a throw, because otherwise you'd use f.close(file)
  97. }
  98. operator FILE * () {
  99. return ptr;
  100. }
  101. void reset (FILE *f) {
  102. if (ptr)
  103. fclose (ptr); // BUG: not a normal closure
  104. ptr = f;
  105. }
  106. void close (MelderFile file) {
  107. if (ptr) {
  108. FILE *tmp = ptr;
  109. ptr = nullptr;
  110. Melder_fclose (file, tmp);
  111. }
  112. }
  113. };
  114. class autoMelderSaveDefaultDir {
  115. structMelderDir _savedDir;
  116. public:
  117. autoMelderSaveDefaultDir () {
  118. Melder_getDefaultDir (& our _savedDir);
  119. }
  120. ~autoMelderSaveDefaultDir () {
  121. Melder_setDefaultDir (& our _savedDir);
  122. }
  123. /*
  124. Disable copying.
  125. */
  126. autoMelderSaveDefaultDir (const autoMelderSaveDefaultDir&) = delete; // disable copy constructor
  127. autoMelderSaveDefaultDir& operator= (const autoMelderSaveDefaultDir&) = delete; // disable copy assignment
  128. };
  129. class autoMelderSetDefaultDir {
  130. structMelderDir _savedDir;
  131. public:
  132. autoMelderSetDefaultDir (MelderDir dir) {
  133. Melder_getDefaultDir (& our _savedDir);
  134. Melder_setDefaultDir (dir);
  135. }
  136. ~autoMelderSetDefaultDir () {
  137. Melder_setDefaultDir (& our _savedDir);
  138. }
  139. /*
  140. Disable copying.
  141. */
  142. autoMelderSetDefaultDir (const autoMelderSetDefaultDir&) = delete; // disable copy constructor
  143. autoMelderSetDefaultDir& operator= (const autoMelderSetDefaultDir&) = delete; // disable copy assignment
  144. };
  145. class autoMelderFileSetDefaultDir {
  146. structMelderDir _savedDir;
  147. public:
  148. autoMelderFileSetDefaultDir (MelderFile file) {
  149. Melder_getDefaultDir (& our _savedDir);
  150. MelderFile_setDefaultDir (file);
  151. }
  152. ~autoMelderFileSetDefaultDir () {
  153. Melder_setDefaultDir (& our _savedDir);
  154. }
  155. /*
  156. Disable copying.
  157. */
  158. autoMelderFileSetDefaultDir (const autoMelderFileSetDefaultDir&) = delete; // disable copy constructor
  159. autoMelderFileSetDefaultDir& operator= (const autoMelderFileSetDefaultDir&) = delete; // disable copy assignment
  160. };
  161. /* Read and write whole text files. */
  162. autostring32 MelderFile_readText (MelderFile file, autostring8 *string8 = nullptr);
  163. void Melder_fwrite32to8 (conststring32 string, FILE *f);
  164. void MelderFile_writeText (MelderFile file, conststring32 text, kMelder_textOutputEncoding outputEncoding);
  165. void MelderFile_appendText (MelderFile file, conststring32 text);
  166. /* End of file melder_files.h */
  167. #endif