FileInMemory.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* FileInMemory.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 "FileInMemory.h"
  19. #include "Strings_.h"
  20. #include "oo_DESTROY.h"
  21. #include "FileInMemory_def.h"
  22. #include "oo_COPY.h"
  23. #include "FileInMemory_def.h"
  24. #include "oo_EQUAL.h"
  25. #include "FileInMemory_def.h"
  26. #include "oo_CAN_WRITE_AS_ENCODING.h"
  27. #include "FileInMemory_def.h"
  28. #include "oo_WRITE_TEXT.h"
  29. #include "FileInMemory_def.h"
  30. #include "oo_READ_TEXT.h"
  31. #include "FileInMemory_def.h"
  32. #include "oo_WRITE_BINARY.h"
  33. #include "FileInMemory_def.h"
  34. #include "oo_READ_BINARY.h"
  35. #include "FileInMemory_def.h"
  36. #include "oo_DESCRIPTION.h"
  37. #include "FileInMemory_def.h"
  38. Thing_implement (FileInMemory, Daata, 0);
  39. void structFileInMemory :: v_info () {
  40. our structDaata :: v_info ();
  41. MelderInfo_writeLine (U"File name: ", our d_path.get());
  42. MelderInfo_writeLine (U"Id: ", our d_id.get());
  43. MelderInfo_writeLine (U"Number of bytes: ", our d_numberOfBytes);
  44. }
  45. autoFileInMemory FileInMemory_create (MelderFile file) {
  46. try {
  47. Melder_require (MelderFile_readable (file), U"File is not readable.");
  48. integer length = MelderFile_length (file);
  49. Melder_require (length > 0, U"File should not be empty.");
  50. autoFileInMemory me = Thing_new (FileInMemory);
  51. my d_path = Melder_dup (file -> path);
  52. my d_id = Melder_dup (MelderFile_name (file));
  53. my d_numberOfBytes = length;
  54. my _dontOwnData = false;
  55. my d_data = NUMvector <uint8> (0, my d_numberOfBytes); // includes room for a final null byte in case the file happens to contain text
  56. MelderFile_open (file);
  57. for (integer i = 0; i < my d_numberOfBytes; i++) {
  58. unsigned int number = bingetu8 (file -> filePointer);
  59. my d_data[i] = number;
  60. }
  61. my d_data[my d_numberOfBytes] = 0; // one extra
  62. MelderFile_close (file);
  63. return me;
  64. } catch (MelderError) {
  65. Melder_throw (U"FileInMemory not created from \"", Melder_fileToPath (file), U"\".");
  66. }
  67. }
  68. autoFileInMemory FileInMemory_createWithData (integer numberOfBytes, const char *data, bool isStaticData, conststring32 path, conststring32 id) {
  69. try {
  70. autoFileInMemory me = Thing_new (FileInMemory);
  71. my d_path = Melder_dup (path);
  72. my d_id = Melder_dup (id);
  73. my d_numberOfBytes = numberOfBytes;
  74. if (isStaticData) {
  75. my _dontOwnData = true; // we cannot dispose of the data!
  76. my d_data = reinterpret_cast<unsigned char *> (const_cast<char *> (data)); // ... just a link
  77. } else {
  78. my _dontOwnData = false;
  79. my d_data = NUMvector <unsigned char> ((integer) 0, numberOfBytes);
  80. NUMvector_copyElements <unsigned char> (reinterpret_cast<unsigned char *> (const_cast<char *> (data)), my d_data, 0, numberOfBytes);
  81. }
  82. return me;
  83. } catch (MelderError) {
  84. Melder_throw (U"FileInMemory not created from data.");
  85. }
  86. }
  87. void FileInMemory_setId (FileInMemory me, conststring32 newId) {
  88. my d_id = Melder_dup (newId);
  89. }
  90. void FileInMemory_showAsCode (FileInMemory me, conststring32 name, integer numberOfBytesPerLine)
  91. {
  92. if (numberOfBytesPerLine < 1) {
  93. numberOfBytesPerLine = 20;
  94. }
  95. MelderInfo_writeLine (U"\t\tstatic unsigned char ", name, U"_data[", my d_numberOfBytes+1, U"] = {");
  96. for (integer i = 0; i < my d_numberOfBytes; i++) {
  97. unsigned char number = my d_data [i];
  98. MelderInfo_write ((i % numberOfBytesPerLine == 0 ? U"\t\t\t" : U""), number, U",",
  99. i % numberOfBytesPerLine == numberOfBytesPerLine - 1 ? U"\n" : U" ");
  100. }
  101. MelderInfo_writeLine ((my d_numberOfBytes - 1) % numberOfBytesPerLine == (numberOfBytesPerLine - 1) ? U"\t\t\t0};" : U"0};");
  102. MelderInfo_write (U"\t\tautoFileInMemory ", name, U" = FileInMemory_createWithData (");
  103. MelderInfo_writeLine (my d_numberOfBytes, U", reinterpret_cast<const char *> (&",
  104. name, U"_data), true, \n\t\t\tU\"", my d_path.get(), U"\", \n\t\t\tU\"", my d_id.get(), U"\");");
  105. }
  106. /* End of file FileInMemory.cpp */