00001 #include "MFile.h"
00002 #include <iostream>
00003 #include <fstream>
00004 #include <string>
00005
00006 using namespace Kalman;
00007
00008 MFileElement::MFileElement()
00009 {
00010 Index=0;
00011 Rows=0;
00012 Cols=0;
00013 }
00014
00015 MFileElement::MFileElement(const MFileElement& tmp)
00016 {
00017 Index = tmp.Index;
00018 Rows = tmp.Rows;
00019 Cols = tmp.Cols;
00020 Name = tmp.Name;
00021 }
00022
00023 MFileElement::~MFileElement()
00024 {
00025 }
00026
00027 MFileElement& MFileElement::operator=(const MFileElement& tmp)
00028 {
00029 Index = tmp.Index;
00030 Rows = tmp.Rows;
00031 Cols = tmp.Cols;
00032 Name = tmp.Name;
00033 return *this;
00034 }
00035
00036
00037 MFile::MFile()
00038 {
00039 }
00040
00041 MFile::~MFile()
00042 {
00043 }
00044
00045 int MFile::read(char * filename)
00046 {
00047 std::ifstream file(filename, std::ios::in);
00048 char tmpline[LINE_MAX_LENGTH];
00049 std::string tmpstring;
00050 std::string tmpvalue;
00051 int a, b;
00052 unsigned int nbcol=0;
00053 char *tmpindex;
00054 MFileElement tmpElement;
00055
00056 if(file)
00057 {
00058 file.getline(tmpline, LINE_MAX_LENGTH);
00059
00060 while(!file.eof())
00061 {
00062
00063 tmpindex = strstr(tmpline, "%");
00064
00065
00066 if (tmpindex!=NULL)
00067 tmpindex[0]=0;
00068
00069 tmpstring = tmpline;
00070
00071 a = tmpstring.find("=",0);
00072
00073 if (a!=std::string::npos)
00074 {
00075
00076 tmpElement.Name = tmpstring.substr(0,a);
00077
00078
00079 b = tmpElement.Name.find_last_of(" ");
00080 if (b!=std::string::npos)
00081 {
00082 tmpElement.Name.erase(b);
00083 }
00084
00085 tmpElement.Rows = 1;
00086 tmpElement.Cols = 0;
00087 tmpElement.Index = Data.size();
00088
00089 tmpstring = tmpstring.substr(a+1, std::string::npos);
00090 }
00091
00092 char current;
00093 tmpvalue.erase(tmpvalue.begin(), tmpvalue.end());
00094 for(unsigned int i=0; i<tmpstring.size(); i++)
00095 {
00096 current = tmpstring[i];
00097 switch(current)
00098 {
00099 case '[':
00100 tmpvalue.erase(tmpvalue.begin(), tmpvalue.end());
00101 nbcol=0;
00102 break;
00103
00104 case ']':
00105 if (add_double(tmpvalue))
00106 nbcol++;
00107
00108 if (nbcol>tmpElement.Cols)
00109 tmpElement.Cols=nbcol;
00110
00111
00112 if (nbcol<tmpElement.Cols)
00113 tmpElement.Rows--;
00114
00115 VectorMFileElement.push_back(tmpElement);
00116 break;
00117
00118 case ';':
00119 if (add_double(tmpvalue))
00120 nbcol++;
00121
00122 if (nbcol>tmpElement.Cols)
00123 tmpElement.Cols=nbcol;
00124
00125 nbcol=0;
00126
00127 tmpElement.Rows++;
00128 break;
00129
00130 case ' ':
00131 if (add_double(tmpvalue))
00132 nbcol++;
00133 break;
00134
00135 case '\t':
00136 if (add_double(tmpvalue))
00137 nbcol++;
00138 break;
00139
00140 default:
00141 tmpvalue.append(1,current);
00142 break;
00143 }
00144
00145 }
00146
00147 file.getline(tmpline, LINE_MAX_LENGTH);
00148
00149 }
00150 }
00151 else
00152 {
00153 std::cerr << "MFile::read: Can not open file "<<filename<<std::endl;
00154 return -1;
00155 }
00156 return 0;
00157 }
00158
00159 void MFile::print()
00160 {
00161 for(unsigned int i=0; i<VectorMFileElement.size(); i++)
00162 {
00163 std::cout<<VectorMFileElement[i].Name.c_str()<<": Rows: "<<VectorMFileElement[i].Rows<<" Cols: "<<VectorMFileElement[i].Cols<<" Index: "<<VectorMFileElement[i].Index<<std::endl;
00164 }
00165 }
00166
00167 bool MFile::add_double(std::string &tmpstr)
00168 {
00169 double tmpdouble;
00170
00171 if (!tmpstr.empty())
00172 {
00173 tmpdouble = atof(tmpstr.c_str());
00174 Data.push_back(tmpdouble);
00175 tmpstr.erase(tmpstr.begin(), tmpstr.end());
00176 return true;
00177 }
00178 return false;
00179 }
00180
00181
00182 int MFile::save(char *filename)
00183 {
00184 std::ofstream file(filename, std::ios::out|std::ios::trunc);
00185 unsigned int index;
00186
00187 if(file)
00188 {
00189
00190 for( unsigned int i=0; i<VectorMFileElement.size(); i++)
00191 {
00192 file<<VectorMFileElement[i].Name.c_str()<<" = ["<<std::endl;
00193
00194 index = VectorMFileElement[i].Index;
00195 for( unsigned int j=0; j<VectorMFileElement[i].Rows; j++)
00196 {
00197 for( unsigned int k=0; k<VectorMFileElement[i].Cols; k++)
00198 {
00199 file<<Data[index++]<<" ";
00200 }
00201 file<<";"<<std::endl;
00202 }
00203
00204 file<<"];"<<std::endl<<std::endl;
00205 }
00206 }
00207 else
00208 {
00209 std::cerr << "MFile::save: Can not open file "<<filename<<std::endl;
00210 return -1;
00211 }
00212
00213 return 0;
00214 }