forked from lighttransport/tinyusdz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
130 lines (104 loc) · 3.05 KB
/
main.cc
File metadata and controls
130 lines (104 loc) · 3.05 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <fcntl.h>
#include <unistd.h>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include "tinyusdz.hh"
static std::string GetFileExtension(const std::string &filename) {
if (filename.find_last_of(".") != std::string::npos)
return filename.substr(filename.find_last_of(".") + 1);
return "";
}
static std::string str_tolower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(),
// static_cast<int(*)(int)>(std::tolower) // wrong
// [](int c){ return std::tolower(c); } // wrong
// [](char c){ return std::tolower(c); } // wrong
[](unsigned char c) { return std::tolower(c); } // correct
);
return s;
}
bool LoadModelFromString(const std::vector<uint8_t> &content,
const std::string &filename, tinyusdz::Stage *stage) {
std::string ext = str_tolower(GetFileExtension(filename));
std::string warn;
std::string err;
bool ret = tinyusdz::LoadUSDFromMemory(content.data(), content.size(), filename, stage, &warn, &err);
if (!warn.empty()) {
std::cerr << "WARN : " << warn << "\n";
}
if (!err.empty()) {
std::cerr << "ERR : " << err << "\n";
}
if (!ret) {
std::cerr << "Failed to load USD(USDA, USDC or USDZ) file: " << filename << "\n";
return false;
}
return true;
}
#if 0
// It looks WASI does not provide fopen(), so use open() to read file content.
std::vector<uint8_t> ReadFile(const char *arg) {
std::ostringstream ss;
ssize_t n;
char buf[BUFSIZ];
int in = open(arg, O_RDONLY);
if (in < 0) {
fprintf(stderr, "error opening input %s: %s\n", arg, strerror(errno));
exit(1);
}
while ((n = read(in, buf, BUFSIZ)) > 0) {
char *ptr = buf;
ss.write(ptr, n);
}
std::string s = ss.str();
size_t len = s.size();
std::vector<uint8_t> dst;
dst.resize(len);
//std::cout << "input = " << s << "\n";
memcpy(dst.data(), s.data(), len);
return dst;
}
#endif
int main(int argc, char **argv) {
std::cout << "bora\n";
if (argc > 1) {
std::string filename = argv[1];
#if 1
tinyusdz::Stage stage;
{
std::string warn;
std::string err;
bool ret = tinyusdz::LoadUSDFromFile(filename, &stage, &warn, &err);
if (!warn.empty()) {
std::cerr << "WARN : " << warn << "\n";
}
if (!err.empty()) {
std::cerr << "ERR : " << err << "\n";
}
if (!ret) {
return EXIT_FAILURE;
}
}
#else
//std::vector<uint8_t> content = ReadFile(filename.c_str());
//if (content.empty()) {
// std::cerr << "File is empty or failed to read: " << filename << "\n";
//}
//tinyusdz::Stage stage;
//bool ret = LoadModelFromString(content, filename, &stage);
//if (!ret) {
// std::cerr << "Load failed.\n";
// return EXIT_FAILURE;
//}
#endif
std::cout << "Load oK\n";
std::string outs = stage.ExportToString();
std::cout << outs;
} else {
std::cout << "Need input USD filename(.usda/.usdc/.usdz)\n";
}
return 0;
}