-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerator.cpp
More file actions
111 lines (106 loc) · 3.48 KB
/
Copy pathgenerator.cpp
File metadata and controls
111 lines (106 loc) · 3.48 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
#include <cstdio>
#include <fstream>
#include <vector>
#include <utility>
#include <string>
#include <type_traits>
#include <cstring>
using namespace std;
const size_t ss = sizeof(size_t);
struct buffer {
char *data, *now, *end;
inline buffer() { data = now = new char[128], end = data + 128; }
inline void add(size_t n) {
now += n;
if(now >= end) [[unlikely]] {
size_t size = now - data, cap = end - data;
do cap <<= 1; while(cap < size);
char *newData = new char[cap];
memcpy(newData, data, size);
data = newData, now = data + size, end = data + cap;
}
}
template<class T> inline void push(const string& s) {
if(is_same<T, string>::value) {
add(ss), *(size_t*)(now - ss) = s.size();
add(s.size()), memcpy(now - s.size(), s.data(), s.size());
} else {
add(sizeof(T));
T res;
if(is_same<T, int>::value) res = stoi(s);
if(is_same<T, long long>::value) res = stoll(s);
if(is_same<T, double>::value) res = stod(s);
*(T*)(now - sizeof(T)) = res;
}
}
};
template<class T> bool readData(int cnt, ifstream& fin, buffer& buf) {
if(cnt == 0) {
if(is_same<T, string>::value) {
string s;
while(1){
s.clear();
char flg=1;
char c;
while(c = fin.get(), c != '\"'){
if(c == '}') return false;
if(c != ' ') flg=0;
}
while(c = fin.get(), c != '\"') s += c;
if(flg) break;
}
buf.push<T>(s);
return true;
} else {
string s;
loop:;
try{
fin >> s;
T res;
if(is_same<T, int>::value) res = stoi(s);
if(is_same<T, long long>::value) res = stoll(s);
if(is_same<T, double>::value) res = stod(s);
}catch(...){
goto loop;
}
buf.push<T>(s);
return true;
}
} else {
size_t size = 0, idx = buf.now - buf.data;
buf.add(ss);
while(fin.get() != '{');
while(readData<T>(cnt - 1, fin, buf)) size++;
*(size_t*)(buf.data + idx) = size;
return true;
}
}
void readData(const string& type, int cnt, ifstream& fin, buffer& buf) {
if(type == "int") readData<int>(cnt, fin, buf);
if(type == "long") readData<long long>(cnt, fin, buf);
if(type == "double") readData<double>(cnt, fin, buf);
if(type == "String") readData<string>(cnt, fin, buf);
}
int main(int argc, const char* argv[]) {
string path = argc == 1 ? "." : argv[1];
path += '/';
ifstream fin(path + "/temp");
vector<pair<string, int>> Parameters;
pair<string, int> Returns;
int pcnt, cnt;
string type;
fin >> pcnt;
while(pcnt--) fin >> type >> cnt, Parameters.emplace_back(type, cnt);
fin >> Returns.first >> Returns.second;
int tcnt;
fin >> tcnt;
for(int k = 1; k <= tcnt; k++) {
buffer in, out;
for(auto&& i : Parameters) readData(i.first, i.second, fin, in);
readData(Returns.first, Returns.second, fin, out);
string name = path + "data/" + to_string(k);
ofstream(name + ".in", ios::binary).write(in.data, in.now - in.data);
ofstream(name + ".out", ios::binary).write(out.data, out.now - out.data);
}
return 0;
}