COPASI API  4.16.103
CTableCell.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2005 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <limits>
16 #include <iostream>
17 #include <sstream>
18 #include <string.h>
19 #include <stdlib.h>
20 
21 #include "copasi.h"
22 
23 #include "CTableCell.h"
24 
25 #include "utilities/utility.h"
26 
27 CTableCell::CTableCell(const char & separator):
28  mSeparator(separator),
29  mName(""),
30  mValue(std::numeric_limits<C_FLOAT64>::quiet_NaN()),
31  mIsValue(false),
32  mIsEmpty(true)
33 {}
34 
36  mSeparator(src.mSeparator),
37  mName(src.mName),
38  mValue(src.mValue),
39  mIsValue(src.mIsValue),
40  mIsEmpty(src.mIsEmpty)
41 {}
42 
44 
45 bool CTableCell::setSeparator(const char & separator)
46 {
47  mSeparator = separator;
48  return true;
49 }
50 
51 const char & CTableCell::getSeparator() const {return mSeparator;}
52 
53 const bool & CTableCell::isValue() const {return mIsValue;}
54 
55 const bool & CTableCell::isEmpty() const {return mIsEmpty;}
56 
57 const std::string & CTableCell::getName() const {return mName;}
58 
59 const C_FLOAT64 & CTableCell::getValue() const {return mValue;}
60 
61 std::istream & operator >> (std::istream &is, CTableCell & cell)
62 {
63  static char buffer[256];
64 
65  cell.mName = "";
66 
67  do
68  {
69  is.clear();
70  is.getline(buffer, 256, cell.mSeparator);
71  cell.mName += buffer;
72  }
73  while (strlen(buffer) == 255 && !is.eof());
74 
75  /* Trim leading and trailing whitespaces from the string */
76  std::string::size_type begin = cell.mName.find_first_not_of("\x20\x09\x0d\x0a");
77 
78  if (begin == std::string::npos)
79  {
80  cell.mName = "";
81  cell.mIsValue = false;
82  cell.mValue = std::numeric_limits<C_FLOAT64>::quiet_NaN();
83  cell.mIsEmpty = true;
84 
85  return is;
86  }
87 
88  std::string::size_type end = cell.mName.find_last_not_of("\x20\x09\x0d\x0a");
89 
90  if (end == std::string::npos)
91  cell.mName = cell.mName.substr(begin);
92  else
93  cell.mName = cell.mName.substr(begin, end - begin + 1);
94 
95  cell.mIsEmpty = false;
96 
97  /* Try to convert the string into a number */
98  const char * Tail = NULL;
99  cell.mValue = strToDouble(cell.mName.c_str(), & Tail);
100 
101  if (Tail != NULL && *Tail == 0x0)
102  {
103  cell.mIsValue = true;
104  }
105  else if (cell.mName == "INF")
106  {
107  cell.mIsValue = true;
108  cell.mValue = std::numeric_limits<C_FLOAT64>::infinity();
109  }
110  else if (cell.mName == "-INF")
111  {
112  cell.mIsValue = true;
113  cell.mValue = - std::numeric_limits<C_FLOAT64>::infinity();
114  }
115  else
116  {
117  cell.mIsValue = false;
118  cell.mValue = std::numeric_limits<C_FLOAT64>::quiet_NaN();
119  }
120 
121  return is;
122 }
123 
124 CTableRow::CTableRow(const size_t & size,
125  const char & separator):
126  mCells(0),
127  mSeparator(separator),
128  mIsEmpty(true),
129  mLastFilledCell(C_INVALID_INDEX)
130 {resize(size);}
131 
133  mCells(src.mCells),
134  mSeparator(src.mSeparator),
135  mIsEmpty(src.mIsEmpty),
136  mLastFilledCell(C_INVALID_INDEX)
137 {}
138 
140 
141 const std::vector< CTableCell > & CTableRow::getCells() const
142 {return mCells;}
143 
144 bool CTableRow::resize(const size_t & size)
145 {
146  mCells.resize(size);
147 
148  std::vector< CTableCell >::iterator it = mCells.begin();
149  std::vector< CTableCell >::iterator end = mCells.end();
150 
151  for (; it != end; ++it)
152  it->setSeparator(mSeparator);
153 
154  return true;
155 }
156 
157 size_t CTableRow::size() const
158 {return mCells.size();}
159 
160 const size_t & CTableRow::getLastFilledCell() const
161 {return mLastFilledCell;}
162 
163 size_t CTableRow::guessColumnNumber(std::istream &is,
164  const bool & rewind)
165 {
166  std::istream::pos_type pos;
167 
168  if (rewind) pos = is.tellg();
169 
170  is >> *this;
171 
172  if (rewind) is.seekg(pos);
173 
174  size_t count;
175 
176  for (count = mCells.size() - 1; count != C_INVALID_INDEX; count--)
177  if (!mCells[count].isEmpty()) break;
178 
179  return count + 1;
180 }
181 
182 const bool & CTableRow::isEmpty() const {return mIsEmpty;}
183 
184 std::istream & CTableRow::readLine(std::istream & is)
185 {
186  // Clear the line;
187  std::stringstream line;
188 
189  char c;
190 
191  for (is.get(c); c != 0x0a && c != 0x0d; is.get(c))
192  {
193  if (is.fail() || is.eof()) break;
194 
195  line.put(c);
196  }
197 
198  // Eat additional line break characters appearing on DOS and Mac text format;
199  if ((c == 0x0d && is.peek() == 0x0a) || // DOS
200  (c == 0x0a && is.peek() == 0x0d)) // Mac
201  is.ignore(1);
202 
203  mIsEmpty = true;
205 
206  std::vector< CTableCell >::iterator it = mCells.begin();
207  std::vector< CTableCell >::iterator end = mCells.end();
208 
209  unsigned C_INT count;
210 
211  for (count = 0; it != end && !line.fail(); ++it, ++count)
212  {
213  line >> *it;
214 
215  if (!it->isEmpty())
216  {
217  mIsEmpty = false;
218  mLastFilledCell = count;
219  }
220  }
221 
222  bool Finished = false;
223 
224  if (it == end)
225  Finished = true;
226 
227  CTableCell Unread(mSeparator);
228 
229  while (!line.fail() && !line.eof())
230  {
231  mCells.push_back(Unread);
232  line >> mCells.back();
233 
234  if (!mCells.back().isEmpty())
235  {
236  mIsEmpty = false;
237  mLastFilledCell = count;
238  }
239 
240  count++;
241  }
242 
243  if (!Finished)
244  {
245  // Missing columns are filled with default
246  for (; it != end; ++it)
247  *it = Unread;
248  }
249 
250  return is;
251 }
252 
253 std::istream & operator >> (std::istream &is, CTableRow & row)
254 {return row.readLine(is);}
#define C_INT
Definition: copasi.h:115
char mSeparator
Definition: CTableCell.h:195
CTableCell(const char &separator= '\t')
Definition: CTableCell.cpp:27
bool mIsEmpty
Definition: CTableCell.h:106
const bool & isEmpty() const
Definition: CTableCell.cpp:182
CTableRow(const size_t &size=0, const char &separator= '\t')
Definition: CTableCell.cpp:124
const std::string & getName() const
Definition: CTableCell.cpp:57
const std::vector< CTableCell > & getCells() const
Definition: CTableCell.cpp:141
std::string mName
Definition: CTableCell.h:100
const size_t & getLastFilledCell() const
Definition: CTableCell.cpp:160
std::istream & readLine(std::istream &is)
Definition: CTableCell.cpp:184
const C_FLOAT64 & getValue() const
Definition: CTableCell.cpp:59
std::istream & operator>>(std::istream &is, CTableCell &cell)
Definition: CTableCell.cpp:61
std::vector< CTableCell > mCells
Definition: CTableCell.h:193
#define C_INVALID_INDEX
Definition: copasi.h:222
bool mIsEmpty
Definition: CTableCell.h:197
const char & getSeparator() const
Definition: CTableCell.cpp:51
char mSeparator
Definition: CTableCell.h:98
size_t size() const
Definition: CTableCell.cpp:157
bool mIsValue
Definition: CTableCell.h:104
C_FLOAT64 mValue
Definition: CTableCell.h:102
const bool & isEmpty() const
Definition: CTableCell.cpp:55
double strToDouble(const char *str, char const **pTail)
Definition: utility.cpp:325
#define C_FLOAT64
Definition: copasi.h:92
size_t guessColumnNumber(std::istream &is, const bool &rewind)
Definition: CTableCell.cpp:163
size_t mLastFilledCell
Definition: CTableCell.h:199
bool setSeparator(const char &separator)
Definition: CTableCell.cpp:45
bool resize(const size_t &size)
Definition: CTableCell.cpp:144
const bool & isValue() const
Definition: CTableCell.cpp:53