dsys2
diff src/dsys.c @ 3:eec499998960
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 31 Aug 2011 23:23:38 +0300 |
parents | 1705e550bd91 |
children | 95f010f7eadc |
line diff
1.1 --- a/src/dsys.c Wed Aug 31 05:08:54 2011 +0300 1.2 +++ b/src/dsys.c Wed Aug 31 23:23:38 2011 +0300 1.3 @@ -41,7 +41,11 @@ 1.4 1.5 demo->src_tm = demo->start_tm = -1; 1.6 1.7 - /* TODO */ 1.8 + if(read_script(demo, fp) == -1) { 1.9 + free(demo); 1.10 + return 0; 1.11 + } 1.12 + 1.13 return demo; 1.14 } 1.15 1.16 @@ -57,6 +61,52 @@ 1.17 } 1.18 1.19 1.20 + 1.21 +static int read_script(struct dsys_demo *demo, FILE *fp) 1.22 +{ 1.23 + int nline = 0; 1.24 + char buf[512], *line, tok; 1.25 + int timestamp; 1.26 + struct dsys_event *ev; 1.27 + 1.28 + while(fgets(buf, sizeof buf, fp)) { 1.29 + nline++; 1.30 + 1.31 + line = buf;/*strip_ws(buf);*/ 1.32 + 1.33 + if(!line || !*line) { 1.34 + continue; 1.35 + } 1.36 + 1.37 + if(!(tok = strtok(line, SEP)) || !isdigit(*tok)) { 1.38 + fprintf(stderr, "error reading script at line: %d: expected timestamp\n", nline); 1.39 + return -1; 1.40 + } 1.41 + timestamp = atoi(tok); 1.42 + 1.43 + if(!(tok = strtok(0, SEP))) { 1.44 + fprintf(stderr, "error reading script at line: %d: expected event name\n", nline); 1.45 + return -1; 1.46 + } 1.47 + 1.48 + if(!(ev = malloc(sizeof *ev))) { 1.49 + perror("read_script: failed to allocate memory for an event\n"); 1.50 + return -1; 1.51 + } 1.52 + ev->t0 = t0; 1.53 + ev->t1 = t1; 1.54 + 1.55 + if(!(ev->name = malloc(strlen(tok) + 1))) { 1.56 + free(ev); 1.57 + sprintf("read_script: failed to allocate memory for the event name: %s\n", tok); 1.58 + return -1; 1.59 + } 1.60 + } 1.61 + 1.62 + return 0; 1.63 +} 1.64 + 1.65 + 1.66 void dsys_update(struct dsys_demo *demo, demotime_t tm) 1.67 { 1.68 demo->src_tm = tm;