qse  0.5.4
qsescan.cpp
Go to the documentation of this file.
1 #include "qsescan.h"
2 #include "qsedataset.h"
3 
4 #include <QRegExp>
5 
6 #include <math.h>
7 #include <stdio.h>
8 
9 QseScan::QseScan(const char *name)
10  : QObject(NULL),
11 // m_Role(UnspecifiedRole),
12  m_Name(name),
13  m_Command(name),
14  m_Mutex(QMutex::Recursive),
15  m_ScanDateTime(QDateTime::currentDateTime()),
16  m_ScanNumber(-1)
17 {
18 }
19 
21 {
22  QMutexLocker lock(&m_Mutex);
23 
25 
26  foreach (column, m_Columns) {
27  delete column;
28  }
29 }
30 
31 QString QseScan::name() const
32 {
33  QMutexLocker lock(&m_Mutex);
34 
35  return m_Name;
36 }
37 
38 void QseScan::setName(QString name)
39 {
40  QMutexLocker lock(&m_Mutex);
41 
42  m_Name = name;
43 }
44 
46 {
47  QMutexLocker lock(&m_Mutex);
48 
49  return m_Columns.size();
50 }
51 
53 {
54  int sz = columnCount();
55 
56  for (int i = sz-1; i>=n; i--) {
57  QMutexLocker lock(&m_Mutex);
58 
59  if (m_Columns[i]) {
60  delete m_Columns.takeAt(i);
61  }
62  }
63 }
64 
66 {
67  setColumnCount(0);
68 
69  QMutexLocker lock(&m_Mutex);
70 
71  m_Header.clear();
72 }
73 
75 {
76  int maxcount = 0;
77  int nc = columnCount();
78 
79  for (int i=0; i<nc; i++) {
80  QseColumn *c = column(i);
81 
82  if (c) {
83  int nr = c->rowCount();
84 
85  if (nr > maxcount) {
86  maxcount = nr;
87  }
88  }
89  }
90 
91  return maxcount;
92 }
93 
95 {
96  QMutexLocker lock(&m_Mutex);
98 
99  foreach (column, m_Columns) {
100  column -> setRowCount(n);
101  }
102 }
103 
105 {
106  if ((0 <= n) && (n < m_Columns.size())) {
107  return m_Columns.at(n);
108  } else {
109  return NULL;
110  }
111 }
112 
113 QseColumn* QseScan::column(const QString& nm) const
114 {
115  QMutexLocker lock(&m_Mutex);
116  QseColumn *column;
117 
118  foreach (column, m_Columns) {
119  if (column && (column->name()==nm)) {
120  return column;
121  }
122  }
123 
124  return NULL;
125 }
126 
127 void QseScan::setColumnNames(const char *nms)
128 {
129  const char *ptr = nms + 3;
130  const char *found, *next;
131  int nc = columnCount();
132  int col;
133  char nam[256];
134 
135  for (col = 0; ptr; col++) {
136  while ((*ptr)=='\t' || (*ptr)==' ') {
137  ptr++;
138  }
139 
140  found = strstr(ptr, " ");
141  if (found) {
142  next = found + 2;
143  } else {
144  next = NULL;
145  }
146 
147  QseColumn* c= NULL;
148 
149  if (next) {
150  qstrncpy(nam, ptr, found-ptr+1);
151  c = appendColumn(nam);
152  } else {
153  c = appendColumn(ptr);
154  }
155 
156  nc++;
157  ptr = next;
158  }
159 }
160 
161 void QseScan::appendHeader(const QString& h)
162 {
163  QMutexLocker lock(&m_Mutex);
164  m_Header.push_back(h);
165 }
166 
167 QStringList QseScan::header() const
168 {
169  QMutexLocker lock(&m_Mutex);
170  return m_Header;
171 }
172 
173 void QseScan::mergeHeaders(const QStringList &hdr)
174 {
175  QString hdrline;
176 
177  foreach (hdrline, hdr) {
178  bool nocont;
179  {
180  QMutexLocker lock(&m_Mutex);
181  nocont = !m_Header.contains(hdrline);
182  }
183 
184  if (nocont) {
185  appendHeader(hdrline);
186  }
187  }
188 }
189 
190 void QseScan::appendData(const char *l)
191 {
192  int nc = columnCount();
193  const char *ptr = l;
194  char *next;
195 
196  for (int col = 0; ptr; col++) {
197  double val;
198  val = strtod(ptr, &next);
199 
200  if (ptr == next) {
201  return ;
202  }
203 
204  QseColumn *c=NULL;
205 
206  if (col >= nc) {
207  c = appendColumn(QString("col%1").arg(col));
208 
209  if (nc > 0) {
210  c -> setRowCount(column(0)->rowCount() - 1);
211  }
212 
213  nc++;
214  } else {
215  c = column(col);
216  }
217 
218  c -> appendData(val);
219 
220  ptr = next;
221  }
222 
223  printf("\n");
224 }
225 
226 QString QseScan::scanCommand() const
227 {
228  QMutexLocker lock(&m_Mutex);
229 
230  return m_Command;
231 }
232 
233 void QseScan::setScanCommand(const QString& cmd)
234 {
235  QMutexLocker lock(&m_Mutex);
236 
237  m_Command = cmd;
238 }
239 
240 QseColumn *QseScan::appendColumn(const QString &name, const QseColumn *proto)
241 {
242  QseColumn *res=NULL;
243 
244  {
245  QMutexLocker lock(&m_Mutex);
246 
247  res = new QseColumn(name);
248  m_Columns.append(res);
250  }
251 
252  if (proto) {
253  res -> setRowCount(proto -> rowCount());
254  res -> assignment(proto);
255  } else if (columnCount() > 0) {
256  res -> setRowCount(column(0)->rowCount());
257  res -> assignment(0.0);
258  }
259 
260  return res;
261 }
262 
263 void QseScan::writeScan(const QString &name)
264 {
265  FILE *f = fopen(qPrintable(name),"w");
266  int nr = maxRowCount();
267  int nc = columnCount();
268  int hsz;
269 
270  {
271  QMutexLocker lock(&m_Mutex);
272  hsz = m_Header.size();
273  }
274 
275  for (int i = 0; i < hsz; i++) {
276  QMutexLocker lock(&m_Mutex);
277  fputs(qPrintable(m_Header[i]+"\n"), f);
278  }
279 
280  fprintf(f,"#N");
281 
282  for (int i = 0; i < nc; i++) {
283  QseColumn *c = column(i);
284  if (c) {
285  fprintf(f,"\t%s", qPrintable(c->name()));
286  }
287  }
288 
289  fprintf(f,"\n");
290 
291  for (int r = 0; r<nr; r++) {
292  fprintf(f, "%d", r);
293 
294  for (int i = 0; i < nc; i++) {
295  QseColumn *c = column(i);
296  if (c) {
297  fprintf(f,"\t%0.12g", c->data(r));
298  }
299  }
300 
301  fprintf(f,"\n");
302  }
303 
304  fclose(f);
305 }
306 
307 QDateTime QseScan::scanDateTime() const
308 {
309  QMutexLocker lock(&m_Mutex);
310 
311  return m_ScanDateTime;
312 }
313 
314 void QseScan::setScanDateTime(const QDateTime& datetime)
315 {
316  QMutexLocker lock(&m_Mutex);
317 
318  m_ScanDateTime = datetime;
319 }
320 
321 QString QseScan::scanDateLine() const
322 {
323  QMutexLocker lock(&m_Mutex);
324 
325  return "#D " + m_ScanDateTime.toString(Qt::TextDate);
326 }
327 
329 {
330  QMutexLocker lock(&m_Mutex);
331 
332  return QString("#N %1").arg(columnCount());
333 }
334 
336 {
337  QMutexLocker lock(&m_Mutex);
338 
339  QString res = "#L ";
340  int ncols = columnCount();
341 
342  for (int i=0; i<ncols; i++) {
343  if (i != 0) {
344  res += " ";
345  }
346 
347  res += column(i) -> name();
348  }
349 
350  return res;
351 }
352 
354 {
355  QMutexLocker lock(&m_Mutex);
356 
357  return m_ScanNumber;
358 }
359 
361 {
362  QMutexLocker lock(&m_Mutex);
363 
364  m_ScanNumber = n;
365 }
366 
void setColumnCount(int n)
Definition: qsescan.cpp:52
const double * data() const
Definition: qsecolumn.cpp:62
int m_ScanNumber
Definition: qsescan.h:72
QStringList header() const
Definition: qsescan.cpp:167
void setScanNumber(int n)
Definition: qsescan.cpp:360
QseColumn * appendColumn(const QString &name, const QseColumn *proto=NULL)
Definition: qsescan.cpp:240
void mergeHeaders(const QStringList &hdr)
Definition: qsescan.cpp:173
QDateTime scanDateTime() const
Definition: qsescan.cpp:307
void writeScan(const QString &name)
Definition: qsescan.cpp:263
void setScanCommand(const QString &cmd)
Definition: qsescan.cpp:233
int rowCount() const
Definition: qsecolumn.cpp:48
QseScan(const char *name)
Definition: qsescan.cpp:9
QString m_Name
Definition: qsescan.h:68
void setRowCount(int n)
Definition: qsescan.cpp:94
void setScanDateTime(const QDateTime &datetime)
Definition: qsescan.cpp:314
int scanNumber() const
Definition: qsescan.cpp:353
QMutex m_Mutex
Definition: qsescan.h:70
QString scanDateLine() const
Definition: qsescan.cpp:321
int columnCount() const
Definition: qsescan.cpp:45
void clear()
Definition: qsescan.cpp:65
void setColumnNames(const char *nms)
Definition: qsescan.cpp:127
QString scanCommand() const
Definition: qsescan.cpp:226
QString scanColumnNamesLine() const
Definition: qsescan.cpp:335
void setName(QString name)
Definition: qsescan.cpp:38
QString m_Command
Definition: qsescan.h:69
QDateTime m_ScanDateTime
Definition: qsescan.h:71
QHash< QString, QseColumn * > m_ColumnsDictionary
Definition: qsescan.h:73
void appendHeader(const QString &h)
Definition: qsescan.cpp:161
void appendData(const char *l)
Definition: qsescan.cpp:190
QString name() const
Definition: qsecolumn.cpp:26
QseColumn * column(int n) const
Definition: qsescan.cpp:104
QString scanColumnCountLine() const
Definition: qsescan.cpp:328
QStringList m_Header
Definition: qsescan.h:66
QList< QseColumn * > m_Columns
Definition: qsescan.h:67
int maxRowCount() const
Definition: qsescan.cpp:74
QString name() const
Definition: qsescan.cpp:31
~QseScan()
Definition: qsescan.cpp:20