📄 gsmpb.c
字号:
// *************************************************************************
// * GSM TA/ME library
// *
// * File: gsmpb.cc
// *
// * Purpose: phonebook management program
// *
// * Author: Peter Hofmann (software@pxh.de)
// *
// * Created: 24.6.1999
// *************************************************************************
#ifdef HAVE_CONFIG_H
#include <gsm_config.h>
#endif
#include <gsmlib/gsm_nls.h>
#include <string>
#ifdef WIN32
#include <gsmlib/gsm_win32_serial.h>
#else
#include <gsmlib/gsm_unix_serial.h>
#include <unistd.h>
#endif
#if defined(HAVE_GETOPT_LONG) || defined(WIN32)
#include <getopt.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <gsmlib/gsm_me_ta.h>
#include <gsmlib/gsm_util.h>
#include <gsmlib/gsm_sorted_phonebook.h>
#include <iostream>
using namespace std;
using namespace gsmlib;
#ifdef HAVE_GETOPT_LONG
static struct option longOpts[] =
{
{"xonxoff", no_argument, (int*)NULL, 'X'},
{"phonebook", required_argument, (int*)NULL, 'p'},
{"init", required_argument, (int*)NULL, 'I'},
{"destination", required_argument, (int*)NULL, 'd'},
{"source", required_argument, (int*)NULL, 's'},
{"destination-backend", required_argument, (int*)NULL, 'D'},
{"source-backend", required_argument, (int*)NULL, 'S'},
{"baudrate", required_argument, (int*)NULL, 'b'},
{"charset", required_argument, (int*)NULL, 't'},
{"copy", no_argument, (int*)NULL, 'c'},
{"synchronize", no_argument, (int*)NULL, 'y'},
{"help", no_argument, (int*)NULL, 'h'},
{"version", no_argument, (int*)NULL, 'v'},
{"verbose", no_argument, (int*)NULL, 'V'},
{"indexed", no_argument, (int*)NULL, 'i'},
{(char*)NULL, 0, (int*)NULL, 0}
};
#else
#define getopt_long(argc, argv, options, longopts, indexptr) \
getopt(argc, argv, options)
#endif
// insert those entries from sourcePhonebook into destPhonebook
// that are not already present in destPhonebook
void insertNotPresent(SortedPhonebookRef sourcePhonebook,
SortedPhonebookRef destPhonebook,
bool indexed, bool verbose)
{
for (SortedPhonebookBase::iterator i = sourcePhonebook->begin();
i != sourcePhonebook->end(); ++i)
{
pair<SortedPhonebookBase::iterator, SortedPhonebookBase::iterator> range;
if (indexed)
{
int index = i->index();
range = destPhonebook->equal_range(index);
}
else
{
string text = i->text();
range = destPhonebook->equal_range(text);
}
// do nothing if the entry is already present in the destination
bool alreadyPresent = false;
for (SortedPhonebookBase::iterator j = range.first;
j != range.second; ++j)
{
i->setUseIndex(indexed);
if (i->telephone() == j->telephone())
{
alreadyPresent = true;
break;
}
}
// ... else insert it
if (! alreadyPresent)
{
if (verbose)
{
cout << stringPrintf(_("inserting '%s' tel# %s"),
i->text().c_str(), i->telephone().c_str());
if (indexed)
cout << stringPrintf(_(" (index #%d)"), i->index());
cout << endl;
}
i->setUseIndex(indexed);
destPhonebook->insert(*i); // insert
}
}
}
// update those entries in destPhonebook, that
// - have the same name as one entry in destPhonebook
// - but have a different telephone number
// this is only done if the name in question is unique in the destPhonebook
// the case of several entries having the same in the sourcePhonebook
// is handled - only the first is considered for updating
void updateEntries(SortedPhonebookRef sourcePhonebook,
SortedPhonebookRef destPhonebook,
bool verbose)
{
bool firstLoop = true;
string lastText;
for (SortedPhonebookBase::iterator i = sourcePhonebook->begin();
i != sourcePhonebook->end(); ++i)
{
string text = i->text();
if (! firstLoop && text != lastText)
{
pair<SortedPhonebookBase::iterator,
SortedPhonebookBase::iterator> range =
destPhonebook->equal_range(text);
SortedPhonebookBase::iterator first = range.first;
if (first != destPhonebook->end() && range.second == ++first)
{ // just one text in the destPhonebook
if (! (*range.first == *i)) // overwrite if different in destination
{
if (verbose)
cout << stringPrintf(_("updating '%s' tel# %s to new tel# %s"),
range.first->text().c_str(),
range.first->telephone().c_str(),
i->telephone().c_str())
<< endl;
*range.first = *i;
}
}
lastText = text;
}
firstLoop = false;
}
}
// the same but for indexed phonebooks
void updateEntriesIndexed(SortedPhonebookRef sourcePhonebook,
SortedPhonebookRef destPhonebook,
bool verbose)
{
for (SortedPhonebookBase::iterator i = sourcePhonebook->begin();
i != sourcePhonebook->end(); ++i)
{
int index = i->index();
SortedPhonebookBase::iterator j = destPhonebook->find(index);
if (j != destPhonebook->end())
{ // index present in the destPhonebook
if (! (*j == *i)) // overwrite if different in destination
{
if (verbose)
cout << stringPrintf(_("updating '%s' tel# %s to new tel# %s"
"(index %d)"),
j->text().c_str(),
j->telephone().c_str(),
i->telephone().c_str(), i->index())
<< endl;
*j = *i;
}
}
}
}
// delete those entries from destPhonebook, that are not present
// in sourcePhonebook
void deleteNotPresent(SortedPhonebookRef sourcePhonebook,
SortedPhonebookRef destPhonebook,
bool indexed, bool verbose)
{
for (SortedPhonebookBase::iterator i = destPhonebook->begin();
i != destPhonebook->end(); ++i)
{
pair<SortedPhonebookBase::iterator, SortedPhonebookBase::iterator> range;
if (indexed)
{
int index = i->index();
range = sourcePhonebook->equal_range(index);
}
else
{
string text = i->text();
range = sourcePhonebook->equal_range(text);
}
bool found = false;
for (SortedPhonebookBase::iterator j = range.first;
j != range.second; ++j)
{
i->setUseIndex(indexed);
if (j->telephone() == i->telephone())
{
found = true;
break;
}
}
if (! found)
{
if (verbose)
{
cout << stringPrintf(_("deleting '%s' tel# %s"),
i->text().c_str(), i->telephone().c_str());
if (indexed)
cout << stringPrintf(_(" (index #%d)"), i->index());
cout << endl;
}
destPhonebook->erase(i);
#ifdef BUGGY_MAP_ERASE
deleteNotPresent(sourcePhonebook, destPhonebook, indexed, verbose);
return;
#endif
}
}
}
// *** main program
int main(int argc, char *argv[])
{
try
{
// handle command line options
string destination;
string source;
string destinationBackend;
string sourceBackend;
string baudrate;
bool doSynchronize = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -