This repository has been archived by the owner on Jul 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.c
359 lines (314 loc) · 9.81 KB
/
loader.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include "stdafx.h"
#include "loader.h"
#include "utils.h"
#include "third-party/cjson/cJSON.h"
#include "database.h"
#include "output.h"
#define FREE(s) if (s != NULL) { free(s); s = NULL; }
#define COPY_STRING(dest, src, index, len)\
{ \
strncpy(dest, src + index, len);\
dest[len] = '\0';\
}
#define INPUT_AND_DUMP(text, scanf_if, name, json_variable, verify_code) \
while (1) {\
printf("%s%s%s", "请输入", text, "(");\
dump_##json_variable();\
printf("):");\
if (scanf_if == EOF) {\
break_while = 1;\
break;\
}\
verify_code\
break;\
}\
if (break_while == 1) break;
#define INPUT_AND_DUMP_ARRAY(text, name, json_variable) \
INPUT_AND_DUMP(text, scanf_s("%d", &name), name, json_variable, {\
if (name >= json_variable##_length) {\
printf("%s%s%s", "输入的", text, "信息错误,请重新输入。\n");\
continue;\
}\
})
#define INPUT_AND_DUMP_OBJECT(text, name, json_variable) \
INPUT_AND_DUMP(text, scanf_s("%s", name, 10), name, json_variable, {\
if (!cJSON_HasObjectItem(json_variable, name)) {\
printf("%s%s%s", "输入的", text, "信息错误,请重新输入。\n");\
continue;\
}\
})
#define SCANF_UTF8(variable, length)\
if (scanf_s("%s", temp, length) == EOF) break;\
utf8_len = gbk_to_utf8((unsigned char *)temp, NULL, (int)NULL);\
variable = malloc(sizeof(char) * (utf8_len + 1));\
gbk_to_utf8((unsigned char *)temp, (unsigned char *)variable, utf8_len);
void birthday_format(int *year, int *month, int *day, char *data) {
char *p = NULL, *q = NULL;
char y[20], m[20], d[20];
p = strchr(data, '-');
if (p) {
q = strchr(p + 1, '-');
if (q == NULL) return;
}
else {
p = strchr(data, '.');
if (p) {
q = strchr(p + 1, '.');
if (q == NULL) return;
}
else {
return;
}
}
strcpy(y, data);
strcpy(m, p + 1);
strcpy(d, q + 1);
y[p - data] = '\0';
m[q - p - 1] = '\0';
*year = atoi(y);
*month = atoi(m);
*day = atoi(d);
return;
}
int check_gov_id(char *gov_id)
{
long sum = 0;
int R[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
char table[11] = { '1','0','X', '9', '8', '7', '6', '5', '4', '3', '2' };
if (18 != strlen(gov_id)) return 0;
for (int i = 0; i < 18; i++) {
if (!isdigit(gov_id[i]) && !(('X' == gov_id[i] || 'x' == gov_id[i]) && 17 == i)) {
return 0;
}
}
for (int i = 0; i <= 16; i++) {
sum += (gov_id[i] - 48) * R[i];
}
if (table[sum % 11] != gov_id[17]) {
return 0;
}
return 1;
}
// 学号、姓名、性别、国别、出生年月、民族、政治面貌、身份证号、学生类别、入学年月、入学方式、学院、专业、学制、培养层次、年级、班级号、指导员
void load_from_stdin(void) {
char* id = malloc(sizeof(char) * 20);
char* admission_date = malloc(sizeof(char) * 60);
char* gov_id = malloc(sizeof(char) * 20);
char* sql;
char* name = NULL;// malloc(sizeof(char) * 60);
char* admission_year_from_id = malloc(sizeof(char) * 3);
char* college_id = malloc(sizeof(char) * 3);
char* birthday_from_gov_id = malloc(sizeof(char) * 10);
char* formatted_birthday = malloc(sizeof(char) * 20);
char* college_from_json = NULL;
char* discipline_from_json = NULL;
char* instructor_from_json = NULL; // malloc(sizeof(char) * 60);
char* nationality = malloc(sizeof(char) * 10);
char* political_status = malloc(sizeof(char) * 10);
char* temp = malloc(sizeof(char) * 100);
char* temp2 = malloc(sizeof(char) * 100);
cJSON* college;
cJSON *discipline = NULL;
int nation = 0;
int sex = 0;
int source = 0;
int admission = 0;
int admission_year = 0, admission_month = 0, admission_day = 0;
int grade;
time_t admission_timestamp = 0;
time_t birthday_timestamp;
int utf8_len;
int break_while = 0;
while (1) {
printf("请输入学号:");
if (scanf_s("%s", id, 10) == EOF) break;
if (strlen(id) != 9) {
printf("学号位数不正确");
break;
}
COPY_STRING(college_id, id, 0, 2);
college = get_college(college_id);
if (college == NULL) {
printf("学号所示的学院不存在。\n");
break;
}
college_from_json = cJSON_GetObjectItem(college, "name")->valuestring;
{
int i, found_flag = 0;
COPY_STRING(temp, id, 4, 1); // temp = discipline
cJSON *disciplines = cJSON_GetObjectItem(college, "disciplines");
for (i = 0; i < cJSON_GetArraySize(disciplines); i++) {
discipline = cJSON_GetArrayItem(disciplines, i);
if (strcmp(temp, cJSON_GetObjectItem(discipline, "id")->valuestring) == 0) {
discipline_from_json = cJSON_GetObjectItem(discipline, "name")->valuestring;
found_flag = 1;
break;
}
}
if (found_flag == 0) {
printf("学号所示的专业不存在。\n");
break;
}
found_flag = 0;
COPY_STRING(temp2, id, 2, 2); // temp2 = grade
cJSON *instructor_single;
cJSON_ArrayForEach(instructor_single, instructors)
{
if (
strcmp(college_id, cJSON_GetObjectItem(instructor_single, "college")->valuestring) == 0 &&
strcmp(temp, cJSON_GetObjectItem(instructor_single, "discipline")->valuestring) == 0 &&
strcmp(temp2, cJSON_GetObjectItem(instructor_single, "grade")->valuestring) == 0
)
{
instructor_from_json = cJSON_GetObjectItem(instructor_single, "id")->valuestring;
found_flag = 1;
break;
}
}
if (found_flag == 0)
{
printf("找不到该学院、专业或班级对应的辅导员。\n");
break;
}
}
{
sql = "SELECT count(id) FROM students WHERE id=?";
sqlite3_stmt *stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, id, -1, SQLITE_TRANSIENT);
int stat = sqlite3_step(stmt);
if (stat == SQLITE_ROW) {
int ret = sqlite3_column_int(stmt, 0);
if (ret > 0) {
printf("这个学号已经存在!\n");
sqlite3_finalize(stmt);
break;
}
}
else {
printf("查询出错!:%s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
break;
}
}
printf("请输入姓名:");
SCANF_UTF8(name, 10)
// scanf_s("%s", name, 10);
while (1) {
printf("请输入性别(0 = 女,1 = 男):");
if (scanf_s("%d", &sex, sizeof(sex)) == EOF) {
break_while = 1;
break;
}
if (sex != 0 && sex != 1) {
printf("性别输入错误,请重新输入。\n");
continue;
}
break;
}
if (break_while == 1) break;
INPUT_AND_DUMP_ARRAY("国家", nation, nations);
INPUT_AND_DUMP_OBJECT("民族", nationality, nationalities);
INPUT_AND_DUMP_OBJECT("政治面貌", political_status, political_statuses);
while (1) {
printf("请输入身份证号:");
if (scanf_s("%s", gov_id, 19) == EOF) {
break_while = 1;
break;
}
if (check_gov_id(gov_id) == 0 || gov_id[16] % 2 != sex) {
printf("身份证号输入错误,请重新核实。\n");
continue;
}
break;
}
if (break_while == 1) break;
INPUT_AND_DUMP_ARRAY("学生类别", source, sources);
while (1) {
printf("请输入入学年月:");
if (scanf_s("%s", admission_date, 20) == EOF) {
break_while = 1;
break;
}
birthday_format(&admission_year, &admission_month, &admission_day, admission_date);
COPY_STRING(admission_year_from_id, id, 2, 2);
admission_year_from_id[2] = '\0';
if (admission_year != atoi(admission_year_from_id) + 2000) {
printf("入学年份和学号不一致,请注意。\n");
}
if (admission_month < 8 || admission_month > 10) {
printf("入学月份不对,请重新核实。\n");
continue;
}
admission_timestamp = get_unix_timestamp(admission_year, admission_month, admission_day);
break;
}
if (break_while == 1) break;
INPUT_AND_DUMP_ARRAY("入学方式", admission, admissions);
int birthday_year, birthday_month, birthday_day;
COPY_STRING(temp, gov_id, 6, 4);
birthday_year = atoi(temp);
COPY_STRING(temp, gov_id, 10, 2);
birthday_month = atoi(temp);
COPY_STRING(temp, gov_id, 12, 2);
birthday_day = atoi(temp);
birthday_timestamp = get_unix_timestamp(birthday_year, birthday_month, birthday_day);
COPY_STRING(temp, id, 2, 2);
grade = atoi(temp);
COPY_STRING(temp, id, 5, 2);
sql = "INSERT INTO students \
(id, name, sex, birth_time, nation, nationality, political_status, gov_id, sources, admission_time, admission_type, college, discipline, grade, learn_year, training_level, class_number, instructor)\
VALUES \
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\
";
sqlite3_stmt *stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_exec(db, "begin;", NULL, NULL, NULL);
sqlite3_bind_text(stmt, 1, id, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, name, -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 3, sex);
sqlite3_bind_int(stmt, 4, (int)birthday_timestamp);
sqlite3_bind_int(stmt, 5, nation);
sqlite3_bind_text(stmt, 6, nationality, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 7, political_status, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 8, gov_id, -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 9, source);
sqlite3_bind_int(stmt, 10, (int)admission_timestamp);
sqlite3_bind_int(stmt, 11, admission);
sqlite3_bind_text(stmt, 12, college_from_json, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 13, discipline_from_json, -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 14, grade);
sqlite3_bind_int(stmt, 15, cJSON_GetObjectItem(discipline, "learn_year")->valueint);
sqlite3_bind_text(stmt, 16, cJSON_GetObjectItem(discipline, "training_level")->valuestring, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 17, temp, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 18, instructor_from_json, -1, SQLITE_TRANSIENT);
int ret = sqlite3_step(stmt);
if (ret != SQLITE_DONE) {
printf("SQLite Error: %s", sqlite3_errmsg(db));
}
sqlite3_exec(db, "commit;", NULL, NULL, NULL);
sqlite3_finalize(stmt);
break;
}
// FREE(discipline);
FREE(id);
FREE(name);
FREE(college_id);
// FREE(college);
FREE(instructor_from_json);
FREE(admission_date);
FREE(temp);
FREE(temp2);
FREE(admission_year_from_id);
FREE(birthday_from_gov_id);
FREE(political_status);
FREE(nationality);
// FREE(college_from_json);
// FREE(discipline_from_json);
// FREE(training_level_from_json);
FREE(formatted_birthday);
}
#undef FREE
#undef COPY_STRING
#undef INPUT_AND_DUMP
#undef SCANF_UTF8