⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gsm_phonebook.cc

📁 对各种手机进行编程的工具包源码gsmlib 1.9版本。
💻 CC
📖 第 1 页 / 共 2 页
字号:
// *************************************************************************// * GSM TA/ME library// *// * File:    gsm_phonebook.cc// *// * Purpose: Phonebook management functions// *// * Author:  Peter Hofmann (software@pxh.de)// *// * Created: 6.5.1999// *************************************************************************#ifdef HAVE_CONFIG_H#include <gsm_config.h>#endif#include <gsmlib/gsm_nls.h>#include <gsmlib/gsm_sysdep.h>#include <gsmlib/gsm_phonebook.h>#include <gsmlib/gsm_parser.h>#include <gsmlib/gsm_me_ta.h>#include <strstream>#include <iostream>#include <assert.h>#include <ctype.h>using namespace std;using namespace gsmlib;// PhonebookEntry membersPhonebookEntry::PhonebookEntry(const PhonebookEntryBase &e)  throw(GsmException) : _cached(true), _myPhonebook(NULL){  set(e.telephone(), e.text(), e.index(), e.useIndex());}void PhonebookEntry::set(string telephone, string text, int index,                         bool useIndex)  throw(GsmException){  checkTextAndTelephone(text, telephone);  if (_myPhonebook != NULL)  {    if (text.length() > _myPhonebook->getMaxTextLen())      throw GsmException(        stringPrintf(_("length of text '%s' exceeds maximum text "                       "length (%d characters) of phonebook '%s'"),                     text.c_str(), _myPhonebook->getMaxTextLen(),                     _myPhonebook->name().c_str()),        ParameterError);        if (telephone.length() > _myPhonebook->getMaxTelephoneLen())      throw GsmException(        stringPrintf(_("length of telephone number '%s' "                        "exceeds maximum telephone number "                       "length (%d characters) of phonebook '%s'"),                     telephone.c_str(), _myPhonebook->getMaxTelephoneLen(),                     _myPhonebook->name().c_str()),        ParameterError);    _myPhonebook->writeEntry(_index, telephone, text);  }  else    _index = index;  _useIndex = useIndex;  _cached = true;  _telephone = telephone;  _text = text;  _changed = true;}string PhonebookEntry::text() const throw(GsmException){  if (! cached())  {    assert(_myPhonebook != NULL);    // these operations are at least "logically const"    PhonebookEntry *thisEntry = const_cast<PhonebookEntry*>(this);    _myPhonebook->readEntry(_index, thisEntry->_telephone, thisEntry->_text);    thisEntry->_cached = true;  }  return _text;}string PhonebookEntry::telephone() const throw(GsmException){  if (! cached())  {    assert(_myPhonebook != NULL);    // these operations are at least "logically const"    PhonebookEntry *thisEntry = const_cast<PhonebookEntry*>(this);    _myPhonebook->readEntry(_index, thisEntry->_telephone, thisEntry->_text);    thisEntry->_cached = true;  }  return _telephone;}bool PhonebookEntry::cached() const{  if (_myPhonebook == NULL)    return _cached;  else    return _cached && _myPhonebook->_useCache;}PhonebookEntry::PhonebookEntry(const PhonebookEntry &e) throw(GsmException){  set(e._telephone, e._text, e._index, e._useIndex);}PhonebookEntry &PhonebookEntry::operator=(const PhonebookEntry &e)  throw(GsmException){  set(e._telephone, e._text, e._index, e._useIndex);  return *this;}// Phonebook membersint Phonebook::parsePhonebookEntry(string response,                                   string &telephone, string &text){  // this is a workaround for a bug that occurs with my ME/TA combination  // some texts are truncated and don't have a trailing "  if (response.length() > 0 && response[response.length() - 1] != '"')    response += '"';  Parser p(response);  int index = p.parseInt();  p.parseComma();  // handle case of empty entry  if (p.getEol().substr(0, 5) == "EMPTY")  {    telephone = "";    text = "";    return index;  }  telephone = p.parseString();  p.parseComma();  unsigned int numberFormat = p.parseInt();  if (numberFormat != UnknownNumberFormat &&      numberFormat != InternationalNumberFormat)    cerr << "*** GSMLIB WARNING: Unexpected number format when reading from "         << "phonebook: " << numberFormat << " ***" << endl;  p.parseComma();  text = p.parseString(false, true);  if (lowercase(_myMeTa.getCurrentCharSet()) == "gsm")    text = gsmToLatin1(text);  if (numberFormat == InternationalNumberFormat)  {    // skip leading "+" signs that may already exist    while (telephone.length() > 0 && telephone[0] == '+')      telephone = telephone.substr(1);    telephone = "+" + telephone;  }  return index;}void Phonebook::readEntry(int index, string &telephone, string &text)  throw(GsmException){  // select phonebook  _myMeTa.setPhonebook(_phonebookName);  // read entry  string response = _at->chat("+CPBR=" + intToStr(index), "+CPBR:",                              false, // dont't ignore errors                              true); // but accept empty responses  // (the latter is necessary for some mobile phones that return nothing  // if the entry is empty)  if (response.length() == 0)   // OK phone returned empty response  {    telephone = text = "";      // then the entry is empty as well  }  else    parsePhonebookEntry(response, telephone, text);#ifndef NDEBUG  if (debugLevel() >= 1)    cerr << "*** Reading PB entry " << index << " number " << telephone          << " text " << text << endl;#endif}void Phonebook::findEntry(string text, int &index, string &telephone)  throw(GsmException){  // select phonebook  _myMeTa.setPhonebook(_phonebookName);  // read entry  string response = _at->chat("+CPBF=\"" + text + "\"", "+CPBF:",                              false, // dont't ignore errors                              true); // but accept empty responses  // (the latter is necessary for some mobile phones that return nothing  // if the entry is empty)  if (response.length() == 0)   // OK phone returned empty response  {    telephone = "";      // then the entry is empty as well    index = 0;  }  else    index=parsePhonebookEntry(response, telephone, text);#ifndef NDEBUG  if (debugLevel() >= 1)    cerr << "*** Finding PB entry " << text << " number " << telephone          << " index " << index << endl;#endif}void Phonebook::writeEntry(int index, string telephone, string text)  throw(GsmException){#ifndef NDEBUG  if (debugLevel() >= 1)    cerr << "*** Writing PB entry #" << index << " number '" << telephone         << "' text '" << text << "'" << endl;#endif  // select phonebook  _myMeTa.setPhonebook(_phonebookName);  // write entry  string s;  if (telephone == "" && text == "")  {    ostrstream os;    os << "+CPBW=" << index;    os << ends;    char *ss = os.str();    s = string(ss);    delete[] ss;  }  else  {    int type;    if (telephone.find('+') == string::npos)      type = UnknownNumberFormat;    else      type = InternationalNumberFormat;    string gsmText = text;    if (lowercase(_myMeTa.getCurrentCharSet()) == "gsm")      gsmText = latin1ToGsm(gsmText);    ostrstream os;    os << "+CPBW=" << index << ",\"" << telephone << "\"," << type       << ",\"";    os << ends;    char *ss = os.str();    s = string(ss);    delete[] ss;    // this cannot be added with ostrstream because the gsmText can    // contain a zero (GSM default alphabet for '@')    s +=  gsmText + "\"";  }  _at->chat(s);}Phonebook::iterator Phonebook::insertFirstEmpty(string telephone, string text)  throw(GsmException){  for (int i = 0; i < _maxSize; i++)    if (_phonebook[i].empty())    {      _phonebook[i].set(telephone, text);      adjustSize(1);      return begin() + i;    }  throw GsmException(_("phonebook full"), OtherError);}Phonebook::iterator Phonebook::insert(const string telephone,                                      const string text,                                      const int index){  for (int i = 0; i < _maxSize; i++)    if (_phonebook[i].index() == index)      if (_phonebook[i].empty())      {        _phonebook[i].set(telephone, text);        adjustSize(1);        return begin() + i;      }      else        throw GsmException(_("attempt to overwrite phonebook entry"),                           OtherError);  return end();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -