Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)

matfile/matfile_concepts/src/Devices/OutputDevice.hh
Go to the documentation of this file.
00001 /*
00002     This file is part of matfile.
00003 
00004     Copyright (C) 2010-2011 Andrea Arteaga <arteagaa@student.ethz.ch>
00005 
00006     matfile is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU Lesser General Public License as published
00008     by the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 
00011     matfile is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU Lesser General Public License for more details.
00015 
00016     You should have received a copy of the GNU Lesser General Public License
00017     along with matfile.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #ifndef OUTPUTDEVICE_HPP_
00021 #define OUTPUTDEVICE_HPP_
00022 
00033 class OutputDevice
00034 {
00035   //TODO: Implement a buffer and the compression of data with zlib.
00036 
00037 public:
00038 
00046   OutputDevice(const std::string& filename)
00047     : fname_(filename), fs_(filename.c_str(), std::ofstream::out | std::ofstream::binary),
00048       fileIsOpen_(true), descriptionIsSet_(false)
00049   { }
00050 
00056   ~OutputDevice() {
00057     if(fileIsOpen_)
00058       fs_.close();
00059   }
00060 
00066   void close() {
00067     fs_.close();
00068     fileIsOpen_ = false;
00069   }
00070 
00083   template<typename charT>
00084   void setDescription(const std::basic_string<charT>& descr);
00085 
00096   template<class WrapperClass>
00097   void writeArray(const std::string& name, const WrapperClass& wrapper) {
00098     setDescription();
00099     detail::Writer<WrapperClass>::write(fs_, name, wrapper);
00100   }
00101 
00102 private:
00103   std::string fname_;
00104   std::ofstream fs_;
00105   bool fileIsOpen_;
00106   bool descriptionIsSet_;
00107 
00108   inline void setDescription();
00109 };
00110 
00111 
00112 void OutputDevice::setDescription()
00113 {
00114   if (descriptionIsSet_) return;
00115 
00116   std::string description = "MATfile++ MAT-file, Created on: ";
00117   
00118   time_t rawtime; 
00119   description.append(ctime(&rawtime));
00120   description.append(116 - description.length(), ' ');
00121   setDescription(description);
00122 }
00123 
00124 template<typename charT>
00125 void OutputDevice::setDescription(const std::basic_string<charT>& descr)
00126 {
00127   if (descriptionIsSet_) return;
00128 
00129   std::string description;
00130 
00131   if (descr.length() > 116)
00132     description = descr.substr(0, 116);
00133   else if (descr.length() < 116)
00134     (description = descr).append(116 - descr.length(), ' ');
00135   else
00136     description = descr;
00137 
00138   fs_.write(description.c_str(), 116);
00139 
00140   const char subsys[8] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
00141   fs_.write(subsys, 8);
00142 
00143   miINT16_t version = 0x0100;
00144   fs_.write(reinterpret_cast<char*>(&version), 2);
00145 
00146   miINT16_t endian = 19785;
00147   fs_.write(reinterpret_cast<char*>(&endian), 2);
00148 
00149   descriptionIsSet_ = true;
00150 }
00151 
00152 #endif /* OUTPUTDEVICE_HPP_ */

Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)