1 #include <sstream> 2 #include <string> 3 4 #include "misc.h" 5 6 using namespace std; 7 indent(unsigned lvl)8string indent(unsigned lvl) 9 { 10 string result; 11 12 while(lvl--) 13 result += " "; 14 15 return result; 16 } 17 int_to_str(int i)18string int_to_str(int i) 19 { 20 stringstream ost; 21 22 ost << i << char(0); 23 24 return ost.str(); 25 } 26 pow(int base,unsigned exp)27int pow(int base, unsigned exp) 28 { 29 if(!exp) return 1; 30 31 if(exp == 1) return base; 32 33 return base * pow(base, exp - 1); 34 } 35 str2int(const string & str,int & arg)36bool str2int(const string& str, int& arg) 37 { 38 if(!str.length()) 39 return false; 40 arg = 0; 41 for(unsigned i = 0; i < str.length(); i++) 42 if(str[i] >= '0' && str[i] <= '9') 43 arg = arg * 10 + (str[i] - '0'); 44 else 45 return false; 46 47 return true; 48 } 49 50 51 str2float(const string & str,float & fl)52bool str2float(const string& str, float& fl) 53 { 54 if(!str.length()) 55 return false; 56 57 bool had_point = false; 58 unsigned int beyond_point = 0; 59 60 int int_first = 0; 61 for(unsigned i = 0; i < str.length(); i++) { 62 if(had_point) 63 beyond_point++; 64 65 if(str[i] >= '0' && str[i] <= '9') 66 int_first = int_first*10 + (str[i] - '0'); 67 else if(str[i] == '.') { 68 if(had_point) 69 return false; //found two . 70 else 71 had_point = true; 72 } else 73 return false; 74 } 75 76 fl = int_first / ((float) pow(10, beyond_point)); 77 78 return true; 79 } 80 81 //helping out for now, should change soon is_validip(const string & ip)82bool is_validip(const string& ip) 83 { 84 int nr_p = 0; 85 for(unsigned i = 0; i < ip.length(); i++) 86 if(ip[i] == '.') 87 nr_p++; 88 else if(ip[i] < '0' || ip[i] > '9') 89 return false; 90 91 if(nr_p != 3) 92 return false; 93 94 return true; 95 } 96 skiptillendofline(istream & in)97void skiptillendofline(istream& in) 98 { 99 char ch; 100 in.get(ch); 101 while(in && !in.eof() && ch != '\n') 102 in.get(ch); 103 } 104