Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
win32_utf8conv.hpp
Go to the documentation of this file.
1 //
3 // FILE: win32_utf8conv.h
4 //
5 // Header file defining prototypes of helper functions for converting
6 // strings between Unicode UTF-8 and UTF-16.
7 // (The implementation file is "utf8conv_inl.h").
8 //
9 // UTF-8 text is stored in std::string;
10 // UTF-16 text is stored in std::wstring.
11 //
12 // This code just uses Win32 Platform SDK and C++ standard library;
13 // so it can be used also with the Express editions of Visual Studio.
14 //
15 //
16 // Original code: February 4th, 2011
17 // Last update: October 15th, 2011
18 //
19 // - Added more information to the utf8_conversion_error class
20 // (like the return code of ::GetLastError());
21 // moreover, the class now derives from std::runtime_error.
22 //
23 // - Added conversion function overloads taking raw C strings as input.
24 // (This is more efficient when there are raw C strings already
25 // available, because it avoids the creation of temporary
26 // new std::[w]string's.)
27 //
28 // - UTF-8 conversion functions now detect invalid UTF-8 sequences
29 // thanks to MB_ERR_INVALID_CHARS flag, and throw an exception
30 // in this case.
31 //
32 //
33 // by Giovanni Dicanio <[email protected]>
34 //
36 
37 #pragma once
38 
39 #include "Common.hpp"
40 
41 #include <stdexcept> // std::runtime_error
42 #include <string> // STL string classes
43 
44 namespace utf8util
45 {
46 
47 // Exception class representing an error occurred during UTF-8 conversion.
48 class utf8_conversion_error : public std::runtime_error
49 {
50 public:
51  //
52  // Naming convention note:
53  //
54  // This exception class is derived from std::runtime_error class,
55  // so I chose to use the same naming convention of STL classes
56  // (e.g. do_something_intersting() instead of DoSomethingInteresting()).
57  //
58 
59  // Error code type
60  // (a DWORD, as the return value type from ::GetLastError())
61  typedef unsigned long error_code_type;
62 
63  // Type of conversion
65  conversion_utf8_from_utf16, // UTF-16 ---> UTF-8
66  conversion_utf16_from_utf8 // UTF-8 ---> UTF-16
67  };
68 
69  // Constructs an UTF-8 conversion error exception
70  // with a raw C string message, conversion type and error code.
73 
74  // Constructs an UTF-8 conversion error exception
75  // with a std::string message, conversion type and error code.
76  utf8_conversion_error(const std::string& message,
79 
80  // Returns the type of conversion (UTF-8 from UTF-16, or vice versa)
82 
83  // Returns the error code occurred during the conversion
84  // (which is typically the return value of ::GetLastError()).
86 
87  //
88  // IMPLEMENTATION
89  //
90 private:
91  conversion_type m_conversion; // kind of conversion
92  error_code_type m_error_code; // error code
93 };
94 
95 // Converts a string from UTF-8 to UTF-16.
96 // On error, can throw an utf8_conversion_error exception.
97 EXPORT std::wstring UTF16FromUTF8(const std::string& utf8);
98 
99 // Converts a raw C string from UTF-8 to UTF-16.
100 // On error, can throw an utf8_conversion_error exception.
101 // If the input pointer is nullptr, an empty string is returned.
102 EXPORT std::wstring UTF16FromUTF8(const char* utf8);
103 
104 // Converts a string from UTF-16 to UTF-8.
105 // On error, can throw an utf8_conversion_error exception.
106 EXPORT std::string UTF8FromUTF16(const std::wstring& utf16);
107 
108 // Converts a raw C string from UTF-16 to UTF-8.
109 // On error, can throw an utf8_conversion_error exception.
110 // If the input pointer is nullptr, an empty string is returned.
111 EXPORT std::string UTF8FromUTF16(const wchar_t* utf16);
112 
113 } // namespace utf8util
114 
115 #include "win32_utf8conv_inl.hpp" // inline implementations
116 
conversion_type conversion() const
utf8_conversion_error(const char *message, conversion_type conversion, error_code_type error_code)
EXPORT std::string UTF8FromUTF16(const std::wstring &utf16)
EXPORT std::wstring UTF16FromUTF8(const std::string &utf8)
error_code_type error_code() const