PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/utils/python/python26/Modules/_ctypes/_ctypes_test.c

https://bitbucket.org/apexgames-ondemand/zombie-onslaught-source
C | 602 lines | 481 code | 91 blank | 30 comment | 22 complexity | bbca305b02ed9784e226e04f4bb59191 MD5 | raw file
  1. /*****************************************************************
  2. This file should be kept compatible with Python 2.3, see PEP 291.
  3. *****************************************************************/
  4. #include <Python.h>
  5. /*
  6. Backwards compatibility:
  7. Python2.2 used LONG_LONG instead of PY_LONG_LONG
  8. */
  9. #if defined(HAVE_LONG_LONG) && !defined(PY_LONG_LONG)
  10. #define PY_LONG_LONG LONG_LONG
  11. #endif
  12. #ifdef MS_WIN32
  13. #include <windows.h>
  14. #endif
  15. #if defined(MS_WIN32) || defined(__CYGWIN__)
  16. #define EXPORT(x) __declspec(dllexport) x
  17. #else
  18. #define EXPORT(x) x
  19. #endif
  20. /* some functions handy for testing */
  21. EXPORT(void)testfunc_array(int values[4])
  22. {
  23. printf("testfunc_array %d %d %d %d\n",
  24. values[0],
  25. values[1],
  26. values[2],
  27. values[3]);
  28. }
  29. EXPORT(long double)testfunc_Ddd(double a, double b)
  30. {
  31. long double result = (long double)(a * b);
  32. printf("testfunc_Ddd(%p, %p)\n", &a, &b);
  33. printf("testfunc_Ddd(%g, %g)\n", a, b);
  34. return result;
  35. }
  36. EXPORT(long double)testfunc_DDD(long double a, long double b)
  37. {
  38. long double result = a * b;
  39. printf("testfunc_DDD(%p, %p)\n", &a, &b);
  40. printf("testfunc_DDD(%Lg, %Lg)\n", a, b);
  41. return result;
  42. }
  43. EXPORT(int)testfunc_iii(int a, int b)
  44. {
  45. int result = a * b;
  46. printf("testfunc_iii(%p, %p)\n", &a, &b);
  47. return result;
  48. }
  49. EXPORT(int)myprintf(char *fmt, ...)
  50. {
  51. int result;
  52. va_list argptr;
  53. va_start(argptr, fmt);
  54. result = vprintf(fmt, argptr);
  55. va_end(argptr);
  56. return result;
  57. }
  58. EXPORT(char *)my_strtok(char *token, const char *delim)
  59. {
  60. return strtok(token, delim);
  61. }
  62. EXPORT(char *)my_strchr(const char *s, int c)
  63. {
  64. return strchr(s, c);
  65. }
  66. EXPORT(double) my_sqrt(double a)
  67. {
  68. return sqrt(a);
  69. }
  70. EXPORT(void) my_qsort(void *base, size_t num, size_t width, int(*compare)(const void*, const void*))
  71. {
  72. qsort(base, num, width, compare);
  73. }
  74. EXPORT(int *) _testfunc_ai8(int a[8])
  75. {
  76. return a;
  77. }
  78. EXPORT(void) _testfunc_v(int a, int b, int *presult)
  79. {
  80. *presult = a + b;
  81. }
  82. EXPORT(int) _testfunc_i_bhilfd(signed char b, short h, int i, long l, float f, double d)
  83. {
  84. /* printf("_testfunc_i_bhilfd got %d %d %d %ld %f %f\n",
  85. b, h, i, l, f, d);
  86. */
  87. return (int)(b + h + i + l + f + d);
  88. }
  89. EXPORT(float) _testfunc_f_bhilfd(signed char b, short h, int i, long l, float f, double d)
  90. {
  91. /* printf("_testfunc_f_bhilfd got %d %d %d %ld %f %f\n",
  92. b, h, i, l, f, d);
  93. */
  94. return (float)(b + h + i + l + f + d);
  95. }
  96. EXPORT(double) _testfunc_d_bhilfd(signed char b, short h, int i, long l, float f, double d)
  97. {
  98. /* printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
  99. b, h, i, l, f, d);
  100. */
  101. return (double)(b + h + i + l + f + d);
  102. }
  103. EXPORT(long double) _testfunc_D_bhilfD(signed char b, short h, int i, long l, float f, long double d)
  104. {
  105. /* printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
  106. b, h, i, l, f, d);
  107. */
  108. return (long double)(b + h + i + l + f + d);
  109. }
  110. EXPORT(char *) _testfunc_p_p(void *s)
  111. {
  112. return (char *)s;
  113. }
  114. EXPORT(void *) _testfunc_c_p_p(int *argcp, char **argv)
  115. {
  116. return argv[(*argcp)-1];
  117. }
  118. EXPORT(void *) get_strchr(void)
  119. {
  120. return (void *)strchr;
  121. }
  122. EXPORT(char *) my_strdup(char *src)
  123. {
  124. char *dst = (char *)malloc(strlen(src)+1);
  125. if (!dst)
  126. return NULL;
  127. strcpy(dst, src);
  128. return dst;
  129. }
  130. EXPORT(void)my_free(void *ptr)
  131. {
  132. free(ptr);
  133. }
  134. #ifdef HAVE_WCHAR_H
  135. EXPORT(wchar_t *) my_wcsdup(wchar_t *src)
  136. {
  137. size_t len = wcslen(src);
  138. wchar_t *ptr = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
  139. if (ptr == NULL)
  140. return NULL;
  141. memcpy(ptr, src, (len+1) * sizeof(wchar_t));
  142. return ptr;
  143. }
  144. EXPORT(size_t) my_wcslen(wchar_t *src)
  145. {
  146. return wcslen(src);
  147. }
  148. #endif
  149. #ifndef MS_WIN32
  150. # ifndef __stdcall
  151. # define __stdcall /* */
  152. # endif
  153. #endif
  154. typedef struct {
  155. int (*c)(int, int);
  156. int (__stdcall *s)(int, int);
  157. } FUNCS;
  158. EXPORT(int) _testfunc_callfuncp(FUNCS *fp)
  159. {
  160. fp->c(1, 2);
  161. fp->s(3, 4);
  162. return 0;
  163. }
  164. EXPORT(int) _testfunc_deref_pointer(int *pi)
  165. {
  166. return *pi;
  167. }
  168. #ifdef MS_WIN32
  169. EXPORT(int) _testfunc_piunk(IUnknown FAR *piunk)
  170. {
  171. piunk->lpVtbl->AddRef(piunk);
  172. return piunk->lpVtbl->Release(piunk);
  173. }
  174. #endif
  175. EXPORT(int) _testfunc_callback_with_pointer(int (*func)(int *))
  176. {
  177. int table[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  178. return (*func)(table);
  179. }
  180. #ifdef HAVE_LONG_LONG
  181. EXPORT(PY_LONG_LONG) _testfunc_q_bhilfdq(signed char b, short h, int i, long l, float f,
  182. double d, PY_LONG_LONG q)
  183. {
  184. return (PY_LONG_LONG)(b + h + i + l + f + d + q);
  185. }
  186. EXPORT(PY_LONG_LONG) _testfunc_q_bhilfd(signed char b, short h, int i, long l, float f, double d)
  187. {
  188. return (PY_LONG_LONG)(b + h + i + l + f + d);
  189. }
  190. EXPORT(int) _testfunc_callback_i_if(int value, int (*func)(int))
  191. {
  192. int sum = 0;
  193. while (value != 0) {
  194. sum += func(value);
  195. value /= 2;
  196. }
  197. return sum;
  198. }
  199. EXPORT(PY_LONG_LONG) _testfunc_callback_q_qf(PY_LONG_LONG value,
  200. PY_LONG_LONG (*func)(PY_LONG_LONG))
  201. {
  202. PY_LONG_LONG sum = 0;
  203. while (value != 0) {
  204. sum += func(value);
  205. value /= 2;
  206. }
  207. return sum;
  208. }
  209. #endif
  210. typedef struct {
  211. char *name;
  212. char *value;
  213. } SPAM;
  214. typedef struct {
  215. char *name;
  216. int num_spams;
  217. SPAM *spams;
  218. } EGG;
  219. SPAM my_spams[2] = {
  220. { "name1", "value1" },
  221. { "name2", "value2" },
  222. };
  223. EGG my_eggs[1] = {
  224. { "first egg", 1, my_spams }
  225. };
  226. EXPORT(int) getSPAMANDEGGS(EGG **eggs)
  227. {
  228. *eggs = my_eggs;
  229. return 1;
  230. }
  231. typedef struct tagpoint {
  232. int x;
  233. int y;
  234. } point;
  235. EXPORT(int) _testfunc_byval(point in, point *pout)
  236. {
  237. if (pout) {
  238. pout->x = in.x;
  239. pout->y = in.y;
  240. }
  241. return in.x + in.y;
  242. }
  243. EXPORT (int) an_integer = 42;
  244. EXPORT(int) get_an_integer(void)
  245. {
  246. return an_integer;
  247. }
  248. EXPORT(double)
  249. integrate(double a, double b, double (*f)(double), long nstep)
  250. {
  251. double x, sum=0.0, dx=(b-a)/(double)nstep;
  252. for(x=a+0.5*dx; (b-x)*(x-a)>0.0; x+=dx)
  253. sum += f(x);
  254. return sum/(double)nstep;
  255. }
  256. typedef struct {
  257. void (*initialize)(void *(*)(int), void(*)(void *));
  258. } xxx_library;
  259. static void _xxx_init(void *(*Xalloc)(int), void (*Xfree)(void *))
  260. {
  261. void *ptr;
  262. printf("_xxx_init got %p %p\n", Xalloc, Xfree);
  263. printf("calling\n");
  264. ptr = Xalloc(32);
  265. Xfree(ptr);
  266. printf("calls done, ptr was %p\n", ptr);
  267. }
  268. xxx_library _xxx_lib = {
  269. _xxx_init
  270. };
  271. EXPORT(xxx_library) *library_get(void)
  272. {
  273. return &_xxx_lib;
  274. }
  275. #ifdef MS_WIN32
  276. /* See Don Box (german), pp 79ff. */
  277. EXPORT(void) GetString(BSTR *pbstr)
  278. {
  279. *pbstr = SysAllocString(L"Goodbye!");
  280. }
  281. #endif
  282. /*
  283. * Some do-nothing functions, for speed tests
  284. */
  285. PyObject *py_func_si(PyObject *self, PyObject *args)
  286. {
  287. char *name;
  288. int i;
  289. if (!PyArg_ParseTuple(args, "si", &name, &i))
  290. return NULL;
  291. Py_INCREF(Py_None);
  292. return Py_None;
  293. }
  294. EXPORT(void) _py_func_si(char *s, int i)
  295. {
  296. }
  297. PyObject *py_func(PyObject *self, PyObject *args)
  298. {
  299. Py_INCREF(Py_None);
  300. return Py_None;
  301. }
  302. EXPORT(void) _py_func(void)
  303. {
  304. }
  305. EXPORT(PY_LONG_LONG) last_tf_arg_s;
  306. EXPORT(unsigned PY_LONG_LONG) last_tf_arg_u;
  307. struct BITS {
  308. int A: 1, B:2, C:3, D:4, E: 5, F: 6, G: 7, H: 8, I: 9;
  309. short M: 1, N: 2, O: 3, P: 4, Q: 5, R: 6, S: 7;
  310. };
  311. DL_EXPORT(void) set_bitfields(struct BITS *bits, char name, int value)
  312. {
  313. switch (name) {
  314. case 'A': bits->A = value; break;
  315. case 'B': bits->B = value; break;
  316. case 'C': bits->C = value; break;
  317. case 'D': bits->D = value; break;
  318. case 'E': bits->E = value; break;
  319. case 'F': bits->F = value; break;
  320. case 'G': bits->G = value; break;
  321. case 'H': bits->H = value; break;
  322. case 'I': bits->I = value; break;
  323. case 'M': bits->M = value; break;
  324. case 'N': bits->N = value; break;
  325. case 'O': bits->O = value; break;
  326. case 'P': bits->P = value; break;
  327. case 'Q': bits->Q = value; break;
  328. case 'R': bits->R = value; break;
  329. case 'S': bits->S = value; break;
  330. }
  331. }
  332. DL_EXPORT(int) unpack_bitfields(struct BITS *bits, char name)
  333. {
  334. switch (name) {
  335. case 'A': return bits->A;
  336. case 'B': return bits->B;
  337. case 'C': return bits->C;
  338. case 'D': return bits->D;
  339. case 'E': return bits->E;
  340. case 'F': return bits->F;
  341. case 'G': return bits->G;
  342. case 'H': return bits->H;
  343. case 'I': return bits->I;
  344. case 'M': return bits->M;
  345. case 'N': return bits->N;
  346. case 'O': return bits->O;
  347. case 'P': return bits->P;
  348. case 'Q': return bits->Q;
  349. case 'R': return bits->R;
  350. case 'S': return bits->S;
  351. }
  352. return 0;
  353. }
  354. static PyMethodDef module_methods[] = {
  355. /* {"get_last_tf_arg_s", get_last_tf_arg_s, METH_NOARGS},
  356. {"get_last_tf_arg_u", get_last_tf_arg_u, METH_NOARGS},
  357. */
  358. {"func_si", py_func_si, METH_VARARGS},
  359. {"func", py_func, METH_NOARGS},
  360. { NULL, NULL, 0, NULL},
  361. };
  362. #define S last_tf_arg_s = (PY_LONG_LONG)c
  363. #define U last_tf_arg_u = (unsigned PY_LONG_LONG)c
  364. EXPORT(signed char) tf_b(signed char c) { S; return c/3; }
  365. EXPORT(unsigned char) tf_B(unsigned char c) { U; return c/3; }
  366. EXPORT(short) tf_h(short c) { S; return c/3; }
  367. EXPORT(unsigned short) tf_H(unsigned short c) { U; return c/3; }
  368. EXPORT(int) tf_i(int c) { S; return c/3; }
  369. EXPORT(unsigned int) tf_I(unsigned int c) { U; return c/3; }
  370. EXPORT(long) tf_l(long c) { S; return c/3; }
  371. EXPORT(unsigned long) tf_L(unsigned long c) { U; return c/3; }
  372. EXPORT(PY_LONG_LONG) tf_q(PY_LONG_LONG c) { S; return c/3; }
  373. EXPORT(unsigned PY_LONG_LONG) tf_Q(unsigned PY_LONG_LONG c) { U; return c/3; }
  374. EXPORT(float) tf_f(float c) { S; return c/3; }
  375. EXPORT(double) tf_d(double c) { S; return c/3; }
  376. EXPORT(long double) tf_D(long double c) { S; return c/3; }
  377. #ifdef MS_WIN32
  378. EXPORT(signed char) __stdcall s_tf_b(signed char c) { S; return c/3; }
  379. EXPORT(unsigned char) __stdcall s_tf_B(unsigned char c) { U; return c/3; }
  380. EXPORT(short) __stdcall s_tf_h(short c) { S; return c/3; }
  381. EXPORT(unsigned short) __stdcall s_tf_H(unsigned short c) { U; return c/3; }
  382. EXPORT(int) __stdcall s_tf_i(int c) { S; return c/3; }
  383. EXPORT(unsigned int) __stdcall s_tf_I(unsigned int c) { U; return c/3; }
  384. EXPORT(long) __stdcall s_tf_l(long c) { S; return c/3; }
  385. EXPORT(unsigned long) __stdcall s_tf_L(unsigned long c) { U; return c/3; }
  386. EXPORT(PY_LONG_LONG) __stdcall s_tf_q(PY_LONG_LONG c) { S; return c/3; }
  387. EXPORT(unsigned PY_LONG_LONG) __stdcall s_tf_Q(unsigned PY_LONG_LONG c) { U; return c/3; }
  388. EXPORT(float) __stdcall s_tf_f(float c) { S; return c/3; }
  389. EXPORT(double) __stdcall s_tf_d(double c) { S; return c/3; }
  390. EXPORT(long double) __stdcall s_tf_D(long double c) { S; return c/3; }
  391. #endif
  392. /*******/
  393. EXPORT(signed char) tf_bb(signed char x, signed char c) { S; return c/3; }
  394. EXPORT(unsigned char) tf_bB(signed char x, unsigned char c) { U; return c/3; }
  395. EXPORT(short) tf_bh(signed char x, short c) { S; return c/3; }
  396. EXPORT(unsigned short) tf_bH(signed char x, unsigned short c) { U; return c/3; }
  397. EXPORT(int) tf_bi(signed char x, int c) { S; return c/3; }
  398. EXPORT(unsigned int) tf_bI(signed char x, unsigned int c) { U; return c/3; }
  399. EXPORT(long) tf_bl(signed char x, long c) { S; return c/3; }
  400. EXPORT(unsigned long) tf_bL(signed char x, unsigned long c) { U; return c/3; }
  401. EXPORT(PY_LONG_LONG) tf_bq(signed char x, PY_LONG_LONG c) { S; return c/3; }
  402. EXPORT(unsigned PY_LONG_LONG) tf_bQ(signed char x, unsigned PY_LONG_LONG c) { U; return c/3; }
  403. EXPORT(float) tf_bf(signed char x, float c) { S; return c/3; }
  404. EXPORT(double) tf_bd(signed char x, double c) { S; return c/3; }
  405. EXPORT(long double) tf_bD(signed char x, long double c) { S; return c/3; }
  406. EXPORT(void) tv_i(int c) { S; return; }
  407. #ifdef MS_WIN32
  408. EXPORT(signed char) __stdcall s_tf_bb(signed char x, signed char c) { S; return c/3; }
  409. EXPORT(unsigned char) __stdcall s_tf_bB(signed char x, unsigned char c) { U; return c/3; }
  410. EXPORT(short) __stdcall s_tf_bh(signed char x, short c) { S; return c/3; }
  411. EXPORT(unsigned short) __stdcall s_tf_bH(signed char x, unsigned short c) { U; return c/3; }
  412. EXPORT(int) __stdcall s_tf_bi(signed char x, int c) { S; return c/3; }
  413. EXPORT(unsigned int) __stdcall s_tf_bI(signed char x, unsigned int c) { U; return c/3; }
  414. EXPORT(long) __stdcall s_tf_bl(signed char x, long c) { S; return c/3; }
  415. EXPORT(unsigned long) __stdcall s_tf_bL(signed char x, unsigned long c) { U; return c/3; }
  416. EXPORT(PY_LONG_LONG) __stdcall s_tf_bq(signed char x, PY_LONG_LONG c) { S; return c/3; }
  417. EXPORT(unsigned PY_LONG_LONG) __stdcall s_tf_bQ(signed char x, unsigned PY_LONG_LONG c) { U; return c/3; }
  418. EXPORT(float) __stdcall s_tf_bf(signed char x, float c) { S; return c/3; }
  419. EXPORT(double) __stdcall s_tf_bd(signed char x, double c) { S; return c/3; }
  420. EXPORT(long double) __stdcall s_tf_bD(signed char x, long double c) { S; return c/3; }
  421. EXPORT(void) __stdcall s_tv_i(int c) { S; return; }
  422. #endif
  423. /********/
  424. #ifndef MS_WIN32
  425. typedef struct {
  426. long x;
  427. long y;
  428. } POINT;
  429. typedef struct {
  430. long left;
  431. long top;
  432. long right;
  433. long bottom;
  434. } RECT;
  435. #endif
  436. EXPORT(int) PointInRect(RECT *prc, POINT pt)
  437. {
  438. if (pt.x < prc->left)
  439. return 0;
  440. if (pt.x > prc->right)
  441. return 0;
  442. if (pt.y < prc->top)
  443. return 0;
  444. if (pt.y > prc->bottom)
  445. return 0;
  446. return 1;
  447. }
  448. typedef struct {
  449. short x;
  450. short y;
  451. } S2H;
  452. EXPORT(S2H) ret_2h_func(S2H inp)
  453. {
  454. inp.x *= 2;
  455. inp.y *= 3;
  456. return inp;
  457. }
  458. typedef struct {
  459. int a, b, c, d, e, f, g, h;
  460. } S8I;
  461. EXPORT(S8I) ret_8i_func(S8I inp)
  462. {
  463. inp.a *= 2;
  464. inp.b *= 3;
  465. inp.c *= 4;
  466. inp.d *= 5;
  467. inp.e *= 6;
  468. inp.f *= 7;
  469. inp.g *= 8;
  470. inp.h *= 9;
  471. return inp;
  472. }
  473. EXPORT(int) GetRectangle(int flag, RECT *prect)
  474. {
  475. if (flag == 0)
  476. return 0;
  477. prect->left = (int)flag;
  478. prect->top = (int)flag + 1;
  479. prect->right = (int)flag + 2;
  480. prect->bottom = (int)flag + 3;
  481. return 1;
  482. }
  483. EXPORT(void) TwoOutArgs(int a, int *pi, int b, int *pj)
  484. {
  485. *pi += a;
  486. *pj += b;
  487. }
  488. #ifdef MS_WIN32
  489. EXPORT(S2H) __stdcall s_ret_2h_func(S2H inp) { return ret_2h_func(inp); }
  490. EXPORT(S8I) __stdcall s_ret_8i_func(S8I inp) { return ret_8i_func(inp); }
  491. #endif
  492. #ifdef MS_WIN32
  493. /* Should port this */
  494. #include <stdlib.h>
  495. #include <search.h>
  496. EXPORT (HRESULT) KeepObject(IUnknown *punk)
  497. {
  498. static IUnknown *pobj;
  499. if (punk)
  500. punk->lpVtbl->AddRef(punk);
  501. if (pobj)
  502. pobj->lpVtbl->Release(pobj);
  503. pobj = punk;
  504. return S_OK;
  505. }
  506. #endif
  507. DL_EXPORT(void)
  508. init_ctypes_test(void)
  509. {
  510. Py_InitModule("_ctypes_test", module_methods);
  511. }