Explicit types in the generator.
[wrappi.git] / generator / wrappi_c.ml
1 (* wrappi
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *)
18
19 open Wrappi_types
20 open Wrappi_utils
21 open Wrappi_pr
22 open Wrappi_boilerplate
23
24 open Printf
25
26 let c_of_any_type = function
27   | TFilePerm -> "int"
28   | TInt32 -> "int32_t"
29   | TInt64 -> "int64_t"
30   | TPathname -> "const char *"
31   | TUInt32 -> "uint32_t"
32   | TUInt64 -> "uint64_t"
33
34 let c_of_return_type = function
35   | RErr -> "int"
36   | Return t -> c_of_any_type t
37
38 let generate_lib_wrappi_h api =
39   generate_header CStyle LGPLv2plus;
40
41   pr "\
42 /* Please read the wrappi(1) man page for full documentation.  If you
43  * are not familiar with man pages or don't have the documentation
44  * installed, it is also available online at http://wrappi.org/
45  */
46
47 #ifndef WRAPPI_H_
48 #define WRAPPI_H_
49
50 #ifdef __cplusplus
51 extern \"C\" {
52 #endif
53
54 #include <stdint.h>
55
56 /* The handle. */
57 typedef struct wrap_h wrap_h;
58
59 /* Connection management. */
60 extern wrap_h *wrap_create (void);
61 extern void wrap_close (wrap_h *w);
62
63 /* API entry points. */
64 ";
65
66   List.iter (
67     fun ep ->
68       pr "extern %s wrap_%s (wrap_h *w, %s);\n"
69         (c_of_return_type ep.ep_return)
70         ep.ep_name
71         (String.concat ", "
72            (List.map (
73              fun (name, t) ->
74                let t = c_of_any_type t in
75                let last_char = t.[String.length t - 1] in
76                let sep = if isalnum last_char then " " else "" in
77                sprintf "%s%s%s" t sep name
78             ) ep.ep_params))
79   ) api.api_entry_points;
80
81   pr "\
82
83 #ifdef __cplusplus
84 }
85 #endif
86
87 #endif /* WRAPPI_H_ */
88 "
89
90 let generate api =
91   output_to "lib/wrappi.h" generate_lib_wrappi_h api