-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsconv.c
167 lines (159 loc) · 4.3 KB
/
sconv.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
/* generate srank input */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "srank.h"
#define LEN(a) (sizeof(a) / sizeof((a)[0]))
/* read a line and split it at tab characters */
static int onemoreline(FILE *fp, char **cols, int sz)
{
int ncols = 0;
int c = fgetc(fp);
int quote = 0;
struct sbuf *sb;
memset(cols, 0, sz * sizeof(cols[0]));
if (c == EOF)
return -1;
sb = sbuf_make();
while (c != EOF) {
if (!quote && c == '\n')
break;
if ((quote && c == '"') || (!quote && c == '\t')) {
quote = 0;
if (ncols + 1 >= sz)
break;
cols[ncols++] = sbuf_done(sb);
sb = sbuf_make();
if (c == '"')
c = fgetc(fp);
if (c == '\n')
ungetc(c, fp);
} else {
if (c == '\n')
sbuf_chr(sb, ',');
else if (c == '\t')
sbuf_chr(sb, ' ');
else if (c == '"')
quote = 1;
else if (c != '\r')
sbuf_chr(sb, c);
}
c = fgetc(fp);
}
cols[ncols++] = sbuf_done(sb);
return ncols;
}
/* convert university data */
static void conv_univs(FILE *fp)
{
char *cols[32];
int n;
while ((n = onemoreline(fp, cols, LEN(cols))) >= 0) {
if (cols[0] && cols[1] && cols[2] && isdigit((unsigned char) cols[0][0])) {
printf("university\t%s\n", cols[1]);
printf("university_grade\t%s\t%s\n", cols[1], cols[2]);
}
}
}
static void minor_reqs(char *min, char *req)
{
struct sbuf *sb = sbuf_make();
while (*req) {
if (*req == ',') {
printf("minor_req\t%s\t\"%s\"\n", min, sbuf_buf(sb));
sbuf_cut(sb, 0);
}
if (*req != ',' && *req != '"')
sbuf_chr(sb, (unsigned char) *req);
req++;
}
printf("minor_req\t%s\t\"%s\"\n", min, sbuf_buf(sb));
sbuf_free(sb);
}
/* convert minor data; requirements are split with a comma sign */
static void conv_minors(FILE *fp)
{
char *cols[32];
int n;
while ((n = onemoreline(fp, cols, LEN(cols))) >= 0) {
if (cols[0] && cols[1] && cols[2] && isdigit((unsigned char) cols[2][0])) {
printf("minor\t%s\n", cols[0]);
printf("minor_msc\t%s\t%s\n", cols[0], cols[2]);
minor_reqs(cols[0], cols[1]);
}
}
}
static int hdrs_find(char **hdrs, int hdrs_n, char *hdr, int def)
{
int i;
for (i = 0; i < hdrs_n; i++)
if (!strcmp(hdr, hdrs[i]))
return i;
fprintf(stderr, "bad header <%s>!\n", hdr);
exit(1);
}
/* convert student data */
static void conv_studs(FILE *fp, int group)
{
char *cols[128];
char stud[128] = "";
char *hdrs[128];
int n;
int hdrs_n = onemoreline(fp, hdrs, LEN(hdrs)); /* the headers */
int h_id = hdrs_find(hdrs, hdrs_n, "شماره پرونده داوطلب", 1);
int h_name1 = hdrs_find(hdrs, hdrs_n, "نام", 7);
int h_name2 = hdrs_find(hdrs, hdrs_n, "نام خانوادگی", 6);
int h_bsc = hdrs_find(hdrs, hdrs_n, "رشته تحصیلی کارشناسی", 21);
int h_uni = hdrs_find(hdrs, hdrs_n, "دانشگاه محل اخذ مدرک کارشناسی", 20);
int h_gpa = hdrs_find(hdrs, hdrs_n, "معدل تا پایان نیمسال ششم", 27);
int h_pref = hdrs_find(hdrs, hdrs_n, "گرایش (های) انتخابی", 5);
while ((n = onemoreline(fp, cols, LEN(cols))) >= 0) {
if (cols[0] && isdigit((unsigned char) cols[0][0])) {
snprintf(stud, sizeof(stud), "%s", cols[h_id]);
printf("student\t%s\n", stud);
printf("student_name\t%s\t%s\t%s\n", stud, cols[h_name1], cols[h_name2]);
printf("student_bsc\t%s\t%s\n", stud, cols[h_bsc]);
printf("student_bscuni\t%s\t%s\n", stud, cols[h_uni]);
printf("student_bscgpa\t%s\t%s\n", stud, cols[h_gpa]);
if (cols[h_pref][0])
printf("student_pref\t%s\t%s\n", stud, cols[h_pref]);
printf("student_grp\t%s\t%d\n", stud, group);
}
if (stud[0] && cols[0] && cols[h_pref] && !cols[0][0] && cols[h_pref][0])
printf("student_pref\t%s\t%s\n", stud, cols[h_pref]);
}
}
int main(int argc, char *argv[])
{
FILE *fp;
int i;
if (argc < 3) {
fprintf(stderr, "Usage: sconv universities minors students [students2]\n");
return 1;
}
fp = fopen(argv[1], "r");
if (!fp) {
fprintf(stderr, "sconv: failed to open <%s>\n", argv[1]);
return 1;
}
conv_univs(fp);
fclose(fp);
fp = fopen(argv[2], "r");
if (!fp) {
fprintf(stderr, "sconv: failed to open <%s>\n", argv[2]);
return 1;
}
conv_minors(fp);
fclose(fp);
for (i = 0; 3 + i < argc; i++) {
fp = fopen(argv[3 + i], "r");
if (!fp) {
fprintf(stderr, "sconv: failed to open <%s>\n", argv[3]);
return 1;
}
conv_studs(fp, i);
fclose(fp);
}
return 0;
}