Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef OUTPUTDEVICE_HPP_
00021 #define OUTPUTDEVICE_HPP_
00022
00033 class OutputDevice
00034 {
00035
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