Mercurial > hg > audiodb
comparison insert.cpp @ 596:6ad0a6e67d4c
Take advantage of those new handy _or_goto_error macros
Use them in various places where previously we either elided the error
checking (various lseek() calls) or used a combination of calls
(replaced by malloc_and_fill_or_goto_error()).
In the process, fix what is probably a bug (or else introduce one, but I
don't think so): audiodb_track_id_datum() computed the offset into the
timesTable wrongly, forgetting to multiply by 2. (TODO: this should be
easily testable using the API).
Now all of LIBOBJS can be produced by my (Debian's) mingw32
cross-compiler, except for lshlib.o.
author | mas01cr |
---|---|
date | Tue, 11 Aug 2009 21:42:49 +0000 |
parents | c850f3433454 |
children |
comparison
equal
deleted
inserted
replaced
595:31a1556fc2d6 | 596:6ad0a6e67d4c |
---|---|
132 */ | 132 */ |
133 offset = adb->header->length; | 133 offset = adb->header->length; |
134 nfiles = adb->header->numFiles; | 134 nfiles = adb->header->numFiles; |
135 | 135 |
136 /* FIXME: checking for all these lseek()s */ | 136 /* FIXME: checking for all these lseek()s */ |
137 lseek(adb->fd, adb->header->fileTableOffset + nfiles * ADB_FILETABLE_ENTRY_SIZE, SEEK_SET); | 137 lseek_set_or_goto_error(adb->fd, adb->header->fileTableOffset + nfiles * ADB_FILETABLE_ENTRY_SIZE); |
138 write_or_goto_error(adb->fd, datum->key, strlen(datum->key)+1); | 138 write_or_goto_error(adb->fd, datum->key, strlen(datum->key)+1); |
139 lseek(adb->fd, adb->header->trackTableOffset + nfiles * ADB_TRACKTABLE_ENTRY_SIZE, SEEK_SET); | 139 lseek_set_or_goto_error(adb->fd, adb->header->trackTableOffset + nfiles * ADB_TRACKTABLE_ENTRY_SIZE); |
140 write_or_goto_error(adb->fd, &datum->nvectors, ADB_TRACKTABLE_ENTRY_SIZE); | 140 write_or_goto_error(adb->fd, &datum->nvectors, ADB_TRACKTABLE_ENTRY_SIZE); |
141 if(adb->header->flags & ADB_HEADER_FLAG_REFERENCES) { | 141 if(adb->header->flags & ADB_HEADER_FLAG_REFERENCES) { |
142 char cwd[PATH_MAX]; | 142 char cwd[PATH_MAX]; |
143 char slash = '/'; | 143 char slash = '/'; |
144 | 144 |
145 if(!getcwd(cwd, PATH_MAX)) { | 145 if(!getcwd(cwd, PATH_MAX)) { |
146 goto error; | 146 goto error; |
147 } | 147 } |
148 lseek(adb->fd, adb->header->dataOffset + nfiles * ADB_FILETABLE_ENTRY_SIZE, SEEK_SET); | 148 lseek_set_or_goto_error(adb->fd, adb->header->dataOffset + nfiles * ADB_FILETABLE_ENTRY_SIZE); |
149 if(*((char *) datum->data) != '/') { | 149 if(*((char *) datum->data) != '/') { |
150 write_or_goto_error(adb->fd, cwd, strlen(cwd)); | 150 write_or_goto_error(adb->fd, cwd, strlen(cwd)); |
151 write_or_goto_error(adb->fd, &slash, 1); | 151 write_or_goto_error(adb->fd, &slash, 1); |
152 } | 152 } |
153 write_or_goto_error(adb->fd, datum->data, strlen((const char *) datum->data)+1); | 153 write_or_goto_error(adb->fd, datum->data, strlen((const char *) datum->data)+1); |
154 if(datum->power) { | 154 if(datum->power) { |
155 lseek(adb->fd, adb->header->powerTableOffset + nfiles * ADB_FILETABLE_ENTRY_SIZE, SEEK_SET); | 155 lseek_set_or_goto_error(adb->fd, adb->header->powerTableOffset + nfiles * ADB_FILETABLE_ENTRY_SIZE); |
156 if(*((char *) datum->power) != '/') { | 156 if(*((char *) datum->power) != '/') { |
157 write_or_goto_error(adb->fd, cwd, strlen(cwd)); | 157 write_or_goto_error(adb->fd, cwd, strlen(cwd)); |
158 write_or_goto_error(adb->fd, &slash, 1); | 158 write_or_goto_error(adb->fd, &slash, 1); |
159 } | 159 } |
160 write_or_goto_error(adb->fd, datum->power, strlen((const char *) datum->power)+1); | 160 write_or_goto_error(adb->fd, datum->power, strlen((const char *) datum->power)+1); |
161 } | 161 } |
162 if(datum->times) { | 162 if(datum->times) { |
163 lseek(adb->fd, adb->header->timesTableOffset + nfiles * ADB_FILETABLE_ENTRY_SIZE, SEEK_SET); | 163 lseek_set_or_goto_error(adb->fd, adb->header->timesTableOffset + nfiles * ADB_FILETABLE_ENTRY_SIZE); |
164 if(*((char *) datum->times) != '/') { | 164 if(*((char *) datum->times) != '/') { |
165 write_or_goto_error(adb->fd, cwd, strlen(cwd)); | 165 write_or_goto_error(adb->fd, cwd, strlen(cwd)); |
166 write_or_goto_error(adb->fd, &slash, 1); | 166 write_or_goto_error(adb->fd, &slash, 1); |
167 } | 167 } |
168 write_or_goto_error(adb->fd, datum->times, strlen((const char *) datum->times)+1); | 168 write_or_goto_error(adb->fd, datum->times, strlen((const char *) datum->times)+1); |
169 } | 169 } |
170 } else { | 170 } else { |
171 lseek(adb->fd, adb->header->dataOffset + offset, SEEK_SET); | 171 lseek_set_or_goto_error(adb->fd, adb->header->dataOffset + offset); |
172 write_or_goto_error(adb->fd, datum->data, sizeof(double) * datum->nvectors * datum->dim); | 172 write_or_goto_error(adb->fd, datum->data, sizeof(double) * datum->nvectors * datum->dim); |
173 if(datum->power) { | 173 if(datum->power) { |
174 lseek(adb->fd, adb->header->powerTableOffset + offset / datum->dim, SEEK_SET); | 174 lseek_set_or_goto_error(adb->fd, adb->header->powerTableOffset + offset / datum->dim); |
175 write_or_goto_error(adb->fd, datum->power, sizeof(double) * datum->nvectors); | 175 write_or_goto_error(adb->fd, datum->power, sizeof(double) * datum->nvectors); |
176 } | 176 } |
177 if(datum->times) { | 177 if(datum->times) { |
178 lseek(adb->fd, adb->header->timesTableOffset + offset / datum->dim * 2, SEEK_SET); | 178 lseek_set_or_goto_error(adb->fd, adb->header->timesTableOffset + offset / datum->dim * 2); |
179 write_or_goto_error(adb->fd, datum->times, sizeof(double) * datum->nvectors * 2); | 179 write_or_goto_error(adb->fd, datum->times, sizeof(double) * datum->nvectors * 2); |
180 } | 180 } |
181 } | 181 } |
182 | 182 |
183 /* 7. if ADB_HEADER_FLAG_L2NORM and !ADB_HEADER_FLAG_REFERENCES, | 183 /* 7. if ADB_HEADER_FLAG_L2NORM and !ADB_HEADER_FLAG_REFERENCES, |
186 if((adb->header->flags & ADB_HEADER_FLAG_L2NORM) && | 186 if((adb->header->flags & ADB_HEADER_FLAG_L2NORM) && |
187 !(adb->header->flags & ADB_HEADER_FLAG_REFERENCES)) { | 187 !(adb->header->flags & ADB_HEADER_FLAG_REFERENCES)) { |
188 l2norm_buffer = (double *) malloc(datum->nvectors * sizeof(double)); | 188 l2norm_buffer = (double *) malloc(datum->nvectors * sizeof(double)); |
189 | 189 |
190 audiodb_l2norm_buffer((double *) datum->data, datum->dim, datum->nvectors, l2norm_buffer); | 190 audiodb_l2norm_buffer((double *) datum->data, datum->dim, datum->nvectors, l2norm_buffer); |
191 lseek(adb->fd, adb->header->l2normTableOffset + offset / datum->dim, SEEK_SET); | 191 lseek_set_or_goto_error(adb->fd, adb->header->l2normTableOffset + offset / datum->dim); |
192 write_or_goto_error(adb->fd, l2norm_buffer, sizeof(double) * datum->nvectors); | 192 write_or_goto_error(adb->fd, l2norm_buffer, sizeof(double) * datum->nvectors); |
193 free(l2norm_buffer); | 193 free(l2norm_buffer); |
194 l2norm_buffer = NULL; | 194 l2norm_buffer = NULL; |
195 } | 195 } |
196 | 196 |
206 | 206 |
207 /* 9. sync adb->header with disk. */ | 207 /* 9. sync adb->header with disk. */ |
208 return audiodb_sync_header(adb); | 208 return audiodb_sync_header(adb); |
209 | 209 |
210 error: | 210 error: |
211 if(l2norm_buffer) { | 211 maybe_free(l2norm_buffer); |
212 free(l2norm_buffer); | |
213 } | |
214 return 1; | 212 return 1; |
215 } | 213 } |
216 | 214 |
217 int audiodb_insert_datum(adb_t *adb, const adb_datum_t *datum) { | 215 int audiodb_insert_datum(adb_t *adb, const adb_datum_t *datum) { |
218 if(adb->header->flags & ADB_HEADER_FLAG_REFERENCES) { | 216 if(adb->header->flags & ADB_HEADER_FLAG_REFERENCES) { |