-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileAssetLibrary.cpp
90 lines (82 loc) · 4.03 KB
/
FileAssetLibrary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "wincompat.h"
#include "FileAssetLibrary.h"
#include "objmasterlog.h"
#include <istream>
#include <fstream>
#include <string>
#include <cstring>
#include <memory>
#include <cerrno> /* for strerror(errno) */
// The maximum size of error messages to print on error logging
#define ERR_MSG_SIZE 512
#ifndef _MSC_VER
// Clang and g++ build support old clang versions without strerror_s
void strerror_secure(char* errMsgHolder, size_t bufLen, int errnum){
strerror_r(errnum, errMsgHolder, bufLen);
}
#else
// Windows build seems to have this
#define strerror_secure strerror_s
#endif
namespace ObjMaster {
std::unique_ptr<std::istream> FileAssetLibrary::getAssetStream(const char *path, const char *assetFileName) const {
// We are just concatenating the two values
const std::string spath(path);
const std::string sassetFileName(assetFileName);
const std::string utf8FullPath = spath + sassetFileName;
OMLOGI("Opening file at %s ...", utf8FullPath.c_str());
OMLOGI(" - path %s ...", spath.c_str());
OMLOGI(" - assetFileName %s ...", sassetFileName.c_str());
// And returning the ifstream to it
std::unique_ptr<std::istream> assetStream = std::make_unique<std::ifstream>(std::ifstream(
#ifdef _MSC_VER
// Rem.: this can fail if the character cannot be represented with 16bit unicodes and this got translated to winapi that only support 16bit UNICODE spaces....
// Rem.: When cross compiling with MSVC to android and stuff this works at least for ASCII - I could not test what happens in that case :-(
UtfHelper::utf8_to_utf16(utf8FullPath) // MSVC has override for wstring paths - this is not standard however!
#else
utf8FullPath // Every normal operating system handles UTF-8 as-is, so in other cases we just do this
#endif
));
if (assetStream->fail()) {
char errMsgHolder[ERR_MSG_SIZE];
// We only log about failure. The stream we return will act as empty so we will end up parsing an empty obj
// If this is not what you wish for, you can implement your own similar asset library on your own...
strerror_secure(errMsgHolder, ERR_MSG_SIZE, errno);
OMLOGE("...Cannot open file because errno == %d", errno);
OMLOGE("...Cannot open file because: %s", errMsgHolder);
} else {
OMLOGI("...Successfully opened %s%s", path, assetFileName);
}
return assetStream;
}
std::unique_ptr<std::ostream> FileAssetLibrary::getAssetOutputStream(const char *path, const char *assetFileName) const {
// We are just concatenating the two values
const std::string spath(path);
const std::string sassetFileName(assetFileName);
const std::string utf8FullPath = spath + sassetFileName;
OMLOGI("Opening OUTPUT file at %s ...", utf8FullPath.c_str());
OMLOGI(" - path %s ...", spath.c_str());
OMLOGI(" - assetFileName %s ...", sassetFileName.c_str());
// And returning the ifstream to it
std::unique_ptr<std::ostream> assetStream = std::make_unique<std::ofstream>(std::ofstream(
#ifdef _MSC_VER
// Rem.: this can fail if the character cannot be represented with 16bit unicodes and this got translated to winapi that only support 16bit UNICODE spaces....
// Rem.: When cross compiling with MSVC to android and stuff this works at least for ASCII - I could not test what happens in that case :-(
UtfHelper::utf8_to_utf16(utf8FullPath), // MSVC has override for wstring paths - this is not standard however!
#else
utf8FullPath, // Every normal operating system handles UTF-8 as-is, so in other cases we just do this
#endif
std::ios::trunc // better overwrite files as there is very little error handling anyways
));
if (assetStream->fail()) {
char errMsgHolder[ERR_MSG_SIZE];
// We only log about failure. The stream we return will act as empty so we will end up parsing an empty obj
// If this is not what you wish for, you can implement your own similar asset library on your own...
strerror_secure(errMsgHolder, ERR_MSG_SIZE, errno);
OMLOGE("...Cannot open OUTPUT file because: %s", errMsgHolder);
} else {
OMLOGI("...Successfully opened %s%s for OUTPUT!", path, assetFileName);
}
return assetStream;
}
}