| Jabber Guest iOS SDK

- Main Page
- Modules
- Classes

All Classes Functions Variables Enumerations Enumerator Properties Groups
utilities.h
1 //
2 // utilities.h
3 // JabberGuest
4 //
5 // Created by davesm on 5/21/13.
6 // Copyright (c) 2013 Cisco. All rights reserved.
7 //
8
9 #ifndef JabberC_utilities_h
10 #define JabberC_utilities_h
11
12 #include <string>
13 #include <vector>
14 #include <sstream>
15
16 inline std::vector<std::string> split( const std::string & text, char delim )
17 {
18 std::vector<std::string> tokens;
19 std::istringstream iss( text );
20 std::string token;
21
22 while ( std::getline( iss, token, delim ) )
23 {
24 tokens.push_back( token );
25 token.clear();
26 }
27
28 return tokens;
29 }
30
31 template <class _T>
32 std::basic_string<_T> & emptyString()
33 {
34 static std::basic_string<_T> empty;
35
36 return empty;
37 }
38
39 template <class _T>
40 class basic_token_iterator : public std::iterator<std::forward_iterator_tag, std::basic_string<_T> >
41 {
42 protected:
43 const std::basic_string<_T> & strInput;
44 const _T * szDelimiters;
45 typename std::basic_string<_T>::size_type bot, eot;
46
47 public:
48 basic_token_iterator( const std::basic_string<_T> & strInput, const _T * szDelimiters ) :
49 strInput( strInput ),
50 szDelimiters( szDelimiters ),
51 bot( strInput.find_first_not_of( szDelimiters ) ),
52 eot( strInput.find_first_of( szDelimiters, bot ) )
53 {
54 }
55 basic_token_iterator( void ) :
56 strInput(emptyString<_T>()),
57 szDelimiters(NULL),
58 bot( 0 ),
59 eot( 0 )
60 {
61 }
62 std::string operator* () const
63 {
64 if ( eot != std::basic_string<_T>::npos )
65 {
66 return strInput.substr( bot, eot-bot );
67 }
68 else if ( bot != std::basic_string<_T>::npos )
69 {
70 return strInput.substr( bot );
71 }
72 else
73 {
74 return std::string();
75 }
76 }
77 basic_token_iterator<_T> & operator++ ()
78 {
79 if ( eot != std::basic_string<_T>::npos )
80 {
81 bot = strInput.find_first_not_of( szDelimiters, eot );
82 eot = strInput.find_first_of( szDelimiters, bot );
83 }
84 else if ( bot != std::basic_string<_T>::npos )
85 {
86 bot = std::basic_string<_T>::npos;
87 }
88
89 return *this;
90 }
91 basic_token_iterator<_T> operator++ (int)
92 {
93 basic_token_iterator<_T> save = *this;
94 operator++();
95
96 return save;
97 }
98 bool operator== ( const basic_token_iterator<_T> & rhs ) const
99 {
100 if ( &strInput == &rhs.strInput && bot == rhs.bot && eot == rhs.eot )
101 {
102 return true;
103 }
104 else if ( strInput.empty() && rhs.strInput.empty() )
105 {
106 return true;
107 }
108 else if ( strInput.empty() && rhs.bot == std::basic_string<_T>::npos && rhs.eot == std::basic_string<_T>::npos )
109 {
110 return true;
111 }
112 else if ( rhs.strInput.empty() && bot == std::basic_string<_T>::npos && eot == std::basic_string<_T>::npos )
113 {
114 return true;
115 }
116 else
117 {
118 return false;
119 }
120 }
121 bool operator!= ( const basic_token_iterator<_T> & rhs ) const
122 {
123 return !(*this == rhs);
124 }
125
126 };
127
128 typedef basic_token_iterator<char> token_iterator;
129 typedef basic_token_iterator<wchar_t> wtoken_iterator;
130
131 #endif