-
Notifications
You must be signed in to change notification settings - Fork 12
/
readEx.c
108 lines (94 loc) · 2.93 KB
/
readEx.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
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <math.h>
#include <fcntl.h>
#include <rdata.h>
const int debug = 0;
static int handle_table(const char *name, void *ctx) {
if (debug) printf("Read table: %s\n", name);
return 0;
}
char *rdata_type_text[] = {
"String",
"Integer",
"Real",
"Logical",
"Timestamp"
};
// Called once for all columns. "data" is NULL for text columns.
static int handle_column(const char *name, rdata_type_t type,
void *data, long count, void *ctx) {
if (debug) printf("Read column: %s with %ld elements of type %s\n", name, count,
rdata_type_text[type]);
for (long i=0; i<count; i++) {
switch(type) {
case RDATA_TYPE_STRING: {
// do nothing, handle_text_value is called
break;
}
case RDATA_TYPE_INT32: {
int *ip = data;
if (debug) printf("%d ", ip[i]);
break;
}
case RDATA_TYPE_REAL: {
double *dp = data;
if (debug) printf("%f ", dp[i]);
break;
}
case RDATA_TYPE_LOGICAL: {
int *ip = data;
if (debug) printf("%d ", ip[i]);
break;
}
case RDATA_TYPE_TIMESTAMP: {
double *dp = data;
if (debug) printf("%f ", dp[i]);
break;
}
default: {
if (debug) printf("(unknown) ");
break;
}
}
}
if (debug && type != RDATA_TYPE_STRING) printf("\n");
/* Do something... */
return 0;
}
static int handle_column_name(const char *name, int index, void *ctx) {
if (debug) printf("Read column name: %s\n", name);
/* Do something... */
return 0;
}
// Called once per row for a text column
static int handle_text_value(const char *value, int index, void *ctx) {
if (debug) printf("Read text value: %s at %d\n", value, index);
/* Do something... */
return 0;
}
// Called for factor variables, once for each level
static int handle_value_label(const char *value, int index, void *ctx) {
if (debug) printf("Read value label: %s at %d\n", value, index);
/* Do something... */
return 0;
}
int main() {
rdata_parser_t *parser = rdata_parser_init();
rdata_set_table_handler(parser, &handle_table);
rdata_set_column_handler(parser, &handle_column);
rdata_set_column_name_handler(parser, &handle_column_name);
rdata_set_text_value_handler(parser, &handle_text_value);
rdata_set_value_label_handler(parser, &handle_value_label);
if (access("example.RData", F_OK) != -1) {
rdata_error_t err = rdata_parse(parser, "example.RData", NULL);
if (debug) printf("Error code %d\n", err);
}
if (access("example.rds", F_OK) != -1) {
rdata_error_t err = rdata_parse(parser, "example.rds", NULL);
if (debug) printf("Error code %d\n", err);
}
exit(0);
}