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 <cstdio>
34#include <cstdarg>
35#include <cstring>
36
37#ifdef HAVE_CONFIG_H
38# include "config.h"
39#endif
40
41#if defined(HAVE_MSC_BYTESWAP)
42# include <stdlib.h>
43#elif defined(HAVE_GLIBC_BYTESWAP)
44# include <byteswap.h>
45#elif defined(HAVE_MAC_BYTESWAP)
46# include <libkern/OSByteOrder.h>
47#elif defined(HAVE_OPENBSD_BYTESWAP)
48# include <sys/endian.h>
49#endif
50
51#include "tstring.h"
52
53namespace TagLib
54{
55 namespace Utils
56 {
57 namespace
58 {
59
63 inline unsigned short byteSwap(unsigned short x)
64 {
65#if defined(HAVE_GCC_BYTESWAP)
66
67 return __builtin_bswap16(x);
68
69#elif defined(HAVE_MSC_BYTESWAP)
70
71 return _byteswap_ushort(x);
72
73#elif defined(HAVE_GLIBC_BYTESWAP)
74
75 return __bswap_16(x);
76
77#elif defined(HAVE_MAC_BYTESWAP)
78
79 return OSSwapInt16(x);
80
81#elif defined(HAVE_OPENBSD_BYTESWAP)
82
83 return swap16(x);
84
85#else
86
87 return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
88
89#endif
90 }
91
95 inline unsigned int byteSwap(unsigned int x)
96 {
97#if defined(HAVE_GCC_BYTESWAP)
98
99 return __builtin_bswap32(x);
100
101#elif defined(HAVE_MSC_BYTESWAP)
102
103 return _byteswap_ulong(x);
104
105#elif defined(HAVE_GLIBC_BYTESWAP)
106
107 return __bswap_32(x);
108
109#elif defined(HAVE_MAC_BYTESWAP)
110
111 return OSSwapInt32(x);
112
113#elif defined(HAVE_OPENBSD_BYTESWAP)
114
115 return swap32(x);
116
117#else
118
119 return ((x & 0xff000000u) >> 24)
120 | ((x & 0x00ff0000u) >> 8)
121 | ((x & 0x0000ff00u) << 8)
122 | ((x & 0x000000ffu) << 24);
123
124#endif
125 }
126
130 inline unsigned long long byteSwap(unsigned long long x)
131 {
132#if defined(HAVE_GCC_BYTESWAP)
133
134 return __builtin_bswap64(x);
135
136#elif defined(HAVE_MSC_BYTESWAP)
137
138 return _byteswap_uint64(x);
139
140#elif defined(HAVE_GLIBC_BYTESWAP)
141
142 return __bswap_64(x);
143
144#elif defined(HAVE_MAC_BYTESWAP)
145
146 return OSSwapInt64(x);
147
148#elif defined(HAVE_OPENBSD_BYTESWAP)
149
150 return swap64(x);
151
152#else
153
154 return ((x & 0xff00000000000000ull) >> 56)
155 | ((x & 0x00ff000000000000ull) >> 40)
156 | ((x & 0x0000ff0000000000ull) >> 24)
157 | ((x & 0x000000ff00000000ull) >> 8)
158 | ((x & 0x00000000ff000000ull) << 8)
159 | ((x & 0x0000000000ff0000ull) << 24)
160 | ((x & 0x000000000000ff00ull) << 40)
161 | ((x & 0x00000000000000ffull) << 56);
162
163#endif
164 }
165
170 inline String formatString(const char *format, ...)
171 {
172 // Sufficient buffer size for the current internal uses.
173 // Consider changing this value when you use this function.
174
175 static const size_t BufferSize = 128;
176
177 va_list args;
178 va_start(args, format);
179
180 char buf[BufferSize];
181 int length;
182
183 length = std::vsnprintf(buf, BufferSize, format, args);
184
185 va_end(args);
186
187 if(length > 0)
188 return String(buf);
189 return String();
190 }
191
195 enum ByteOrder
196 {
198 LittleEndian,
200 BigEndian
201 };
202
206 constexpr ByteOrder systemByteOrder()
207 {
208 constexpr union IntCharUnion {
209 int i;
210 char c;
211 constexpr IntCharUnion(int j) : i(j) {}
212 } u{1};
213
214 if(u.c == 1)
215 return LittleEndian;
216 return BigEndian;
217 }
218 } // namespace
219 } // namespace Utils
220} // namespace TagLib
221
222#endif
223
224#endif
A namespace for all TagLib related classes and functions.
Definition: apefile.h:41