You.i Engine
YiUtilities.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_UTILITIES_H_
3 #define _YI_UTILITIES_H_
4 
5 #include "framework/YiPredef.h"
7 #include "utility/YiString.h"
9 
10 #include <istream>
11 
17 bool YiGetFileName(char &pFileName, char *pFilter);
18 
20 
21 bool YiMakeDirectory(const CYIString &path);
22 
23 bool YiDeleteFile(const CYIString &path);
24 bool YiDeleteFile(const char *pPath);
25 
26 bool YiCopyFile(const char *pSourcePath, const char *pDestinationPath);
27 
32 bool YiFilesAreIdentical(const char *pReferencePath, const char *pTestPath);
33 
34 /* YI_SWAP
35  * Swaps a and b using temp storage t *
36  */
37 template<typename YI_SWAP_TYPE>
38 inline void YI_SWAP(YI_SWAP_TYPE &a, YI_SWAP_TYPE &b)
39 {
40  YI_SWAP_TYPE t = a;
41  a = b;
42  b = t;
43 }
44 
45 YI_EXTERNINLINE uint16_t RandomWORD(void);
46 YI_EXTERNINLINE int32_t YI_MIN4(int32_t a, int32_t b, int32_t c, int32_t d);
47 YI_EXTERNINLINE int32_t YI_MAX4(int32_t a, int32_t b, int32_t c, int32_t d);
48 
49 bool CompareNames(wchar_t *Name, wchar_t *Compare);
50 
51 YI_FILE *YI_FOPEN(const char *pPath, const char *pMode);
52 YI_FILE *YI_WFOPEN(const wchar_t *pPath, const wchar_t *pMode);
53 void YI_FCLOSE(YI_FILE *pFile);
54 ssize_t YI_FSEEK(YI_FILE *pFile, size_t pos, int32_t mode);
55 size_t YI_FTELL(YI_FILE *pFile);
56 int32_t YI_TRUNCATE(YI_FILE *pFile, int32_t length);
57 
58 uint64_t YiGetTimeuS(void);
59 
60 void YILockFrameRate(int32_t fps);
61 
62 #if defined(YI_WIN32)
63 int32_t GetLine(char *buff, int32_t n, YI_FILE *in);
64 #endif
65 
66 /* YiReadFile
67  * Returns the contents of a file into an allocated buffer
68  * Returned buffer must be freed (YI_FREE)
69  */
70 uint8_t *YiReadFile(YI_FILE *pFile, size_t *pBufferSize, size_t additionalPadding);
71 
75 std::vector<char> YiBase64Encode(CYIStringView data);
76 
80 inline std::vector<char> YiBase64Encode(const char *pData, size_t unInputLength)
81 {
82  return YiBase64Encode(CYIStringView(pData, unInputLength));
83 }
84 
88 std::vector<char> YiBase64Decode(CYIStringView data);
89 
93 inline std::vector<char> YiBase64Decode(const char *pData, size_t unInputLength)
94 {
95  return YiBase64Decode(CYIStringView(pData, unInputLength));
96 }
102 std::vector<char> YiBase16Encode(CYIStringView data);
103 
107 inline std::vector<char> YiBase16Encode(const char *pData, size_t unInputLength)
108 {
109  return YiBase16Encode(CYIStringView(pData, unInputLength));
110 }
111 
115 std::vector<char> YiBase16Decode(CYIStringView str);
116 
120 inline std::vector<char> YiBase16Decode(const char *pData, size_t unInputLength)
121 {
122  return YiBase16Decode(CYIStringView(pData, unInputLength));
123 }
124 
128 constexpr bool YiIsLittleEndian()
129 {
130  return YI_IS_LITTLE_ENDIAN == 1;
131 }
132 
136 constexpr bool YiIsBigEndian()
137 {
138  return YI_IS_LITTLE_ENDIAN != 1;
139 }
140 
146 uint32_t YiFromBigEndian(uint32_t bigEndianValue);
147 
176 template<typename T, size_t N>
177 inline size_t YI_SIZE_OF_ARRAY(const T (&a)[N])
178 {
179  YI_UNUSED(a);
180  YI_UNUSED(sizeof(0 [a])); // This line should cause an error if you pass an object with a user-defined subscript operator
181  return N;
182 }
183 
192 uint64_t YiTimestampToUs(uint64_t timestamp, uint64_t scale);
193 
199 class CYIReadOnlyFILEBuffer : public std::streambuf
200 {
201 public:
202  CYIReadOnlyFILEBuffer(YI_FILE *pFile, size_t bufferSize = 1024);
203 
204  virtual std::streambuf::int_type underflow() override;
205  virtual std::streampos seekpos(std::streampos sp, std::ios_base::openmode which) override;
206  virtual std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode which) override;
207 
208 protected:
210  std::vector<char> m_buffer;
211 };
212 
220 class CYIAssetIStream : public std::istream
221 {
222 public:
223  CYIAssetIStream(const CYIString &file);
224  virtual ~CYIAssetIStream();
225 
226 private:
227  std::filebuf m_buffer;
228 };
229 
232 #endif // _YI_UTILITIES_H_
#define YI_EXTERNINLINE
Definition: YiPredef.h:83
bool YiDeleteFile(const CYIString &path)
CYIString YiGetLocalDirectory()
std::vector< char > YiBase16Decode(CYIStringView str)
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:36
YI_EXTERNINLINE int32_t YI_MAX4(int32_t a, int32_t b, int32_t c, int32_t d)
bool CompareNames(wchar_t *Name, wchar_t *Compare)
uint32_t YiFromBigEndian(uint32_t bigEndianValue)
virtual std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode which) override
void YI_FCLOSE(YI_FILE *pFile)
std::vector< char > YiBase16Encode(CYIStringView data)
std::vector< char > YiBase64Encode(CYIStringView data)
An implementation of std::istream used to access assets.
Definition: YiUtilities.h:220
uint8_t * YiReadFile(YI_FILE *pFile, size_t *pBufferSize, size_t additionalPadding)
virtual std::streampos seekpos(std::streampos sp, std::ios_base::openmode which) override
CYIReadOnlyFILEBuffer(YI_FILE *pFile, size_t bufferSize=1024)
An implementation of std::streambuf backed by a YI_FILE handle. The wrapper uses YI_FREAD, YI_FSEEK and YI_FTELL, and only supports read operations.
Definition: YiUtilities.h:199
bool YiFilesAreIdentical(const char *pReferencePath, const char *pTestPath)
YI_FILE * m_pFile
Definition: YiUtilities.h:209
bool YiMakeDirectory(const CYIString &path)
YI_EXTERNINLINE uint16_t RandomWORD(void)
YI_FILE * YI_WFOPEN(const wchar_t *pPath, const wchar_t *pMode)
uint64_t YiGetTimeuS(void)
void YI_SWAP(YI_SWAP_TYPE &a, YI_SWAP_TYPE &b)
Definition: YiUtilities.h:38
size_t YI_SIZE_OF_ARRAY(const T(&a)[N])
Definition: YiUtilities.h:177
std::vector< char > m_buffer
Definition: YiUtilities.h:210
void YILockFrameRate(int32_t fps)
ssize_t YI_FSEEK(YI_FILE *pFile, size_t pos, int32_t mode)
YI_FILE * YI_FOPEN(const char *pPath, const char *pMode)
#define YI_UNUSED(param)
Definition: YiPredef.h:94
YI_EXTERNINLINE int32_t YI_MIN4(int32_t a, int32_t b, int32_t c, int32_t d)
uint64_t YiTimestampToUs(uint64_t timestamp, uint64_t scale)
int32_t YI_TRUNCATE(YI_FILE *pFile, int32_t length)
#define YI_IS_LITTLE_ENDIAN
Definition: YiPredef.h:41
int ssize_t
Definition: YiPredefWin32.h:131
size_t YI_FTELL(YI_FILE *pFile)
constexpr bool YiIsLittleEndian()
Definition: YiUtilities.h:128
bool YiGetFileName(char &pFileName, char *pFilter)
std::vector< char > YiBase64Decode(CYIStringView data)
bool YiCopyFile(const char *pSourcePath, const char *pDestinationPath)
constexpr bool YiIsBigEndian()
Definition: YiUtilities.h:136
#define YI_FILE
Definition: YiPredefAndroid.h:101
virtual std::streambuf::int_type underflow() override
A lightweight wrapper around a sequence of Unicode characters.
Definition: YiStringView.h:57