YAPOG  0.0.1
Yet Another Pokemon Online Game
StringHelper.hxx
Go to the documentation of this file.
1 #ifndef YAPOG_STRINGHELPER_HXX
2 # define YAPOG_STRINGHELPER_HXX
3 
4 # include <sstream>
5 
6 # include <boost/lexical_cast.hpp>
7 
8 # include "YAPOG/Macros.hpp"
14 
15 namespace yap
16 {
17  template <typename T>
18  inline String StringHelper::ToString (const T& value)
19  {
20  std::ostringstream oss;
21 
22  oss << value;
23 
24  return oss.str ();
25  }
26 
27  template <typename T>
28  inline int StringHelper::CompareString(const T& s1, const T& s2)
29  {
30  String::const_iterator it1 = s1.begin();
31  String::const_iterator it2 = s2.begin();
32 
33  while ((it1 != s1.end()) && (it2 != s2.end())) {
34  if(::toupper(*it1) != ::toupper(*it2))
35  return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1;
36  ++it1;
37  ++it2;
38  }
39 
40  size_t size1 = s1.size();
41  size_t size2 = s2.size();
42 
43  if (size1 == size2)
44  return 0;
45  return (size1 < size2) ? -1 : 1;
46  }
47 
48  template <typename T>
50  {
51  String::iterator it = s.begin();
52 
53  while (it != s.end())
54  {
55  if (!isspace(*it))
56  break;
57  ++it;
58  }
59 
60  s.erase(s.begin(), it);
61  return s;
62  }
63 
64  template <typename T>
66  {
67  String::reverse_iterator it = s.rbegin();
68 
69  while (it != s.rend())
70  {
71  if (!isspace(*it))
72  break;
73  ++it;
74  }
75  String::difference_type diff = s.rend() - it;
76  s.erase(s.begin() + diff, s.end());
77  return s;
78  }
79 
80  template <typename T>
81  inline String& StringHelper::Trim(T& s)
82  {
83  return Ltrim(Rtrim(s));
84  }
85 
86  template <typename T>
87  inline String StringHelper::Trim(const T& s)
88  {
89  String str = s;
90  return Trim(str);
91  }
92 
93  template <typename T>
94  inline String StringHelper::Rtrim(const T& s)
95  {
96  String str = s;
97  return Rtrim(str);
98  }
99 
100  template <typename T>
101  inline String StringHelper::Ltrim(const T& s)
102  {
103  String str = s;
104  return Ltrim(str);
105  }
106 
107  template <typename T>
108  inline T StringHelper::Parse (const String& str)
109  {
110  return boost::lexical_cast <T> (str);
111  }
112 
113  template <>
114  inline String StringHelper::ToString<Direction> (const Direction& value)
115  {
116  switch (value)
117  {
118  case Direction::North: return "North";
119  case Direction::NorthEast: return "NorthEast";
120  case Direction::East: return "East";
121  case Direction::SouthEast: return "SouthEast";
122  case Direction::South: return "South";
123  case Direction::SouthWest: return "SouthWest";
124  case Direction::West: return "West";
125  case Direction::NorthWest: return "NorthWest";
126  default: YAPOG_THROW("Not a valid direction.");
127  }
128  }
129 
130  template <>
131  inline Direction StringHelper::Parse<Direction> (const String& str)
132  {
133  if (str == "North") return Direction::North;
134  if (str == "NorthEast") return Direction::NorthEast;
135  if (str == "East") return Direction::East;
136  if (str == "SouthEast") return Direction::SouthEast;
137  if (str == "South") return Direction::South;
138  if (str == "SouthWest") return Direction::SouthWest;
139  if (str == "West") return Direction::West;
140  if (str == "NorthWest") return Direction::NorthWest;
141  YAPOG_THROW("Not a valid direction: " + str);
142  }
143 
144  template <>
145  inline ExperienceType StringHelper::Parse<ExperienceType> (const String& str)
146  {
147  if (str == "Slow") return ExperienceType::Slow;
148  if (str == "MediumSlow") return ExperienceType::MediumSlow;
149  if (str == "MediumFast") return ExperienceType::MediumFast;
150  if (str == "Fast") return ExperienceType::Fast;
151  if (str == "Fluctuating") return ExperienceType::Fluctuating;
152  if (str == "Erratic") return ExperienceType::Erratic;
153  YAPOG_THROW("Not a valid experience type: " + str);
154  }
155 
156  template <>
157  inline String StringHelper::ToString<Time> (const Time& value)
158  {
159  uint s = 0;
160  uint m = 0;
161  uint h = 0;
162  String result = "";
163 
164  if (value.GetValue () > 59)
165  {
166  m = static_cast<uint>(value.GetValue () / 60);
167  s = static_cast<uint>(value.GetValue ()) % 60;
168 
169  if (m > 59)
170  {
171  h = m / 60;
172  m = m % 60;
173  }
174  }
175  else
176  {
177  s = static_cast<uint>(value.GetValue ());
178  }
179 
180  if (h < 10)
181  result += "0";
182 
183  result += ToString (h) + ":";
184 
185  if (m < 10)
186  result += "0";
187 
188  result += ToString (m) + ":";
189 
190  if (s < 10)
191  result += "0";
192 
193  result += ToString (s);
194 
195  return result;
196  }
197 
198 } // namespace yap
199 
200 #endif // YAPOG_STRINGHELPER_HXX