qse  0.5.4
qseinputfile.cpp
Go to the documentation of this file.
1 #include "qseinputfile.h"
2 
3 #include <stdio.h>
4 
5 #ifdef Q_OS_WIN
6 #define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n)
7 #endif
8 
9 QseInputFile::QseInputFile(const QString& name, QObject *parent)
10  : QseDataSet(name, parent),
11  m_Buffer(NULL),
12  m_Length(0),
13  m_BufferSize(0),
14  m_PathName(""),
15  m_File(),
16 
17  m_NLines(0),
18  m_MaxLength(0),
19  m_Scan(NULL),
20  m_NPoints(0)
21 {
22  m_Buffer = (char*) malloc(2500001);
23  m_Length = 0;
24  m_BufferSize = 2500000;
25 }
26 
28 {
29  QMutexLocker lock(&m_Mutex);
30 
31  if (m_Buffer) {
32  free(m_Buffer);
33  }
34 }
35 
37 {
38  QMutexLocker lock(&m_Mutex);
39 
40  return m_PathName;
41 }
42 
44 {
45 // printf("QseInputFile::emitDataChanged()\n");
46 
47 // emit dataChanged();
48 }
49 
51 {
52  QMutexLocker lock(&m_Mutex);
53 
54  qint64 sz = m_File.size();
55  qint64 pz = m_File.pos();
56 
57  double pct = ((double) pz)/((double) sz)*100.0;
58 
59  emit madeProgress((int) pct);
60 
61  // QApplication::processEvents();
62 }
63 
65 {
66  QMutexLocker lock(&m_Mutex);
67 
68  if (m_File.isOpen()) {
69  m_File.close();
70  }
71 
72  m_PathName = nm;
73  m_File.setFileName(m_PathName);
74  m_File.open(QIODevice::ReadOnly);
75  m_NLines = 0;
76  m_MaxLength = 0;
77  m_Length = 0;
78  m_Scan = NULL;
79  m_NPoints = 0;
80 
81  setFileName(nm);
82 }
83 
85 {
86  QMutexLocker lock(&m_Mutex);
87 
88  if (m_File.isOpen()) {
89  qint64 sz = m_File.size();
90  qint64 pz = m_File.pos();
91 
92  return pz>=sz;
93  } else{
94 // printf("atEnd on closed file\n");
95  return true;
96  }
97 }
98 
100 {
101  QMutexLocker lock(&m_Mutex);
102 
103  int lnth = m_File.readLine(m_Buffer+m_Length, m_BufferSize-m_Length);
104  bool lineended = false;
105 
106  while (lnth > 0 && (m_Buffer[lnth-1]=='\n' || m_Buffer[lnth-1]=='\r')) {
107  m_Buffer[lnth-1]=0;
108  lnth--;
109  lineended = true;
110  }
111 
112  m_Length += lnth;
113 
114  if (lineended) {
115  m_NLines++;
116 
117  if (m_Length > m_MaxLength) {
119  }
120 
121  if (strncasecmp(m_Buffer, "#S", 2) == 0) {
122  int ns = scanCount();
123 // printf("QseInputFile:: Scan Added %d\n", ns);
124 
125  m_Scan = appendScan();
126  m_Scan -> setName(m_Buffer);
127  m_Scan -> setScanCommand(m_Buffer);
128 
129  int sn;
130  if (sscanf(m_Buffer, "#S%d", &sn)==1) {
131  m_Scan -> setScanNumber(sn);
132  }
133 
134  m_NPoints = 0;
135  updateProgress();
136 
137 
138 // emit scanAdded(ns);
139  emit message(QString("Loading scan %1 of file %2").arg(scanCount()).arg(m_PathName));
140  } else if (strncasecmp(m_Buffer, "#N", 2) == 0) {
141  int nc;
142 
143  if (m_Scan && (sscanf(m_Buffer, "#N%d", &nc) == 1)) {
144  m_Scan -> setColumnCount(nc);
145  }
146  } else if (strncasecmp(m_Buffer, "#L", 2) == 0) {
147  if (m_Scan) {
148  m_Scan -> setColumnNames(m_Buffer);
149 // emit scanAdded(scanCount()-1);
150  }
151  } else if (strncasecmp(m_Buffer, "#D", 2) == 0) {
152  QDateTime dt = QDateTime::fromString(m_Buffer+2);
153 
154  if (dt.isValid()) {
155 // printf("Valid date %s\n", m_Buffer+2);
156 // printf(" == %s\n", qPrintable(dt.toString("yyyyMMdd_hhmmss")));
157 
158  if (m_Scan) {
159  m_Scan -> setScanDateTime(dt);
160  }
161  }
162  } else if (strncasecmp(m_Buffer, "#", 1) == 0) {
163  if (m_Scan) {
165  } else {
167  }
168  } else {
169  if (m_Scan) {
170  // printf("Append: <%s>\n", l);
171  m_Scan -> appendData(m_Buffer);
172  m_NPoints++;
173 
174  } else {
176  }
177  }
178 
179  if (atEnd()) {
180  emit madeProgress(110);
181 // emit message(QString("File %1 scanned, %2 lines, longest %3")
182 // .arg(m_PathName).arg(m_NLines).arg(m_MaxLength));
183 // emit message(QString("%1 scans found\n").arg(scanCount()));
184 
185 // printf("QseInputFile:: load completed\n");
186 
187  emit operationCompleted();
188 // emitDataChanged();
189  }
190 
191  m_Length = 0;
192  }
193 }
194 
int scanCount() const
Definition: qsedataset.cpp:31
void nextInputLine()
void message(const QString &msg)
QseScan * m_Scan
Definition: qseinputfile.h:32
QString m_PathName
Definition: qseinputfile.h:27
QMutex m_Mutex
Definition: qsedataset.h:62
QString filePath()
void operationCompleted()
void setFileName(const QString &name)
Definition: qsedataset.cpp:191
QseInputFile(const QString &name, QObject *parent=0)
Definition: qseinputfile.cpp:9
void updateProgress()
void nextDataFile(QString fileName)
void appendHeader(const QString &hline)
Definition: qsedataset.cpp:137
char * m_Buffer
Definition: qseinputfile.h:24
void madeProgress(int level)
QseScan * appendScan()
Definition: qsedataset.cpp:100
void emitDataChanged()