File: gsm_util.h

package info (click to toggle)
gsmlib 1.10%2B20120414.gita5e5ae9a-0.3
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 3,468 kB
  • ctags: 1,907
  • sloc: cpp: 12,315; sh: 11,376; ansic: 2,366; makefile: 263; sed: 93
file content (229 lines) | stat: -rw-r--r-- 5,822 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// *************************************************************************
// * GSM TA/ME library
// *
// * File:    gsm_util.h
// *
// * Purpose: Various utilities
// *
// * Author:  Peter Hofmann (software@pxh.de)
// *
// * Created: 4.5.1999
// *************************************************************************

#ifndef GSM_UTIL_H
#define GSM_UTIL_H

#include <string>
#include <vector>
#include <gsmlib/gsm_error.h>
#ifndef WIN32
#include <sys/time.h>
#endif
#include <stdio.h>

namespace gsmlib
{
  // time type
  typedef struct timeval *GsmTime;

  // some constants
  const char CR = 13;             // ASCII carriage return
  const char LF = 10;             // ASCII line feed

  // common number formats
  const unsigned int UnknownNumberFormat = 129;
  const unsigned int InternationalNumberFormat = 145;

  // convert gsm to Latin-1
  // characters that have no counterpart in Latin-1 are converted to
  // code 172 (Latin-1 boolean not, "")
  std::string gsmToLatin1(std::string s);

  // convert Latin-1 to gsm
  // characters that have no counterpart in GSM are converted to
  // code 16 (GSM Delta)
  std::string latin1ToGsm(std::string s);

  // convert byte buffer of length to hexadecimal string
  std::string bufToHex(const unsigned char *buf, unsigned long length);

  // convert hexString to byte buffer, return false if no hexString
  bool hexToBuf(const std::string &hexString, unsigned char *buf);

  // indicate that a value is not set
  const int NOT_SET = -1;

  // An integer range
  struct IntRange
  {
    int _high, _low;
    IntRange() : _high(NOT_SET), _low(NOT_SET) {}
  };

  // A valid integer range for a given parameter
  struct ParameterRange
  {
    std::string _parameter;
    IntRange _range;
  };

  // *** general-purpose pointer wrapper with reference counting
  class RefBase
  {
  private:
    int _refCount;
    
  public:
    RefBase() : _refCount(0) {}
    int ref() {return _refCount++;}
    int unref() {return --_refCount;}
    int refCount() const {return _refCount;}
  };
  
  template <class T>
    class Ref
    {
    private:
      T *_rep;
    public:
      T *operator->() const {return _rep;}
      T &operator()() {return *_rep;}
      T *getptr() {return _rep;}
      bool isnull() const {return _rep == (T*)NULL;}
      Ref() : _rep((T*)NULL) {}
      Ref(T *pp) : _rep(pp) {if (pp != (T*)NULL) pp->ref();}
      Ref(const Ref &r);
      Ref &operator=(const Ref &r);
      ~Ref();
      bool operator==(const Ref &r) const
        {
          return _rep == r._rep;
        }
    };

  template <class T>
    Ref<T>::Ref(const Ref<T> &r) : _rep(r._rep)
    {
      if (_rep != (T*)NULL) _rep->ref();
    }

  template <class T>
    Ref<T> &Ref<T>::operator=(const Ref<T> &r)
    {
      if (r._rep != (T*)NULL) r._rep->ref();
      if (_rep != (T*)NULL && _rep->unref() == 0) delete _rep;
      _rep = r._rep;
      return *this;
    }

  template <class T>
    Ref<T>::~Ref()
    {
      if (_rep != (T*)NULL && _rep->unref() == 0) delete _rep;
    }

  // utility function return std::string given an int
  std::string intToStr(int i);

  // remove white space from the std::string
  std::string removeWhiteSpace(std::string s);

  // return true if bit is set in vector<bool>
  bool isSet(std::vector<bool> &b, unsigned int bit);

  // return true if filename refers to a file
  // throws exception if filename is neither file nor device
  bool isFile(std::string filename);

  // make backup file adequate for this operating system
  void renameToBackupFile(std::string filename) throw(GsmException);

  // Base class for class for which copying is not allow
  // only used for debugging

  class NoCopy
  {
  public:
    NoCopy() {}

#ifndef NDEBUG
    NoCopy(NoCopy &n);

    NoCopy &operator=(NoCopy &n);
#endif
  };

  // convert std::string to lower case
  std::string lowercase(std::string s);

  // convert std::string to number and check for all digits
  int checkNumber(std::string s) throw(GsmException);

  // like printf, but return C++ std::string
#ifdef HAVE_VSNPRINTF
  std::string stringPrintf(const char *format, ...)
#if	__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
     __attribute__((format (printf, 1, 2)))
#endif
    ;
#else
  // WARNING: This replacement code is
  // - not threadsafe
  // - subject to buffer overruns
#define stringPrintf(format, args...)                   \
        (sprintf(__s, format, ## args), std::string(__s))

  extern char __s[];
#endif // HAVE_VSNPRINTF

  // return debug level
#ifndef NDEBUG
  extern int debugLevel();
#endif

  // interface for interrupting gsmlib activity

  class InterruptBase
  {
  public:
    virtual ~InterruptBase() { }

    // this member should return true if gsmlib is to be interrupted
    virtual bool interrupted() = 0;
  };

  // set global interrupt object
  extern void setInterruptObject(InterruptBase *intObject);
  
  // return true if interrupted
  extern bool interrupted();

  // interface for reporting progress
  
  class ProgressBase
  {
  public:
    virtual ~ProgressBase() { }

    // override this to receive progress reports
    virtual void reportProgress(int part, int total) = 0;
  };

  // set global progress object
  extern void setProgressObject(ProgressBase *progObject);

  // report progress (part/total * 100 is meant to be the percentage)
  // this function is called by
  // - GsmAt::chatv() without arguments, used by Phonebook::Phonebook()
  // - Phonebook::Phonebook()
  // - SortedPhonebook::SortedPhonebook()
  // - SortedSMSStore::SortedSMSStore()
  extern void reportProgress(int part = -1, int total = -1);

  // check for valid text and telephone number
  // throw exception if error
  extern void checkTextAndTelephone(std::string text, std::string telephone)
    throw(GsmException);
};

#endif // GSM_UTIL_H