source: tags/0.55/wutils.cpp @ 9

Last change on this file since 9 was 9, checked in by willem, 11 years ago

willem

File size: 4.8 KB
Line 
1#include <string>
2#include <vector>
3#include <string.h>
4#include <iostream>
5#include <algorithm>
6#include <sys/time.h>
7#include "wutils.h"
8#include <sys/stat.h>
9#include <sys/types.h>
10#include <errno.h>
11#include <stdlib.h>
12
13//
14// splits string s to vector v, using separator character sep
15// return value: v
16// It seems that it follows the python implementation of split()
17//
18std::vector <std::string> split(std::vector <std::string> &v, const std::string s, const char sep)
19{
20  v.clear();
21  for (size_t p=0, q=0; q != s.npos; p = q + 1)
22    v.push_back(s.substr(p,(q=s.find(sep,p))-p));
23  return v;
24}
25
26// converts string to char *
27// resulting string is created using malloc. Use free to free.
28char* str_to_charp(const std::string s)
29{
30  return strdup(s.c_str());
31}
32
33/*
34 * converts string to hex representation
35 */
36std::string strtohex(const std::string s)
37{
38  std::string a;
39  const std::string c=
40  "000102030405060708090a0b0c0d0e0f"
41  "101112131415161718191a1b1c1d1e1f"
42  "202122232425262728292a2b2c2d2e2f"
43  "303132333435363738393a3b3c3d3e3f"
44  "404142434445464748494a4b4c4d4e4f"
45  "505152535455565758595a5b5c5d5e5f"
46  "606162636465666768696a6b6c6d6e6f"
47  "707172737475767778797a7b7c7d7e7f"
48  "808182838485868788898a8b8c8d8e8f"
49  "909192939495969798999a9b9c9d9e9f"
50  "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
51  "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
52  "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
53  "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
54  "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
55  "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
56
57  a.resize(2*s.size());
58  int k = 0;
59  for (unsigned int i=0; i<s.size(); i++)
60  {
61    int p  = 2*(unsigned char)s[i];
62    a[k++] = c[p++];
63    a[k++] = c[p];
64  }
65
66  return a;
67}
68
69/*
70 * convert hex represented string to string
71 * unknown hex numbers result in '?'
72 */
73std::string hextostr(const std:: string s)
74{
75const char c[] = {
76  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
77  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
78  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
79   0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
80  -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
81  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
82  -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
83  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
84  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
85  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
86  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
87  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
88  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
89  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
90  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
91  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
92               };
93
94  std::string a;
95  unsigned int l = s.size();
96  a.resize((l+1)/2);
97
98  unsigned int k=0;
99  unsigned int l2 = (l/2)*2;
100  for (unsigned int i=0; i<l2; i+=2)
101  {
102    int p = c[(unsigned int)s[i]];
103    int q = c[(unsigned int)s[i+1]];
104    if (p >= 0 && q >= 0)
105      a[k++] = 16*p + q;
106    else
107      a[k++] = '?';
108  }
109
110  if (l2 < l)
111    a[k] = '?';
112
113  return a;
114
115}
116
117std::string zstrtohex(const std::string s)
118{
119  return ZBEGIN_+strtohex(s)+ZEND_;
120}
121
122std::string zhextostr(const std::string s)
123{
124  size_t lb = ZBEGIN_.size();
125  size_t le = ZEND_.size();
126  size_t ls = s.size();
127
128  if (ls < lb + le)
129    return s;
130
131  if (s.substr(0,lb) != ZBEGIN_)
132    return s;
133
134  if (s.substr(ls-le) != ZEND_)
135    return s;
136
137  return hextostr(s.substr(lb,ls-lb-le));
138}
139
140//generates random passwoord of length len
141std::string gen_random(const int len) {
142 timeval t1;
143 gettimeofday(&t1, NULL);
144 srand(t1.tv_usec * t1.tv_sec);
145 static const char alphanum[] =
146   "0123456789"
147   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
148   "abcdefghijklmnopqrstuvwxyz";
149
150 std::string s;
151 s.resize(len);
152 for (int i = 0; i < len; ++i) 
153 {
154   s[i] = alphanum[std::rand() % (sizeof(alphanum) - 1)];
155 }
156 return s;
157}
158
159#include <fstream>
160#include <string>
161#include <cerrno>
162
163int get_file_contents(std::string &contents,const std::string &filename)
164{
165  std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary);
166  if (in)
167  {
168    in.seekg(0, std::ios::end);
169    contents.resize(in.tellg());
170    in.seekg(0, std::ios::beg);
171    in.read(&contents[0], contents.size());
172    in.close();
173    return(0);
174  }
175  return -1;
176}
177
178// tries to mimic mkdir -p
179int mkdir_p(const std::string &d,mode_t mode)
180{
181  size_t p=0;
182
183  std::string s = d;
184  if (s[s.size()-1] != '/')
185    s += '/';
186
187  int rc;
188  std::string t;
189  while (( p = s.find_first_of('/',p)) != s.npos)
190  {
191    p++;
192    t = s.substr(0,p);
193    rc = mkdir (t.c_str(),mode);
194    if (rc < 0)
195    {
196      if (errno == EEXIST)
197      {
198        struct stat buf;
199        rc = lstat(t.c_str(),&buf);
200        if (rc < 0)
201          break;
202        if (!S_ISDIR(buf.st_mode))
203        {
204          rc = -1;
205          break;
206        }
207      }
208      else
209        break;
210    }
211    if ( p == s.size())
212      break;
213  }
214  return rc;
215}
216
217std::string envtostr(const std::string &s)
218{
219  char *a = getenv(s.c_str());
220  if (a == 0)
221    return "";
222
223  return a;
224}
Note: See TracBrowser for help on using the repository browser.