wcharIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "error.H"
29 
30 #include "wchar.H"
31 #include "IOstreams.H"
32 
33 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
34 
35 Foam::Ostream& Foam::operator<<(Ostream& os, const wchar_t wc)
36 {
37  if (!(wc & ~0x0000007F))
38  {
39  // 0x00000000 - 0x0000007F: (1-byte output)
40  // 0xxxxxxx
41  os.write(char(wc));
42  }
43  else if (!(wc & ~0x000007FF))
44  {
45  // 0x00000080 - 0x000007FF: (2-byte output)
46  // 110bbbaa 10aaaaaa
47  os.write(char(0xC0 | ((wc >> 6) & 0x1F)));
48  os.write(char(0x80 | ((wc) & 0x3F)));
49  }
50  else if (!(wc & ~0x0000FFFF))
51  {
52  // 0x00000800 - 0x0000FFFF: (3-byte output)
53  // 1110bbbb 10bbbbaa 10aaaaaa
54  os.write(char(0xE0 | ((wc >> 12) & 0x0F)));
55  os.write(char(0x80 | ((wc >> 6) & 0x3F)));
56  os.write(char(0x80 | ((wc) & 0x3F)));
57  }
58  else if (!(wc & ~0x001FFFFF))
59  {
60  // 0x00010000 - 0x001FFFFF: (4-byte output)
61  // 11110ccc 10ccbbbb 10bbbbaa 10aaaaaa
62  os.write(char(0xF0 | ((wc >> 18) & 0x07)));
63  os.write(char(0x80 | ((wc >> 12) & 0x3F)));
64  os.write(char(0x80 | ((wc >> 6) & 0x3F)));
65  os.write(char(0x80 | ((wc) & 0x3F)));
66  }
67  else if (!(wc & ~0x03FFFFFF))
68  {
69  // 0x00200000 - 0x03FFFFFF: (5-byte output)
70  // 111110dd 10cccccc 10ccbbbb 10bbbbaa 10aaaaaa
71  os.write(char(0xF8 | ((wc >> 24) & 0x03)));
72  os.write(char(0x80 | ((wc >> 18) & 0x3F)));
73  os.write(char(0x80 | ((wc >> 12) & 0x3F)));
74  os.write(char(0x80 | ((wc >> 6) & 0x3F)));
75  os.write(char(0x80 | ((wc) & 0x3F)));
76  }
77  else if (!(wc & ~0x7FFFFFFF))
78  {
79  // 0x04000000 - 0x7FFFFFFF: (6-byte output)
80  // 1111110d 10dddddd 10cccccc 10ccbbbb 10bbbbaa 10aaaaaa
81  os.write(char(0xFC | ((wc >> 30) & 0x01)));
82  os.write(char(0x80 | ((wc >> 24) & 0x3F)));
83  os.write(char(0x80 | ((wc >> 18) & 0x3F)));
84  os.write(char(0x80 | ((wc >> 12) & 0x3F)));
85  os.write(char(0x80 | ((wc >> 6) & 0x3F)));
86  os.write(char(0x80 | ((wc) & 0x3F)));
87  }
88  else
89  {
90  // according to man page utf8(7)
91  // the Unicode standard specifies no characters above 0x0010FFFF,
92  // so Unicode characters can only be up to four bytes long in UTF-8.
93 
94  // report anything unknown/invalid as replacement character U+FFFD
95  os.write(char(0xEF));
96  os.write(char(0xBF));
97  os.write(char(0xBD));
98  }
99 
100  os.check(FUNCTION_NAME);
101  return os;
102 }
103 
104 
105 Foam::Ostream& Foam::operator<<(Ostream& os, const wchar_t* wstr)
106 {
107  if (wstr)
108  {
109  for (const wchar_t* iter = wstr; *iter; ++iter)
110  {
111  os << *iter;
112  }
113  }
114 
115  return os;
116 }
117 
118 
119 Foam::Ostream& Foam::operator<<(Ostream& os, const std::wstring& wstr)
120 {
121  for (auto iter = wstr.cbegin(); iter != wstr.cend(); ++iter)
122  {
123  os << *iter;
124  }
125 
126  return os;
127 }
128 
129 
130 // ************************************************************************* //
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
error.H
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
wchar.H
A wide-character and a pointer to a wide-character string.
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56