-
Notifications
You must be signed in to change notification settings - Fork 6
/
H5.cpp
134 lines (114 loc) · 3.41 KB
/
H5.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
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
131
132
133
134
/*
copyright 2022 @elodream
H5assembler is a software made by elodream which help all frontend devellopper and integrator to
create a static page in html without any repetitions
it will guarantee you to be able to easily interer your frontend designs in websites
*/
typedef void *yyscan_t;
#include "h5parse.hxx"
#include "h5lex.hpp"
#include <iostream>
#include <fstream>
using namespace std;
typedef void *yyscan_t;
extern string initialdata;
extern string pdata;
extern string basedir;
extern bool loop;
string compile(string content);
string compilefile(string path);
int runwithargs(int argc, char **argv);
int compilewithh5m(char **argv, int argc);
int runwithoutarg(char **argv);
int saveoutput(string compileddata, string outputpath = "");
// our main function
int main(int argc, char **argv)
{
int c = (argc > 1) ? runwithargs(argc, argv) : runwithoutarg(argv); // if there are arguments run with them
// don't quit the app at the end of assembly
return (c);
}
// run h5A by using arguments
int runwithargs(int argc, char **argv)
{
if (string(argv[1]) == "-m")
{
if (argc < 3)
{
// will not assemble a file using a H5maker file
cout << "no H5maker file specified" << endl;
}
else
{
compilewithh5m(argv, argc);
}
}
else
{
if (argc > 2)
{
for (int i = 2; i < argc; i++)
saveoutput(compilefile(argv[1]), argv[i]);
}
else
saveoutput(compilefile(argv[1]));
}
system("pause");
return EXIT_SUCCESS;
}
// assemble a string
string compile(string content)
{
do
{
loop = false;
yyscan_t *scan = new yyscan_t;
pdata.clear();
yylex_init(scan);
YY_BUFFER_STATE buf = yy_scan_bytes(content.c_str(), strlen(content.c_str()), *scan);
yyparse(*scan);
yylex_destroy(*scan);
content = pdata;
} while (loop == true);
pdata.erase();
return content;
}
// assemble file
string compilefile(string path)
{
string data;
ifstream inputfile(path, ios::in | ios::binary | ios::ate);
if (inputfile.is_open())
{
int length = inputfile.tellg();
inputfile.seekg(0, std::ios::beg);
char *buffer = new char[length + 1]; // allocate memory for a buffer of appropriate dimension
inputfile.read(buffer, length); // read the whole file into the buffer
inputfile.close();
cout << "start assembly : " << path << endl;
const size_t last_slash_idx = path.rfind('\\');
if (std::string::npos != last_slash_idx)
{
basedir = path.substr(0, last_slash_idx);
}
cout << "directory is :" << basedir << endl;
string data = buffer;
free(buffer);
inputfile.close();
return compile(data);
}
cout << "cannot find file (" << path << ")" << endl;
return "";
}
// save assembled file to a specified path
int saveoutput(string compileddata, string outputpath)
{
outputpath = (outputpath == "") ? "output.html" : outputpath;
ofstream outputfile(outputpath, ios::out | ios::binary);
outputfile.write(compileddata.c_str(), compileddata.size());
cout
<< "\noperation terminated successfuly , output at : "
<< outputpath << endl;
outputfile.close();
return 0;
}