-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilename.cpp
More file actions
132 lines (105 loc) · 2.49 KB
/
Filename.cpp
File metadata and controls
132 lines (105 loc) · 2.49 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "../classes/Filename.h"
#include <iostream>
#include <windows.h>
namespace {
std::string ws_to_utf8(std::wstring const & wstr)
{
int total = WideCharToMultiByte(CP_UTF8,0,wstr.c_str(),wstr.size(),0,0,0,0);
std::string converted(total,'\0');
WideCharToMultiByte(CP_UTF8,0,wstr.c_str(),wstr.size(),&converted[0],total,0,0);
return converted;
}
std::wstring utf8_to_ws(char const * text, int size)
{
if( size == 0 ) return std::wstring();
// std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
// blah = converter.from_bytes(text);
int total = MultiByteToWideChar(CP_UTF8,0,text,size,0,0);
std::wstring converted(total,L'\0');
if( MultiByteToWideChar(CP_UTF8,0,text,size,&converted[0],total) == 0 ) {
#ifdef NDEBUG
std::cerr << "error converting string" << std::endl;
#else
std::cerr << "error converting [" << text << "] size " << size << std::endl;
#endif
}
return converted;
}
std::wstring utf8_to_ws(char const * text)
{
auto converted = utf8_to_ws(text,-1);
converted.pop_back(); // remove the null terminated char
return converted;
}
std::wstring utf8_to_ws(std::string const & text)
{
if( text.empty() ) return std::wstring();
return utf8_to_ws(text.c_str(),text.size());
}
}
namespace fs {
Filename::Filename():
wstr()
{}
Filename::Filename(std::string const & str):
wstr(utf8_to_ws(str))
{}
Filename::Filename(char const * str):
wstr(utf8_to_ws(str))
{}
Filename::Filename(std::wstring const & str):
wstr(str)
{}
Filename::Filename(wchar_t const * str):
wstr(str)
{}
Filename::~Filename()
{
using std::wstring;
wstr.~wstring();
}
Filename::Filename(Filename const & other):
wstr(other.wstr)
{}
Filename::Filename(Filename && other):
wstr(std::move(other.wstr))
{}
Filename & Filename::operator=(Filename other)
{
swap(other);
return *this;
}
void Filename::swap(Filename & other)
{
wstr.swap(other.wstr);
}
std::string Filename::toUTF8() const
{
return ws_to_utf8(wstr);
}
std::wstring Filename::toUTF16() const
{
return wstr;
}
bool Filename::empty() const
{
return wstr.empty();
}
Filename & Filename::operator+=(Filename const & other)
{
wstr += other.wstr;
return *this;
}
bool Filename::operator==(Filename const & other) const
{
return wstr == other.wstr;
}
bool Filename::operator!=(Filename const & other) const
{
return wstr != other.wstr;
}
bool operator<(Filename const & a, Filename const & b)
{
return a.wstr < b.wstr;
}
}