-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring2wstring.h
More file actions
59 lines (51 loc) · 1.29 KB
/
string2wstring.h
File metadata and controls
59 lines (51 loc) · 1.29 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
/* string2wstring.h */
#pragma once
#include <string>
#include <vector>
#include <locale>
#include <functional>
#include <iostream>
// Put this class in your personal toolbox...
template<class E,
class T = std::char_traits<E>,
class A = std::allocator<E> >
class Widen : public std::unary_function<
const std::string&,std::basic_string<E,T,A> >
{
std::locale loc_;
const std::ctype<E>* pCType_;
// No copy-constructor, no assignment operator...
Widen( const Widen& );
Widen& operator= ( const Widen& );
public:
// Constructor...
Widen( const std::locale& loc = std::locale() ) : loc_( loc )
{
#if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6.0...
using namespace std;
pCType_ = &_USE( loc,ctype<E> );
#else
pCType_ = &std::use_facet<std::ctype<E> >( loc );
#endif
}
// Conversion...
std::basic_string<E,T,A> operator() ( const std::string& str ) const
{
typename std::basic_string<E,T,A>::size_type srcLen =
str.length();
const char* pSrcBeg = str.c_str();
std::vector<E> tmp( srcLen );
pCType_->widen( pSrcBeg,pSrcBeg + srcLen,&tmp[ 0 ] );
return std::basic_string<E,T,A>( &tmp[ 0 ],srcLen );
}
};
/*
// How to use it...
int main()
{
Widen<wchar_t> to_wstring;
std::string s = "my test string";
std::wstring w = to_wstring( s );
std::wcout << w << L"\n";
}
*/