Fix itemdef drop on NULL texture
[oweals/minetest.git] / src / strfnd.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef STRFND_HEADER
21 #define STRFND_HEADER
22
23 #include <string>
24
25 std::string trim(const std::string &str);
26
27 class Strfnd{
28     std::string tek;
29     unsigned int p;
30 public:
31     void start(std::string niinq){
32         tek = niinq;
33         p=0;
34     }
35     unsigned int where(){
36         return p;
37     }
38     void to(unsigned int i){
39         p = i;
40     }
41     std::string what(){
42         return tek;
43     }
44     std::string next(std::string plop){
45         //std::cout<<"tek=\""<<tek<<"\" plop=\""<<plop<<"\""<<std::endl;
46         size_t n;
47         std::string palautus;
48         if (p < tek.size())
49         {  
50             //std::cout<<"\tp<tek.size()"<<std::endl;
51             if ((n = tek.find(plop, p)) == std::string::npos || plop == "")
52             {  
53                 //std::cout<<"\t\tn == string::npos || plop == \"\""<<std::endl;
54                 n = tek.size();
55             }
56             else
57             {  
58                 //std::cout<<"\t\tn != string::npos"<<std::endl;
59             }
60             palautus = tek.substr(p, n-p);
61             p = n + plop.length();
62         }
63         //else
64             //std::cout<<"\tp>=tek.size()"<<std::endl;
65                 //std::cout<<"palautus=\""<<palautus<<"\""<<std::endl;
66         return palautus;
67     }
68     
69     // Returns substr of tek up to the next occurence of plop that isn't escaped with '\'
70     std::string next_esc(std::string plop) {
71                 size_t n, realp;
72                 
73         if (p >= tek.size())
74                 return "";
75                 
76                 realp = p;
77                 do {
78                         n = tek.find(plop, p);
79                         if (n == std::string::npos || plop == "")
80                                 n = tek.length();
81                         p = n + plop.length();
82                 } while (n > 0 && tek[n - 1] == '\\');
83                 
84                 return tek.substr(realp, n - realp);
85     }
86     
87         void skip_over(std::string chars){
88                 while(p < tek.size()){
89                         bool is = false;
90                         for(unsigned int i=0; i<chars.size(); i++){
91                                 if(chars[i] == tek[p]){
92                                         is = true;
93                                         break;
94                                 }
95                         }
96                         if(!is) break;
97                         p++;
98                 }
99         }
100     bool atend(){
101         if(p>=tek.size()) return true;
102         return false;
103     }
104     Strfnd(std::string s){
105         start(s);
106     }
107 };
108
109 class WStrfnd{
110     std::wstring tek;
111     unsigned int p;
112 public:
113     void start(std::wstring niinq){
114         tek = niinq;
115         p=0;
116     }
117     unsigned int where(){
118         return p;
119     }
120     void to(unsigned int i){
121         p = i;
122     }
123     std::wstring what(){
124         return tek;
125     }
126     std::wstring next(std::wstring plop){
127         //std::cout<<"tek=\""<<tek<<"\" plop=\""<<plop<<"\""<<std::endl;
128         size_t n;
129         std::wstring palautus;
130         if (p < tek.size())
131         {  
132             //std::cout<<"\tp<tek.size()"<<std::endl;
133             if ((n = tek.find(plop, p)) == std::wstring::npos || plop == L"")
134             {  
135                 //std::cout<<"\t\tn == string::npos || plop == \"\""<<std::endl;
136                 n = tek.size();
137             }
138             else
139             {  
140                 //std::cout<<"\t\tn != string::npos"<<std::endl;
141             }
142             palautus = tek.substr(p, n-p);
143             p = n + plop.length();
144         }
145         //else
146             //std::cout<<"\tp>=tek.size()"<<std::endl;
147                 //std::cout<<"palautus=\""<<palautus<<"\""<<std::endl;
148         return palautus;
149     }
150     
151     std::wstring next_esc(std::wstring plop) {
152                 size_t n, realp;
153                 
154         if (p >= tek.size())
155                 return L"";
156                 
157                 realp = p;
158                 do {
159                         n = tek.find(plop, p);
160                         if (n == std::wstring::npos || plop == L"")
161                                 n = tek.length();
162                         p = n + plop.length();
163                 } while (n > 0 && tek[n - 1] == '\\');
164                 
165                 return tek.substr(realp, n - realp);
166     }
167     
168     bool atend(){
169         if(p>=tek.size()) return true;
170         return false;
171     }
172     WStrfnd(std::wstring s){
173         start(s);
174     }
175 };
176
177 inline std::string trim(const std::string &s)
178 {
179         std::string str = s;
180     while( 
181             str.length()>0
182             &&
183             (
184              str.substr(0,               1)==" "     ||
185              str.substr(0,               1)=="\t"    ||
186              str.substr(0,               1)=="\r"    ||
187              str.substr(0,               1)=="\n"    ||
188              str.substr(str.length()-1,  1)==" "     ||
189              str.substr(str.length()-1,  1)=="\t"    ||
190              str.substr(str.length()-1,  1)=="\r"    ||
191              str.substr(str.length()-1,  1)=="\n"
192             )
193          )
194     {  
195         if      (str.substr(0,              1)==" ")
196                         str = str.substr(1,str.length()-1);
197         else if (str.substr(0,              1)=="\t")
198                         str = str.substr(1,str.length()-1);
199         else if (str.substr(0,              1)=="\r")
200                         str = str.substr(1,str.length()-1);
201         else if (str.substr(0,              1)=="\n")
202                         str = str.substr(1,str.length()-1);
203         else if (str.substr(str.length()-1, 1)==" ")
204                         str = str.substr(0,str.length()-1);
205         else if (str.substr(str.length()-1, 1)=="\t")
206                         str = str.substr(0,str.length()-1);
207         else if (str.substr(str.length()-1, 1)=="\r")
208                         str = str.substr(0,str.length()-1);
209         else if (str.substr(str.length()-1, 1)=="\n")
210                         str = str.substr(0,str.length()-1);
211     }
212     return str;
213 }
214
215 #endif
216