-
Notifications
You must be signed in to change notification settings - Fork 0
/
getinfo.cpp
272 lines (249 loc) · 9.17 KB
/
getinfo.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* Copyright (C) Johnny Saenz
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. */
#include "getinfo.h"
namespace JSApm
{
tags _tags_opts[] =
{
{"Version", MAX_VER_LEN},
{"Section", MAX_SECTION_LEN},
{"Depends", MAX_DEP_LEN},
{"Description", MAX_DSCR_LEN},
{0, 0}
};
// We avoided problems of relink of memory.
const tags *tags_opts = _tags_opts;
/**
* @brief Constructor for GetInfo class.
* Initializes variables
* @param idconf: (int) 1 or 2, 1 for get the pointer to the begin of the linked list,
* pertaining to AV_CONF_FILE, and 2 for LOCAL_CONF_FILE.
*/
GetInfo::GetInfo(int idconf)
{
debug = 0;
objutils = new Utils;
objreadav = new Read(1);
objreadlo = new Read(2);
if ( idconf == 1 )
begin = objreadav->get_begin();
if ( idconf == 2 )
begin = objreadlo->get_begin();
found_tag_name = false;
}
/**
* Parse {AV|LOCAL}_CONF_FILE, searching package name.
* @param package: Name of package.
* @return "True" if the package found, and return "False" if not found.
*/
bool GetInfo::ParsePkg(std::string package)
{
std::string package_tag = "Package: ";
std::string esc_chr = "\n";
std::string index_name = package_tag + package + esc_chr;
index = begin;
while( index != NULL )
{
if( !(*index->line).compare(index_name) )
{
index=index->next;
return true;
}
index=index->next;
}
return false;
}
/**
* Parse {AV|LOCAL}_CONF_FILE, searching a tag.
* @param package: Name of package.
* @param tag_name: Id for tag, defined in "tags_opts" variable.
* @return "True" if the tag found, and return "False" if not found.
*/
bool GetInfo::ParseTag(std::string package, int tag_name)
{
found_tag_name == false;
std::string tag(tags_opts[tag_name].name); tag.append(":");
if ( ParsePkg(package) )
{
while( index != NULL )
{
if ( !(*index->line).compare("\n") ) { break; }
if ((*index->line).rfind(tag) != std::string::npos)
{
boost::smatch what; boost::regex reg;
// @<space>@:@ @<hexa>@\n@<hexa>.*@;
reg = "([^\\s]+): ([^\\W].*)\n";
if( boost::regex_match((*index->line), what, reg, boost::match_perl) )
{
found_tag_name = true;
index = index->next;
continue;
}
else
{
std::cout << "[BAD]" << std::endl;
std::cout << "Error of parsing, line: " << index->nline << std::endl;
}
}
index = index->next;
}
}
if ( found_tag_name ) { return true; }
else { return false; };
}
/**
* Parse {AV|LOCAL}_CONF_FILE, searching a tag.
* @param package: Name of package.
* @param tag_name: Id for tag, defined in "tags_opts" variable.
* @param taginfo_o: Struct in that save the results of the search, tag.
* @return "True" if the tag found, and return "False" if not found.
*/
bool GetInfo::ParseTag(std::string package, int tag_name, taginfo &taginfo_o)
{
found_tag_name == false;
std::string tag(tags_opts[tag_name].name);
tag.append(":");
if ( ParsePkg( package ) )
{
while( index != NULL )
{
if (!(*index->line).compare("\n")) { break; };
if ((*index->line).rfind(tag) != std::string::npos)
{
boost::smatch what; boost::regex reg;
// @<space>@:@ @<hexa>@\n@<hexa>.*@;
reg = "([^\\s]+): ([^\\W].*)\n";
if( boost::regex_match((*index->line), what, reg, boost::match_perl) )
{
taginfo_o.content = what[2];
taginfo_o.nline = index->nline;
found_tag_name = true;
index = index->next;
continue;
}
else
{
std::cout << "[BAD]" << std::endl;
std::cout << "Error of parsing, line: " << index->nline << std::endl;
exit(2);
}
}
if (tag_name == 3 && found_tag_name == true){(taginfo_o.content).append(*index->line);};
index = index->next;
}
if ( found_tag_name ) { return true; }
else { return false; };
}
else
{
std::cout << "[BAD]" << std::endl;
std::cout << "Package not exists: " << package << std::endl;
exit(2);
}
}
/**
* Verifies the existence of a package.
* @param package: Name of package.
* @return True if the package found, and return "False" if not found.
*/
bool GetInfo::PkgExist(std::string package)
{
if ( ParsePkg(package) ){ return true; }
else { return false; };
}
/**
* Verifies the existence of a tag.
* @param package: Name of package.
* @param tag_name: Id for tag, defined in "tags_opts" variable.
* @return True if the tag found, and return "False" if not found.
*/
bool GetInfo::TagExist(std::string package, int tag_name)
{
if ( ParseTag(package, tag_name) ) {return true; }
else { return false; };
}
/**
* Member that return and save information for a tag in the respective variables.
* @param package: Name of package.
* @param tag_name: Id for tag, defined in "tags_opts" variable.
* @param results_o: Variable in that save the content of the tag.
* @param nline_o: Variable in that save the line number of the tag.
* @return True if the tag found, and return "False" if not found.
*/
bool GetInfo::GetPkgTag(std::string package, int tag_name, std::string &results_o, int &nline_o)
{
taginfo taginfo_o;
bool return_code = ParseTag(package, tag_name, taginfo_o);
results_o = taginfo_o.content;
nline_o = taginfo_o.nline;
return return_code;
}
/**
* Member that return and save information for a tag in the respective variables.
* @param package: Name of package.
* @param tag_name: Id for tag, defined in "tags_opts" variable.
* @param results_o: Variable in that save the content of the tag.
* @return True if the tag found, and return "False" if not found.
*/
bool GetInfo::GetPkgTag(std::string package, int tag_name, std::string &results_o)
{
taginfo taginfo_o;
bool return_code = ParseTag(package, tag_name, taginfo_o);
results_o = taginfo_o.content;
return return_code;
}
/**
* Member that save information in a vector, about the dependencies.
* @param package: Name of package.
* @param vector_depends: Dependencies of package.
*/
void GetInfo::get_DependsMap(std::string package, std::vector<Programa> &vector_depends)
{
std::string depends;
int out_code;
int nline;
out_code = GetPkgTag(package, 2, depends, nline);
if ( out_code )
{
std::vector<std::string> split_depends;
objutils->split_vec(depends, ", ", MAX_DEP_GROUP_LEN, split_depends);
int m = 0;
while ( m < split_depends.size() )
{
Programa prog;
boost::smatch what; boost::regex reg;
reg = "([^\\s]+) \\(([^\\s]+) ([^\\)]+)\\)";
if( boost::regex_match(split_depends[m], what, reg, boost::match_perl) )
{
//Programa * prog = new Programa();
prog.name = what[1];
prog.op = what[2];
prog.ver = what[3];
prog.nline = nline;
vector_depends.push_back(prog);
m++;
}
else
{
std::cout << "[BAD]" << std::endl;
std::cout << "Error of parsing, line: " << nline <<std::endl;
std::cout << "Download again: " << AV_CONF_FILE << " and it tries again" << std::endl;
exit (2);
}
}
}
}
GetInfo::~GetInfo()
{
delete objreadav;
delete objreadlo;
delete objutils;
}
}