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