Show More
@@ -1,183 +1,199 b'' | |||
|
1 | 1 | /* |
|
2 | 2 | util.h - utility functions for interfacing with the various python APIs. |
|
3 | 3 | |
|
4 | 4 | This software may be used and distributed according to the terms of |
|
5 | 5 | the GNU General Public License, incorporated herein by reference. |
|
6 | 6 | */ |
|
7 | 7 | |
|
8 | 8 | #ifndef _HG_UTIL_H_ |
|
9 | 9 | #define _HG_UTIL_H_ |
|
10 | 10 | |
|
11 | 11 | #if PY_MAJOR_VERSION >= 3 |
|
12 | 12 | |
|
13 | 13 | #define IS_PY3K |
|
14 | 14 | #define PyInt_FromLong PyLong_FromLong |
|
15 | 15 | #define PyInt_AsLong PyLong_AsLong |
|
16 | 16 | |
|
17 | 17 | /* |
|
18 | 18 | Mapping of some of the python < 2.x PyString* functions to py3k's PyUnicode. |
|
19 | 19 | |
|
20 | 20 | The commented names below represent those that are present in the PyBytes |
|
21 | 21 | definitions for python < 2.6 (below in this file) that don't have a direct |
|
22 | 22 | implementation. |
|
23 | 23 | */ |
|
24 | 24 | |
|
25 | 25 | #define PyStringObject PyUnicodeObject |
|
26 | 26 | #define PyString_Type PyUnicode_Type |
|
27 | 27 | |
|
28 | 28 | #define PyString_Check PyUnicode_Check |
|
29 | 29 | #define PyString_CheckExact PyUnicode_CheckExact |
|
30 | 30 | #define PyString_CHECK_INTERNED PyUnicode_CHECK_INTERNED |
|
31 | 31 | #define PyString_AS_STRING PyUnicode_AsLatin1String |
|
32 | 32 | #define PyString_GET_SIZE PyUnicode_GET_SIZE |
|
33 | 33 | |
|
34 | 34 | #define PyString_FromStringAndSize PyUnicode_FromStringAndSize |
|
35 | 35 | #define PyString_FromString PyUnicode_FromString |
|
36 | 36 | #define PyString_FromFormatV PyUnicode_FromFormatV |
|
37 | 37 | #define PyString_FromFormat PyUnicode_FromFormat |
|
38 | 38 | /* #define PyString_Size PyUnicode_GET_SIZE */ |
|
39 | 39 | /* #define PyString_AsString */ |
|
40 | 40 | /* #define PyString_Repr */ |
|
41 | 41 | #define PyString_Concat PyUnicode_Concat |
|
42 | 42 | #define PyString_ConcatAndDel PyUnicode_AppendAndDel |
|
43 | 43 | #define _PyString_Resize PyUnicode_Resize |
|
44 | 44 | /* #define _PyString_Eq */ |
|
45 | 45 | #define PyString_Format PyUnicode_Format |
|
46 | 46 | /* #define _PyString_FormatLong */ |
|
47 | 47 | /* #define PyString_DecodeEscape */ |
|
48 | 48 | #define _PyString_Join PyUnicode_Join |
|
49 | 49 | #define PyString_Decode PyUnicode_Decode |
|
50 | 50 | #define PyString_Encode PyUnicode_Encode |
|
51 | 51 | #define PyString_AsEncodedObject PyUnicode_AsEncodedObject |
|
52 | 52 | #define PyString_AsEncodedString PyUnicode_AsEncodedString |
|
53 | 53 | #define PyString_AsDecodedObject PyUnicode_AsDecodedObject |
|
54 | 54 | #define PyString_AsDecodedString PyUnicode_AsDecodedUnicode |
|
55 | 55 | /* #define PyString_AsStringAndSize */ |
|
56 | 56 | #define _PyString_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
|
57 | 57 | |
|
58 | 58 | #endif /* PY_MAJOR_VERSION */ |
|
59 | 59 | |
|
60 | 60 | /* Backports from 2.6 */ |
|
61 | 61 | #if PY_VERSION_HEX < 0x02060000 |
|
62 | 62 | |
|
63 | 63 | #define Py_TYPE(ob) (ob)->ob_type |
|
64 | 64 | #define Py_SIZE(ob) (ob)->ob_size |
|
65 | 65 | #define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, |
|
66 | 66 | |
|
67 | 67 | /* Shamelessly stolen from bytesobject.h */ |
|
68 | 68 | #define PyBytesObject PyStringObject |
|
69 | 69 | #define PyBytes_Type PyString_Type |
|
70 | 70 | |
|
71 | 71 | #define PyBytes_Check PyString_Check |
|
72 | 72 | #define PyBytes_CheckExact PyString_CheckExact |
|
73 | 73 | #define PyBytes_CHECK_INTERNED PyString_CHECK_INTERNED |
|
74 | 74 | #define PyBytes_AS_STRING PyString_AS_STRING |
|
75 | 75 | #define PyBytes_GET_SIZE PyString_GET_SIZE |
|
76 | 76 | #define Py_TPFLAGS_BYTES_SUBCLASS Py_TPFLAGS_STRING_SUBCLASS |
|
77 | 77 | |
|
78 | 78 | #define PyBytes_FromStringAndSize PyString_FromStringAndSize |
|
79 | 79 | #define PyBytes_FromString PyString_FromString |
|
80 | 80 | #define PyBytes_FromFormatV PyString_FromFormatV |
|
81 | 81 | #define PyBytes_FromFormat PyString_FromFormat |
|
82 | 82 | #define PyBytes_Size PyString_Size |
|
83 | 83 | #define PyBytes_AsString PyString_AsString |
|
84 | 84 | #define PyBytes_Repr PyString_Repr |
|
85 | 85 | #define PyBytes_Concat PyString_Concat |
|
86 | 86 | #define PyBytes_ConcatAndDel PyString_ConcatAndDel |
|
87 | 87 | #define _PyBytes_Resize _PyString_Resize |
|
88 | 88 | #define _PyBytes_Eq _PyString_Eq |
|
89 | 89 | #define PyBytes_Format PyString_Format |
|
90 | 90 | #define _PyBytes_FormatLong _PyString_FormatLong |
|
91 | 91 | #define PyBytes_DecodeEscape PyString_DecodeEscape |
|
92 | 92 | #define _PyBytes_Join _PyString_Join |
|
93 | 93 | #define PyBytes_Decode PyString_Decode |
|
94 | 94 | #define PyBytes_Encode PyString_Encode |
|
95 | 95 | #define PyBytes_AsEncodedObject PyString_AsEncodedObject |
|
96 | 96 | #define PyBytes_AsEncodedString PyString_AsEncodedString |
|
97 | 97 | #define PyBytes_AsDecodedObject PyString_AsDecodedObject |
|
98 | 98 | #define PyBytes_AsDecodedString PyString_AsDecodedString |
|
99 | 99 | #define PyBytes_AsStringAndSize PyString_AsStringAndSize |
|
100 | 100 | #define _PyBytes_InsertThousandsGrouping _PyString_InsertThousandsGrouping |
|
101 | 101 | |
|
102 | 102 | #endif /* PY_VERSION_HEX */ |
|
103 | 103 | |
|
104 | 104 | #if (PY_VERSION_HEX < 0x02050000) |
|
105 | 105 | /* Definitions to get compatibility with python 2.4 and earlier which |
|
106 | 106 | does not have Py_ssize_t. See also PEP 353. |
|
107 | 107 | Note: msvc (8 or earlier) does not have ssize_t, so we use Py_ssize_t. |
|
108 | 108 | */ |
|
109 | 109 | typedef int Py_ssize_t; |
|
110 | 110 | typedef Py_ssize_t (*lenfunc)(PyObject *); |
|
111 | 111 | typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t); |
|
112 | 112 | #define PyInt_FromSsize_t PyInt_FromLong |
|
113 | 113 | |
|
114 | 114 | #if !defined(PY_SSIZE_T_MIN) |
|
115 | 115 | #define PY_SSIZE_T_MAX INT_MAX |
|
116 | 116 | #define PY_SSIZE_T_MIN INT_MIN |
|
117 | 117 | #endif |
|
118 | 118 | #endif |
|
119 | 119 | |
|
120 | 120 | #ifdef _WIN32 |
|
121 | 121 | #ifdef _MSC_VER |
|
122 | 122 | /* msvc 6.0 has problems */ |
|
123 | 123 | #define inline __inline |
|
124 | 124 | typedef signed char int8_t; |
|
125 | 125 | typedef short int16_t; |
|
126 | 126 | typedef long int32_t; |
|
127 | 127 | typedef __int64 int64_t; |
|
128 | 128 | typedef unsigned char uint8_t; |
|
129 | 129 | typedef unsigned short uint16_t; |
|
130 | 130 | typedef unsigned long uint32_t; |
|
131 | 131 | typedef unsigned __int64 uint64_t; |
|
132 | 132 | #else |
|
133 | 133 | #include <stdint.h> |
|
134 | 134 | #endif |
|
135 | 135 | #else |
|
136 | 136 | /* not windows */ |
|
137 | 137 | #include <sys/types.h> |
|
138 | 138 | #if defined __BEOS__ && !defined __HAIKU__ |
|
139 | 139 | #include <ByteOrder.h> |
|
140 | 140 | #else |
|
141 | 141 | #include <arpa/inet.h> |
|
142 | 142 | #endif |
|
143 | 143 | #include <inttypes.h> |
|
144 | 144 | #endif |
|
145 | 145 | |
|
146 | 146 | #if defined __hpux || defined __SUNPRO_C || defined _AIX |
|
147 | 147 | #define inline |
|
148 | 148 | #endif |
|
149 | 149 | |
|
150 | 150 | #ifdef __linux |
|
151 | 151 | #define inline __inline |
|
152 | 152 | #endif |
|
153 | 153 | |
|
154 | 154 | typedef struct { |
|
155 | 155 | PyObject_HEAD |
|
156 | 156 | char state; |
|
157 | 157 | int mode; |
|
158 | 158 | int size; |
|
159 | 159 | int mtime; |
|
160 | 160 | } dirstateTupleObject; |
|
161 | 161 | |
|
162 | 162 | extern PyTypeObject dirstateTupleType; |
|
163 | 163 | #define dirstate_tuple_check(op) (Py_TYPE(op) == &dirstateTupleType) |
|
164 | 164 | |
|
165 | 165 | static inline uint32_t getbe32(const char *c) |
|
166 | 166 | { |
|
167 | 167 | const unsigned char *d = (const unsigned char *)c; |
|
168 | 168 | |
|
169 | 169 | return ((d[0] << 24) | |
|
170 | 170 | (d[1] << 16) | |
|
171 | 171 | (d[2] << 8) | |
|
172 | 172 | (d[3])); |
|
173 | 173 | } |
|
174 | 174 | |
|
175 | static inline int16_t getbeint16(const char *c) | |
|
176 | { | |
|
177 | const unsigned char *d = (const unsigned char *)c; | |
|
178 | ||
|
179 | return ((d[0] << 8) | | |
|
180 | (d[1])); | |
|
181 | } | |
|
182 | ||
|
183 | static inline uint16_t getbeuint16(const char *c) | |
|
184 | { | |
|
185 | const unsigned char *d = (const unsigned char *)c; | |
|
186 | ||
|
187 | return ((d[0] << 8) | | |
|
188 | (d[1])); | |
|
189 | } | |
|
190 | ||
|
175 | 191 | static inline void putbe32(uint32_t x, char *c) |
|
176 | 192 | { |
|
177 | 193 | c[0] = (x >> 24) & 0xff; |
|
178 | 194 | c[1] = (x >> 16) & 0xff; |
|
179 | 195 | c[2] = (x >> 8) & 0xff; |
|
180 | 196 | c[3] = (x) & 0xff; |
|
181 | 197 | } |
|
182 | 198 | |
|
183 | 199 | #endif /* _HG_UTIL_H_ */ |
General Comments 0
You need to be logged in to leave comments.
Login now