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