COPASI API  4.16.103
CExperimentFileInfo.cpp
Go to the documentation of this file.
1 /* Begin CVS Header
2  $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/parameterFitting/CExperimentFileInfo.cpp,v $
3  $Revision: 1.14 $
4  $Name: $
5  $Author: shoops $
6  $Date: 2011/03/07 19:32:03 $
7  End CVS Header */
8 
9 // Copyright (C) 2011 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc. and EML Research, gGmbH.
16 // All rights reserved.
17 
18 #include <sstream>
19 
20 #include "copasi.h"
21 
22 #include "CExperimentFileInfo.h"
23 #include "CExperiment.h"
24 #include "CExperimentSet.h"
25 
27 
29  mpSet(NULL),
30  mFileName(""),
31  mList(),
32  mLines(0),
33  mUsedEnd(C_INVALID_INDEX),
34  mEmptyLines()
35 {}
36 
38  mpSet(&set),
39  mFileName(""),
40  mList(),
41  mLines(0),
42  mUsedEnd(C_INVALID_INDEX),
43  mEmptyLines()
44 {}
45 
47 {
48  size_t i, imax;
49 
50  for (i = 0, imax = mList.size(); i < imax; i++)
51  pdelete(mList[i]);
52 
53  mList.clear();
54 }
55 
56 bool CExperimentFileInfo::setFileName(const std::string & fileName)
57 {
58  mFileName = fileName;
59  mLines = 0;
60  mEmptyLines.clear();
61 
62  std::ifstream in;
63  in.open(CLocaleString::fromUtf8(mFileName).c_str(), std::ios::binary);
64 
65  if (in.fail()) // File can not be opened.
66  {
67  mLines = 0;
68  return sync();
69  }
70 
71  bool isEmpty;
72 
73  // forwind to count lines in file
74  while (!in.eof())
75  {
76  isEmpty = true;
77  char c;
78 
79  for (in.get(c); c != 0x0a && c != 0x0d; in.get(c))
80  {
81  if (in.fail() || in.eof()) break;
82 
83  switch (c)
84  {
85  case '\x20':
86  case '\x09':
87  case '\x0d':
88  case '\x0a':
89  break;
90 
91  default:
92  isEmpty = false;
93  break;
94  }
95  }
96 
97  // Eat additional line break characters appearing on DOS and Mac text format;
98  if ((c == 0x0d && in.peek() == 0x0a) || // DOS
99  (c == 0x0a && in.peek() == 0x0d)) // Mac
100  in.ignore(1);
101 
102  mLines++;
103 
104  if (isEmpty)
105  mEmptyLines.push_back(mLines);
106  }
107 
108  return sync();
109 }
110 
111 const std::string & CExperimentFileInfo::getFileName() const
112 {return mFileName;}
113 
115 {
116  mpSet->sort();
117 
118  size_t i, imax;
119 
120  for (i = 0, imax = mList.size(); i < imax; i++)
121  pdelete(mList[i]);
122 
123  mList.clear();
124 
125  // Find the desired file name
126  for (i = 0, imax = mpSet->getExperimentCount(); i < imax; i++)
127  if (mpSet->getExperiment(i)->getFileName() == mFileName) break;
128 
129  // Continue as long as the file name does not change
130  for (; i < imax; i++)
131  {
132  if (mpSet->getExperiment(i)->getFileName() != mFileName) break;
133 
134  mList.push_back(new CExperimentInfo(*mpSet->getExperiment(i)));
135  }
136 
138 
139  return validate();
140 }
141 
143 {
144  size_t Last = 0;
145  size_t i, imax;
146 
147  for (i = 0, imax = mList.size(); i < imax; i++)
148  {
149  if (Last >= mList[i]->First) return false;
150 
151  Last = mList[i]->Last;
152 
153  if (Last > mLines) return false;
154  }
155 
156  return true;
157 }
158 
159 bool CExperimentFileInfo::validateFirst(const size_t & index,
160  const size_t & value)
161 {
162  if (mLines < value ||
163  mList[index]->Last < value ||
164  (value == mList[index]->Last &&
165  value == mList[index]->pExperiment->getHeaderRow())) return false;
166 
167  size_t Saved = mList[index]->First;
168  mList[index]->First = value;
169 
170  bool Result = validate();
171  mList[index]->First = Saved;
172 
173  return Result;
174 }
175 
176 bool CExperimentFileInfo::validateLast(const size_t & index,
177  const size_t & value)
178 {
179  if (mLines < value ||
180  value < mList[index]->First ||
181  (value == mList[index]->First &&
182  value == mList[index]->pExperiment->getHeaderRow())) return false;
183 
184  size_t Saved = mList[index]->Last;
185  mList[index]->Last = value;
186 
187  bool Result = validate();
188  mList[index]->Last = Saved;
189 
190  return Result;
191 }
192 
193 bool CExperimentFileInfo::validateHeader(const size_t & index,
194  const size_t & value)
195 {
196  return (value <= mLines &&
197  (mList[index]->First < mList[index]->Last ||
198  value < mList[index]->First ||
199  mList[index]->Last < value));
200 }
201 
202 std::vector< std::string > CExperimentFileInfo::getExperimentNames() const
203 {
204  std::vector< std::string > List;
205 
206  size_t i, imax;
207 
208  for (i = 0, imax = mList.size(); i < imax; i++)
209  List.push_back(mList[i]->pExperiment->getObjectName());
210 
211  return List;
212 }
213 
215 {
216  size_t i, imax;
217 
218  for (i = 0, imax = mList.size(); i < imax; i++)
219  if (mList[i]->pExperiment->getObjectName() == name)
220  return mList[i]->pExperiment;
221 
222  return NULL;
223 }
224 
226  size_t & Last)
227 {
228  mUsedEnd = 0;
229 
230  return getNextUnusedSection(First, Last);
231 }
232 
234  size_t & Last)
235 {
236  First = mUsedEnd + 1;
237 
238  size_t i, imax = mList.size();
239 
240  for (i = 0; i < imax; i++)
241  {
242  if (First < mList[i]->First)
243  {
244  Last = mList[i]->First - 1;
245  mUsedEnd = Last;
246 
247  return adjustForEmptyLines(First, Last);
248  }
249 
250  if (mList[i]->Last + 1 > First)
251  First = mList[i]->Last + 1;
252  }
253 
254  if (First < mLines)
255  {
256  Last = mLines;
257  mUsedEnd = Last;
258 
259  return adjustForEmptyLines(First, Last);
260  }
261 
262  First = C_INVALID_INDEX;
263  Last = C_INVALID_INDEX;
264 
265  mUsedEnd = mLines;
266 
267  return false;
268 }
269 
271  size_t & Last)
272 {
273  std::vector<size_t>::const_iterator it = mEmptyLines.begin();
274  std::vector<size_t>::const_iterator end = mEmptyLines.end();
275 
276  while (it != end && *it < First) ++it;
277 
278  if (it == end)
279  return true;
280 
281  if (it != end && *it == First)
282  {
283  if (First == Last)
284  return getNextUnusedSection(First, Last);
285 
286  First++;
287  return adjustForEmptyLines(First, Last);
288  }
289 
290  if (*it <= Last)
291  {
292  Last = *it - 1;
293  mUsedEnd = Last;
294  }
295 
296  return true;
297 }
298 
300  pExperiment(NULL),
301  First(C_INVALID_INDEX),
302  Last(C_INVALID_INDEX)
303 {}
304 
306  pExperiment(&Experiment),
307  First(pExperiment->getFirstRow()),
308  Last(pExperiment->getLastRow())
309 {}
310 
const std::string & getFileName() const
#define pdelete(p)
Definition: copasi.h:215
bool getFirstUnusedSection(size_t &First, size_t &Last)
std::vector< CExperimentInfo * > mList
bool validateLast(const size_t &index, const size_t &value)
const std::string & getFileName() const
#define C_INVALID_INDEX
Definition: copasi.h:222
CExperiment * getExperiment(const size_t &index)
bool adjustForEmptyLines(size_t &First, size_t &Last)
std::vector< size_t > mEmptyLines
bool setFileName(const std::string &fileName)
bool getNextUnusedSection(size_t &First, size_t &Last)
bool validateFirst(const size_t &index, const size_t &value)
std::vector< std::string > getExperimentNames() const
CExperimentSet * mpSet
static CLocaleString fromUtf8(const std::string &utf8)
bool validateHeader(const size_t &index, const size_t &value)
CExperiment * getExperiment(const std::string &name)
size_t getExperimentCount() const