-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.hpp
More file actions
115 lines (106 loc) · 3.1 KB
/
Copy pathstring.hpp
File metadata and controls
115 lines (106 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#pragma once
#include <string>
#include <memory>
#include "iconv.h"
#ifdef _WIN32
#include <Windows.h>
#endif
namespace string
{
std::string lstrip(std::string str,std::string chars="\r\n\t ")
{
if(chars.length()!=0)
{
while(str.length()!=0 &&
chars.find(str[0])!=std::string::npos)
{
str = str.substr(1);
}
}
return str;
}
std::string rstrip(std::string str,std::string chars="\r\n\t ")
{
if(chars.length()!=0)
{
while(str.length()!=0 &&
chars.find(str.back())!=std::string::npos)
{
str.pop_back();
}
}
return str;
}
std::string strip(std::string str,std::string chars="\r\n\t ")
{
return rstrip(lstrip(str,chars),chars);
}
template<typename Iter_Begin,typename Iter_End>
std::string join(const char* sep,Iter_Begin begin,Iter_End end)
{
std::string result;
if(begin!=end)
{
result = *(begin++);
for(;begin!=end;++begin)
{
result.append(sep).append(*begin);
}
}
return result;
}
int _convert(const char *from, const char *to,char* save, size_t savelen,const char *src, size_t srclen)
{
if(srclen<=0 || srclen<=0) return -1;
libiconv_t cd = iconv_open(to, from);
if( (int)cd == -1) return -1;
const char *inbuf = src;
char *outbuf = save;
size_t outbufsize = savelen;
size_t savesize = 0;
size_t inbufsize = srclen;
const char* inptr = inbuf;
size_t insize = inbufsize;
char* outptr = outbuf;
size_t outsize = outbufsize;
while (insize > 0)
{
size_t res = iconv(cd,(const char**)&inptr,&insize,&outptr,&outsize);
if (outptr != outbuf) // iconv may change the address of inptr and outptr
{
int saved_errno = errno;
int outsize = outptr - outbuf;
strncpy(save+savesize, outbuf, outsize);
errno = saved_errno;
}
if (res == (size_t)(-1))
{
if (errno == EILSEQ)
{
int one = 1;
iconvctl(cd,ICONV_SET_DISCARD_ILSEQ,&one);
}
else
{
break;
}
}
}
iconv_close(cd);
return errno;
}
std::string decode(const char* from,std::string str)
{
size_t bsize=(str.length()+1)*2;
std::auto_ptr<char> buffer(new char[bsize]());
_convert(from,"UCS-2",buffer.get(),bsize,str.c_str(),str.length());
return std::string(buffer.get());
}
std::string encode(const char* to,std::string str)
{
size_t bsize=(str.length()+1)*4;
std::auto_ptr<char> buffer(new char[bsize]());
_convert("UCS-2",to,buffer.get(),bsize,str.c_str(),str.length());
return std::string(buffer.get());
}
}