FileInMemorySet.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* FileInMemorySet.cpp
  2. *
  3. * Copyright (C) 2012-2013, 2015-2017 David Weenink, 2017 Paul Boersma
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * 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. #include "FileInMemorySet.h"
  19. #include "oo_DESTROY.h"
  20. #include "FileInMemorySet_def.h"
  21. #include "oo_COPY.h"
  22. #include "FileInMemorySet_def.h"
  23. #include "oo_EQUAL.h"
  24. #include "FileInMemorySet_def.h"
  25. #include "oo_CAN_WRITE_AS_ENCODING.h"
  26. #include "FileInMemorySet_def.h"
  27. #include "oo_WRITE_TEXT.h"
  28. #include "FileInMemorySet_def.h"
  29. #include "oo_READ_TEXT.h"
  30. #include "FileInMemorySet_def.h"
  31. #include "oo_WRITE_BINARY.h"
  32. #include "FileInMemorySet_def.h"
  33. #include "oo_READ_BINARY.h"
  34. #include "FileInMemorySet_def.h"
  35. #include "oo_DESCRIPTION.h"
  36. #include "FileInMemorySet_def.h"
  37. Thing_implement (FileInMemorySet, SortedSet, 0);
  38. void structFileInMemorySet :: v_info () {
  39. FileInMemorySet_Parent :: v_info ();
  40. MelderInfo_writeLine (U"Number of files: ", size);
  41. MelderInfo_writeLine (U"Total number of bytes: ", FileInMemorySet_getTotalNumberOfBytes (this));
  42. }
  43. integer FileInMemorySet_getTotalNumberOfBytes (FileInMemorySet me) {
  44. integer numberOfBytes = 0;
  45. for (integer ifile = 1; ifile <= my size; ifile ++) {
  46. FileInMemory fim = (FileInMemory) my at [ifile];
  47. numberOfBytes += fim -> d_numberOfBytes;
  48. }
  49. return numberOfBytes;
  50. }
  51. autoFileInMemorySet FileInMemorySet_create () {
  52. try {
  53. autoFileInMemorySet me = Thing_new (FileInMemorySet);
  54. return me;
  55. } catch (MelderError) {
  56. Melder_throw (U"FileInMemorySet not created.");
  57. }
  58. }
  59. autoFileInMemorySet FileInMemorySets_merge (OrderedOf<structFileInMemorySet>& list) {
  60. try {
  61. autoFileInMemorySet thee = Data_copy (list.at[1]);
  62. for (integer iset = 1; iset <= list.size; iset ++) {
  63. thy merge (list.at [iset]);
  64. }
  65. return thee;
  66. } catch (MelderError) {
  67. Melder_throw (U"FileInMemorySets not merged.");
  68. }
  69. }
  70. autoFileInMemorySet FileInMemorySet_createFromDirectoryContents (conststring32 dirpath, conststring32 fileGlobber) {
  71. try {
  72. structMelderDir parent { };
  73. Melder_pathToDir (dirpath, & parent);
  74. autoStrings thee = Strings_createAsFileList (Melder_cat (dirpath, U"/", fileGlobber));
  75. if (thy numberOfStrings < 1) {
  76. Melder_throw (U"No files found.");
  77. }
  78. autoFileInMemorySet me = FileInMemorySet_create ();
  79. for (integer i = 1; i <= thy numberOfStrings; i ++) {
  80. structMelderFile file { };
  81. MelderDir_getFile (& parent, thy strings [i].get(), & file);
  82. autoFileInMemory fim = FileInMemory_create (& file);
  83. my addItem_move (fim.move());
  84. }
  85. return me;
  86. } catch (MelderError) {
  87. Melder_throw (U"FileInMemorySet not created from directory \"", dirpath, U"\" for files that match \"", fileGlobber, U"\".");
  88. }
  89. }
  90. autoFileInMemorySet FilesInMemory_to_FileInMemorySet (OrderedOf<structFileInMemory>& list) {
  91. try {
  92. autoFileInMemorySet thee = FileInMemorySet_create ();
  93. for (integer ifile = 1; ifile <= list.size; ifile ++) {
  94. autoFileInMemory fim = Data_copy (list.at [ifile]);
  95. thy addItem_move (fim.move());
  96. }
  97. return thee;
  98. } catch (MelderError) {
  99. Melder_throw (U"FilesInMemory not collected in FileInMemorySet.");
  100. }
  101. }
  102. autoFileInMemorySet FileInMemorySet_extractFiles (FileInMemorySet me, kMelder_string which, conststring32 criterion) {
  103. try {
  104. autoFileInMemorySet thee = Thing_new (FileInMemorySet);
  105. for (integer ifile = 1; ifile <= my size; ifile ++) {
  106. FileInMemory fim = static_cast <FileInMemory> (my at [ifile]);
  107. if (Melder_stringMatchesCriterion (fim -> d_path.get(), which, criterion, true)) {
  108. autoFileInMemory item = Data_copy (fim);
  109. thy addItem_move (item.move());
  110. }
  111. }
  112. return thee;
  113. } catch (MelderError) {
  114. Melder_throw (me, U": cannot extract files.");
  115. }
  116. }
  117. autoFileInMemorySet FileInMemorySet_listFiles (FileInMemorySet me, kMelder_string which, conststring32 criterion) {
  118. try {
  119. autoFileInMemorySet thee = Thing_new (FileInMemorySet);
  120. for (integer ifile = 1; ifile <= my size; ifile ++) {
  121. FileInMemory fim = static_cast<FileInMemory> (my at [ifile]);
  122. if (Melder_stringMatchesCriterion (fim -> d_path.get(), which, criterion, true)) {
  123. thy addItem_ref (fim);
  124. }
  125. }
  126. return thee;
  127. } catch (MelderError) {
  128. Melder_throw (me, U": cannot extract files.");
  129. }
  130. }
  131. void FileInMemorySet_showAsCode (FileInMemorySet me, conststring32 name, integer numberOfBytesPerLine) {
  132. autoMelderString one_fim;
  133. MelderInfo_writeLine (U"#include \"FileInMemorySet.h\"");
  134. MelderInfo_writeLine (U"#include \"melder.h\"\n");
  135. MelderInfo_writeLine (U"autoFileInMemorySet create_", name, U" () {");
  136. MelderInfo_writeLine (U"\ttry {");
  137. MelderInfo_writeLine (U"\t\tautoFileInMemorySet me = FileInMemorySet_create ();");
  138. for (integer ifile = 1; ifile <= my size; ifile ++) {
  139. FileInMemory fim = (FileInMemory) my at [ifile];
  140. MelderString_copy (& one_fim, name, ifile);
  141. FileInMemory_showAsCode (fim, one_fim.string, numberOfBytesPerLine);
  142. MelderInfo_writeLine (U"\t\tmy addItem_move (", one_fim.string, U".move());\n");
  143. }
  144. MelderInfo_writeLine (U"\t\treturn me;");
  145. MelderInfo_writeLine (U"\t} catch (MelderError) {");
  146. MelderInfo_writeLine (U"\t\tMelder_throw (U\"FileInMemorySet not created.\");");
  147. MelderInfo_writeLine (U"\t}");
  148. MelderInfo_writeLine (U"}\n\n");
  149. }
  150. void FileInMemorySet_showOneFileAsCode (FileInMemorySet me, integer index, conststring32 name, integer numberOfBytesPerLine)
  151. {
  152. if (index < 1 || index > my size) return;
  153. MelderInfo_writeLine (U"#include \"FileInMemory.h\"");
  154. MelderInfo_writeLine (U"#include \"melder.h\"\n");
  155. MelderInfo_writeLine (U"static autoFileInMemory create_new_object () {");
  156. MelderInfo_writeLine (U"\ttry {");
  157. autoMelderString one_fim;
  158. FileInMemory fim = (FileInMemory) my at [index];
  159. MelderString_append (& one_fim, name, index);
  160. FileInMemory_showAsCode (fim, U"me", numberOfBytesPerLine);
  161. MelderInfo_writeLine (U"\t\treturn me;");
  162. MelderInfo_writeLine (U"\t} catch (MelderError) {");
  163. MelderInfo_writeLine (U"\t\tMelder_throw (U\"FileInMemory not created.\");");
  164. MelderInfo_writeLine (U"\t}");
  165. MelderInfo_writeLine (U"}\n\n");
  166. MelderInfo_writeLine (U"autoFileInMemory ", name, U" = create_new_object ();");
  167. }
  168. integer FileInMemorySet_getIndexFromId (FileInMemorySet me, conststring32 id) {
  169. integer index = 0;
  170. for (integer i = 1; i <= my size; i ++) {
  171. FileInMemory fim = (FileInMemory) my at [i];
  172. if (Melder_equ (id, fim -> d_id.get())) {
  173. index = i;
  174. break;
  175. }
  176. }
  177. return index;
  178. }
  179. integer FileInMemorySet_lookUp (FileInMemorySet me, conststring32 path) {
  180. integer index = 0;
  181. for (integer i = 1; i <= my size; i ++) {
  182. FileInMemory fim = (FileInMemory) my at [i];
  183. if (Melder_equ (path, fim -> d_path.get())) {
  184. index = i;
  185. break;
  186. }
  187. }
  188. return index;
  189. }
  190. integer FileInMemorySet_findNumberOfMatches_path (FileInMemorySet me, kMelder_string which, conststring32 criterion) {
  191. integer numberOfMatches = 0;
  192. for (integer ifile = 1; ifile <= my size; ifile ++) {
  193. FileInMemory fim = static_cast <FileInMemory> (my at [ifile]);
  194. if (Melder_stringMatchesCriterion (fim -> d_path.get(), which, criterion, true)) {
  195. numberOfMatches ++;
  196. }
  197. }
  198. return numberOfMatches;
  199. }
  200. bool FileInMemorySet_hasDirectory (FileInMemorySet me, conststring32 name) {
  201. bool match = false;
  202. autoMelderString regex;
  203. for (integer i = 1; i <= my size; i ++) {
  204. FileInMemory fim = (FileInMemory) my at [i];
  205. MelderString_append (& regex, U".*/", name, U"/.*");
  206. if (Melder_stringMatchesCriterion (fim -> d_path.get(), kMelder_string :: MATCH_REGEXP, regex.string, true)) {
  207. match = true;
  208. break;
  209. }
  210. MelderString_empty (& regex);
  211. }
  212. return match;
  213. }
  214. autoStrings FileInMemorySet_to_Strings_id (FileInMemorySet me) {
  215. try {
  216. autoStrings thee = Thing_new (Strings);
  217. thy strings = autostring32vector (my size);
  218. thy numberOfStrings = 0;
  219. for (integer ifile = 1; ifile <= my size; ifile ++) {
  220. FileInMemory fim = (FileInMemory) my at [ifile];
  221. thy strings [ifile] = Melder_dup_f (fim -> d_id.get());
  222. thy numberOfStrings ++;
  223. }
  224. return thee;
  225. } catch (MelderError) {
  226. Melder_throw (U"No Strings created from FilesinMemory.");
  227. }
  228. }
  229. char * FileInMemorySet_getCopyOfData (FileInMemorySet me, conststring32 id, integer *numberOfBytes) {
  230. *numberOfBytes = 0;
  231. integer index = FileInMemorySet_getIndexFromId (me, id);
  232. if (index == 0) {
  233. return nullptr;
  234. }
  235. FileInMemory fim = (FileInMemory) my at [index];
  236. char *data = (char *) _Melder_malloc (fim -> d_numberOfBytes + 1);
  237. if (! data || ! memcpy (data, fim -> d_data, fim -> d_numberOfBytes)) {
  238. //Melder_appendError (U"No memory for dictionary.");
  239. return nullptr;
  240. }
  241. data [fim -> d_numberOfBytes] = '\0';
  242. *numberOfBytes = fim -> d_numberOfBytes;
  243. return data;
  244. }
  245. const char * FileInMemorySet_getData (FileInMemorySet me, conststring32 id, integer *numberOfBytes) {
  246. *numberOfBytes = 0;
  247. integer index = FileInMemorySet_getIndexFromId (me, id);
  248. if (index == 0) {
  249. return nullptr;
  250. }
  251. FileInMemory fim = (FileInMemory) my at [index];
  252. *numberOfBytes = fim -> d_numberOfBytes;
  253. return reinterpret_cast<const char *> (fim -> d_data);
  254. }
  255. /* End of file FileInMemorySet.cpp */