-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitializer.c
148 lines (132 loc) · 3.84 KB
/
initializer.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/types.h>
#include "loader.h"
#include "initializer.h"
#include "IO.h"
struct MensaList {
struct MensaList * nextItem;
char * mensaName;
char * mensaId;
};
enum parseState { IDLE, HASID };
struct MensaList * collectMensen(FILE * htmlFile){
struct MensaList * head = 0, * next = 0;
positionStream(htmlFile, MENSA_LIST_START, 1);
free(readLine(htmlFile));
int divcount = 1;
enum parseState _ps = IDLE;
char * _tempId = NULL;
while(divcount){
char * cLine = readLine(htmlFile);
char * cTemp = cLine;
/* printf("divcount = %d - %s\n",divcount, cLine); */
while((cTemp = strstr(cTemp,"<div"))){
divcount++;
cTemp++;
}
cTemp = cLine;
while((cTemp = strstr(cTemp,"</div"))){
divcount--;
cTemp++;
}
if (_ps == IDLE && (cTemp = strstr(cLine, "xhrLoad('"))) {
cTemp += 9;
(strchr(cTemp, '\''))[0] = '\0';
_tempId = strdup(cTemp);
_ps = HASID;
continue;
}
if (_ps == HASID && (cTemp = strstr(cLine, "class=\"dummy\"><div>"))) {
cTemp += 19;
(strchr(cTemp, '<'))[0] = '\0';
if(!head){
head = malloc(sizeof(struct MensaList));
next = head;
} else {
next->nextItem = malloc(sizeof(struct MensaList));
next = next->nextItem;
}
next->mensaId = _tempId;
next->mensaName = strdup(cTemp);
next->nextItem = 0;
_ps = IDLE;
continue;
}
free(cLine);
}
return head;
}
int readUserNumber(int min, int max){
char buf[4], *p;
int i = 0;
while(!i){
if (fgets(buf, sizeof(buf), stdin) != NULL) {
i = strtol(buf, &p, 10);
if (buf[0] != '\n' && (*p == '\n' || *p == '\0') && i >= min && i <= max){
return i;
} else {
printf("Try again: ");
/* flush input buffer... */
if(strchr(buf, '\n') == NULL){
while ((i = getchar()) != '\n' && i != EOF);
}
i = 0;
}
}
}
return 1;
}
struct MensaList * getMensaListElement(struct MensaList * head, int index){
while(--index){
head = head->nextItem;
}
return head;
}
char * getConfigPath(){
struct passwd *pw = getpwuid(getuid());
char * path = malloc(strlen(pw->pw_dir) + strlen(CONFIG_FILE) + 1);
strcpy(path, pw->pw_dir);
strcat(path, CONFIG_FILE);
return path;
}
int initializer(){
puts("\n### Welcome to the Studentenwerk Mensa application! ###\n");
FILE * pListPage = tmpfile();
if(!downloadPage(LIST_URL, NULL, pListPage)){
struct MensaList * alleMensen = collectMensen(pListPage);
if (!alleMensen) {
return 1;
}
puts("Select your Mensa:\n");
struct MensaList * mTemp = alleMensen;
int i = 1;
while(mTemp){
printf(" %2d - %s\n",i,mTemp->mensaName);
i++;
mTemp = mTemp->nextItem;
}
printf("\nMensa number: ");
int selection = readUserNumber(1, i - 1);
struct MensaList * myMensa = getMensaListElement(alleMensen, selection);
printf("Selected: \"%s\"\n\n", myMensa->mensaName);
puts("Writing configuration");
char * cPath = getConfigPath();
FILE * confy = fopen(cPath,"w+");
if(confy){
fprintf(confy, "%s", myMensa->mensaId);
fclose(confy);
printf("Ok > %s\n", cPath);
} else {
puts("FAIL!");
}
free(cPath);
} else {
puts("Error downloading information...");
return 1;
}
return 0;
}