fbxfile.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /****************************************************************************************
  2. Copyright (C) 2015 Autodesk, Inc.
  3. All rights reserved.
  4. Use of this software is subject to the terms of the Autodesk license agreement
  5. provided at the time of installation or download, or which otherwise accompanies
  6. this software in either electronic or hard copy form.
  7. ****************************************************************************************/
  8. //! \file fbxfile.h
  9. #ifndef _FBXSDK_CORE_BASE_FILE_H_
  10. #define _FBXSDK_CORE_BASE_FILE_H_
  11. #include <fbxsdk/fbxsdk_def.h>
  12. #include <fbxsdk/core/base/fbxstring.h>
  13. #include <fbxsdk/fbxsdk_nsbegin.h>
  14. class FbxStream;
  15. /**
  16. Class for interfacing with files, providing a similar interface for files independant of the OS or filesystem.
  17. */
  18. class FBXSDK_DLL FbxFile
  19. {
  20. public:
  21. enum EMode {eNone, eReadOnly, eReadWrite, eCreateWriteOnly, eCreateReadWrite, eCreateAppend};
  22. enum ESeekPos {eBegin, eCurrent, eEnd};
  23. FbxFile();
  24. virtual ~FbxFile();
  25. /** Opens a file on disk using the specified read/write mode.
  26. * \param pFileName_UTF8 Filename in UTF8 (compatible with ASCII)
  27. * \param pMode Mode in which to open the file, e.g. eReadOnly, eCreateReadWrite, etc.
  28. * \param pBinary Whether the file is to be opened in binary or text mode.
  29. * \return True if opening is successful.
  30. */
  31. virtual bool Open(const char* pFileName_UTF8, const EMode pMode=eCreateReadWrite, const bool pBinary=true);
  32. /** Opens a file from a data stream using the specified read/write mode.
  33. * \param pStream Stream instance with which the file will be read/written
  34. * \param pStreamData User-defined data to pass as a parameter to the stream's Open() method.
  35. * \param pMode Deprecated/Unused.
  36. * \return True if opening is successful.
  37. */
  38. virtual bool Open(FbxStream* pStream, void* pStreamData, const char* pMode);
  39. /** Closes a file, freeing its handle.
  40. * \return True if closing is successful.
  41. */
  42. virtual bool Close();
  43. /** Seek to a specific position in the file, starting from either beginning, current position or end
  44. * \param pOffset Offset to seek to (advance the file position cursor) starting from pSeekPos
  45. * \param pSeekPos Starting position from which to seek to. Beginning, current position or end.
  46. */
  47. virtual void Seek(const FbxInt64 pOffset, const ESeekPos pSeekPos=eBegin);
  48. /** Returns the position at which the file cursor currently is. For example, will be ==0 for beginning and ==FileSize for end.
  49. * \return The position at which the file cursor currently is.
  50. */
  51. virtual FbxInt64 Tell() const;
  52. /** Read a part of the file into a buffer
  53. * \param pDstBuf Pre-allocated buffer in which to read data
  54. * \param pSize Size of the data chunk to be read in bytes
  55. * \return Number of bytes read.
  56. */
  57. virtual size_t Read(void* pDstBuf, const size_t pSize);
  58. /** Read a part of the file as a string into a buffer
  59. * \param pDstBuf Pre-allocated buffer in which to read the string
  60. * \param pDstSize Size of the data chunk to be read in characters
  61. * \param pStopAtFirstWhiteSpace If true, will stop reading at first white space, otherwise it will stop at the first line feed (\n)
  62. * \return Pointer on the data read. Equivalent to parameter pDstBuf
  63. */
  64. virtual char* ReadString(char* pDstBuf, const size_t pDstSize, bool pStopAtFirstWhiteSpace=false);
  65. /** Write a buffer to an opened file
  66. * \param pSrcBuf Pre-allocated buffer from which to write data
  67. * \param pSize Size of the data chunk to be written in bytes
  68. * \return Number of bytes written.
  69. */
  70. virtual size_t Write(const void* pSrcBuf, const size_t pSize);
  71. /** Write a formatted string to an opened file
  72. * \param pFormat Pre-allocated format buffer from which to write data
  73. * \param ... Variable number of arguments describing the values in the previous parameter.
  74. * \return True if data was successfully written
  75. */
  76. virtual bool WriteFormat(const char* pFormat, ...);
  77. /** Modify the size of a file. Null characters ('\0') are appended if the file is extended.
  78. * If the file is truncated, all data from the end of the shortened file to the original length of the file is lost.
  79. * Please note that this function considers the current file cursor as the beginning of the file.
  80. * It is therefore required to use Seek(0) prior to calling it if we want the size specified by the
  81. * pSize parameter to be absolute.
  82. * \param pSize New desired file size
  83. * \return True if file was successfully truncated
  84. */
  85. virtual bool Truncate(const FbxInt64 pSize);
  86. /** Checks whether the current file cursor position is at the end of file.
  87. * \return True if the cursor is at the end of file, false otherwise.
  88. */
  89. virtual bool EndOfFile() const;
  90. /** Gets the size of the currently opened file.
  91. * \return File size
  92. */
  93. virtual FbxInt64 GetSize();
  94. /** Unused function in this default implementation. Must be implemented by memory files.
  95. * \param pMemPtr Unused
  96. * \param pSize Unused
  97. */
  98. virtual void GetMemoryFileInfo(void** pMemPtr, size_t pSize);
  99. /** Checks whether the file is currently opened.
  100. * \return True if file is opened, false otherwise
  101. */
  102. bool IsOpen() const;
  103. /** Checks whether the file is currently opened with a user-provided streaming interface instead of just the file name
  104. * \return True if file has been opened with a stream interface, false otherwise
  105. */
  106. bool IsStream() const;
  107. /** Returns the full file path name, as provided when opening it.
  108. * \return File full path
  109. */
  110. const char* GetFilePathName() const;
  111. /** Returns the mode with which the file was opened, when calling the Open() method.
  112. * \return Mode with which the file was opened
  113. */
  114. EMode GetFileMode() const;
  115. /** Returns last encountered error when performing any operation on the file.
  116. * \return Last error code
  117. */
  118. int GetLastError();
  119. /** Resets the current error code and the end of file indicator of the opened file
  120. */
  121. void ClearError();
  122. protected:
  123. FILE* mFilePtr;
  124. FbxStream* mStreamPtr;
  125. bool mIsOpen;
  126. bool mIsStream;
  127. EMode mMode;
  128. FbxString mFileName;
  129. };
  130. class FBXSDK_DLL FbxFileUtils
  131. {
  132. public:
  133. /** Delete a file from disk.
  134. * \param pFileName_UTF8 The file to be deleted.
  135. * \return True if delete is successful.
  136. */
  137. static bool Delete(const char* pFileName_UTF8);
  138. /** Rename a file on disk.
  139. * \param pFileName_UTF8 The file to be renamed.
  140. * \param pNewName_UTF8 The new file name upon rename.
  141. * \return True if rename is successful.
  142. */
  143. static bool Rename(const char* pFileName_UTF8, const char* pNewName_UTF8);
  144. /** Copy one file's content to another file (if the destination file not exist, it will be created).
  145. * \param pDestination_UTF8 The destination file path
  146. * \param pSource_UTF8 The source file path
  147. * \return Return true if copy is successfully.
  148. */
  149. static bool Copy(const char* pDestination_UTF8, const char* pSource_UTF8);
  150. //! Get given file's size.
  151. static FbxInt64 Size(const char* pFilePath_UTF8);
  152. /** Find if the specified file exist.
  153. * \param pFilePath_UTF8 The file path to test against.
  154. * \return Returns true if the file exist.
  155. */
  156. static bool Exist(const char* pFilePath_UTF8);
  157. /** Find if the specified file is in read-only mode.
  158. * \param pFilePath_UTF8 The file path to test against.
  159. * \return Returns true if the file is in read-only mode.
  160. */
  161. static bool IsReadOnly(const char* pFilePath_UTF8);
  162. // We return a KLong that in fact is a cast of a time_t.
  163. //! Get given file's last date.
  164. static FbxLong GetLastDate(const char* pPath_UTF8);
  165. //! Set the given file's last date as the given date.
  166. static bool SetLastDate(const char* pPath_UTF8, FbxLong pTime);
  167. /** Get some content of a file.
  168. * \param pStr The content get from file.
  169. * \param pSize The size of content.
  170. * \param pStream The opened stream of file.
  171. */
  172. static char* FGets(char* pStr, int pSize, FILE* pStream);
  173. };
  174. template<class T> inline const T FbxSwab(const T x)
  175. {
  176. switch( sizeof(x) )
  177. {
  178. case 2:
  179. {
  180. FbxUInt8 t[2];
  181. t[0] = ((FbxUInt8*)&x)[1];
  182. t[1] = ((FbxUInt8*)&x)[0];
  183. return *(T*)&t;
  184. }
  185. case 4:
  186. {
  187. FbxUInt8 t[4];
  188. t[0] = ((FbxUInt8*)&x)[3];
  189. t[1] = ((FbxUInt8*)&x)[2];
  190. t[2] = ((FbxUInt8*)&x)[1];
  191. t[3] = ((FbxUInt8*)&x)[0];
  192. return *(T*)&t;
  193. }
  194. case 8:
  195. {
  196. FbxUInt8 t[8];
  197. t[0] = ((FbxUInt8*)&x)[7];
  198. t[1] = ((FbxUInt8*)&x)[6];
  199. t[2] = ((FbxUInt8*)&x)[5];
  200. t[3] = ((FbxUInt8*)&x)[4];
  201. t[4] = ((FbxUInt8*)&x)[3];
  202. t[5] = ((FbxUInt8*)&x)[2];
  203. t[6] = ((FbxUInt8*)&x)[1];
  204. t[7] = ((FbxUInt8*)&x)[0];
  205. return *(T*)&t;
  206. }
  207. default:
  208. return x;
  209. }
  210. }
  211. #include <fbxsdk/fbxsdk_nsend.h>
  212. #endif /* _FBXSDK_CORE_BASE_FILE_H_ */