-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadnumerictable.c
215 lines (185 loc) · 4.74 KB
/
readnumerictable.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "readnumerictable.h"
#ifndef __EMSCRIPTEN__
#include <sqlite3.h>
#else
#include "sqlite3.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
static int callback(void *count, int argc, char **argv, char **azColName) {
int *c = count;
*c = atoi(argv[0]);
return 0;
}
float* readnumerictables(char * filename, int nrColumns, int* nrow) {
int nrTables;
char** tNames = tableNames(filename, &nrTables);
*nrow = 0;
for (int i = 0; i < nrTables; i++) {
int nn;
float* xx = readnumerictable(filename, tNames[i], nrColumns, &nn);
free(xx);
(*nrow) += nn;
}
// now we know the total number;
float* self = malloc(nrColumns * *nrow * sizeof(float));
int k = 0;
for (int i = 0; i < nrTables; i++) {
int nn;
float* xx = readnumerictable(filename, tNames[i], nrColumns, &nn);
for (int j = 0; j < nrColumns * nn; j++) {
self[k++] = xx[j];
}
free(xx);
free(tNames[i]);
}
free(tNames);
return self;
}
float* readnumerictable(char * filename, char* tablename, int nrColumns, int* nrow) {
sqlite3 *db;
sqlite3_stmt *stmt;
int kk = sqlite3_open(filename, &db);
if (db == NULL || kk != SQLITE_OK)
{
printf("Failed to open DB ");
printf(filename);
printf(" with error code %i \n", kk);
exit(1);
}
sqlite3_extended_result_codes(db, 1);
char m[500];
strcpy(m, "select count(*) from '");
strcat(m, tablename);
strcat(m, "';");
char* zErrMsg;
int rc = sqlite3_exec(db, m, callback, nrow, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
fprintf(stdout, m);
fprintf(stdout, "%i rows \n", *nrow);
// now, we have nrow
float* res = malloc(nrColumns * *nrow * sizeof(float));
strcpy(m, "select * from '");
strcat(m, tablename);
strcat(m, "';");
int k = sqlite3_prepare_v2(db, m, -1, &stmt, NULL);
if (k != SQLITE_OK) {
printf("Failed to open DB ");
printf("error code %i\n", k);
printf(filename);
printf("\n");
exit(1);
}
int i = 0;
while (sqlite3_step(stmt) != SQLITE_DONE) {
for (int j = 0; j < nrColumns; j++) {
float f = (float)sqlite3_column_double(stmt, j);
// i is row index, j is column
res[j + nrColumns * i] = f;
}
i++;
}
sqlite3_finalize(stmt);
sqlite3_close(db);
fprintf(stdout, "I pass the value %i along.\n", *nrow);
return res;
}
/*
int* readintegertable(char * filename, char* tablename, int nrColumns, int* nrow) {
sqlite3 *db;
sqlite3_stmt *stmt;
int kk = sqlite3_open(filename, &db);
if (db == NULL || kk != SQLITE_OK)
{
printf("Failed to open DB ");
printf(filename);
printf(" with error code %i \n", kk);
exit(1);
}
sqlite3_extended_result_codes(db, 1);
char m[500];
strcpy(m, "select count(*) from '");
strcat(m, tablename);
strcat(m, "';");
char* zErrMsg;
int rc = sqlite3_exec(db, m, callback, nrow, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
// now, we have nrow
long long int* res = malloc(nrColumns * *nrow * sizeof(long long int));
strcpy(m, "select * from '");
strcat(m, tablename);
strcat(m, "';");
int k = sqlite3_prepare_v2(db, m, -1, &stmt, NULL);
if (k != SQLITE_OK) {
printf("Failed to open DB ");
printf("error code %i\n", k);
printf(filename);
printf("\n");
exit(1);
}
int i = 0;
while (sqlite3_step(stmt) != SQLITE_DONE) {
for (int j = 0; j < nrColumns; j++) {
int f;
if (j == 0) {
int long long ff = sqlite3_column_integer(stmt, j);
f = (int) (ff / 1000);
} else {
f = sqlite3_column_integer(stmt, j);
}
// i is row index, j is column
res[j + nrColumns * i] = f;
}
i++;
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return res;
}
*/
char** tableNames(char * filename, int* nrTables) {
sqlite3 *db;
int kk = sqlite3_open(filename, &db);
if (db == NULL || kk != SQLITE_OK)
{
printf("Failed to open DB ");
printf(filename);
printf(" with error code %i \n", kk);
exit(1);
}
sqlite3_extended_result_codes(db, 1);
char* m = "SELECT count(tbl_name) FROM sqlite_master WHERE type='table' AND tbl_name != 'legend';";
char* zErrMsg;
int rc = sqlite3_exec(db, m, callback, nrTables, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
// now, we have nrTables
char** res = malloc(*nrTables * sizeof(char*));
char* n = "SELECT tbl_name FROM sqlite_master WHERE type='table' AND tbl_name != 'legend';";
sqlite3_stmt *stmt;
int k = sqlite3_prepare_v2(db, n, -1, &stmt, NULL);
if (k != SQLITE_OK) {
printf("Failed to open DB ");
printf("error code %i\n", k);
printf(filename);
printf("\n");
exit(1);
}
int i = 0;
while (sqlite3_step(stmt) != SQLITE_DONE) {
char* tname = (char*)sqlite3_column_text(stmt, 0);
res[i++] = strdup(tname);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return res;
}