File.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Copyright (c) 2006-2015 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "File.h"
  21. namespace love
  22. {
  23. namespace filesystem
  24. {
  25. File::~File()
  26. {
  27. }
  28. FileData *File::read(int64 size)
  29. {
  30. bool isopen = isOpen();
  31. if (!isopen && !open(MODE_READ))
  32. throw love::Exception("Could not read file %s.", getFilename().c_str());
  33. int64 max = getSize();
  34. int64 cur = tell();
  35. size = (size == ALL) ? max : size;
  36. if (size < 0)
  37. throw love::Exception("Invalid read size.");
  38. // Clamping because the file offset may be in a weird position.
  39. if (cur < 0)
  40. cur = 0;
  41. else if (cur > max)
  42. cur = max;
  43. if (cur + size > max)
  44. size = max - cur;
  45. FileData *fileData = new FileData(size, getFilename());
  46. int64 bytesRead = read(fileData->getData(), size);
  47. if (bytesRead < 0 || (bytesRead == 0 && bytesRead != size))
  48. {
  49. delete fileData;
  50. throw love::Exception("Could not read from file.");
  51. }
  52. if (bytesRead < size)
  53. {
  54. FileData *tmpFileData = new FileData(bytesRead, getFilename());
  55. memcpy(tmpFileData->getData(), fileData->getData(), (size_t) bytesRead);
  56. fileData->release();
  57. fileData = tmpFileData;
  58. }
  59. if (!isopen)
  60. close();
  61. return fileData;
  62. }
  63. bool File::write(const Data *data, int64 size)
  64. {
  65. return write(data->getData(), (size == ALL) ? data->getSize() : size);
  66. }
  67. std::string File::getExtension() const
  68. {
  69. const std::string &filename = getFilename();
  70. std::string::size_type idx = filename.rfind('.');
  71. if (idx != std::string::npos)
  72. return filename.substr(idx+1);
  73. else
  74. return std::string();
  75. }
  76. bool File::getConstant(const char *in, Mode &out)
  77. {
  78. return modes.find(in, out);
  79. }
  80. bool File::getConstant(Mode in, const char *&out)
  81. {
  82. return modes.find(in, out);
  83. }
  84. bool File::getConstant(const char *in, BufferMode &out)
  85. {
  86. return bufferModes.find(in, out);
  87. }
  88. bool File::getConstant(BufferMode in, const char *&out)
  89. {
  90. return bufferModes.find(in, out);
  91. }
  92. StringMap<File::Mode, File::MODE_MAX_ENUM>::Entry File::modeEntries[] =
  93. {
  94. {"c", File::MODE_CLOSED},
  95. {"r", File::MODE_READ},
  96. {"w", File::MODE_WRITE},
  97. {"a", File::MODE_APPEND},
  98. };
  99. StringMap<File::Mode, File::MODE_MAX_ENUM> File::modes(File::modeEntries, sizeof(File::modeEntries));
  100. StringMap<File::BufferMode, File::BUFFER_MAX_ENUM>::Entry File::bufferModeEntries[] =
  101. {
  102. {"none", File::BUFFER_NONE},
  103. {"line", File::BUFFER_LINE},
  104. {"full", File::BUFFER_FULL},
  105. };
  106. StringMap<File::BufferMode, File::BUFFER_MAX_ENUM> File::bufferModes(File::bufferModeEntries, sizeof(File::bufferModeEntries));
  107. } // filesystem
  108. } // love