]> git.lizzy.rs Git - dragonfireclient.git/blob - src/strfnd.h
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[dragonfireclient.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 class Strfnd{
26     std::string tek;
27     unsigned int p;
28 public:
29     void start(std::string niinq){
30         tek = niinq;
31         p=0;
32     }
33     unsigned int where(){
34         return p;
35     }
36     void to(unsigned int i){
37         p = i;
38     }
39     std::string what(){
40         return tek;
41     }
42     std::string next(std::string plop){
43         //std::cout<<"tek=\""<<tek<<"\" plop=\""<<plop<<"\""<<std::endl;
44         size_t n;
45         std::string palautus;
46         if (p < tek.size())
47         {  
48             //std::cout<<"\tp<tek.size()"<<std::endl;
49             if ((n = tek.find(plop, p)) == std::string::npos || plop == "")
50             {  
51                 //std::cout<<"\t\tn == string::npos || plop == \"\""<<std::endl;
52                 n = tek.size();
53             }
54             else
55             {  
56                 //std::cout<<"\t\tn != string::npos"<<std::endl;
57             }
58             palautus = tek.substr(p, n-p);
59             p = n + plop.length();
60         }
61         //else
62             //std::cout<<"\tp>=tek.size()"<<std::endl;
63                 //std::cout<<"palautus=\""<<palautus<<"\""<<std::endl;
64         return palautus;
65     }
66     
67     // Returns substr of tek up to the next occurence of plop that isn't escaped with '\'
68     std::string next_esc(std::string plop) {
69                 size_t n, realp;
70                 
71         if (p >= tek.size())
72                 return "";
73                 
74                 realp = p;
75                 do {
76                         n = tek.find(plop, p);
77                         if (n == std::string::npos || plop == "")
78                                 n = tek.length();
79                         p = n + plop.length();
80                 } while (n > 0 && tek[n - 1] == '\\');
81                 
82                 return tek.substr(realp, n - realp);
83     }
84     
85         void skip_over(std::string chars){
86                 while(p < tek.size()){
87                         bool is = false;
88                         for(unsigned int i=0; i<chars.size(); i++){
89                                 if(chars[i] == tek[p]){
90                                         is = true;
91                                         break;
92                                 }
93                         }
94                         if(!is) break;
95                         p++;
96                 }
97         }
98     bool atend(){
99         if(p>=tek.size()) return true;
100         return false;
101     }
102     Strfnd(std::string s){
103         start(s);
104     }
105 };
106
107 class WStrfnd{
108     std::wstring tek;
109     unsigned int p;
110 public:
111     void start(std::wstring niinq){
112         tek = niinq;
113         p=0;
114     }
115     unsigned int where(){
116         return p;
117     }
118     void to(unsigned int i){
119         p = i;
120     }
121     std::wstring what(){
122         return tek;
123     }
124     std::wstring next(std::wstring plop){
125         //std::cout<<"tek=\""<<tek<<"\" plop=\""<<plop<<"\""<<std::endl;
126         size_t n;
127         std::wstring palautus;
128         if (p < tek.size())
129         {  
130             //std::cout<<"\tp<tek.size()"<<std::endl;
131             if ((n = tek.find(plop, p)) == std::wstring::npos || plop == L"")
132             {  
133                 //std::cout<<"\t\tn == string::npos || plop == \"\""<<std::endl;
134                 n = tek.size();
135             }
136             else
137             {  
138                 //std::cout<<"\t\tn != string::npos"<<std::endl;
139             }
140             palautus = tek.substr(p, n-p);
141             p = n + plop.length();
142         }
143         //else
144             //std::cout<<"\tp>=tek.size()"<<std::endl;
145                 //std::cout<<"palautus=\""<<palautus<<"\""<<std::endl;
146         return palautus;
147     }
148     
149     std::wstring next_esc(std::wstring plop) {
150                 size_t n, realp;
151                 
152         if (p >= tek.size())
153                 return L"";
154                 
155                 realp = p;
156                 do {
157                         n = tek.find(plop, p);
158                         if (n == std::wstring::npos || plop == L"")
159                                 n = tek.length();
160                         p = n + plop.length();
161                 } while (n > 0 && tek[n - 1] == '\\');
162                 
163                 return tek.substr(realp, n - realp);
164     }
165     
166     bool atend(){
167         if(p>=tek.size()) return true;
168         return false;
169     }
170     WStrfnd(std::wstring s){
171         start(s);
172     }
173 };
174
175 #endif
176