forked from j-kaltes/Jugglucohelp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml2xml.cpp
72 lines (65 loc) · 1.78 KB
/
html2xml.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
#include <string_view>
#include <iostream>
#include <algorithm>
using namespace std;
#include "inout.h"
void rewritefile(const char *name) {
Readall cont(name);
if(!cont.data()) {
cerr<<name<<" does not exist\n";
return;
}
const char find[]=R"(<body)";
auto comp=[](const char one,const char two)->bool {return tolower(one)==tolower(two);};
const char *found=std::search(cont.begin(),cont.end(),begin(find),end(find)-1,comp);
if(found==cont.end()) {
cerr<<name<<": "<<find<<" not found"<<endl;
return;
}
const char *start=std::find(found,cont.end(),'>');
if(start==cont.end()) {
cerr<<name<<": "<<"> not found\n";
return;
}
start++;
const char findend[]="</body>";
const char *foundend=std::search(start,cont.end(),begin(findend),end(findend)-1,comp);
if(foundend==cont.end()) {
cerr<<name<<": "<<findend<<" not found\n";
return;
}
const char *iter=start;
string_view label(name);
const char strbegin[]=R"(<string name=")";
const char cdata[]=R"(" >
<![CDATA[
)";
const char cend[]=R"(]]>
</string>
)";
write(STDOUT_FILENO,strbegin,sizeof(strbegin)-1);
write(STDOUT_FILENO,label.data(),label.size()-5);
write(STDOUT_FILENO,cdata,sizeof(cdata)-1);
while(true){
// const char *teken=std::find(iter,foundend,'\'');
const char zoek[]= "\\\'";
const char *teken=std::find_first_of(iter,foundend,begin(zoek),end(zoek));
write(STDOUT_FILENO,iter,teken-iter);
if(teken==foundend) {
write(STDOUT_FILENO,cend,sizeof(cend)-1);
return;
}
char teek[3]=R"(\)";
teek[1]=*teken;
write(STDOUT_FILENO,teek,2);
iter=teken+1;
}
}
int main(int argc,char **argv) {
const string_view resstart("<resources>\n");
write(STDOUT_FILENO,resstart.data(),resstart.size());
for(int i=1;i<argc;i++)
rewritefile(argv[i]);
const string_view resend("</resources>\n");
write(STDOUT_FILENO,resend.data(),resend.size());
}