/
datatypes.c
187 lines (154 loc) · 4.34 KB
/
datatypes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/fail.h>
#include <caml/callback.h>
#include <caml/alloc.h>
#include <caml/custom.h>
#include <alpm.h>
#include <alpm_list.h>
#include "datatypes.h"
CUSTOM_ALPM_TYPE( db, Database )
CUSTOM_ALPM_TYPE( pkg, Package )
void finalize_autofree_pkg ( value package )
{
fprintf( stderr, "FREEING PACKAGE!\n" );
alpm_pkg_free( Package_val( package ));
return;
}
static struct custom_operations alpm_pkg_free_opts = {
"org.archlinux.caml.alpm.pkg.autofree",
finalize_autofree_pkg,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default,
};
value alloc_alpm_pkg_autofree ( pmpkg_t * pkg )
{
value custom = alloc_custom( &alpm_pkg_free_opts,
sizeof ( pmpkg_t * ),
0, 1 );
Package_val( custom ) = pkg;
return custom;
}
value alpm_to_caml_list ( alpm_list_t * list, alpm_elem_conv converter )
{
CAMLparam0();
CAMLlocal1( cell );
if ( list ) {
cell = caml_alloc( 2, 0 );
Store_field( cell, 0, (*converter)( list->data ));
Store_field( cell, 1, alpm_to_caml_list( list->next, converter ));
}
else {
cell = Val_int( 0 );
}
CAMLreturn( cell );
}
value alpm_to_caml_strelem ( void * elem )
{
CAMLparam0();
CAMLreturn( caml_copy_string( (char *) elem ));
}
value alpm_to_caml_dependency ( void * elem )
{
CAMLparam0();
CAMLreturn( caml_copy_dependency( (pmdepend_t *) elem ));
}
value alpm_to_caml_grpelem ( void * elem )
{
CAMLparam0();
CAMLreturn( caml_copy_group( (pmgrp_t *) elem ));
}
static alpm_list_t * build_alpm_list ( value list,
caml_elem_conv converter,
alpm_list_t **last )
{
alpm_list_t * elem, * next;
if ( Is_block( list )) {
elem = malloc( sizeof( alpm_list_t ));
elem->prev = NULL;
elem->data = (*converter)( Field( list, 0 ));
next = caml_to_alpm_list( Field( list, 1 ), converter );
elem->next = next;
if ( next == NULL ) *last = elem;
else next->prev = elem;
}
else {
/* Field 0 of the block list should be 0. */
elem = NULL;
}
return elem;
}
value alpm_to_caml_dbelem ( void * elem )
{
return alloc_alpm_db( (pmdb_t *) elem );
}
value alpm_to_caml_pkgelem ( void * elem )
{
return alloc_alpm_pkg( (pmpkg_t *) elem );
}
alpm_list_t * caml_to_alpm_list ( value list,
caml_elem_conv converter )
{
alpm_list_t * head, * tail;
head = tail = NULL;
head = build_alpm_list( list, converter, &tail );
if ( head && tail ) head->prev = tail;
return head;
}
void * caml_to_alpm_strelem ( value str )
{
char * alpm_str;
int len;
len = caml_string_length( str );
alpm_str = calloc( len, sizeof( char ));
if ( alpm_str == NULL ) return NULL;
memcpy( alpm_str, String_val( str ),
len * sizeof( char ));
return (void *) alpm_str;
}
value caml_copy_depmod ( pmdepmod_t depmod )
{
int idx;
CAMLparam0();
CAMLlocal1( camldepmod );
switch ( depmod ) {
case PM_DEP_MOD_ANY: idx = 0; break;
case PM_DEP_MOD_EQ: idx = 1; break;
case PM_DEP_MOD_GE: idx = 2; break;
case PM_DEP_MOD_LE: idx = 3; break;
case PM_DEP_MOD_GT: idx = 4; break;
case PM_DEP_MOD_LT: idx = 5; break;
caml_invalid_argument( "Unrecognized depmod data-type" );
}
camldepmod = Val_int( idx );
CAMLreturn( camldepmod );
}
value caml_copy_dependency ( pmdepend_t * dep )
{
CAMLparam0();
CAMLlocal1( camldep );
camldep = caml_alloc( 3, 0 );
Store_field( camldep, 0, caml_copy_string( dep->name ));
Store_field( camldep, 1, caml_copy_depmod( dep->mod ));
if ( dep->mod == PM_DEP_MOD_ANY ) {
Store_field( camldep, 2, caml_copy_string( "" ));
}
else {
Store_field( camldep, 2, caml_copy_string( dep->version ));
}
CAMLreturn( camldep );
}
value caml_copy_group ( pmgrp_t * group )
{
CAMLparam0();
CAMLlocal1( camlgroup );
camlgroup = caml_alloc( 2, 0 );
Store_field( camlgroup, 0, caml_copy_string( group->name ));
Store_field( camlgroup, 1, CAML_PKG_LIST( group->packages ));
CAMLreturn( camlgroup );
}