Fix / Improve with-foreign-pointer-as-string in manual
[cffi.git] / tests / libfsbv.c
blob8df645ec2d41b61a28e2eecc62807f0d4d405dc7
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*-
3 * libfsbv.c --- auxiliary C lib for testing foreign structure by value calls
5 * Copyright (C) 2011, 2015 Liam M. Healy
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy,
11 * modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
28 #ifdef WIN32
29 #define DLLEXPORT __declspec(dllexport)
30 #else
31 #define DLLEXPORT
32 #endif
34 #include <stdio.h>
35 #include <limits.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <stdbool.h>
39 #include <math.h>
40 #include <float.h>
42 /* MSVC doesn't have stdint.h and uses a different syntax for stdcall */
43 #ifndef _MSC_VER
44 #include <stdint.h>
45 #endif
47 #ifdef WIN32
48 #ifdef _MSC_VER
49 #define STDCALL __stdcall
50 #else
51 #define STDCALL __attribute__((stdcall))
52 #endif
53 #else
54 #define STDCALL
55 #endif
57 struct struct_pair {
58 int a;
59 int b;
62 struct struct_pair_double {
63 struct struct_pair pr;
64 double dbl;
67 typedef enum {
68 ONE = 1,
69 TWO,
70 THREE,
71 FOUR,
72 FORTY_ONE = 41,
73 FORTY_TWO
74 } numeros;
76 int sumpair (struct struct_pair sp);
77 int enumpair (numeros mynum, struct struct_pair sp);
78 struct struct_pair doublepair (struct struct_pair dp);
79 double prodsumpair (struct struct_pair_double spd);
80 struct struct_pair_double doublepairdouble (struct struct_pair_double pd);
82 DLLEXPORT
83 int sumpair (struct struct_pair sp)
85 return sp.a + sp.b;
88 DLLEXPORT
89 int enumpair (numeros mynum, struct struct_pair sp)
91 if ( mynum == ONE )
93 return sp.a + sp.b;
95 else if ( mynum == TWO )
97 return sp.a + 2*sp.b;
99 else if ( mynum == THREE )
101 return 2*sp.a + sp.b;
103 else if ( mynum == FOUR )
105 return 2*sp.a + 2*sp.b;
107 else
109 return 41*sp.a + 42*sp.b;
113 DLLEXPORT
114 struct struct_pair makepair (bool cond)
116 struct struct_pair ret;
117 ret.a = -127;
118 ret.b = cond ? 42 : 43;
119 return ret;
122 const struct struct_pair static_pair = { 40, 2};
124 DLLEXPORT
125 struct struct_pair * returnpairpointer (struct struct_pair ignored)
127 return &static_pair;
130 DLLEXPORT
131 struct struct_pair doublepair (struct struct_pair dp)
133 struct struct_pair ret;
134 ret.a = 2*dp.a;
135 ret.b = 2*dp.b;
136 return ret;
139 DLLEXPORT
140 double prodsumpair (struct struct_pair_double pd)
142 return pd.dbl * sumpair(pd.pr);
145 DLLEXPORT
146 struct struct_pair_double doublepairdouble (struct struct_pair_double pd)
148 struct struct_pair_double ret;
149 ret.pr = doublepair(pd.pr);
150 ret.dbl = 2*pd.dbl;
151 return ret;
154 DLLEXPORT
155 unsigned long long ullsum (unsigned long long a, unsigned long long b)
157 return a + b;
160 DLLEXPORT
161 struct struct_pair stringlenpair (char *string, struct struct_pair dp)
163 struct struct_pair ret;
164 int len = strlen(string);
165 ret.a = len*dp.a;
166 ret.b = len*dp.b;
167 return ret;
170 struct bitfield_struct {
171 unsigned int b;
174 DLLEXPORT
175 struct bitfield_struct structbitfield (unsigned int x) {
176 struct bitfield_struct ret;
177 ret.b = x;
178 return ret;