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