tokenwriter.cpp

Go to the documentation of this file.
00001 
00003 /* FAU Discrete Event Systems Library (libfaudes)
00004 
00005    Copyright (C) 2006  Bernd Opitz
00006    Copyright (C) 2006  Thomas Moor
00007    Exclusive copyright is granted to Klaus Schmidt
00008 
00009    This library is free software; you can redistribute it and/or
00010    modify it under the terms of the GNU Lesser General Public
00011    License as published by the Free Software Foundation; either
00012    version 2.1 of the License, or (at your option) any later version.
00013 
00014    This library is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017    Lesser General Public License for more details.
00018 
00019    You should have received a copy of the GNU Lesser General Public
00020    License along with this library; if not, write to the Free Software
00021    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00022 
00023 
00024 #include "tokenwriter.h"
00025 
00026 namespace faudes {
00027 
00028 // TokenWriter(rFilename, openmode)
00029 TokenWriter::TokenWriter(const std::string& rFilename, 
00030     std::ios::openmode openmode) 
00031   : mMode(File), mFileName(rFilename), mColumns(80/FD_NAMELEN), mColCount(0) {
00032   // set up mFStream
00033   mFStream.exceptions(std::ios::badbit|std::ios::failbit);
00034   try{
00035     mFStream.open(rFilename.c_str(), openmode); 
00036   }
00037   catch (std::ios::failure&) {
00038     std::stringstream errstr;
00039     errstr << "Exception opening/writing file \""<< rFilename << "\"";
00040     throw Exception("TokenWriter::TokenWriter", errstr.str(), 2);
00041   }
00042   // use mFStream
00043   mpStream=&mFStream;      
00044 }
00045 
00046 // TokenWriter(mode)
00047 TokenWriter::TokenWriter(Mode mode)
00048   : mMode(mode), mFileName(""), mColumns(80/FD_NAMELEN), mColCount(0) {
00049   switch(mode) {
00050   case Stdout: 
00051     // set up mFStream
00052     mFileName="stdout";
00053     try {
00054       mFStream.copyfmt(std::cout);
00055       mFStream.clear(std::cout.rdstate());
00056       typedef std::basic_ios<char> ___basic_ios_char_; // trick for vc++
00057       mFStream.___basic_ios_char_::rdbuf(std::cout.rdbuf());
00058     }
00059     catch (std::ios::failure&) {
00060       std::stringstream errstr;
00061       errstr << "Exception opening/writing file \""<< mFileName << "\"";
00062       throw Exception("TokenWriter::TokenWriter", errstr.str(), 2);
00063     }
00064     // use mFStream
00065     mFileName="string";
00066     mpStream=&mFStream;
00067     break;
00068   case String:
00069     // use mSStream
00070     mpStream=&mSStream;
00071     break;
00072   default:
00073     std::stringstream errstr;
00074     errstr << "Invalid Mode / Not Implemented";
00075     throw Exception("TokenWriter::TokenWriter", errstr.str(), 2);
00076   }
00077 }
00078 
00079 
00080 // destructor
00081 TokenWriter::~TokenWriter(void) {
00082   if(mMode==File) mFStream.close();
00083 }
00084 
00085 // Str()
00086 std::string TokenWriter::Str(void) {
00087   if(mMode!=String) {
00088     std::stringstream errstr;
00089     errstr << "Not in String Mode";
00090     throw Exception("TokenWriter::Str()", errstr.str(), 2);
00091   }
00092   return mSStream.str();
00093 }
00094 
00095 // Columns()
00096 int TokenWriter::Columns(void) const {
00097   return mColumns;
00098 }
00099 
00100 // Columns()
00101 void TokenWriter::Columns(int columns) {
00102   mColumns = columns;
00103 }
00104 
00105 // Endl()
00106 void TokenWriter::Endl(void) {
00107   try{
00108     if(mMode!=String) *mpStream << std::endl;
00109     else *mpStream << " ";
00110   }
00111   catch (std::ios::failure&) {
00112     std::stringstream errstr;
00113     errstr << "Exception opening/writing file \"" << mFileName << "\"";
00114     throw Exception("Generator::write", errstr.str(), 2);
00115   }
00116 }
00117 
00118 // Write(rToken)
00119 void TokenWriter::Write(Token& rToken) {
00120     FD_DV("TokenWriter::Write(token)");
00121     try{
00122   if ( (rToken.Type() == Token::Begin) || (rToken.Type() == Token::End) ) {
00123      if(mColCount !=0) Endl();
00124          rToken.Write(mpStream);
00125          Endl();
00126          mColCount = 0;
00127       }
00128       else {
00129          if(mColCount == mColumns) {
00130             mColCount = 0;
00131             Endl();
00132          }
00133          mColCount++;
00134          rToken.Write(mpStream);
00135          *mpStream << " ";
00136       }
00137     }
00138     catch (std::ios::failure&) {
00139         std::stringstream errstr;
00140         errstr << "Exception opening/writing file \"" << mFileName << "\"";
00141         throw Exception("TokenWriter::Write(token)", errstr.str(), 2);
00142     }
00143 }
00144 
00145 // Write(rName)
00146 void TokenWriter::Write(const std::string& rName) {
00147   if ((rName == "\n") || (rName == "\r\n")) {
00148     Endl();
00149     mColCount = 0;
00150     return;
00151   }
00152   Token token;
00153   token.SetString(rName);
00154   Write(token);
00155 }
00156 
00157 
00158 // Write(index)
00159 void TokenWriter::Write(Idx index) {
00160   Token token;
00161   token.SetInteger(index);
00162   Write(token);
00163 }
00164 
00165 // WriteFloat(float)
00166 void TokenWriter::WriteFloat(const double& val) {
00167   Token token;
00168   token.SetFloat(val);
00169   Write(token);
00170 }
00171 
00172 
00173 // WriteOption(rOpt)
00174 void TokenWriter::WriteOption(const std::string& rOpt) {
00175   Token token;
00176   token.SetOption(rOpt);
00177   Write(token);
00178 }
00179 
00180 
00181 // WriteBegin(rLabel)
00182 void TokenWriter::WriteBegin(const std::string& rLabel) {
00183   Token token;
00184   token.SetBegin(rLabel);
00185   Write(token);
00186 }
00187 
00188 // WriteEnd(rLabel)
00189 void TokenWriter::WriteEnd(const std::string& rLabel) {
00190   Token token;
00191   token.SetEnd(rLabel);
00192   Write(token);
00193 }
00194 
00195 // Comment(comment)
00196 void TokenWriter::Comment(const std::string& comment){
00197   if(mMode==String) return; // since string has no endl, we want no comments
00198    try{
00199         if(mColCount!=0)  Endl();
00200       *mpStream << "%" << comment;
00201       Endl();
00202       mColCount = 0;
00203     }
00204     catch (std::ios::failure&) {
00205         std::stringstream errstr;
00206         errstr << "Exception opening/writing file \"" << mFileName << "\"";
00207         throw Exception("TokenWriter::Comment", errstr.str(), 2);
00208     }
00209 }
00210 
00211 
00212 
00213 } // namespace faudes

Generated on Fri May 9 11:26:47 2008 for libFAUDES 2.09b by  doxygen 1.4.4