PVRTResourceFile.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /******************************************************************************
  2. @File PVRTResourceFile.cpp
  3. @Title PVRTResourceFile.cpp
  4. @Version
  5. @Copyright Copyright (C) Imagination Technologies Limited.
  6. @Platform ANSI compatible
  7. @Description Simple resource file wrapper
  8. ******************************************************************************/
  9. #include "PVRTResourceFile.h"
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "PVRTResourceFile.h"
  13. #include "PVRTString.h"
  14. #include "PVRTMemoryFileSystem.h"
  15. CPVRTString CPVRTResourceFile::s_ReadPath;
  16. /*!***************************************************************************
  17. @Function SetReadPath
  18. @Input pszReadPath The path where you would like to read from
  19. @Description Sets the read path
  20. *****************************************************************************/
  21. void CPVRTResourceFile::SetReadPath(const char* const pszReadPath)
  22. {
  23. s_ReadPath = (pszReadPath) ? pszReadPath : "";
  24. }
  25. /*!***************************************************************************
  26. @Function GetReadPath
  27. @Returns The currently set read path
  28. @Description Returns the currently set read path
  29. *****************************************************************************/
  30. CPVRTString CPVRTResourceFile::GetReadPath()
  31. {
  32. return CPVRTString(s_ReadPath);
  33. }
  34. /*!***************************************************************************
  35. @Function CPVRTResourceFile
  36. @Input pszFilename Name of the file you would like to open
  37. @Description Constructor
  38. *****************************************************************************/
  39. CPVRTResourceFile::CPVRTResourceFile(const char* const pszFilename) :
  40. m_bOpen(false),
  41. m_bMemoryFile(false),
  42. m_Size(0),
  43. m_pData(0)
  44. {
  45. CPVRTString Path(s_ReadPath);
  46. Path += pszFilename;
  47. FILE* pFile = fopen(Path.c_str(), "rb");
  48. if (pFile)
  49. {
  50. // Get the file size
  51. fseek(pFile, 0, SEEK_END);
  52. m_Size = ftell(pFile);
  53. fseek(pFile, 0, SEEK_SET);
  54. // read the data, append a 0 byte as the data might represent a string
  55. char* pData = new char[m_Size + 1];
  56. pData[m_Size] = '\0';
  57. size_t BytesRead = fread(pData, 1, m_Size, pFile);
  58. if (BytesRead != m_Size)
  59. {
  60. delete [] pData;
  61. m_Size = 0;
  62. }
  63. else
  64. {
  65. m_pData = pData;
  66. m_bOpen = true;
  67. }
  68. fclose(pFile);
  69. }
  70. if (!m_bOpen)
  71. {
  72. m_bOpen = m_bMemoryFile = CPVRTMemoryFileSystem::GetFile(pszFilename, (const void**)(&m_pData), &m_Size);
  73. }
  74. }
  75. /*!***************************************************************************
  76. @Function CPVRTResourceFile
  77. @Input pData A pointer to the data you would like to use
  78. @Input i32Size The size of the data
  79. @Description Constructor
  80. *****************************************************************************/
  81. CPVRTResourceFile::CPVRTResourceFile(const char* pData, size_t i32Size) :
  82. m_bOpen(true),
  83. m_bMemoryFile(true),
  84. m_Size(i32Size),
  85. m_pData(pData)
  86. {
  87. }
  88. /*!***************************************************************************
  89. @Function ~CPVRTResourceFile
  90. @Description Destructor
  91. *****************************************************************************/
  92. CPVRTResourceFile::~CPVRTResourceFile()
  93. {
  94. Close();
  95. }
  96. /*!***************************************************************************
  97. @Function IsOpen
  98. @Returns true if the file is open
  99. @Description Is the file open
  100. *****************************************************************************/
  101. bool CPVRTResourceFile::IsOpen() const
  102. {
  103. return m_bOpen;
  104. }
  105. /*!***************************************************************************
  106. @Function IsMemoryFile
  107. @Returns true if the file was opened from memory
  108. @Description Was the file opened from memory
  109. *****************************************************************************/
  110. bool CPVRTResourceFile::IsMemoryFile() const
  111. {
  112. return m_bMemoryFile;
  113. }
  114. /*!***************************************************************************
  115. @Function Size
  116. @Returns The size of the opened file
  117. @Description Returns the size of the opened file
  118. *****************************************************************************/
  119. size_t CPVRTResourceFile::Size() const
  120. {
  121. return m_Size;
  122. }
  123. /*!***************************************************************************
  124. @Function DataPtr
  125. @Returns A pointer to the file data
  126. @Description Returns a pointer to the file data
  127. *****************************************************************************/
  128. const void* CPVRTResourceFile::DataPtr() const
  129. {
  130. return m_pData;
  131. }
  132. /*!***************************************************************************
  133. @Function StringPtr
  134. @Returns The file data as a string
  135. @Description Returns the file as a null-terminated string
  136. *****************************************************************************/
  137. const char* CPVRTResourceFile::StringPtr() const
  138. {
  139. return m_pData;
  140. }
  141. /*!***************************************************************************
  142. @Function Close
  143. @Description Closes the file
  144. *****************************************************************************/
  145. void CPVRTResourceFile::Close()
  146. {
  147. if (m_bOpen)
  148. {
  149. if (!m_bMemoryFile)
  150. {
  151. delete [] (char*)m_pData;
  152. }
  153. m_bMemoryFile = false;
  154. m_bOpen = false;
  155. m_pData = 0;
  156. m_Size = 0;
  157. }
  158. }
  159. /****************************************************************************
  160. ** class CPVRTMemoryFileSystem
  161. ****************************************************************************/
  162. CPVRTMemoryFileSystem::CAtExit CPVRTMemoryFileSystem::s_AtExit;
  163. CPVRTMemoryFileSystem::SFileInfo* CPVRTMemoryFileSystem::s_pFileInfo = 0;
  164. int CPVRTMemoryFileSystem::s_i32Capacity = 0;
  165. int CPVRTMemoryFileSystem::s_i32NumFiles = 0;
  166. /*!***************************************************************************
  167. @Function Destructor
  168. @Description Destructor of CAtExit class. Workaround for platforms that
  169. don't support the atexit() function. This deletes any memory
  170. file system data.
  171. *****************************************************************************/
  172. CPVRTMemoryFileSystem::CAtExit::~CAtExit()
  173. {
  174. for (int i = 0; i < CPVRTMemoryFileSystem::s_i32NumFiles; ++i)
  175. {
  176. if (CPVRTMemoryFileSystem::s_pFileInfo[i].bAllocated)
  177. {
  178. delete [] (char*)CPVRTMemoryFileSystem::s_pFileInfo[i].pszFilename;
  179. delete [] (char*)CPVRTMemoryFileSystem::s_pFileInfo[i].pBuffer;
  180. }
  181. }
  182. delete [] CPVRTMemoryFileSystem::s_pFileInfo;
  183. }
  184. CPVRTMemoryFileSystem::CPVRTMemoryFileSystem(const char* pszFilename, const void* pBuffer, size_t Size, bool bCopy)
  185. {
  186. RegisterMemoryFile(pszFilename, pBuffer, Size, bCopy);
  187. }
  188. /*!***************************************************************************
  189. @Function RegisterMemoryFile
  190. @Input pszFilename Name of file to register
  191. @Input pBuffer Pointer to file data
  192. @Input Size File size
  193. @Input bCopy Name and data should be copied?
  194. @Description Registers a block of memory as a file that can be looked up
  195. by name.
  196. *****************************************************************************/
  197. void CPVRTMemoryFileSystem::RegisterMemoryFile(const char* pszFilename, const void* pBuffer, size_t Size, bool bCopy)
  198. {
  199. if (s_i32NumFiles == s_i32Capacity)
  200. {
  201. SFileInfo* pFileInfo = new SFileInfo[s_i32Capacity + 10];
  202. memcpy(pFileInfo, s_pFileInfo, sizeof(SFileInfo) * s_i32Capacity);
  203. delete [] s_pFileInfo;
  204. s_pFileInfo = pFileInfo;
  205. s_i32Capacity += 10;
  206. }
  207. s_pFileInfo[s_i32NumFiles].pszFilename = pszFilename;
  208. s_pFileInfo[s_i32NumFiles].pBuffer = pBuffer;
  209. if (bCopy)
  210. {
  211. char* pszNewFilename = new char[strlen(pszFilename)];
  212. strcpy(pszNewFilename, pszFilename);
  213. s_pFileInfo[s_i32NumFiles].pszFilename = pszNewFilename;
  214. void* pszNewBuffer = new char[Size];
  215. memcpy(pszNewBuffer, pBuffer, Size);
  216. s_pFileInfo[s_i32NumFiles].pBuffer = pszNewBuffer;
  217. }
  218. s_pFileInfo[s_i32NumFiles].Size = Size;
  219. s_pFileInfo[s_i32NumFiles].bAllocated = bCopy;
  220. ++s_i32NumFiles;
  221. }
  222. /*!***************************************************************************
  223. @Function GetFile
  224. @Input pszFilename Name of file to open
  225. @Output ppBuffer Pointer to file data
  226. @Output pSize File size
  227. @Return true if the file was found in memory, false otherwise
  228. @Description Looks up a file in the memory file system by name. Returns a
  229. pointer to the file data as well as its size on success.
  230. *****************************************************************************/
  231. bool CPVRTMemoryFileSystem::GetFile(const char* pszFilename, const void** ppBuffer, size_t* pSize)
  232. {
  233. for (int i = 0; i < s_i32NumFiles; ++i)
  234. {
  235. if (strcmp(s_pFileInfo[i].pszFilename, pszFilename) == 0)
  236. {
  237. if (ppBuffer) *ppBuffer = s_pFileInfo[i].pBuffer;
  238. if (pSize) *pSize = s_pFileInfo[i].Size;
  239. return true;
  240. }
  241. }
  242. return false;
  243. }
  244. /*!***************************************************************************
  245. @Function GetNumFiles
  246. @Return The number of registered files
  247. @Description Getter for the number of registered files
  248. *****************************************************************************/
  249. int CPVRTMemoryFileSystem::GetNumFiles()
  250. {
  251. return s_i32NumFiles;
  252. }
  253. /*!***************************************************************************
  254. @Function GetFilename
  255. @Input i32Index Index of file
  256. @Return A pointer to the filename of the requested file
  257. @Description Looks up a file in the memory file system by name. Returns a
  258. pointer to the file data as well as its size on success.
  259. *****************************************************************************/
  260. const char* CPVRTMemoryFileSystem::GetFilename(int i32Index)
  261. {
  262. if (i32Index < 0 || i32Index > s_i32NumFiles) return 0;
  263. return s_pFileInfo[i32Index].pszFilename;
  264. }
  265. /*****************************************************************************
  266. End of file (PVRTResourceFile.cpp)
  267. *****************************************************************************/