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