-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio.cpp
329 lines (288 loc) · 10.3 KB
/
io.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/****************************************************************************
** This is a part of FourierFit **
** Copyright (C) 2016 Simon Garnotel **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License as published by **
** the Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see <http://www.gnu.org/licenses/>. **
** **
** **************************************************************************
** **
** Author: Simon Garnotel **
** Contact: [email protected] **
** Date: 06/2016 **
** Version: 1.0 **
****************************************************************************/
#include "header.h"
bool SetCurrentDirectory(QString Directory){
QFile File("system/directory.dat");
if (File.open(QIODevice::WriteOnly)){
File.write(Directory.toStdString().c_str());
File.close();
return true;
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Warning: " << "Unable to write system/directory.dat";
return false;
}
}
QString GetCurrentDirectory(bool *Res){
QString Directory;
QFile File("system/directory.dat");
if (File.open(QIODevice::ReadOnly)){
Directory = File.readLine();
Directory = Directory.remove("\n");
File.close();
(*Res) = true;
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Warning: " << "Unable to read system/directory.dat";
(*Res) = false;
}
return Directory;
}
bool SetCurrentOrder(int Order){
QFile File("system/order.dat");
if (File.open(QIODevice::WriteOnly)){
File.write(QString::number(Order).toStdString().c_str());
File.close();
return true;
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Warning: " << "Unable to write system/order.dat";
return false;
}
}
int GetCurrentOrder(bool *Res){
int Order;
QFile File("system/order.dat");
if (File.open(QIODevice::ReadOnly)){
QString TMP = File.readLine();
TMP = TMP.remove("\n");
Order = TMP.toInt(Res);
if (!(*Res)) return Order;
File.close();
(*Res) = true;
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Warning: " << "Unable to read system/order.dat";
(*Res) = false;
}
return Order;
}
bool SetCurrentSeparator(QString Separator){
QFile File("system/separator.dat");
if (File.open(QIODevice::WriteOnly)){
File.write(Separator.toStdString().c_str());
File.close();
return true;
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Warning: " << "Unable to write system/separator.dat";
return false;
}
}
QString GetCurrentSeparator(bool *Res){
QString Separator;
QFile File("system/separator.dat");
if (File.open(QIODevice::ReadOnly)){
Separator = File.readLine();
Separator = Separator.remove("\n");
File.close();
(*Res) = true;
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Warning: " << "Unable to read system/separator.dat";
(*Res) = false;
}
return Separator;
}
QStringList *ReadFile(const QString FileName, bool *Res){
QStringList *Lines = new QStringList;
QFile File(FileName);
if (File.open(QIODevice::ReadOnly)){
while(!File.atEnd()){
QString Line = File.readLine();
Line = Line.remove("\n");
Lines->append(Line);
}
File.close();
(*Res) = true;
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Warning: " << "Unable to read " << FileName;
(*Res) = false;
}
return Lines;
}
bool ExportCurve(const QString FileName, const QVector<double> *X, const QVector<double> *Y){
int NX = X->size();
int NY = Y->size();
if ((NX > 0) && (NY == NX)){
QFile File(FileName);
if (File.open(QIODevice::WriteOnly)){
QTextStream Stream(&File);
for (int i = 0; i < NX; i++){
Stream << X->at(i) << "\t" << Y->at(i) << endl;
}
File.close();
return true;
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Unable to write " << FileName;
return false;
}
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Null size";
return false;
}
}
bool ExportCoefficients(const QString FileName, const QVector<double> *Coefficients){
int N = Coefficients->size();
int Order = (N-2)/2;
if (N > 1){
QFile File(FileName);
if (File.open(QIODevice::WriteOnly)){
QTextStream Stream(&File);
int k = 0;
Stream << "omega = " << Coefficients->at(k) << endl;
k++;
Stream << "a0 = " << Coefficients->at(k) << endl;
k++;
for (int i = 1; i <= Order; i++){
Stream << "a" << i << " = " << Coefficients->at(k) << endl;
k++;
Stream << "b" << i << " = " << Coefficients->at(k) << endl;
k++;
}
File.close();
return true;
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Warning: " << "Unable to write " << FileName;
return false;
}
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Warning: " << "Non coherent size";
return false;
}
}
bool ExportFreeFem(const QString FileName, const QVector<double> *Coefficients){
int N = Coefficients->size();
int Order = (N-2) / 2;
if (N > 1){
QFile File(FileName);
if (File.open(QIODevice::WriteOnly)){
QTextStream Stream(&File);
int k = 0;
Stream << "real omega = " << Coefficients->at(k) << ";" << endl;
k++;
Stream << "real a0 = " << Coefficients->at(k) << ";" << endl;
k++;
for (int i = 1; i <= Order; i++){
Stream << "real a" << i << " = " << Coefficients->at(k) << ";" << endl;
k++;
Stream << "real b" << i << " = " << Coefficients->at(k) << ";" << endl;
k++;
}
Stream << endl;
Stream << "func real FourierFit (real t){" << endl;
Stream << "\treturn a0" << endl;
for (int i = 1; i <= Order; i++){
Stream << "\t\t+ a" << i << " * cos(" << i << ".*omega*t)";
Stream << " + b" << i << " * sin(" << i << ".*omega*t)";
Stream << endl;
}
Stream << "\t\t;" << endl;
Stream << "}" << endl;
Stream << "\n\n\n" << endl;
File.close();
return true;
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Warning: " << "Unable to write " << FileName;
return false;
}
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Warning: " << "Non coherent size";
return false;
}
}
bool ExportFeel(const QString FileName, const QVector<double> *Coefficients){
int N = Coefficients->size();
int Order = (N-2)/2;
if (N > 1){
QFile File(FileName);
if (File.open(QIODevice::WriteOnly)){
QTextStream Stream(&File);
int k = 0;
Stream << "\"Parameters\":" << endl;
Stream << "{" << endl;
Stream << "\t\"omega\":\"" << Coefficients->at(k) << "\"," << endl;
k++;
Stream << "\t\"a0\":\"" << Coefficients->at(k) << "\"," << endl;
k++;
for (int i = 1; i <= Order; i++){
Stream << "\t\"a" << i << "\":\"" << Coefficients->at(k) << "\"," << endl;
k++;
if (k != (N-1)){
Stream << "\t\"b" << i << "\":\"" << Coefficients->at(k) << "\"," << endl;
k++;
}
else{
Stream << "\t\"b" << i << "\":\"" << Coefficients->at(k) << "\"" << endl;
}
}
Stream << "}\n" << endl;
Stream << "\n{" << endl;
Stream << "\t\"expr\":\"{a0";
for (int i = 1; i <= Order; i++){
Stream << " + a" << i << "*cos(" << i << ".*omega*t)";
Stream << " + b" << i << "*sin(" << i << ".*omega*t)";
}
Stream << ",0,0}:omega:a0";
for (int i = 1; i <= Order; i++){
Stream << ":a" << i << ":b" << i;
}
Stream << "\"" << endl;
Stream << "}" << endl;
File.close();
return true;
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Warning: " << "Unable to write " << FileName;
return false;
}
}
else{
qWarning() << "Warning: " << Q_FUNC_INFO;
qWarning() << "Warning: " << "Non coherent size";
return false;
}
}