File.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Ulrich Friedrich Klank <klank@in.tum.de>
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 3 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, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 
00019 #include "File.h"
00020 
00021 #ifndef WIN32
00022 #ifndef sprintf_s
00023 #define sprintf_s(a,b,c,d) sprintf(a,c,d)
00024 #include <string.h>
00025 #include <stdio.h>
00026 #endif
00027 #endif
00028 
00029 
00030 
00031 linfile::File::~File(void)
00032 {
00033         delete nextElem;
00034         nextElem = NULL;
00035         m_file.close();
00036 }
00037 
00038 linfile::File::File(std::string filename, bool bOut, bool bAppend)
00039 {
00040         if(!bAppend)
00041         {
00042                 m_file.open(filename.c_str(), bOut ? std::ios_base::out : std::ios_base::in);
00043         }
00044         else
00045         {
00046                 m_file.open(filename.c_str(), std::ios_base::app);
00047         }
00048         if(m_file.bad())
00049                 printf("Error opening the file");
00050         nextElem = new char[LIN__BUFFERSIZE];
00051 }
00052 
00053 double linfile::File::GetNextDouble()
00054 {
00055         m_file.get(nextElem, LIN__BUFFERSIZE, ' ');
00056         while(nextElem[0] == '#')
00057         {
00058                 m_file.getline(nextElem, LIN__BUFFERSIZE);
00059                 m_file.get(nextElem, LIN__BUFFERSIZE, ' ');
00060         }
00061         double dRet = atof(nextElem);
00062         m_file.get(nextElem, 2);
00063         return dRet;
00064 }
00065 int linfile::File::GetNextInt()
00066 {
00067         m_file.get(nextElem, LIN__BUFFERSIZE, ' ');
00068         while(nextElem[0] == '#')
00069         {
00070                 m_file.getline(nextElem, LIN__BUFFERSIZE);
00071                 m_file.get(nextElem, LIN__BUFFERSIZE, ' ');
00072         }
00073         int nRet = atoi(nextElem);
00074         m_file.get(nextElem, 2);
00075         return nRet;
00076 }
00077 char* linfile::File::GetNextCommentLine()
00078 {
00079         m_file.getline(nextElem, LIN__BUFFERSIZE);
00080         return nextElem;
00081 }
00082 
00083 char* linfile::File::GetNextWord()
00084 {
00085         m_file.get(nextElem, LIN__BUFFERSIZE, ' ');
00086         return nextElem;
00087 }
00088 
00089 std::vector<int> linfile::File::GetNextIntVector()
00090 {
00091         std::vector<int> viReturn;
00092         int nSize = GetNextInt();
00093         for(int i = 0; i < nSize; i++)
00094                 viReturn.push_back(GetNextInt());
00095         return viReturn;
00096 }
00097 
00098 std::vector<double> linfile::File::GetNextDoubleVector()
00099 {
00100         std::vector<double> vdReturn;
00101         int nSize = GetNextInt();
00102         for(int i = 0; i < nSize; i++)
00103                 vdReturn.push_back(GetNextDouble());
00104         return vdReturn;
00105 }
00106 
00107 void linfile::File::WriteInt(int nValue)
00108 {
00109         sprintf_s(nextElem, LIN__BUFFERSIZE, "%d ", nValue);
00110         int nLen = strlen(nextElem);
00111         m_file.write(nextElem, nLen);
00112 }
00113 
00114 void linfile::File::WriteSingleInt(int nValue)
00115 {
00116         sprintf_s(nextElem, LIN__BUFFERSIZE, "%d", nValue);
00117         int nLen = strlen(nextElem);
00118         m_file.write(nextElem, nLen);
00119 }
00120 
00121 void linfile::File::WriteIntAsHex(int nValue)
00122 {
00123         sprintf_s(nextElem, LIN__BUFFERSIZE, "%X ", nValue);
00124         int nLen = strlen(nextElem);
00125         m_file.write(nextElem, nLen);
00126 }
00127 
00128 void linfile::File::WriteSingleIntAsHex(int nValue)
00129 {
00130         sprintf_s(nextElem, LIN__BUFFERSIZE, "%X", nValue);
00131         int nLen = strlen(nextElem);
00132         m_file.write(nextElem, nLen);
00133 }
00134 
00135 void linfile::File::WriteDouble(const double dValue)
00136 {
00137         sprintf_s(nextElem, LIN__BUFFERSIZE, "%f ", dValue);
00138         int nLen = strlen(nextElem);
00139         m_file.write(nextElem, nLen);
00140 }
00141 
00142 void linfile::File::WriteSingleDouble(const double dValue)
00143 {
00144         sprintf_s(nextElem, LIN__BUFFERSIZE, "%f", dValue);
00145         int nLen = strlen(nextElem);
00146         m_file.write(nextElem, nLen);
00147 }
00148 
00149 void linfile::File::WriteDoubleLimLength(const double dValue, const int nDigits)
00150 {
00151         sprintf_s(nextElem, LIN__BUFFERSIZE, "%.1f", dValue);
00152         int nLen = strlen(nextElem);
00153         m_file.write(nextElem, nLen);
00154 }
00155 
00156 void linfile::File::WriteLine(const char* stLine)
00157 {
00158         int nLen = strlen(stLine);
00159         m_file.write(stLine, nLen);
00160 }
00161 
00162 void linfile::File::WriteVector(std::vector<int> viData)
00163 {
00164         std::vector<int>::const_iterator it = viData.begin();
00165         WriteInt(viData.size());
00166         while(it != viData.end())
00167         {
00168                 WriteInt((*it));
00169                 it++;
00170         }
00171 }
00172 
00173 void linfile::File::WriteVector(std::vector<double> vdData)
00174 {
00175         std::vector<double>::const_iterator it = vdData.begin();
00176         WriteInt(vdData.size());
00177         while(it != vdData.end())
00178         {
00179                 WriteDouble((*it));
00180                 it++;
00181         }
00182 }


cognitive_perception
Author(s): Ulrich F Klank
autogenerated on Mon Oct 6 2014 10:48:45