Daemon compiles now
[mnenc] / php.cpp
1 //      php.cpp
2 //      
3 //      Copyright 2010 Micke Nordin <mickewiki@gmail.com>
4 //      
5 //      This program is free software; you can redistribute it and/or modify
6 //      it under the terms of the GNU General Public License as published by
7 //      the Free Software Foundation; either version 3 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 General Public License for more details.
14 //      
15 //      You should have received a copy of the GNU General Public License
16 //      along with this program; if not, write to the Free Software
17 //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 //      MA 02110-1301, USA.
19 #include "php.hpp"
20
21 vector<string> explode(string str, string separator) {
22         vector<string> results; //To hold the reslut
23     string::size_type found; //Varible to compare with
24     found = str.find_first_of(separator); //See if/where we have the separator
25     while(found != string::npos) { //While we have separators
26         if(found > 0) { //If we have a match
27             results.push_back(str.substr(0,found)); //Put it in the vector
28         }
29         str = str.substr(found+1); //Remove the part that has allready been used
30         found = str.find_first_of(separator);//Find next
31     }
32     if(str.length() > 0) { //Special case for the last bit
33         results.push_back(str);
34     }
35         
36         return results;
37 }
38
39 string reverse(string &str) {
40         char c[str.size() + 1];
41         int j = 0;
42
43         for(int i = str.size(); i > 0; i--) {
44                 c[j] = str[i];
45                 j++;
46         }
47         c[str.size()] = '\0';
48         string rstr = c;
49         return rstr;
50
51 }
52
53 string trim(string &str, char const* sepSet) {
54         string::size_type const first = str.find_first_not_of(sepSet); //Find first char that is not the ones we want to remove
55         
56         if(first==string::npos ) {
57                 return string();
58         } else {
59                 str = str.substr(first, str.find_last_not_of(sepSet)-first+1); //If we have chars we want to keep return those
60                 str = reverse(str);
61                 str.substr(first, str.find_last_not_of(sepSet)-first+1);
62                 return reverse(str);
63         }
64 }