deepstone
annotate src/record.c @ 39:f9b1ff21fd62
merged
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 10 Mar 2014 17:28:28 +0200 |
parents | |
children |
rev | line source |
---|---|
nuclear@38 | 1 #include <stdio.h> |
nuclear@38 | 2 #include <stdlib.h> |
nuclear@38 | 3 #include <string.h> |
nuclear@38 | 4 #include <errno.h> |
nuclear@38 | 5 #include "record.h" |
nuclear@38 | 6 |
nuclear@38 | 7 struct event { |
nuclear@38 | 8 unsigned long tm; |
nuclear@38 | 9 float x, y, z; |
nuclear@38 | 10 float theta, phi; |
nuclear@38 | 11 |
nuclear@38 | 12 struct event *next; |
nuclear@38 | 13 }; |
nuclear@38 | 14 |
nuclear@38 | 15 static struct event *head, *tail; |
nuclear@38 | 16 static struct event *iter; |
nuclear@38 | 17 static int num_events; |
nuclear@38 | 18 |
nuclear@38 | 19 void rec_reset(void) |
nuclear@38 | 20 { |
nuclear@38 | 21 while(head) { |
nuclear@38 | 22 iter = head; |
nuclear@38 | 23 head = head->next; |
nuclear@38 | 24 free(iter); |
nuclear@38 | 25 } |
nuclear@38 | 26 num_events = 0; |
nuclear@38 | 27 } |
nuclear@38 | 28 |
nuclear@38 | 29 int rec_load(const char *fname) |
nuclear@38 | 30 { |
nuclear@38 | 31 FILE *fp; |
nuclear@38 | 32 int i; |
nuclear@38 | 33 |
nuclear@38 | 34 if(!(fp = fopen(fname, "rb"))) { |
nuclear@38 | 35 fprintf(stderr, "failed to open recording: %s: %s\n", fname, strerror(errno)); |
nuclear@38 | 36 return -1; |
nuclear@38 | 37 } |
nuclear@38 | 38 fread(&num_events, sizeof num_events, 1, fp); |
nuclear@38 | 39 |
nuclear@38 | 40 head = tail = 0; |
nuclear@38 | 41 |
nuclear@38 | 42 for(i=0; i<num_events; i++) { |
nuclear@38 | 43 struct event *ev = malloc(sizeof *ev); |
nuclear@38 | 44 if(!ev) { |
nuclear@38 | 45 perror("failed to allocate event structure"); |
nuclear@38 | 46 fclose(fp); |
nuclear@38 | 47 return -1; |
nuclear@38 | 48 } |
nuclear@38 | 49 if(fread(ev, sizeof *ev, 1, fp) < 1) { |
nuclear@38 | 50 perror("unexpected end of file"); |
nuclear@38 | 51 fclose(fp); |
nuclear@38 | 52 return -1; |
nuclear@38 | 53 } |
nuclear@38 | 54 |
nuclear@38 | 55 if(!head) { |
nuclear@38 | 56 head = tail = ev; |
nuclear@38 | 57 } else { |
nuclear@38 | 58 tail->next = ev; |
nuclear@38 | 59 tail = ev; |
nuclear@38 | 60 } |
nuclear@38 | 61 ev->next = 0; |
nuclear@38 | 62 } |
nuclear@38 | 63 iter = 0; |
nuclear@38 | 64 |
nuclear@38 | 65 fclose(fp); |
nuclear@38 | 66 return 0; |
nuclear@38 | 67 } |
nuclear@38 | 68 |
nuclear@38 | 69 int rec_save(const char *fname) |
nuclear@38 | 70 { |
nuclear@38 | 71 FILE *fp; |
nuclear@38 | 72 struct event *ev; |
nuclear@38 | 73 |
nuclear@38 | 74 if(!(fp = fopen(fname, "wb"))) { |
nuclear@38 | 75 fprintf(stderr, "failed to open recording: %s: %s\n", fname, strerror(errno)); |
nuclear@38 | 76 return -1; |
nuclear@38 | 77 } |
nuclear@38 | 78 fwrite(&num_events, sizeof num_events, 1, fp); |
nuclear@38 | 79 |
nuclear@38 | 80 ev = head; |
nuclear@38 | 81 while(ev) { |
nuclear@38 | 82 fwrite(ev, sizeof *ev, 1, fp); |
nuclear@38 | 83 ev = ev->next; |
nuclear@38 | 84 } |
nuclear@38 | 85 |
nuclear@38 | 86 fclose(fp); |
nuclear@38 | 87 return 0; |
nuclear@38 | 88 } |
nuclear@38 | 89 |
nuclear@38 | 90 void rec_add(unsigned long tm, float x, float y, float z, float theta, float phi) |
nuclear@38 | 91 { |
nuclear@38 | 92 struct event *ev = malloc(sizeof *ev); |
nuclear@38 | 93 if(!ev) { |
nuclear@38 | 94 fprintf(stderr, "failed to append rec-event: %s\n", strerror(errno)); |
nuclear@38 | 95 return; |
nuclear@38 | 96 } |
nuclear@38 | 97 ev->tm = tm; |
nuclear@38 | 98 ev->x = x; |
nuclear@38 | 99 ev->y = y; |
nuclear@38 | 100 ev->z = z; |
nuclear@38 | 101 ev->theta = theta; |
nuclear@38 | 102 ev->phi = phi; |
nuclear@38 | 103 |
nuclear@38 | 104 ev->next = 0; |
nuclear@38 | 105 if(head) { |
nuclear@38 | 106 tail->next = ev; |
nuclear@38 | 107 tail = ev; |
nuclear@38 | 108 } else { |
nuclear@38 | 109 head = tail = ev; |
nuclear@38 | 110 } |
nuclear@38 | 111 |
nuclear@38 | 112 num_events++; |
nuclear@38 | 113 } |
nuclear@38 | 114 |
nuclear@38 | 115 void rec_get(unsigned long tm, float *x, float *y, float *z, float *theta, float *phi) |
nuclear@38 | 116 { |
nuclear@38 | 117 if(!iter || iter->tm > tm) { |
nuclear@38 | 118 iter = head; |
nuclear@38 | 119 } |
nuclear@38 | 120 |
nuclear@38 | 121 while(iter && iter->next) { |
nuclear@38 | 122 if(iter->next->tm > tm) { |
nuclear@38 | 123 break; |
nuclear@38 | 124 } |
nuclear@38 | 125 iter = iter->next; |
nuclear@38 | 126 } |
nuclear@38 | 127 |
nuclear@38 | 128 if(iter) { |
nuclear@38 | 129 *x = iter->x; |
nuclear@38 | 130 *y = iter->y; |
nuclear@38 | 131 *z = iter->z; |
nuclear@38 | 132 *theta = iter->theta; |
nuclear@38 | 133 *phi = iter->phi; |
nuclear@38 | 134 } |
nuclear@38 | 135 } |