1 |
// Smersh |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Person.hpp" |
8 |
|
9 |
Person::Person(const string& sn) : name("Unknown Person") |
10 |
{ |
11 |
#ifdef __FreeBSD__ |
12 |
if (db.empty()) configure(); |
13 |
#else |
14 |
if (file.empty()) configure(); |
15 |
#endif |
16 |
if (!sn.empty()) query(sn); |
17 |
} |
18 |
|
19 |
#ifdef __FreeBSD__ |
20 |
string Person::user, Person::db; |
21 |
#else |
22 |
string Person::file; |
23 |
vector<string> Person::args(1, "smersh"); |
24 |
string Person::separator, Person::begin, Person::end; |
25 |
#endif |
26 |
|
27 |
void Person::configure() |
28 |
{ |
29 |
ext::Handle<xml::Document> document(xml::Parse("smersh.xml")); |
30 |
ext::Handle<xml::Node> smersh(*document/"smersh"); |
31 |
|
32 |
#ifdef __FreeBSD__ |
33 |
user = *smersh/"user"; |
34 |
db = *smersh/"db"; |
35 |
|
36 |
if (debug) cerr << "user = " << user << "\ndb = " << db << '\n'; |
37 |
#else |
38 |
file = *smersh/"command"/"file"; |
39 |
|
40 |
xml::NodeSet nodes(*smersh/"command"/"arg"); |
41 |
|
42 |
for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End(); |
43 |
++node) args.push_back(**node); |
44 |
|
45 |
separator = *smersh/"separator"; |
46 |
begin = *smersh/"begin"; |
47 |
end = *smersh/"end"; |
48 |
|
49 |
if (debug) |
50 |
{ |
51 |
cerr << "file = " << file << '\n' |
52 |
<< "args = {\n"; |
53 |
|
54 |
for (vector<string>::size_type index(0); index < args.size(); ++index) |
55 |
{ |
56 |
cerr << " [" << index << "] = " << args[index] << '\n'; |
57 |
} |
58 |
|
59 |
cerr << "}\n" |
60 |
<< "separator = " << separator << '\n' |
61 |
<< "begin = " << begin << '\n' |
62 |
<< "end = " << end << '\n'; |
63 |
} |
64 |
#endif |
65 |
} |
66 |
|
67 |
void Person::query(const string& sn) |
68 |
{ |
69 |
#ifdef __FreeBSD__ |
70 |
ext::Handle<dbi::Connection> db(dbi::Connect(user, this->db)); |
71 |
ext::Handle<dbi::ResultSet> people(db->Execute(string("SELECT name FROM pe") |
72 |
+ "ople, peopleaimmap, aim WHERE people.id=pid AND aid=aim.id AND sn=LO" |
73 |
+ "WER(\'" + sn + "\')")); |
74 |
|
75 |
if (people->MoveNext()) name = people->GetString("name"); |
76 |
|
77 |
while (people->MoveNext()) |
78 |
{ |
79 |
Person person; |
80 |
|
81 |
person.name = people->GetString("name"); |
82 |
|
83 |
multiple.push_back(person); |
84 |
} |
85 |
#else |
86 |
vector<string> args(Person::args); |
87 |
|
88 |
args.push_back(string("SELECT name FROM people, peopleaimmap, aim WHERE p") |
89 |
+ "eople.id=pid AND aid=aim.id AND sn=LOWER(\'" + sn + "\');"); |
90 |
|
91 |
ipstream sql(file, args); |
92 |
bool first(true); |
93 |
Matcher separator(this->separator), heading(begin + "\\s*name\\s*" + end), |
94 |
matcher(begin + "(.*)" + end); |
95 |
|
96 |
do |
97 |
{ |
98 |
string line; |
99 |
|
100 |
getline(sql, line); |
101 |
|
102 |
if (debug) cerr << "line = " << line << '\n'; |
103 |
|
104 |
if (line == separator) continue; |
105 |
else if (line == heading) continue; |
106 |
else if (line == matcher) |
107 |
{ |
108 |
if (first) |
109 |
{ |
110 |
name = matcher[1]; |
111 |
first = false; |
112 |
} |
113 |
else |
114 |
{ |
115 |
Person person; |
116 |
|
117 |
person.name = matcher[1]; |
118 |
|
119 |
multiple.push_back(person); |
120 |
} |
121 |
} |
122 |
else break; |
123 |
} |
124 |
while (sql.good()); |
125 |
#endif |
126 |
} |