-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmkcsb.cpp
More file actions
118 lines (108 loc) · 2.89 KB
/
mkcsb.cpp
File metadata and controls
118 lines (108 loc) · 2.89 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
#include <iostream>
#include "editor-support/cocostudio/FlatBuffersSerialize.h"
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
using namespace std;
// http://stackoverflow.com/a/11366985
bool mkpath( std::string path )
{
bool bSuccess = false;
int nRC = ::mkdir( path.c_str(), 0775 );
if( nRC == -1 )
{
switch( errno )
{
case ENOENT:
//parent didn't exist, try to create it
if( mkpath( path.substr(0, path.find_last_of('/')) ) )
//Now, try to create again.
bSuccess = 0 == ::mkdir( path.c_str(), 0775 );
else
bSuccess = false;
break;
case EEXIST:
//Done!
bSuccess = true;
break;
default:
bSuccess = false;
break;
}
}
else
bSuccess = true;
return bSuccess;
}
cocostudio::FlatBuffersSerialize fbs;
void copy(const std::string& root, const std::string& input, const std::string& dest)
{
std::string output = dest + input.substr(root.size());
printf("Copy: %s \n", output.c_str());
std::string dirname = output.substr(0, output.find_last_of('/'));
mkpath(dirname);
ifstream inputStream(input, ios::binary);
ofstream outputStream(output, ios::binary);
outputStream << inputStream.rdbuf();
inputStream.close();
outputStream.close();
}
void serialize(const std::string& root, const std::string& input, const std::string& dest)
{
std::string output = dest + input.substr(root.size());
printf("Process: %s \n", output.c_str());
std::string dirname = output.substr(0, output.find_last_of('/'));
mkpath(dirname);
fbs.serializeFlatBuffersWithXMLFile(input, output);
}
void processDir(const std::string root, const std::string& dirname, const std::string& dest)
{
DIR *dir;
struct dirent *ent;
if((dir = opendir(dirname.c_str())) == NULL){
return;
}
while((ent = readdir(dir)) != NULL){
string fname = ent->d_name;
switch(ent->d_type){
case DT_DIR: {
if (fname == "."){
continue;
}else if(fname == ".."){
continue;
}
stringstream buf;
buf << dirname;
buf << "/";
buf << fname;
processDir(root, buf.str().c_str(), dest);
continue;
}
case DT_REG: {
string extname = fname.substr(fname.find_last_of('.'));
const string& src = dirname + "/" +fname;
if(extname == ".csd"){
serialize(root, src , dest);
}else{
printf("%s", src.c_str());
copy(root, src, dest);
}
continue;
}
}
}
closedir(dir);
}
int main(int argc, char *argv[])
{
if(argc < 2){
cout << "USAGE: " ;
cout << argv[0];
cout << " COCOSTUDIO_DIR";
return -1;
}
auto root = argv[1];
auto dest = argv[2];
processDir(root, root, dest);
}