24 #include <fvutils/fileformat/fvfile.h> 26 #include <core/exceptions/system.h> 27 #include <utils/misc/strndup.h> 32 #include <netinet/in.h> 95 FireVisionDataFile::FireVisionDataFile(
unsigned short int magic_token,
96 unsigned short int version)
100 __magic_token = magic_token;
102 __comment = strdup(
"");
105 _spec_header_size = 0;
107 __owns_blocks =
true;
114 FireVisionDataFile::~FireVisionDataFile()
120 if ( _spec_header ) {
130 FireVisionDataFile::clear()
133 for (__bi = __blocks.begin(); __bi != __blocks.end(); ++__bi) {
141 __header->magic_token = htons(__magic_token);
142 __header->version = __version;
143 __header->num_blocks = 0;
144 #if __BYTE_ORDER == __BIG_ENDIAN 145 __header->endianess = 1;
147 __header->endianess = 0;
150 __comment = strdup(
"");
158 FireVisionDataFile::magic_token()
160 return __header->magic_token;
168 FireVisionDataFile::version()
170 return __header->version;
178 FireVisionDataFile::is_big_endian()
180 return (__header->endianess == 1);
188 FireVisionDataFile::is_little_endian()
190 return (__header->endianess == 0);
198 FireVisionDataFile::get_comment()
const 208 FireVisionDataFile::set_comment(
const char *comment)
211 __comment = strndup(comment, FVFF_COMMENT_SIZE);
212 strncpy(__header->comment, comment, FVFF_COMMENT_SIZE);
222 FireVisionDataFile::set_owns_blocks(
bool owns_blocks)
224 __owns_blocks = owns_blocks;
232 FireVisionDataFile::num_blocks()
234 return __blocks.size();
244 __blocks.push_back(block);
252 FireVisionDataFile::blocks()
262 FireVisionDataFile::write(
const char *file_name)
264 FILE *f = fopen(file_name,
"w");
270 __header->num_blocks = (
unsigned int)__blocks.size();
272 gettimeofday(&t, NULL);
273 __header->created_sec = t.tv_sec;
274 __header->created_usec = t.tv_usec;
275 __header->spec_head_size = _spec_header_size;
283 if ( _spec_header_size > 0 ) {
285 if ( fwrite(_spec_header, _spec_header_size, 1, f) != 1 ) {
291 for (__bi = __blocks.begin(); __bi != __blocks.end(); ++__bi) {
294 if ( fwrite((*__bi)->block_memptr(), (*__bi)->block_size(), 1, f) != 1 ) {
308 FireVisionDataFile::read(
const char *file_name)
310 FILE *f = fopen(file_name,
"r");
324 if ( __header->magic_token != htons(__magic_token) ) {
326 throw Exception(
"Unknown magic in fvff file (read: 0x%04x req: 0x%04x)",
327 __header->magic_token, __magic_token);
330 if ( __header->version != __version ) {
332 throw Exception(
"Unsupported version of fvff file (read: %u req: %u)",
333 __header->version, __version);
336 if ( __header->endianess ==
337 #
if __BYTE_ORDER == __BIG_ENDIAN
344 throw Exception(
"FVFile header cannot be translated for endianess by now");
348 __comment = strndup(__header->comment, FVFF_COMMENT_SIZE);
350 if ( _spec_header ) {
353 _spec_header = calloc(1, __header->spec_head_size);
354 if ( ! _spec_header ) {
358 if ( __header->spec_head_size > 0 ) {
360 if ( fread(_spec_header, __header->spec_head_size, 1, f) != 1) {
362 throw FileReadException(file_name, errno,
"Reading content specific header failed");
367 for (
unsigned int b = 0; b < __header->num_blocks && !feof(f); ++b) {
370 if ( fread(&bh,
sizeof(bh), 1, f) != 1 ) {
373 "Could not read block info header while there should be one");
375 void *spec_header = NULL;
380 if ( ! spec_header ) {
390 "Could not read content specific block header");
404 "Could not read block data");
407 __blocks.push_back(block);
419 FireVisionDataFile::read_magic_token(
const char *filename)
421 uint16_t magic_token = 0;
424 f = fopen(filename,
"r");
426 if ( fread((
char *)&magic_token,
sizeof(magic_token), 1, f) != 1 ) {
428 throw FileReadException(filename, errno,
"Could not read magic token from file");
433 "Could not read magic token from file");
446 FireVisionDataFile::has_magic_token(
const char *filename,
unsigned short int magic_token)
448 uint16_t file_magic_token = read_magic_token(filename);
449 return (htons(magic_token) == file_magic_token);
File could not be opened.
Fawkes library namespace.
FireVision File Format data block.
std::list< FireVisionDataFileBlock * > BlockList
List of FireVision data file blocks.
void * data_ptr() const
Get data pointer.
Base class for exceptions in Fawkes.
System ran out of memory and desired operation could not be fulfilled.