Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
stringhelper.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2011 John Selbie
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 
18 
19 #include "commonincludes.hpp"
20 #include <stdlib.h>
21 #include <vector>
22 #include <string>
23 #include "stringhelper.h"
24 
25 
26 
27 #define ISWHITESPACE(ch) (((ch >= 9)&&(ch<=0xd))||(ch== ' '))
28 
29 namespace StringHelper
30 {
31  bool IsNullOrEmpty(const char* psz)
32  {
33  return ((psz == NULL) || (psz[0] == '\0'));
34  }
35 
36 
37  void ToLower(std::string& str)
38  {
39  const char* psz = str.c_str();
40  size_t length = str.length();
41  std::string str2;
42  const int diff = ('a' - 'A');
43 
44  if ((psz == NULL) || (length == 0))
45  {
46  return;
47  }
48 
49  str2.reserve(length);
50 
51  for (size_t index = 0; index < length; index++)
52  {
53  char ch = str[index];
54  if ((ch >= 'A') && (ch <= 'Z'))
55  {
56  ch = ch + diff;
57  }
58 
59  str2.push_back(ch);
60  }
61 
62  str = str2;
63 
64  }
65 
66 
67  void Trim(std::string& str)
68  {
69  const char* psz = str.c_str();
70 
71  if (psz == NULL)
72  {
73  return;
74  }
75 
76  int length = str.length();
77  int start = -1;
78  int end = -1;
79  char ch;
80 
81  for (int index = 0; index < length; index++)
82  {
83  ch = psz[index];
84 
85  if (ISWHITESPACE(ch))
86  {
87  continue;
88  }
89  else if (start == -1)
90  {
91  start = index;
92  end = index;
93  }
94  else
95  {
96  end = index;
97  }
98  }
99 
100  if (start != -1)
101  {
102  str = str.substr(start, end-start+1);
103  }
104  }
105 
106 
107  int ValidateNumberString(const char* psz, int nMinValue, int nMaxValue, int* pnResult)
108  {
109  int nVal = 0;
110 
111  if (IsNullOrEmpty(psz) || (pnResult==NULL))
112  {
113  return -1;
114  }
115 
116  nVal = atoi(psz);
117 
118  if(nVal < nMinValue) return -1;
119  if(nVal > nMaxValue) return -1;
120 
121  *pnResult = nVal;
122  return 0;
123 
124  }
125 
126 }
127 
int ValidateNumberString(const char *psz, int nMinValue, int nMaxValue, int *pnResult)
bool IsNullOrEmpty(const char *psz)
void ToLower(std::string &str)
void Trim(std::string &str)
#define ISWHITESPACE(ch)