PANDORA: Make GLES context compatible with latest driver (FB only, no X11)
[mupen64plus-pandora.git] / source / mupen64plus-video-arachnoid / src / config / StringFunctions.cpp
1 /******************************************************************************
2  * Arachnoid Graphics Plugin for Mupen64Plus
3  * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/
4  *
5  * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  *****************************************************************************/
21
22 #include "StringFunctions.h"
23 #include <cstring>
24 using std::string;
25 using std::vector;
26
27 namespace StringFunctions {
28
29 //-----------------------------------------------------------------------------
30 // split
31 // @param str The string you want to split into many strings
32 // @param delims The character(s) that split strings
33 // @return Vector with the new strings
34 //-----------------------------------------------------------------------------
35 vector<string> split(const string& str, const string& delims, size_t maxSplits)
36 {
37     size_t pos;
38     size_t start = 0;
39     vector<string> strings;
40     size_t numSplits = 0;
41        
42     do 
43     {
44         //Find next interesting data
45         start = str.find_first_not_of(delims, start);
46
47         //Try to find delimiter
48         pos = str.find_first_of(delims, start);
49
50         if (pos == start)
51         {            
52             //Do nothing
53             start = pos + 1;  
54         }
55         else if (pos == string::npos || (maxSplits!=string::npos && numSplits == maxSplits)  )
56         {
57             //No more spliting, copy the rest of the string
58             strings.push_back( str.substr(start) );
59             break;
60         }
61         else
62         {
63             //Split string and add to return 
64             strings.push_back( str.substr(start, pos - start) );
65             start = pos + 1;
66             numSplits++;
67         }
68
69     } while (pos != string::npos);
70
71     //Return vector with strings
72     return strings;
73 }
74
75 //-----------------------------------------------------------------------------
76 // String Trim
77 //-----------------------------------------------------------------------------
78 void trim(string& str, bool left, bool right, const string delims)
79 {
80     //Erase characters from the left
81     if(left)
82     {
83         str.erase(0, str.find_first_not_of(delims)); 
84     }
85
86     //Erase characters from the right
87     if(right) 
88     {
89         str.erase(str.find_last_not_of(delims)+1); 
90     }    
91 }
92
93 //-----------------------------------------------------------------------------
94 // String Trim
95 //-----------------------------------------------------------------------------
96 char* trim(char* str, bool left, bool right)
97 {
98     //Trim from the left
99     if(left)
100     {
101         //Set pointer to string
102         char* p1 = str;
103         char* p2 = str;
104         char* end = &str[strlen(str)-1];
105
106         //Skip white spaces
107         while ( isspace( *p1 ) && p1 != end )
108         {
109             ++p1;
110         }
111
112         char* newEnd = p1;
113
114         //Copy characters to begining of string
115         while ( p2 != end )
116         {
117             if ( p1 < newEnd )
118             {
119                 *p2 = '\0';
120             }
121             else
122             {
123                 *p2 = *p1;
124             }
125
126             ++p1;
127             ++p2;
128         }
129     }
130
131     //Trim from the right
132     if(right) 
133     {
134         //Point to end of string
135         char* end = str + strlen(str) - 1;
136
137         //Remove white spaces in the end
138         while( end >= str && *end == ' ' )
139         {
140             *end-- = '\0';
141         }
142     }  
143     return str;
144 }
145
146 //-----------------------------------------------------------------------------
147 // String Trim
148 //-----------------------------------------------------------------------------
149 std::vector<string> split(const char* str, const std::string& delims)
150 {
151     return split(string(str), delims);
152 }
153
154
155 } //namespace StringFunctions