1 | /* |
---|
2 | * File: main.cpp |
---|
3 | * Autor: José Orlando Ruiz Hernández (joseph2283@gmail.com) |
---|
4 | * Institución: Fundación CENDITEL |
---|
5 | * |
---|
6 | * Created on 18 de junio de 2012, 10:05 AM |
---|
7 | * Modified on 02 de mayo de 2014 |
---|
8 | * |
---|
9 | * Modificación por Alejandro J. Mujica (amujica@cenditel.gob.ve) con la |
---|
10 | * finalidad de adaptar para que genere la visualización estándar definida. |
---|
11 | */ |
---|
12 | |
---|
13 | #include <cstdlib> |
---|
14 | #include <argp.h> |
---|
15 | #include <string> |
---|
16 | |
---|
17 | # include <tpl_dynDlist.H> |
---|
18 | |
---|
19 | # include <dbProperties.H> |
---|
20 | # include <ioManager.H> |
---|
21 | # include <process.H> |
---|
22 | |
---|
23 | using namespace std; |
---|
24 | |
---|
25 | const int MAX_LEVELS_DOWN = 10; //Cantidad máxima permitida de niveles abajo |
---|
26 | const int MAX_LEVELS_UP = 10; //Cantidad máxima permitida de niveles arriba |
---|
27 | |
---|
28 | /* Keys for options without short-options. */ |
---|
29 | #define OPT_ABORT 1 /* --abort */ |
---|
30 | |
---|
31 | /* Opciones disponibles como argumentos del ejecutable del programa*/ |
---|
32 | static struct argp_option options[] = { |
---|
33 | {"levelsup", 'u', "levelsUp", 0, "Número de niveles hacia arriba", 0}, |
---|
34 | {"levelsdown", 'd', "LevelsDown", 0, "Número de niveles hacia abajo", 0}, |
---|
35 | {"year", 'y', "year", 0, "Año de estudio", 0}, |
---|
36 | {"output", 'o', "OutputName", 0, "Nombre del archivo de salida sin extensión", 0}, |
---|
37 | {"host", 'H', "Host", 0, "Host de la base de datos", 0}, |
---|
38 | {"port", 'P', "Port", 0, "Puerto de conexión a la base de datos", 0}, |
---|
39 | {"username", 'U', "Username", 0, "Usuario de la base de datos", 0}, |
---|
40 | {"password", 'W', "Password", 0, "Password de la base de datos", 0}, |
---|
41 | {"dbname", 'D', "Dbname", 0, "Nombre de la base de datos", 0}, |
---|
42 | {"verbose", 'v', 0, 0, "Modo verbose", 0}, |
---|
43 | {0, 0, 0, 0, 0, 0} |
---|
44 | }; |
---|
45 | const char *argp_program_version = "Generador de Cadenas Productivas 2.0\n" |
---|
46 | "CENDITEL Nodo Mérida (www.cenditel.gob.ve)"; |
---|
47 | |
---|
48 | static char doc[] = "Generador de Cadenas Productivas"; |
---|
49 | |
---|
50 | /* Descripcion de los argumentos que se aceptan */ |
---|
51 | static char args_doc[] = " PROD cod_producto [cod_producto ...]"; |
---|
52 | |
---|
53 | struct arguments |
---|
54 | { |
---|
55 | int levelsup; /* '-u', '--levelsup' */ |
---|
56 | int levelsdown; /* '-d', '--levelsdown' */ |
---|
57 | string year; /* '-y', '--year' */ |
---|
58 | string output; /* '-o', '--output' */ |
---|
59 | string host; /* '-H', '--host' */ |
---|
60 | string port; /* '-P', '--port' */ |
---|
61 | string username; /* '-U', '--username' */ |
---|
62 | string password; /* '-W', '--password' */ |
---|
63 | string dbname; /* '-D', '--dbname' */ |
---|
64 | string type; /* 'arg1' */ |
---|
65 | char ** values; /* 'arg2 [arg3...]' */ |
---|
66 | int num_values; /* */ |
---|
67 | bool verbose; |
---|
68 | }; |
---|
69 | |
---|
70 | |
---|
71 | static error_t parse_opt(int key, char *arg, struct argp_state *state) |
---|
72 | { |
---|
73 | struct arguments *arguments = (struct arguments*)state->input; |
---|
74 | |
---|
75 | switch (key) { |
---|
76 | break; |
---|
77 | break; |
---|
78 | case 'u': |
---|
79 | arguments->levelsup = atoi(arg); |
---|
80 | break; |
---|
81 | case 'd': |
---|
82 | arguments->levelsdown = atoi(arg); |
---|
83 | break; |
---|
84 | case 'y': |
---|
85 | arguments->year = arg; |
---|
86 | break; |
---|
87 | case 'o': |
---|
88 | arguments->output = arg; |
---|
89 | break; |
---|
90 | case 'H': |
---|
91 | arguments->host = arg; |
---|
92 | break; |
---|
93 | case 'P': |
---|
94 | arguments->port = arg; |
---|
95 | break; |
---|
96 | case 'U': |
---|
97 | arguments->username = arg; |
---|
98 | break; |
---|
99 | case 'W': |
---|
100 | arguments->password = arg; |
---|
101 | break; |
---|
102 | case 'D': |
---|
103 | arguments->dbname = arg; |
---|
104 | break; |
---|
105 | |
---|
106 | case 'v': |
---|
107 | arguments->verbose = true; |
---|
108 | break; |
---|
109 | |
---|
110 | case ARGP_KEY_NO_ARGS: |
---|
111 | argp_usage (state); |
---|
112 | break; |
---|
113 | |
---|
114 | case ARGP_KEY_ARG: |
---|
115 | /* Here we know that state->arg_num == 0, since we |
---|
116 | force argument parsing to end before any more arguments can |
---|
117 | get here. */ |
---|
118 | arguments->type = arg; |
---|
119 | if(arguments->type!="PROD") { argp_usage (state); } |
---|
120 | |
---|
121 | /* Now we consume all the rest of the arguments. |
---|
122 | state->next is the index in state->argv of the |
---|
123 | next argument to be parsed, which is the first string |
---|
124 | we're interested in, so we can just use |
---|
125 | &state->argv[state->next] as the value for |
---|
126 | arguments->strings. |
---|
127 | |
---|
128 | In addition, by setting state->next to the end |
---|
129 | of the arguments, we can force argp to stop parsing here and |
---|
130 | return. */ |
---|
131 | arguments->values = &state->argv[state->next]; |
---|
132 | state->next = state->argc; |
---|
133 | |
---|
134 | break; |
---|
135 | |
---|
136 | case ARGP_KEY_END: |
---|
137 | arguments->num_values = state->arg_num; |
---|
138 | if (state->arg_num < 2 ) |
---|
139 | /* Not enough arguments. */ |
---|
140 | argp_usage (state); |
---|
141 | break; |
---|
142 | |
---|
143 | default: |
---|
144 | return ARGP_ERR_UNKNOWN; |
---|
145 | } |
---|
146 | return 0; |
---|
147 | } |
---|
148 | |
---|
149 | static struct argp argp_s = { options, parse_opt, args_doc, doc, 0, 0, 0 }; |
---|
150 | |
---|
151 | |
---|
152 | /* |
---|
153 | * |
---|
154 | */ |
---|
155 | int main(int argc, char** argv) { |
---|
156 | |
---|
157 | struct arguments arguments; |
---|
158 | arguments.levelsdown=0; |
---|
159 | arguments.levelsup=0; |
---|
160 | arguments.output="output"; |
---|
161 | arguments.host="127.0.0.1"; |
---|
162 | arguments.port="5432"; |
---|
163 | arguments.username="desarrollos"; |
---|
164 | arguments.password="development"; |
---|
165 | //arguments.dbname="sig"; |
---|
166 | arguments.dbname="sigesic_prueba"; |
---|
167 | arguments.year="2010"; |
---|
168 | arguments.verbose=false; |
---|
169 | |
---|
170 | |
---|
171 | if (argp_parse (&argp_s, argc, argv, 0/*ARGP_LONG_ONLY*/,0, &arguments) != 0) |
---|
172 | return 0; |
---|
173 | |
---|
174 | int lvsUp = arguments.levelsup; |
---|
175 | int lvsDown = arguments.levelsdown; |
---|
176 | |
---|
177 | DBProperties & dbProp = DBProperties::getInstance(); |
---|
178 | |
---|
179 | dbProp.setHost(arguments.host); |
---|
180 | dbProp.setPort(arguments.port); |
---|
181 | dbProp.setDbname(arguments.dbname); |
---|
182 | dbProp.setUser(arguments.username); |
---|
183 | dbProp.setPassword(arguments.password); |
---|
184 | |
---|
185 | if (arguments.verbose) { |
---|
186 | std::cout << "Llenando lista de argumentos...\n"; |
---|
187 | } |
---|
188 | |
---|
189 | DynDlist <long> ids; |
---|
190 | |
---|
191 | for (size_t i = 0; i < arguments.num_values - 1; ++i) { |
---|
192 | ids.append(std::atol(arguments.values[i])); |
---|
193 | } |
---|
194 | |
---|
195 | if (arguments.verbose) { |
---|
196 | std::cout << "Lista de argumentos completada\n"; |
---|
197 | } |
---|
198 | |
---|
199 | std::string baseName = arguments.output; |
---|
200 | |
---|
201 | std::string dotFileName = baseName + ".dot"; |
---|
202 | |
---|
203 | std::string svgOutputFileName = baseName + ".svg"; |
---|
204 | |
---|
205 | std::string gvOutputFileName = baseName + ".gv"; |
---|
206 | |
---|
207 | const char * const xmlRoots = "temp.xml"; |
---|
208 | |
---|
209 | IOManager ioManager; |
---|
210 | |
---|
211 | if (arguments.verbose) { |
---|
212 | std::cout << "Creando " << xmlRoots << "...\n"; |
---|
213 | } |
---|
214 | |
---|
215 | ioManager.createXmlRootsFile(arguments.year, ids, lvsUp, lvsDown, xmlRoots); |
---|
216 | |
---|
217 | if (arguments.verbose) { |
---|
218 | std::cout << xmlRoots << " creado.\n"; |
---|
219 | } |
---|
220 | |
---|
221 | try { |
---|
222 | |
---|
223 | Graph graph; |
---|
224 | |
---|
225 | if (arguments.verbose) { |
---|
226 | std::cout << "Construyendo red...\n"; |
---|
227 | } |
---|
228 | |
---|
229 | ioManager.buildGraph(xmlRoots, graph); |
---|
230 | |
---|
231 | if (arguments.verbose) { |
---|
232 | std::cout << "Red construída\n"; |
---|
233 | } |
---|
234 | |
---|
235 | if (arguments.verbose) { |
---|
236 | std::cout << "Eliminando " << xmlRoots << "...\n"; |
---|
237 | } |
---|
238 | |
---|
239 | remove(xmlRoots); |
---|
240 | |
---|
241 | if (arguments.verbose) { |
---|
242 | std::cout << xmlRoots << " eliminado.\n"; |
---|
243 | } |
---|
244 | |
---|
245 | if (arguments.verbose) { |
---|
246 | std::cout << "Generando " << dotFileName << "...\n"; |
---|
247 | } |
---|
248 | |
---|
249 | ioManager.generateDot<IOManager::NodeLabel, IOManager::ArcLabel> |
---|
250 | (graph, dotFileName); |
---|
251 | |
---|
252 | if (arguments.verbose) { |
---|
253 | std::cerr << dotFileName << " generado.\n"; |
---|
254 | } |
---|
255 | |
---|
256 | int returnValue = 0; |
---|
257 | |
---|
258 | char const * path = "dot"; |
---|
259 | |
---|
260 | char * args[] = {"dot", "", (char *) dotFileName.c_str(), "-o", "", 0}; |
---|
261 | |
---|
262 | args[1] = "-Tsvg"; |
---|
263 | args[4] = (char *) svgOutputFileName.c_str(); |
---|
264 | |
---|
265 | if (arguments.verbose) { |
---|
266 | std::cout << "Generando " << svgOutputFileName << "...\n"; |
---|
267 | } |
---|
268 | |
---|
269 | returnValue = Process::exec(path, args); |
---|
270 | |
---|
271 | if (returnValue == 0) { |
---|
272 | if (arguments.verbose) { |
---|
273 | std::cerr << svgOutputFileName << " generado.\n"; |
---|
274 | } |
---|
275 | } else { |
---|
276 | std::cout << "Error generando " << svgOutputFileName << "\n"; |
---|
277 | } |
---|
278 | |
---|
279 | args[1] = "-Txdot"; |
---|
280 | args[4] = (char *) gvOutputFileName.c_str(); |
---|
281 | |
---|
282 | if (arguments.verbose) { |
---|
283 | std::cout << "Generando " << gvOutputFileName << "...\n"; |
---|
284 | } |
---|
285 | |
---|
286 | returnValue = Process::exec(path, args); |
---|
287 | |
---|
288 | if (returnValue == 0) { |
---|
289 | if (arguments.verbose) { |
---|
290 | std::cerr << gvOutputFileName << " generado.\n"; |
---|
291 | } |
---|
292 | } else { |
---|
293 | std::cout << "Error generando " << gvOutputFileName << "\n"; |
---|
294 | } |
---|
295 | |
---|
296 | if (arguments.verbose) { |
---|
297 | std::cout << "Eliminando " << dotFileName << "...\n"; |
---|
298 | } |
---|
299 | |
---|
300 | if (arguments.verbose) { |
---|
301 | std::cerr << dotFileName << " eliminado.\n"; |
---|
302 | } |
---|
303 | |
---|
304 | } catch (const std::exception & e) { |
---|
305 | std::cout << "Excepción capturada: " << e.what() << std::endl; |
---|
306 | return 1; |
---|
307 | } |
---|
308 | |
---|
309 | return 0; |
---|
310 | } |
---|
311 | |
---|
312 | |
---|