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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
* macrotool_fieldnames.cc
*
* Mon Jul 6 14:15:05 CEST 2009
* Copyright 2009 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of Pracro.
*
* Pracro 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.
*
* Pracro 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.
*
* You should have received a copy of the GNU General Public License
* along with Pracro; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "fieldnames.h"
#include <string.h>
#include <stdio.h>
#include "util.h"
#include "macroparser.h"
#include "debug.h"
#include "database.h"
#include "configuration.h"
static const char usage_str[] =
" help Prints this helptext.\n"
" add name desc Add a field called 'name', described as 'desc'\n"
" del name Delete a field called 'name'\n"
" list List all fieldnames as well as their macro references.\n"
;
static void add(std::string name, std::string desc)
{
Database db("pgsql", Conf::database_addr, "", Conf::database_user, Conf::database_passwd, "");
db.addFieldname(name, desc);
}
static void del(std::string name)
{
Database db("pgsql", Conf::database_addr, "", Conf::database_user, Conf::database_passwd, "");
db.delFieldname(name);
}
static std::vector<std::string> getWidgetNames(Widget &w)
{
std::vector<std::string> v;
if(w.attributes.find("name") != w.attributes.end()) v.push_back(w.attributes["name"]);
std::vector< Widget >::iterator i = w.widgets.begin();
while(i != w.widgets.end()) {
std::vector<std::string> _v = getWidgetNames(*i);
v.insert(v.end(), _v.begin(), _v.end());
i++;
}
return v;
}
static std::map<std::string, std::vector<std::string> > getMacroRefsList()
{
std::map<std::string, std::vector<std::string> > reflist;
std::vector<std::string> macrofiles = getMacros();
std::vector<std::string>::iterator mfs = macrofiles.begin();
while(mfs != macrofiles.end()) {
std::string name = mfs->substr(0, mfs->length() - 4);
MacroParser parser(Conf::xml_basedir + "/macros/" + *mfs);
parser.parse();
Macro *macro = parser.getMacro();
std::string key = name;
reflist[key] = getWidgetNames(macro->widgets);
mfs++;
}
return reflist;
}
static std::vector<std::string> getMacroRefs(std::map<std::string, std::vector<std::string> > reflist, std::string name)
{
std::vector<std::string> macros;
std::map<std::string, std::vector<std::string> >::iterator macro = reflist.begin();
while(macro != reflist.end()) {
std::vector<std::string>::iterator field = macro->second.begin();
while(field != macro->second.end()) {
if(*field == name) macros.push_back(macro->first);
field++;
}
macro++;
}
return macros;
}
static void list()
{
Database db("pgsql", Conf::database_addr, "", Conf::database_user, Conf::database_passwd, "");
std::vector<Fieldname> fieldnames = db.getFieldnames();
std::map<std::string, std::vector<std::string> > reflist = getMacroRefsList();
size_t name_sz = 0;
size_t desc_sz = 0;
size_t time_sz = 0;
std::vector<Fieldname>::iterator i = fieldnames.begin();
while(i != fieldnames.end()) {
if(i->name.length() > name_sz) name_sz = i->name.length();
if(i->description.length() > desc_sz) desc_sz = i->description.length();
char ts[32];
sprintf(ts, "%u", (unsigned int)i->timestamp);
if(strlen(ts) > time_sz) time_sz = strlen(ts);
i++;
}
printcolumn("Name:", name_sz);
printcolumn("Description:", desc_sz);
printcolumn("Timestamp:", time_sz);
printf("Macros:");
printf("\n");
i = fieldnames.begin();
while(i != fieldnames.end()) {
printcolumn(i->name, name_sz);
printcolumn(i->description, desc_sz);
char ts[32];
sprintf(ts, "%u", (unsigned int)i->timestamp);
printcolumn(ts, time_sz);
std::vector<std::string> macros = getMacroRefs(reflist, i->name);
std::vector<std::string>::iterator j = macros.begin();
while(j != macros.end()) {
if(j != macros.begin()) printf(", ");
printf("%s", j->c_str());
j++;
}
printf("\n");
i++;
}
}
void macrotool_fieldnames(std::vector<std::string> params)
{
if(params.size() < 1) {
printf("%s", usage_str);
return;
}
DEBUG(fieldnames, "fieldnames: %s\n", params[0].c_str());
if(params[0] == "list") {
if(params.size() != 1) {
printf("The command 'list' does not need any parameters.\n");
printf("%s", usage_str);
return;
}
list();
return;
}
if(params[0] == "add") {
if(params.size() != 3) {
printf("The command 'add' needs 2 parameters.\n");
printf("%s", usage_str);
return;
}
add(params[1], params[2]);
return;
}
if(params[0] == "del") {
if(params.size() != 2) {
printf("The command 'del' needs 1 parameter.\n");
printf("%s", usage_str);
return;
}
del(params[1]);
return;
}
if(params[0] == "help") {
printf("%s", usage_str);
return;
}
printf("Unknown command '%s'\n", params[0].c_str());
printf("%s", usage_str);
return;
}
|