Skip to content

Commit d24ac8b

Browse files
authored
Create pip.cpp
1 parent 59d042b commit d24ac8b

File tree

1 file changed

+245
-0
lines changed

1 file changed

+245
-0
lines changed

pip.cpp

+245
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
#include "pip.h"
2+
3+
#include <iostream>
4+
#include <sstream>
5+
#include <istream>
6+
#include <iomanip>
7+
#include <fstream>
8+
#include <sys/stat.h>
9+
#include <algorithm>
10+
11+
12+
inline std::string trim(const std::string &s)
13+
{
14+
auto wsfront=std::find_if_not(s.begin(),s.end(),[](int c){return std::isspace(c);});
15+
auto wsback=std::find_if_not(s.rbegin(),s.rend(),[](int c){return std::isspace(c);}).base();
16+
return (wsback<=wsfront ? std::string() : std::string(wsfront,wsback));
17+
}
18+
19+
void pip::getMetaData(std::string inpfile)
20+
{
21+
enum metadata_state
22+
{
23+
start = 0,
24+
begin,
25+
end
26+
};
27+
28+
std::vector<std::string> tokens;
29+
metadata_state state = start;
30+
std::stringstream ss;
31+
32+
std::ifstream is(inpfile, std::ios::binary);
33+
if (!is)
34+
throw std::runtime_error("Error opening file: " + inpfile);
35+
36+
metaData.clear();
37+
38+
std::string line;
39+
while (std::getline(is, line)) {
40+
41+
if(state == start)
42+
{
43+
if(line.find("BEGIN_JUCE_PIP_METADATA") != std::string::npos)
44+
{
45+
state = begin;
46+
continue;
47+
}
48+
}
49+
else if(state == begin)
50+
{
51+
if(line.find("END_JUCE_PIP_METADATA") != std::string::npos)
52+
{
53+
state = end;
54+
}
55+
else
56+
{
57+
ss << line << "\n";
58+
}
59+
}
60+
}
61+
62+
std::string key;
63+
auto lines = split(ss.str(),'\n');
64+
for(auto &l : lines)
65+
{
66+
l = trim(l);
67+
if(l.find(':') != std::string::npos)
68+
{
69+
auto kv = split(l, ':');
70+
if(!kv.empty())
71+
{
72+
key = kv[0];
73+
metaData[key] = trim(kv[1]);
74+
}
75+
}
76+
else
77+
{
78+
std::string value = metaData[key];
79+
metaData[key] += l;
80+
}
81+
}
82+
83+
dependencies = split(metaData["dependencies"],',');
84+
for(auto &d : dependencies)
85+
{
86+
d = trim(d);
87+
}
88+
89+
exporters = split(metaData["exporters"],',');
90+
for(auto &e : exporters)
91+
{
92+
e = trim(e);
93+
}
94+
95+
moduleFlags = split(metaData["moduleFlags"],',');
96+
for(auto &m : moduleFlags)
97+
{
98+
m = trim(m);
99+
}
100+
}
101+
102+
std::vector<std::string> pip::getDependencies()
103+
{
104+
return dependencies;
105+
}
106+
107+
std::vector<std::string> pip::getExporters()
108+
{
109+
return exporters;
110+
}
111+
112+
std::vector<std::string> pip::getModuleFlags()
113+
{
114+
return moduleFlags;
115+
}
116+
117+
std::string pip::getDescription()
118+
{
119+
return metaData["description"];
120+
}
121+
122+
std::string pip::getMainClass()
123+
{
124+
return metaData["mainClass"];
125+
}
126+
127+
std::string pip::getName()
128+
{
129+
return metaData["name"];
130+
}
131+
132+
std::string pip::getType()
133+
{
134+
return metaData["type"];
135+
}
136+
137+
std::string pip::getVendor()
138+
{
139+
return metaData["vendor"];
140+
}
141+
142+
std::string pip::getVersion()
143+
{
144+
return metaData["version"];
145+
}
146+
147+
std::string pip::getWebsite()
148+
{
149+
return metaData["website"];
150+
}
151+
152+
pip::pip(std::string infile, std::string outpath)
153+
{
154+
static constexpr char DEFAULT_VERSION[] = { '1', '.', '0', '.', '0', 0 };
155+
156+
version = DEFAULT_VERSION;
157+
158+
base_path = infile.substr(0, infile.find_last_of("\\/"));
159+
160+
if(outpath.empty())
161+
{
162+
output_path = base_path;
163+
}
164+
else
165+
{
166+
output_path = outpath;
167+
}
168+
169+
getMetaData(infile);
170+
}
171+
172+
void pip::print()
173+
{
174+
std::cout << "[Dependencies]\n";
175+
for(auto const& item : getDependencies())
176+
{
177+
std::cout << item << std::endl;
178+
}
179+
180+
std::cout << "[Exporters]\n";
181+
for(auto const& item : getExporters())
182+
{
183+
std::cout << item << std::endl;
184+
}
185+
186+
std::cout << "[ModuleFlags]\n";
187+
for(auto const& item : getModuleFlags())
188+
{
189+
std::cout << item << std::endl;
190+
}
191+
192+
std::cout << "[Description]\n";
193+
std::cout << getDescription() << std::endl;
194+
std::cout << "[MainClass]\n";
195+
std::cout << getMainClass() << std::endl;
196+
std::cout << "[Name]\n";
197+
std::cout << getName() << std::endl;
198+
std::cout << "[Type]\n";
199+
std::cout << getType() << std::endl;
200+
std::cout << "[Vendor]\n";
201+
std::cout << getVendor() << std::endl;
202+
std::cout << "[Version]\n";
203+
std::cout << getVersion() << std::endl;
204+
std::cout << "[Website]\n";
205+
std::cout << getWebsite() << std::endl;
206+
}
207+
208+
std::string pip::get_cmake_file()
209+
{
210+
std::string sepd = (base_path.find("\\") != std::string::npos) ? "\\" : "/";
211+
std::string path = output_path + sepd + "CMakeLists.txt";
212+
return path;
213+
}
214+
215+
void pip::gen_cmake()
216+
{
217+
std::ofstream outfile (get_cmake_file(), std::ofstream::binary);
218+
#if 0
219+
outfile << get_header();
220+
outfile << get_dependencies();
221+
outfile << get_cpp_standard();
222+
outfile << get_defines();
223+
outfile << get_include_dirs();
224+
outfile << get_autogen_vars();
225+
outfile << get_resource_files();
226+
outfile << get_source_list();
227+
//outfile << get_source_groups();
228+
outfile << get_executable();
229+
outfile << get_common_options();
230+
outfile << get_target_config();
231+
#endif
232+
outfile.close();
233+
}
234+
235+
std::vector<std::string> pip::split(const std::string& s, char delimiter)
236+
{
237+
std::vector<std::string> tokens;
238+
std::string token;
239+
std::istringstream tokenStream(s);
240+
while (std::getline(tokenStream, token, delimiter))
241+
{
242+
tokens.push_back(token);
243+
}
244+
return tokens;
245+
}

0 commit comments

Comments
 (0)