MFile.cpp
Go to the documentation of this file.
1 #include "MFile.h"
2 #include <iostream>
3 #include <fstream>
4 #include <string>
5 
6 using namespace Kalman;
7 
9 {
10  Index=0;
11  Rows=0;
12  Cols=0;
13 }
14 
16 {
17  Index = tmp.Index;
18  Rows = tmp.Rows;
19  Cols = tmp.Cols;
20  Name = tmp.Name;
21 }
22 
24 {
25 }
26 
28 {
29  Index = tmp.Index;
30  Rows = tmp.Rows;
31  Cols = tmp.Cols;
32  Name = tmp.Name;
33  return *this;
34 }
35 
36 
38 {
39 }
40 
42 {
43 }
44 
45 int MFile::read(char * filename)
46 {
47  std::ifstream file(filename, std::ios::in);
48  char tmpline[LINE_MAX_LENGTH];
49  std::string tmpstring;
50  std::string tmpvalue;
51  int a, b;
52  unsigned int nbcol=0;
53  char *tmpindex;
54  MFileElement tmpElement;
55 
56  if(file)
57  {
58  file.getline(tmpline, LINE_MAX_LENGTH);
59 
60  while(!file.eof())
61  {
62 
63  tmpindex = strstr(tmpline, "%");
64 
65  // Remove comment
66  if (tmpindex!=NULL)
67  tmpindex[0]=0;
68 
69  tmpstring = tmpline;
70 
71  a = tmpstring.find("=",0);
72 
73  if (a!=std::string::npos)
74  {
75  // New element
76  tmpElement.Name = tmpstring.substr(0,a);
77 
78  // Enlever les espaces inutiles
79  b = tmpElement.Name.find_last_of(" ");
80  if (b!=std::string::npos)
81  {
82  tmpElement.Name.erase(b);
83  }
84 
85  tmpElement.Rows = 1;
86  tmpElement.Cols = 0;
87  tmpElement.Index = Data.size();
88 
89  tmpstring = tmpstring.substr(a+1, std::string::npos);
90  }
91 
92  char current;
93  tmpvalue.erase(tmpvalue.begin(), tmpvalue.end());
94  for(unsigned int i=0; i<tmpstring.size(); i++)
95  {
96  current = tmpstring[i];
97  switch(current)
98  {
99  case '[':
100  tmpvalue.erase(tmpvalue.begin(), tmpvalue.end());
101  nbcol=0;
102  break;
103 
104  case ']':
105  if (add_double(tmpvalue))
106  nbcol++;
107 
108  if (nbcol>tmpElement.Cols)
109  tmpElement.Cols=nbcol;
110 
111  // Do not count incomplete row
112  if (nbcol<tmpElement.Cols)
113  tmpElement.Rows--;
114 
115  VectorMFileElement.push_back(tmpElement);
116  break;
117 
118  case ';':
119  if (add_double(tmpvalue))
120  nbcol++;
121 
122  if (nbcol>tmpElement.Cols)
123  tmpElement.Cols=nbcol;
124 
125  nbcol=0;
126 
127  tmpElement.Rows++;
128  break;
129 
130  case ' ':
131  if (add_double(tmpvalue))
132  nbcol++;
133  break;
134 
135  case '\t':
136  if (add_double(tmpvalue))
137  nbcol++;
138  break;
139 
140  default:
141  tmpvalue.append(1,current);
142  break;
143  }
144 
145  }
146 
147  file.getline(tmpline, LINE_MAX_LENGTH);
148 
149  }
150  }
151  else
152  {
153  std::cerr << "MFile::read: Can not open file "<<filename<<std::endl;
154  return -1;
155  }
156  return 0;
157 }
158 
160 {
161  for(unsigned int i=0; i<VectorMFileElement.size(); i++)
162  {
163  std::cout<<VectorMFileElement[i].Name.c_str()<<": Rows: "<<VectorMFileElement[i].Rows<<" Cols: "<<VectorMFileElement[i].Cols<<" Index: "<<VectorMFileElement[i].Index<<std::endl;
164  }
165 }
166 
167 bool MFile::add_double(std::string &tmpstr)
168 {
169  double tmpdouble;
170 
171  if (!tmpstr.empty())
172  {
173  tmpdouble = atof(tmpstr.c_str());
174  Data.push_back(tmpdouble);
175  tmpstr.erase(tmpstr.begin(), tmpstr.end());
176  return true;
177  }
178  return false;
179 }
180 
181 
182 int MFile::save(char *filename)
183 {
184  std::ofstream file(filename, std::ios::out|std::ios::trunc);
185  unsigned int index;
186 
187  if(file)
188  {
189 
190  for( unsigned int i=0; i<VectorMFileElement.size(); i++)
191  {
192  file<<VectorMFileElement[i].Name.c_str()<<" = ["<<std::endl;
193 
194  index = VectorMFileElement[i].Index;
195  for( unsigned int j=0; j<VectorMFileElement[i].Rows; j++)
196  {
197  for( unsigned int k=0; k<VectorMFileElement[i].Cols; k++)
198  {
199  file<<Data[index++]<<" ";
200  }
201  file<<";"<<std::endl;
202  }
203 
204  file<<"];"<<std::endl<<std::endl;
205  }
206  }
207  else
208  {
209  std::cerr << "MFile::save: Can not open file "<<filename<<std::endl;
210  return -1;
211  }
212 
213  return 0;
214 }
#define LINE_MAX_LENGTH
Definition: MFile.h:38
int save(char *filename)
Definition: MFile.cpp:182
int read(char *filename)
Definition: MFile.cpp:45
unsigned int Rows
Definition: MFile.h:45
std::vector< double > Data
Definition: MFile.h:82
unsigned int Index
Definition: MFile.h:44
unsigned int Cols
Definition: MFile.h:46
void print()
Definition: MFile.cpp:159
MFileElement & operator=(const MFileElement &tmp)
Definition: MFile.cpp:27
std::vector< MFileElement > VectorMFileElement
Definition: MFile.h:81
std::string Name
Definition: MFile.h:47
bool add_double(std::string &tmpstr)
Definition: MFile.cpp:167


kfilter
Author(s): Jorge Almeida, Vincent Zalzal, Sylvain Marleau, Richard Gourdeau
autogenerated on Mon Mar 2 2015 01:31:45