~ubuntu-branches/debian/jessie/gsmlib/jessie

« back to all changes in this revision

Viewing changes to gsmlib/gsm_sorted_phonebook.cc

  • Committer: Package Import Robot
  • Author(s): Andrew Shadura
  • Date: 2013-10-15 13:29:27 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20131015132927-1i5iwvj21wue3uvu
Tags: 1.10+20120414.gita5e5ae9a-0.1
* Non-maintainer upload.
* Update to the latest Git version by Vianney Bouchaud.
* Use 3.0 (quilt) source package format.
* Own the run subdirectory (Closes: #689891).
* Don't remove the system user on package remove.
* Fix init script (LP: #30228).

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
const int MAX_LINE_SIZE = 1000;
26
26
 
27
 
using namespace std;
28
27
using namespace gsmlib;
29
28
 
30
 
string SortedPhonebook::escapeString(string s)
 
29
std::string SortedPhonebook::escapeString(std::string s)
31
30
{
32
 
  string result;
 
31
  std::string result;
33
32
  
34
33
  for (const char *pp = s.c_str(); *pp != 0; ++pp)
35
34
  {
47
46
  return result;
48
47
}
49
48
 
50
 
string SortedPhonebook::unescapeString(char *line, unsigned int &pos)
 
49
std::string SortedPhonebook::unescapeString(char *line, unsigned int &pos)
51
50
{
52
 
  string result;
 
51
  std::string result;
53
52
  bool escaped = false;
54
53
 
55
54
  while (! (line[pos] == '|' && ! escaped) && line[pos] != 0 &&
80
79
  return result;
81
80
}
82
81
 
83
 
void SortedPhonebook::readPhonebookFile(istream &pbs, string filename)
 
82
void SortedPhonebook::readPhonebookFile(std::istream &pbs, std::string filename)
84
83
  throw(GsmException)
85
84
{
86
85
  // read entries
98
97
                         OSError);
99
98
 
100
99
    // convert line to newEntry (line format : [index] '|' text '|' number
101
 
    string text, telephone;
 
100
    std::string text, telephone;
102
101
    unsigned int pos = 0;
103
102
 
104
103
    // parse index
105
 
    string indexS = unescapeString(line, pos);
 
104
    std::string indexS = unescapeString(line, pos);
106
105
    int index = -1;
107
106
    if (indexS.length() == 0)
108
107
    {
161
160
    }
162
161
 
163
162
    // open stream
164
 
    ostream *pbs = NULL;
 
163
    std::ostream *pbs = NULL;
165
164
    try
166
165
    {
167
166
      if (_filename == "")
168
 
        pbs = &cout;
 
167
        pbs = &std::cout;
169
168
      else
170
 
        pbs = new ofstream(_filename.c_str());
 
169
        pbs = new std::ofstream(_filename.c_str());
171
170
      
172
171
      if (pbs->bad())
173
172
        throw GsmException(
181
180
           i != _sortedPhonebook.end(); ++i)
182
181
      {
183
182
        // convert entry to output line
184
 
        string line =
 
183
        std::string line =
185
184
          (_useIndices ? intToStr(i->second->index()) : "") + "|" +
186
185
          escapeString(i->second->text()) + "|" +
187
186
          escapeString(i->second->telephone());
188
187
      
189
188
        // write out the line
190
 
        *pbs << line << endl;
 
189
        *pbs << line << std::endl;
191
190
        if (pbs->bad())
192
191
          throw GsmException(
193
192
            stringPrintf(_("error writing to file '%s'"),
198
197
    }
199
198
    catch(GsmException &e)
200
199
    {
201
 
      if (pbs != &cout) delete pbs;
 
200
      if (pbs != &std::cout) delete pbs;
202
201
      throw;
203
202
    }
204
203
    // close file
205
 
    if (pbs != &cout) delete pbs;
 
204
    if (pbs != &std::cout) delete pbs;
206
205
 
207
206
    // reset all changed states
208
207
    _changed = false;
218
217
    ParameterError);
219
218
}
220
219
 
221
 
SortedPhonebook::SortedPhonebook(string filename, bool useIndices)
 
220
SortedPhonebook::SortedPhonebook(std::string filename, bool useIndices)
222
221
  throw(GsmException) :
223
222
  _changed(false), _fromFile(true), _madeBackupFile(false),
224
223
  _sortOrder(ByIndex), _useIndices(useIndices), _readonly(false),
225
224
  _filename(filename)
226
225
{
227
226
  // open the file
228
 
  ifstream pbs(filename.c_str());
 
227
  std::ifstream pbs(filename.c_str());
229
228
  if (pbs.bad())
230
229
    throw GsmException(stringPrintf(_("cannot open file '%s'"),
231
230
                                    filename.c_str()),
242
241
{
243
242
  // read from stdin
244
243
  if (fromStdin)
245
 
    readPhonebookFile(cin, (string)_("<STDIN>"));
 
244
    readPhonebookFile(std::cin, (std::string)_("<STDIN>"));
246
245
}
247
246
 
248
247
SortedPhonebook::SortedPhonebook(PhonebookRef mePhonebook)
415
414
  return insert(x);
416
415
}
417
416
 
418
 
SortedPhonebook::size_type SortedPhonebook::erase(string &key)
 
417
SortedPhonebook::size_type SortedPhonebook::erase(std::string &key)
419
418
  throw(GsmException)
420
419
{
421
420
  // deallocate memory or remove from underlying ME phonebook