now reads/writes 3-partition files
[asdcplib.git] / AS_DCP_system.h
1 /*
2 Copyright (c) 2004-2005, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    AS_DCP_system.h
28     \version $Id$
29     \brief   AS-DCP portable types and byte swapping
30 */
31
32
33 #ifndef _AS_DCP_SYSTEM_H_
34 #define _AS_DCP_SYSTEM_H_
35
36 #ifdef __APPLE__
37 #define ASDCP_BIG_ENDIAN
38 #endif
39
40 // 64 bit types are not used in the public interface
41 #ifdef WIN32
42 #define WIN32_LEAN_AND_MEAN
43 #define VC_EXTRALEAN
44 #include <windows.h>
45 #pragma warning(disable:4786)                   // Ignore "identifer > 255 characters" warning
46 #define snprintf _snprintf
47
48 #ifndef ASDCP_NO_BASE_TYPES
49 typedef unsigned __int64   ui64_t;
50 typedef __int64            i64_t;
51 #define i64_C(c)  (i64_t)(c)
52 #define ui64_C(c) (ui64_t)(c)
53 #endif
54
55 #else // WIN32
56
57 #ifndef ASDCP_NO_BASE_TYPES
58 typedef unsigned long long ui64_t;
59 typedef long long          i64_t;
60 #define i64_C(c)  c##LL
61 #define ui64_C(c) c##ULL
62 #endif
63
64 #endif // WIN32
65
66 #include <AS_DCP.h>
67 #include <assert.h>
68
69 namespace ASDCP {
70
71   inline ui16_t Swap2(ui16_t i)
72     {
73       return ( (i << 8) | (( i & 0xff00) >> 8) );
74     }
75
76   inline ui32_t Swap4(ui32_t i)
77     {
78       return
79         ( (i & 0x000000ffUL) << 24 ) |
80         ( (i & 0xff000000UL) >> 24 ) |
81         ( (i & 0x0000ff00UL) << 8  ) |
82         ( (i & 0x00ff0000UL) >> 8  );
83     }
84
85   inline ui64_t Swap8(ui64_t i)
86     {
87       return
88         ( (i & ui64_C(0x00000000000000FF)) << 56 ) |
89         ( (i & ui64_C(0xFF00000000000000)) >> 56 ) |
90         ( (i & ui64_C(0x000000000000FF00)) << 40 ) |
91         ( (i & ui64_C(0x00FF000000000000)) >> 40 ) |
92         ( (i & ui64_C(0x0000000000FF0000)) << 24 ) |
93         ( (i & ui64_C(0x0000FF0000000000)) >> 24 ) |
94         ( (i & ui64_C(0x00000000FF000000)) << 8  ) |
95         ( (i & ui64_C(0x000000FF00000000)) >> 8  );
96     }
97
98   //
99   template<class T>
100   inline T xmin(T lhs, T rhs)
101     {
102       return (lhs < rhs) ? lhs : rhs;
103     }
104
105   //
106   template<class T>
107   inline T xmax(T lhs, T rhs)
108     {
109       return (lhs > rhs) ? lhs : rhs;
110     }
111
112   // read an integer from byte-structured storage
113   template<class T>
114   inline T    cp2i(const byte_t* p) { return *(T*)p; }
115
116   // write an integer to byte-structured storage
117   template<class T>
118   inline void i2p(T i, byte_t* p) { *(T*)p = i; }
119
120 #ifdef ASDCP_BIG_ENDIAN
121 #define ASDCP_i16_LE(i)        ASDCP::Swap2(i)
122 #define ASDCP_i32_LE(i)        ASDCP::Swap4(i)
123 #define ASDCP_i64_LE(i)        ASDCP::Swap8(i)
124 #define ASDCP_i16_BE(i)        (i)
125 #define ASDCP_i32_BE(i)        (i)
126 #define ASDCP_i64_BE(i)        (i)
127 #else
128 #define ASDCP_i16_LE(i)        (i)
129 #define ASDCP_i32_LE(i)        (i)
130 #define ASDCP_i64_LE(i)        (i)
131 #define ASDCP_i16_BE(i)        ASDCP::Swap2(i)
132 #define ASDCP_i32_BE(i)        ASDCP::Swap4(i)
133 #define ASDCP_i64_BE(i)        ASDCP::Swap8(i)
134 #endif // ASDCP_BIG_ENDIAN
135
136
137 // 64 bit integer/text conversion
138 //
139   const ui32_t IntBufferLen = 32;
140
141   inline i64_t atoi64(const char *str) {
142 #ifdef WIN32
143     return _atoi64(str);
144 #else
145     return strtoll(str, NULL, 10);
146 #endif
147   }
148
149   inline const char* i64sz(i64_t i, char* buf)
150     { 
151       assert(buf);
152 #ifdef WIN32
153       snprintf(buf, IntBufferLen, "%I64d", i);
154 #else
155       snprintf(buf, IntBufferLen, "%lld", i);
156 #endif
157       return buf;
158     }
159
160   inline const char* ui64sz(ui64_t i, char* buf)
161     { 
162       assert(buf);
163 #ifdef WIN32
164       snprintf(buf, IntBufferLen, "%I64u", i);
165 #else
166       snprintf(buf, IntBufferLen, "%llu", i);
167 #endif
168       return buf;
169     }
170
171   inline const char* i64szx(i64_t i, ui32_t digits, char* buf)
172     {
173       assert(buf);
174       if ( digits > 30 ) digits = 30;
175 #ifdef WIN32
176       snprintf(buf, IntBufferLen, "%0*I64x", digits, i);
177 #else
178       snprintf(buf, IntBufferLen, "%0*llx", digits, i);
179 #endif
180       return buf;
181     }
182
183 } // namespace ASDCP
184
185
186
187 #endif // _AS_DCP_SYSTEM_H_
188
189 //
190 // AS_DCP_system.h
191 //