binaryfilereader.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // This file is part of libgvectors.
00004 // libgvectors is a C++ library intended for visualizing mathematical vectors.
00005 // Copyright (C) 2007  Rafael Ostertag
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
00020 // USA.
00021 //
00022 // $Id: binaryfilereader_8h-source.html,v 1.1 2007-09-08 19:02:23 rafi Exp $
00023 //
00024 // File:   binaryfilereader.h
00025 // Author: Rafael Ostertag
00026 //
00027 // Created on August 18, 2007, 12:04 AM
00028 //
00029 
00143 #ifndef _BINARYFILEREADER_H
00144 #define _BINARYFILEREADER_H
00145 
00146 #include "gvectors-config.h"
00147 
00148 #ifdef HAVE_GFILEIO
00149 
00150 #include <stdlib.h>
00151 #include <string.h>
00152 #include <string>
00153 #include <gfileio.h>
00154 
00155 #include <gvectortypes.h>
00156 #include <gvexceptions.h>
00157 #include <vectorstore.h>
00158 #include <filereader.h>
00159 #include <visualvector.h>
00160 #include <color.h>
00161 
00162 namespace GVectors {
00180     template<class Fct> class BinaryReader : public FileReader<Fct> {
00181     private:
00193         GFileIO::GFile file;
00194         
00195     public:
00208         BinaryReader(std::string filename, VectorStore& vs, Fct callback) : FileReader<Fct>(vs, callback), file(filename) {
00209             
00210         }
00218         BinaryReader(const BinaryReader& br) : FileReader<Fct>(br), file(br.file) {
00219             
00220         }
00221         
00227         virtual ~BinaryReader() {
00228             file.close();
00229         }
00230         
00237         void read(){
00238             file.open();
00239             
00240             // Read header
00241             BFileHeaderV_0_1 header;
00242             bool ret_val = file.read<BFileHeaderV_0_1>(header);
00243             
00244             if (header.version != BWVER_0_1)
00245                 throw GVEWrongFileVersion();
00246             
00247             while (ret_val) {
00248                 // Read the length of the tag
00249                 size_t taglen;
00250                 ret_val = file.read<size_t>(taglen);
00251                 if (!ret_val)
00252                     break;
00253                 
00254                 // Read the tag
00255                 char* tag = (char*)malloc(taglen + 1);
00256                 ret_val = file.read<char>(*tag, taglen);
00257                 if (!ret_val) {
00258                     free(tag);
00259                 }
00260                 tag[taglen] = '\0';
00261 
00262                 // Read the vector
00263                 BFileVectorV_0_1 vector;
00264                 ret_val = file.read<BFileVectorV_0_1>(vector);
00265                 
00266                 // Create the visual vector
00267                 VisualVector vvector(tag, vector.x, vector.y, vector.z);
00268                 Color color(vector.r, vector.g, vector.b);
00269                 vvector.set_color(color);
00270                 // Put the visual vector into the vector store
00271                 FileReader<Fct>::put(vector.level, vvector);
00272                 
00273                 free(tag);
00274             }
00275         }
00276     };
00277 }
00278 
00279 #endif /* HAVE_GFILEIO */
00280 
00281 #endif  /* _BINARYFILEREADER_H */

Generated on Sat Sep 8 19:37:29 2007 for libgvectors by  doxygen 1.4.7