Debian Codegraph @

apps/gsmpb.cc

raw symbols SCIP
1 // *************************************************************************
2 // * GSM TA/ME library
3 // *
4 // * File: gsmpb.cc
5 // *
6 // * Purpose: phonebook management program
7 // *
8 // * Author: Peter Hofmann ([email protected])
9 // *
10 // * Created: 24.6.1999
11 // *************************************************************************
12
13 #ifdef HAVE_CONFIG_H
14 #include <gsm_config.h>
15 #endif
16 #include <gsmlib/gsm_nls.h>
17 #include <string>
18 #ifdef WIN32
19 #include <gsmlib/gsm_win32_serial.h>
20 #else
21 #include <gsmlib/gsm_unix_serial.h>
22 #include <unistd.h>
23 #endif
24 #if defined(HAVE_GETOPT_LONG) || defined(WIN32)
25 #include <getopt.h>
26 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <gsmlib/gsm_me_ta.h>
31 #include <gsmlib/gsm_util.h>
32 #include <gsmlib/gsm_sorted_phonebook.h>
33 #include <iostream>
34
35 #ifdef HAVE_GETOPT_LONG
36 static struct option longOpts[] =
37 {
38 {"xonxoff", no_argument, (int*)NULL, 'X'},
39 {"phonebook", required_argument, (int*)NULL, 'p'},
40 {"init", required_argument, (int*)NULL, 'I'},
41 {"destination", required_argument, (int*)NULL, 'd'},
42 {"source", required_argument, (int*)NULL, 's'},
43 {"destination-backend", required_argument, (int*)NULL, 'D'},
44 {"source-backend", required_argument, (int*)NULL, 'S'},
45 {"baudrate", required_argument, (int*)NULL, 'b'},
46 {"charset", required_argument, (int*)NULL, 't'},
47 {"copy", no_argument, (int*)NULL, 'c'},
48 {"synchronize", no_argument, (int*)NULL, 'y'},
49 {"help", no_argument, (int*)NULL, 'h'},
50 {"version", no_argument, (int*)NULL, 'v'},
51 {"verbose", no_argument, (int*)NULL, 'V'},
52 {"indexed", no_argument, (int*)NULL, 'i'},
53 {(char*)NULL, 0, (int*)NULL, 0}
54 };
55 #else
56 #define getopt_long(argc, argv, options, longopts, indexptr) \
57 getopt(argc, argv, options)
58 #endif
59
60 // insert those entries from sourcePhonebook into destPhonebook
61 // that are not already present in destPhonebook
62
63 void insertNotPresent(gsmlib::SortedPhonebookRef sourcePhonebook,
64 gsmlib::SortedPhonebookRef destPhonebook,
65 bool indexed, bool verbose)
66 {
67 for (gsmlib::SortedPhonebookBase::iterator i = sourcePhonebook->begin();
68 i != sourcePhonebook->end(); ++i)
69 {
70 std::pair<gsmlib::SortedPhonebookBase::iterator, gsmlib::SortedPhonebookBase::iterator> range;
71 if (indexed)
72 {
73 int index = i->index();
74 range = destPhonebook->equal_range(index);
75 }
76 else
77 {
78 std::string text = i->text();
79 range = destPhonebook->equal_range(text);
80 }
81
82 // do nothing if the entry is already present in the destination
83 bool alreadyPresent = false;
84 for (gsmlib::SortedPhonebookBase::iterator j = range.first;
85 j != range.second; ++j)
86 {
87 i->setUseIndex(indexed);
88 if (i->telephone() == j->telephone())
89 {
90 alreadyPresent = true;
91 break;
92 }
93 }
94 // ... else insert it
95 if (!alreadyPresent)
96 {
97 if (verbose)
98 {
99 std::cout << gsmlib::stringPrintf(_("inserting '%s' tel# %s"),
100 i->text().c_str(), i->telephone().c_str());
101 if (indexed)
102 std::cout << gsmlib::stringPrintf(_(" (index #%d)"), i->index());
103 std::cout << std::endl;
104 }
105 i->setUseIndex(indexed);
106 destPhonebook->insert(*i); // insert
107 }
108 }
109 }
110
111 // update those entries in destPhonebook, that
112 // - have the same name as one entry in destPhonebook
113 // - but have a different telephone number
114 // this is only done if the name in question is unique in the destPhonebook
115 // the case of several entries having the same in the sourcePhonebook
116 // is handled - only the first is considered for updating
117
118 void updateEntries(gsmlib::SortedPhonebookRef sourcePhonebook,
119 gsmlib::SortedPhonebookRef destPhonebook,
120 bool verbose)
121 {
122 bool firstLoop = true;
123 std::string lastText;
124
125 for (gsmlib::SortedPhonebookBase::iterator i = sourcePhonebook->begin();
126 i != sourcePhonebook->end(); ++i)
127 {
128 std::string text = i->text();
129 if (!firstLoop && text != lastText)
130 {
131 std::pair<gsmlib::SortedPhonebookBase::iterator,
132 gsmlib::SortedPhonebookBase::iterator> range =
133 destPhonebook->equal_range(text);
134
135 gsmlib::SortedPhonebookBase::iterator first = range.first;
136 if (first != destPhonebook->end() && range.second == ++first)
137 { // just one text in the destPhonebook
138 if (!(*range.first == *i)) // overwrite if different in destination
139 {
140 if (verbose)
141 std::cout << gsmlib::stringPrintf(_("updating '%s' tel# %s to new tel# %s"),
142 range.first->text().c_str(),
143 range.first->telephone().c_str(),
144 i->telephone().c_str())
145 << std::endl;
146
147 *range.first = *i;
148 }
149 }
150 lastText = text;
151 }
152 firstLoop = false;
153 }
154 }
155
156 // the same but for indexed phonebooks
157
158 void updateEntriesIndexed(gsmlib::SortedPhonebookRef sourcePhonebook,
159 gsmlib::SortedPhonebookRef destPhonebook,
160 bool verbose)
161 {
162 for (gsmlib::SortedPhonebookBase::iterator i = sourcePhonebook->begin();
163 i != sourcePhonebook->end(); ++i)
164 {
165 int index = i->index();
166
167 gsmlib::SortedPhonebookBase::iterator j = destPhonebook->find(index);
168
169 if (j != destPhonebook->end())
170 { // index present in the destPhonebook
171 if (! (*j == *i)) // overwrite if different in destination
172 {
173 if (verbose)
174 std::cout << gsmlib::stringPrintf(_("updating '%s' tel# %s to new tel# %s"
175 "(index %d)"),
176 j->text().c_str(),
177 j->telephone().c_str(),
178 i->telephone().c_str(), i->index())
179 << std::endl;
180 *j = *i;
181 }
182 }
183 }
184 }
185
186 // delete those entries from destPhonebook, that are not present
187 // in sourcePhonebook
188
189 void deleteNotPresent(gsmlib::SortedPhonebookRef sourcePhonebook,
190 gsmlib::SortedPhonebookRef destPhonebook,
191 bool indexed, bool verbose)
192 {
193 for (gsmlib::SortedPhonebookBase::iterator i = destPhonebook->begin();
194 i != destPhonebook->end(); ++i)
195 {
196 std::pair<gsmlib::SortedPhonebookBase::iterator, gsmlib::SortedPhonebookBase::iterator> range;
197 if (indexed)
198 {
199 int index = i->index();
200 range = sourcePhonebook->equal_range(index);
201 }
202 else
203 {
204 std::string text = i->text();
205 range = sourcePhonebook->equal_range(text);
206 }
207
208 bool found = false;
209 for (gsmlib::SortedPhonebookBase::iterator j = range.first;
210 j != range.second; ++j)
211 {
212 i->setUseIndex(indexed);
213 if (j->telephone() == i->telephone())
214 {
215 found = true;
216 break;
217 }
218 }
219 if (!found)
220 {
221 if (verbose)
222 {
223 std::cout << gsmlib::stringPrintf(_("deleting '%s' tel# %s"),
224 i->text().c_str(), i->telephone().c_str());
225 if (indexed)
226 std::cout << gsmlib::stringPrintf(_(" (index #%d)"), i->index());
227 std::cout << std::endl;
228 }
229 destPhonebook->erase(i);
230 #ifdef BUGGY_MAP_ERASE
231 deleteNotPresent(sourcePhonebook, destPhonebook, indexed, verbose);
232 return;
233 #endif
234 }
235 }
236 }
237
238 // *** main program
239
240 int main(int argc, char *argv[])
241 {
242 try
243 {
244 // handle command line options
245 std::string destination;
246 std::string source;
247 std::string destinationBackend;
248 std::string sourceBackend;
249 std::string baudrate;
250 bool doSynchronize = true;
251 std::string phonebook;
252 gsmlib::SortedPhonebookRef sourcePhonebook, destPhonebook;
253 bool verbose = false;
254 bool indexed = false;
255 std::string initString = gsmlib::DEFAULT_INIT_STRING;
256 bool swHandshake = false;
257 std::string charSet;
258 gsmlib::Ref<gsmlib::MeTa> sourceMeTa, destMeTa;
259
260 int opt;
261 int dummy;
262 while((opt = getopt_long(argc, argv, "I:p:s:d:b:cyhvViD:S:Xt:", longOpts,
263 &dummy))
264 != -1)
265 switch (opt)
266 {
267 case 'X':
268 swHandshake = true;
269 break;
270 case 'I':
271 initString = optarg;
272 break;
273 case 'V':
274 verbose = true;
275 break;
276 case 'p':
277 phonebook = optarg;
278 break;
279 case 'd':
280 destination = optarg;
281 break;
282 case 's':
283 source = optarg;
284 break;
285 case 'D':
286 destinationBackend = optarg;
287 break;
288 case 'S':
289 sourceBackend = optarg;
290 break;
291 case 'b':
292 baudrate = optarg;
293 break;
294 case 't':
295 charSet = optarg;
296 break;
297 case 'c':
298 doSynchronize = false;
299 break;
300 case 'i':
301 indexed = true;
302 break;
303 case 'y':
304 doSynchronize = true;
305 break;
306 case 'v':
307 std::cerr << argv[0] << gsmlib::stringPrintf(_(": version %s [compiled %s]"),
308 VERSION, __DATE__) << std::endl;
309 exit(0);
310 break;
311 case 'h':
312 std::cerr << argv[0] << _(": [-b baudrate][-c][-d device or file][-h]"
313 "[-I init string]\n"
314 " [-p phonebook name][-s device or file]"
315 "[-t charset][-v]"
316 "[-V][-y][-X]") << std::endl
317 << std::endl
318 << _(" -b, --baudrate baudrate to use for device "
319 "(default: 38400)")
320 << std::endl
321 << _(" -c, --copy copy source entries to destination")
322 << std::endl
323 << _(" -d, --destination sets the destination device to "
324 "connect \n"
325 " to, or the file to write") << std::endl
326 << _(" -D, --destination-backend sets the destination backend")
327 << std::endl
328 << _(" -h, --help prints this message") << std::endl
329 << _(" -i, --index takes index positions into account")
330 << std::endl
331 << _(" -I, --init device AT init sequence") << std::endl
332 << _(" -p, --phonebook name of phonebook to use") << std::endl
333 << _(" -s, --source sets the source device to connect to,\n"
334 " or the file to read") << std::endl
335 << _(" -t, --charset sets the character set to use for\n"
336 " phonebook entries") << std::endl
337 << _(" -S, --source-backend sets the source backend")
338 << std::endl
339 << _(" -v, --version prints version and exits") << std::endl
340 << _(" -V, --verbose print detailed progress messages")
341 << std::endl
342 << _(" -X, --xonxoff switch on software handshake") << std::endl
343 << _(" -y, --synchronize synchronize destination with source\n"
344 " entries (destination is overwritten)\n"
345 " (see gsmpb(1) for details)")
346 << std::endl << std::endl;
347 exit(0);
348 break;
349 case '?':
350 throw gsmlib::GsmException(_("unknown option"), gsmlib::ParameterError);
351 break;
352 }
353
354 // check if all parameters all present
355 if (destination == "" || source == "")
356 throw gsmlib::GsmException(_("both source and destination must be given"),
357 gsmlib::ParameterError);
358
359 // start accessing source mobile phone or file
360 if (sourceBackend != "")
361 sourcePhonebook =
362 gsmlib::CustomPhonebookRegistry::createPhonebook(sourceBackend, source);
363 else if (source == "-")
364 sourcePhonebook = new gsmlib::SortedPhonebook(true, indexed);
365 else if (gsmlib::isFile(source))
366 sourcePhonebook = new gsmlib::SortedPhonebook(source, indexed);
367 else
368 {
369 if (phonebook == "")
370 throw gsmlib::GsmException(_("phonebook name must be given"), gsmlib::ParameterError);
371
372 sourceMeTa = new gsmlib::MeTa(new
373 #ifdef WIN32
374 gsmlib::Win32SerialPort
375 #else
376 gsmlib::UnixSerialPort
377 #endif
378 (source,
379 baudrate == "" ? gsmlib::DEFAULT_BAUD_RATE :
380 gsmlib::baudRateStrToSpeed(baudrate), initString,
381 swHandshake));
382 if (charSet != "")
383 sourceMeTa->setCharSet(charSet);
384 sourcePhonebook =
385 new gsmlib::SortedPhonebook(sourceMeTa->getPhonebook(phonebook));
386 }
387
388 // make sure destination.c_str file exists
389 if (destination != "")
390 {
391 try
392 {
393 std::ofstream f(destination.c_str(), std::ios::out | std::ios::app);
394 }
395 catch (std::exception)
396 {
397 }
398 }
399
400 // start accessing destination mobile phone or file
401 if (destinationBackend != "")
402 destPhonebook =
403 gsmlib::CustomPhonebookRegistry::createPhonebook(destinationBackend,
404 destination);
405 else if (destination == "-")
406 destPhonebook = new gsmlib::SortedPhonebook(false, indexed);
407 else if (gsmlib::isFile(destination))
408 destPhonebook = new gsmlib::SortedPhonebook(destination, indexed);
409 else
410 {
411 if (phonebook == "")
412 throw gsmlib::GsmException(_("phonebook name must be given"), gsmlib::ParameterError);
413
414 destMeTa = new gsmlib::MeTa(new
415 #ifdef WIN32
416 gsmlib::Win32SerialPort
417 #else
418 gsmlib::UnixSerialPort
419 #endif
420 (destination,
421 baudrate == "" ? gsmlib::DEFAULT_BAUD_RATE :
422 gsmlib::baudRateStrToSpeed(baudrate), initString,
423 swHandshake));
424 if (charSet != "")
425 destMeTa->setCharSet(charSet);
426 gsmlib::PhonebookRef destPb = destMeTa->getPhonebook(phonebook);
427
428 // check maximum lengths of source text and phonenumber when writing to
429 // mobile phone
430 unsigned int maxTextLen = destPb->getMaxTextLen();
431 unsigned int maxTelLen = destPb->getMaxTelephoneLen();
432
433 for (gsmlib::SortedPhonebookBase::iterator i = sourcePhonebook->begin();
434 i != sourcePhonebook->end(); ++i)
435 if (i->text().length() > maxTextLen)
436 throw gsmlib::GsmException(
437 gsmlib::stringPrintf(_("text '%s' is too large to fit into destination "
438 "(maximum size %d characters)"),
439 i->text().c_str(), maxTextLen),
440 gsmlib::ParameterError);
441 else if (i->telephone().length() > maxTelLen)
442 throw gsmlib::GsmException(
443 gsmlib::stringPrintf(_("phone number '%s' is too large to fit into "
444 "destination (maximum size %d characters)"),
445 i->telephone().c_str(), maxTelLen),
446 gsmlib::ParameterError);
447
448 // read phonebook
449 destPhonebook = new gsmlib::SortedPhonebook(destPb);
450 }
451
452 // now do the actual work
453 if (doSynchronize)
454 { // synchronizing
455 if (indexed)
456 {
457 sourcePhonebook->setSortOrder(gsmlib::ByIndex);
458 destPhonebook->setSortOrder(gsmlib::ByIndex);
459 // for an explanation see below
460 updateEntriesIndexed(sourcePhonebook, destPhonebook, verbose);
461 deleteNotPresent(sourcePhonebook, destPhonebook, true, verbose);
462 insertNotPresent(sourcePhonebook, destPhonebook, true, verbose);
463 }
464 else
465 {
466 sourcePhonebook->setSortOrder(gsmlib::ByText);
467 destPhonebook->setSortOrder(gsmlib::ByText);
468 // the following is done to avoid superfluous writes to the TA
469 // (that takes time) and keep updated (ie. telephone number changed)
470 // entries at the same place
471 // 1. update entries in place where just the number changed
472 updateEntries(sourcePhonebook, destPhonebook, verbose);
473 // 2. delete those that are not present anymore
474 deleteNotPresent(sourcePhonebook, destPhonebook, false, verbose);
475 // 3. insert the new ones
476 insertNotPresent(sourcePhonebook, destPhonebook, false, verbose);
477 }
478 }
479 else
480 { // copying
481 destPhonebook->clear();
482 for (gsmlib::SortedPhonebookBase::iterator i = sourcePhonebook->begin();
483 i != sourcePhonebook->end(); ++i)
484 {
485 if (verbose)
486 {
487 std::cout << gsmlib::stringPrintf(_("inserting '%s' tel# %s"),
488 i->text().c_str(), i->telephone().c_str());
489 if (indexed)
490 std::cout << gsmlib::stringPrintf(_(" (index #%d)"), i->index());
491 std::cout << std::endl;
492 }
493 destPhonebook->insert(*i);
494 }
495 }
496 }
497 catch (gsmlib::GsmException &ge)
498 {
499 std::cerr << argv[0] << _("[ERROR]: ") << ge.what() << std::endl;
500 return 1;
501 }
502 return 0;
503 }