Prevent potential side effects and use local variables here
[pdns-ldap-backend/landonf.git] / src / utils.hh
blob35803d247b3f7de302fc72df555545bc21ef20fe
1 #include <string>
2 #include <vector>
3 #include <time.h>
4 #include <stdlib.h>
5 #include <pdns/misc.hh>
6 #include <pdns/utility.hh>
9 #ifndef LDAPBACKEND_UTILS_HH
10 #define LDAPBACKEND_UTILS_HH
12 using std::string;
13 using std::vector;
16 inline string ptr2ip4( vector<string>& parts )
18 string ip;
19 parts.pop_back();
20 parts.pop_back();
23 ip = parts.back();
24 parts.pop_back();
26 while( !parts.empty() )
28 ip += "." + parts.back();
29 parts.pop_back();
32 return ip;
36 inline string ptr2ip6( vector<string>& parts )
38 int i = 0;
39 string ip;
42 parts.pop_back();
43 parts.pop_back();
45 while( i < 3 && parts.size() > 1 && parts.back() == "0" )
47 parts.pop_back();
48 i++;
51 while( i++ < 4 && !parts.empty() )
53 ip += parts.back();
54 parts.pop_back();
57 while( !parts.empty() )
59 i = 0;
60 ip += ":";
62 while( i < 3 && parts.size() > 1 && parts.back() == "0" )
64 parts.pop_back();
65 i++;
68 while( i++ < 4 && !parts.empty() )
70 ip += parts.back();
71 parts.pop_back();
75 return ip;
79 inline string ip2ptr4( const string& ip )
81 string ptr;
82 vector<string> parts;
84 stringtok( parts, ip, "." );
85 while( !parts.empty() )
87 ptr += parts.back() + ".";
88 parts.pop_back();
91 return ptr + "in-addr.arpa";
95 inline string ip2ptr6( const string& ip )
97 string ptr, part, defstr;
98 vector<string> parts;
100 stringtok( parts, ip, ":" );
101 while( !parts.empty() )
103 defstr = "0.0.0.0.";
104 part = parts.back();
106 while( part.length() < 4 )
108 part = "0" + part;
111 defstr[0] = part[3];
112 defstr[2] = part[2];
113 defstr[4] = part[1];
114 defstr[6] = part[0];
115 ptr += defstr;
116 parts.pop_back();
119 return ptr + "ip6.arpa";
123 inline string strbind( const string& search, const string& replace, string subject )
125 size_t pos = 0;
128 while( ( pos = subject.find( search, pos ) ) != string::npos )
130 subject.replace( pos, search.size(), replace );
131 pos += replace.size();
134 return subject;
138 * Convert a LDAP time string to a time_t. Return 0 if unable to convert
141 inline time_t str2tstamp( const string& str )
143 char* tmp;
144 struct tm tm;
146 tmp = strptime( str.c_str(), "%Y%m%d%H%M%SZ", &tm );
148 if( tmp != NULL && *tmp == 0 )
150 return Utility::timegm( &tm );
153 return 0;
156 #endif