Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/debug/file_io.h
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2013 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #ifndef MARSYAS_DEBUG_FILE_IO_INCLUDED
00020 #define MARSYAS_DEBUG_FILE_IO_INCLUDED
00021 
00022 #include <marsyas/debug/record.h>
00023 #include <marsyas/system/MarSystem.h>
00024 #include <marsyas/export.h>
00025 
00026 #include <fstream>
00027 
00028 namespace Marsyas { namespace Debug {
00029 
00030 void copy_swap_bytes(char *destination, const char * source, int size );
00031 
00032 template <typename T>
00033 void to_bytes( const T & src, char dst_bytes[sizeof(T)] )
00034 {
00035   const char *src_bytes = reinterpret_cast<const char*>(&src);
00036   copy_swap_bytes(dst_bytes, src_bytes, sizeof(T));
00037 }
00038 
00039 template <typename T>
00040 void from_bytes( T & dst, const char src_bytes[sizeof(T)] )
00041 {
00042   char *dst_bytes = reinterpret_cast<char*>(&dst);
00043   copy_swap_bytes(dst_bytes, src_bytes, sizeof(T));
00044 }
00045 
00046 
00047 class marsyas_EXPORT FileWriter
00048 {
00049   struct SystemDescriptor
00050   {
00051     std::string path;
00052     int out_columns;
00053     int out_rows;
00054   };
00055 
00056   std::ofstream m_file;
00057   std::vector<SystemDescriptor> m_descriptors;
00058 
00059 public:
00060   FileWriter( const std::string & filename, MarSystem * system );
00061   ~FileWriter();
00062   bool isOpen() { return m_file.is_open(); }
00063   bool write( const Record & record )
00064   {
00065     return write_record(record);
00066   }
00067 
00068 private:
00069   void recursive_store_descriptor( MarSystem * system );
00070   void write_magic();
00071   void write_header();
00072   bool write_record( const Record & record );
00073 };
00074 
00075 
00076 class marsyas_EXPORT FileReader
00077 {
00078   struct SystemDescriptor
00079   {
00080     std::string path;
00081     int out_columns;
00082     int out_rows;
00083   };
00084 
00085   typedef std::ifstream::pos_type pos_t;
00086 
00087   std::ifstream m_file;
00088   std::vector<SystemDescriptor> m_descriptors;
00089   size_t m_record_size;
00090   pos_t m_start_pos;
00091 
00092 public:
00093   FileReader( const std::string & filename );
00094   ~FileReader();
00095   bool isOpen() { return m_file.is_open(); }
00096   bool eof() { return m_file.eof(); }
00097   bool read( Record & record )
00098   {
00099     return read_record(record);
00100   }
00101   void rewind();
00102 
00103 private:
00104   bool read_magic();
00105   bool read_header();
00106   bool read_record( Record & record );
00107 };
00108 
00109 }} // namespace Marsyas::Debug
00110 
00111 #endif // MARSYAS_DEBUG_FILE_IO_INCLUDED