summaryrefslogtreecommitdiff
path: root/testing/toxic/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'testing/toxic/main.c')
-rw-r--r--testing/toxic/main.c114
1 files changed, 70 insertions, 44 deletions
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index aa025a1a..4d52469a 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -25,6 +25,8 @@
25#include "prompt.h" 25#include "prompt.h"
26#include "friendlist.h" 26#include "friendlist.h"
27 27
28/* Export for use in Callbacks */
29char *DATA_FILE = NULL;
28 30
29void on_window_resize(int sig) 31void on_window_resize(int sig)
30{ 32{
@@ -145,67 +147,90 @@ static void do_tox(Messenger *m, ToxWindow * prompt)
145 doMessenger(m); 147 doMessenger(m);
146} 148}
147 149
148static void load_data(Messenger *m, char *path) 150/*
151 * Store Messenger to given location
152 * Return 0 stored successfully
153 * Return 1 malloc failed
154 * Return 2 opening path failed
155 * Return 3 fwrite failed
156 */
157int store_data(Messenger *m, char *path)
149{ 158{
150 FILE *fd; 159 FILE *fd;
151 size_t len; 160 size_t len;
152 uint8_t *buf; 161 uint8_t *buf;
153
154 if ((fd = fopen(path, "r")) != NULL) {
155 fseek(fd, 0, SEEK_END);
156 len = ftell(fd);
157 fseek(fd, 0, SEEK_SET);
158 162
159 buf = malloc(len);
160 if (buf == NULL) {
161 fprintf(stderr, "malloc() failed.\n");
162 fclose(fd);
163 endwin();
164 exit(1);
165 }
166 if (fread(buf, len, 1, fd) != 1){
167 fprintf(stderr, "fread() failed.\n");
168 free(buf);
169 fclose(fd);
170 endwin();
171 exit(1);
172 }
173 Messenger_load(m, buf, len);
174 }
175 else {
176 len = Messenger_size(m); 163 len = Messenger_size(m);
177 buf = malloc(len); 164 buf = malloc(len);
178 if (buf == NULL) { 165 if (buf == NULL) {
179 fprintf(stderr, "malloc() failed.\n"); 166 return 1;
180 endwin();
181 exit(1);
182 } 167 }
183 Messenger_save(m, buf); 168 Messenger_save(m, buf);
184 169
185 fd = fopen(path, "w"); 170 fd = fopen(path, "w");
186 if (fd == NULL) { 171 if (fd == NULL) {
187 fprintf(stderr, "fopen() failed.\n"); 172 free(buf);
188 free(buf); 173 return 2;
189 endwin();
190 exit(1);
191 } 174 }
192 175
193 if (fwrite(buf, len, 1, fd) != 1){ 176 if (fwrite(buf, len, 1, fd) != 1) {
194 fprintf(stderr, "fwrite() failed.\n"); 177 free(buf);
195 free(buf); 178 fclose(fd);
196 fclose(fd); 179 return 3;
197 endwin(); 180 }
198 exit(1); 181
182 free(buf);
183 fclose(fd);
184 return 0;
185}
186
187static void load_data(Messenger *m, char *path)
188{
189 FILE *fd;
190 size_t len;
191 uint8_t *buf;
192
193 if ((fd = fopen(path, "r")) != NULL) {
194 fseek(fd, 0, SEEK_END);
195 len = ftell(fd);
196 fseek(fd, 0, SEEK_SET);
197
198 buf = malloc(len);
199 if (buf == NULL) {
200 fprintf(stderr, "malloc() failed.\n");
201 fclose(fd);
202 endwin();
203 exit(1);
204 }
205 if (fread(buf, len, 1, fd) != 1) {
206 fprintf(stderr, "fread() failed.\n");
207 free(buf);
208 fclose(fd);
209 endwin();
210 exit(1);
211 }
212 Messenger_load(m, buf, len);
213
214 uint32_t i;
215 for (i = 0; i < m->numfriends; i++) {
216 on_friendadded(m, i);
217 }
218
219 free(buf);
220 fclose(fd);
221 } else {
222 int st;
223 if ((st = store_data(m, path)) != 0) {
224 fprintf(stderr, "Store messenger failed with return code: %d\n", st);
225 endwin();
226 exit(1);
227 }
199 } 228 }
200 }
201 free(buf);
202 fclose(fd);
203} 229}
204 230
205int main(int argc, char *argv[]) 231int main(int argc, char *argv[])
206{ 232{
207 char *user_config_dir = get_user_config_dir(); 233 char *user_config_dir = get_user_config_dir();
208 char *DATA_FILE = NULL;
209 int config_err = 0; 234 int config_err = 0;
210 235
211 int f_loadfromfile = 1; 236 int f_loadfromfile = 1;
@@ -246,7 +271,6 @@ int main(int argc, char *argv[])
246 271
247 if(f_loadfromfile) 272 if(f_loadfromfile)
248 load_data(m, DATA_FILE); 273 load_data(m, DATA_FILE);
249 free(DATA_FILE);
250 274
251 if (f_flag == -1) { 275 if (f_flag == -1) {
252 attron(COLOR_PAIR(3) | A_BOLD); 276 attron(COLOR_PAIR(3) | A_BOLD);
@@ -268,6 +292,8 @@ int main(int argc, char *argv[])
268 /* Draw */ 292 /* Draw */
269 draw_active_window(m); 293 draw_active_window(m);
270 } 294 }
295
271 cleanupMessenger(m); 296 cleanupMessenger(m);
297 free(DATA_FILE);
272 return 0; 298 return 0;
273} 299}