TagLib API Documentation
tutils.h
Go to the documentation of this file.
1/***************************************************************************
2 copyright : (C) 2013 by Tsuda Kageyu
3 email : tsuda.kageyu@gmail.com
4 ***************************************************************************/
5
6/***************************************************************************
7 * This library is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License version *
9 * 2.1 as published by the Free Software Foundation. *
10 * *
11 * This library is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the Free Software *
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
19 * 02110-1301 USA *
20 * *
21 * Alternatively, this file is available under the Mozilla Public *
22 * License Version 1.1. You may obtain a copy of the License at *
23 * http://www.mozilla.org/MPL/ *
24 ***************************************************************************/
25
26#ifndef TAGLIB_TUTILS_H
27#define TAGLIB_TUTILS_H
28
29// THIS FILE IS NOT A PART OF THE TAGLIB API
30
31#ifndef DO_NOT_DOCUMENT // tell Doxygen not to document this header
32
33#include <cstdint>
34#include <cstdio>
35#include <cstdarg>
36#include <cstring>
37
38#ifdef HAVE_CONFIG_H
39# include "config.h"
40#endif
41
42#if defined(HAVE_MSC_BYTESWAP)
43# include <stdlib.h>
44#elif defined(HAVE_GLIBC_BYTESWAP)
45# include <byteswap.h>
46#elif defined(HAVE_MAC_BYTESWAP)
47# include <libkern/OSByteOrder.h>
48#elif defined(HAVE_OPENBSD_BYTESWAP)
49# include <sys/endian.h>
50#endif
51
52#include "tstring.h"
53
54namespace TagLib
55{
56 namespace Utils
57 {
58 namespace
59 {
60
71 {
72 switch(static_cast<unsigned char>(c)) {
73 case String::Latin1:
74 case String::UTF16:
75 case String::UTF16BE:
76 case String::UTF8:
77 case String::UTF16LE:
78 return static_cast<String::Type>(static_cast<unsigned char>(c));
79 default:
80 return String::Latin1;
81 }
82 }
83
88 {
89#if defined(HAVE_GCC_BYTESWAP)
90
91 return __builtin_bswap16(x);
92
93#elif defined(HAVE_MSC_BYTESWAP)
94
95 return _byteswap_ushort(x);
96
97#elif defined(HAVE_GLIBC_BYTESWAP)
98
99 return __bswap_16(x);
100
101#elif defined(HAVE_MAC_BYTESWAP)
102
103 return OSSwapInt16(x);
104
105#elif defined(HAVE_OPENBSD_BYTESWAP)
106
107 return swap16(x);
108
109#else
110
111 return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
112
113#endif
114 }
115
120 {
121#if defined(HAVE_GCC_BYTESWAP)
122
123 return __builtin_bswap32(x);
124
125#elif defined(HAVE_MSC_BYTESWAP)
126
127 return _byteswap_ulong(x);
128
129#elif defined(HAVE_GLIBC_BYTESWAP)
130
131 return __bswap_32(x);
132
133#elif defined(HAVE_MAC_BYTESWAP)
134
135 return OSSwapInt32(x);
136
137#elif defined(HAVE_OPENBSD_BYTESWAP)
138
139 return swap32(x);
140
141#else
142
143 return ((x & 0xff000000u) >> 24)
144 | ((x & 0x00ff0000u) >> 8)
145 | ((x & 0x0000ff00u) << 8)
146 | ((x & 0x000000ffu) << 24);
147
148#endif
149 }
150
155 {
156#if defined(HAVE_GCC_BYTESWAP)
157
158 return __builtin_bswap64(x);
159
160#elif defined(HAVE_MSC_BYTESWAP)
161
162 return _byteswap_uint64(x);
163
164#elif defined(HAVE_GLIBC_BYTESWAP)
165
166 return __bswap_64(x);
167
168#elif defined(HAVE_MAC_BYTESWAP)
169
170 return OSSwapInt64(x);
171
172#elif defined(HAVE_OPENBSD_BYTESWAP)
173
174 return swap64(x);
175
176#else
177
178 return ((x & 0xff00000000000000ull) >> 56)
179 | ((x & 0x00ff000000000000ull) >> 40)
180 | ((x & 0x0000ff0000000000ull) >> 24)
181 | ((x & 0x000000ff00000000ull) >> 8)
182 | ((x & 0x00000000ff000000ull) << 8)
183 | ((x & 0x0000000000ff0000ull) << 24)
184 | ((x & 0x000000000000ff00ull) << 40)
185 | ((x & 0x00000000000000ffull) << 56);
186
187#endif
188 }
189
194 inline String formatString(const char *format, ...)
195 {
196 // Sufficient buffer size for the current internal uses.
197 // Consider changing this value when you use this function.
198
199 static const size_t BufferSize = 128;
200
202 va_start(args, format);
203
204 char buf[BufferSize];
205 int length;
206
207 length = std::vsnprintf(buf, BufferSize, format, args);
208
209 va_end(args);
210
211 if(length > 0)
212 return String(buf);
213 return String();
214 }
215
219 enum ByteOrder
220 {
222 LittleEndian,
224 BigEndian
225 };
226
230 constexpr ByteOrder systemByteOrder()
231 {
232 constexpr union IntCharUnion {
233 int i;
234 char c;
235 constexpr IntCharUnion(int j) : i(j) {}
236 } u{1};
237
238 if(u.c == 1)
239 return LittleEndian;
240 return BigEndian;
241 }
242 } // namespace
243 } // namespace Utils
244} // namespace TagLib
245
246#endif
247
248#endif
Type
Definition tstring.h:96
@ Latin1
Definition tstring.h:100
@ UTF16BE
Definition tstring.h:109
@ UTF16
Definition tstring.h:104
@ UTF8
Definition tstring.h:113
@ UTF16LE
Definition tstring.h:117
A namespace for all TagLib related classes and functions.
Definition apefile.h:41