File: wallsprojectparser.cpp

package info (click to toggle)
dewalls 1.0.0%2Bds1-9
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 780 kB
  • sloc: cpp: 6,464; makefile: 62
file content (458 lines) | stat: -rw-r--r-- 12,192 bytes parent folder | download
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include "wallsprojectparser.h"

#include <iostream>

#include <QDir>
#include <QDebug>
#include "signum.h"

#include "segment.h"
#include "segmentparseexception.h"

namespace dewalls {

typedef UnitizedDouble<Length> ULength;
typedef UnitizedDouble<Angle> UAngle;

// STATUS BITS
//   2^0 : Type = Book
//   2^1 : ?
//   2^2 : ?
//   2^3 : Name defines segment
//   2^4 : 1 = Feet, 0 = Meters
//   2^5 : ?
//   2^6 : reference unspecified
//   2^7 : ?
// Derive decl from date:
//   2^8 : 1 = no
//   2^9 : 1 = yes
// UTM/GPS Grid-relative:
//   2^10: 1 = no
//   2^11: 1 = yes
// Preserve vertical shot orientation:
//   2^12: 1 = no
//   2^13: 1 = yes
// Preserve vertical shot length:
//   2^14: 1 = no
//   2^15: 1 = yes
// Other type
//   2^16: 1 = type is other
//   2^17: edit on launch
//   2^18: open on launch
// Default view after compilation (bits 21-19):
//      1: North or East
// 	   10: North or West
//     11: North
//    100: East
//    101: West

// REFERENCE FIELDS
//  	northing	easting	   zn conv   el ? d  m  s  lat d  m  s long index/name
// .REF	2308552.729 324501.432 16 -0.601 27 6 20 52 14.229 88 41 13.085 0 "Adindan"

WallsProjectParser::WallsProjectParser(QObject* parent)
    : QObject(parent), LineParser()
{

}

WpjEntry::WpjEntry(WpjBookPtr parent, QString title)
    : Parent(parent), Title(title), Name(), Path(), Status(0), Options(), Reference()
{

}

WpjBook::WpjBook(WpjBookPtr parent, QString title)
    : WpjEntry(parent, title)
{
}

const int WpjEntry::NameDefinesSegmentBit = 1 << 3;
const int WpjEntry::FeetBit = 1 << 4;
const int WpjEntry::ReferenceUnspecifiedBit = 1 << 6;
const int WpjEntry::DontDeriveDeclBit = 1 << 8;
const int WpjEntry::DeriveDeclBit = 1 << 9;
const int WpjEntry::NotGridRelativeBit = 1 << 10;
const int WpjEntry::GridRelativeBit = 1 << 11;
const int WpjEntry::DontPreserveVertShotOrientationBit = 1 << 12;
const int WpjEntry::PreserveVertShotOrientationBit = 1 << 13;
const int WpjEntry::DontPreserveVertShotLengthBit = 1 << 14;
const int WpjEntry::PreserveVertShotLengthBit = 1 << 15;
const int WpjEntry::OtherTypeBit = 1 << 16;
const int WpjEntry::EditOnLaunchBit = 1 << 17;
const int WpjEntry::OpenOnLaunchBit = 1 << 18;
const int WpjEntry::DefaultViewAfterCompilationMask = 7 << 19;
const int WpjEntry::NorthOrEastViewBits = 1 << 19;
const int WpjEntry::NorthOrWestViewBits = 2 << 19;
const int WpjEntry::NorthViewBits = 3 << 19;
const int WpjEntry::EastViewBits = 4 << 19;
const int WpjEntry::WestViewBits = 5 << 19;

bool WpjEntry::isOther() const {
    return Status & OtherTypeBit;
}

bool WpjEntry::isSurvey() const {
    return !isBook() && !isOther();
}

bool WpjEntry::nameDefinesSegment() const {
    return Status & NameDefinesSegmentBit;
}

GeoReferencePtr WpjEntry::reference() const {
    if (Status & ReferenceUnspecifiedBit) {
        return GeoReferencePtr();
    }
    if (!Reference.isNull()) {
        return Reference;
    }
    if (!Parent.isNull()) {
        return Parent.toStrongRef()->reference();
    }
    return GeoReferencePtr();
}

WpjEntry::ReviewUnits WpjEntry::reviewUnits() const {
    return Status & FeetBit ? Feet : Meters;
}

bool WpjEntry::deriveDeclFromDate() const {
    if (Status & DontDeriveDeclBit) {
        return false;
    }
    if (Status & DeriveDeclBit) {
        return true;
    }
    if (!Parent.isNull()) {
        return Parent.toStrongRef()->deriveDeclFromDate();
    }
    return false;
}

bool WpjEntry::gridRelative() const {
    if (Status & NotGridRelativeBit) {
        return false;
    }
    if (Status & GridRelativeBit) {
        return true;
    }
    if (!Parent.isNull()) {
        return Parent.toStrongRef()->gridRelative();
    }
    return false;
}

bool WpjEntry::preserveVertShotOrientation() const {
    if (Status & DontPreserveVertShotOrientationBit) {
        return false;
    }
    if (Status & PreserveVertShotOrientationBit) {
        return true;
    }
    if (!Parent.isNull()) {
        return Parent.toStrongRef()->preserveVertShotOrientation();
    }
    return false;
}

bool WpjEntry::preserveVertShotLength() const {
    if (Status & DontPreserveVertShotLengthBit) {
        return false;
    }
    if (Status & PreserveVertShotLengthBit) {
        return true;
    }
    if (!Parent.isNull()) {
        return Parent.toStrongRef()->preserveVertShotLength();
    }
    return false;
}

WpjEntry::LaunchOptions WpjEntry::launchOptions() const {
    if (Status & EditOnLaunchBit) {
        return Edit;
    }
    if (Status & OpenOnLaunchBit) {
        return Open;
    }
    return Properties;
}

WpjEntry::View WpjEntry::defaultViewAfterCompilation() const {
    switch (Status & DefaultViewAfterCompilationMask) {
    case NorthOrEastViewBits:
        return NorthOrEast;
    case NorthOrWestViewBits:
        return NorthOrWest;
    case NorthViewBits:
        return North;
    case EastViewBits:
        return East;
    case WestViewBits:
        return West;
    default:
        if (!Parent.isNull()) {
            return Parent.toStrongRef()->defaultViewAfterCompilation();
        }
        return NorthOrEast;
    }
}

QDir WpjEntry::dir() const {
    if (Parent.isNull() || QDir::isAbsolutePath(Path)) {
        return QDir(Path);
    }
    if (!Path.isNull()) {
        return QDir(QDir::cleanPath(Parent.toStrongRef()->dir().path() + '/' + Path));
    }
    return Parent.toStrongRef()->dir();
}

QString WpjEntry::absolutePath() const {
    if (isBook()) {
        return QDir::cleanPath(dir().absolutePath());
    }
    else {
        QString name = Name.value();
        if (name.isEmpty()) {
            return QString();
        }
        if (isSurvey() && !name.endsWith(".SRV", Qt::CaseInsensitive)) {
            if (QFileInfo(QDir::cleanPath(dir().absoluteFilePath(name + ".SRV"))).exists()) {
                name += ".SRV";
            }
            else if (QFileInfo(QDir::cleanPath(dir().absoluteFilePath(name + ".srv"))).exists()) {
                name += ".srv";
            }
            else {
                name += ".SRV";
            }
        }
        return QDir::cleanPath(dir().absoluteFilePath(name));
    }
}

QList<Segment> WpjEntry::allOptions() const {
    QList<Segment> options;
    if (!Parent.isNull()) {
        options = Parent.toStrongRef()->allOptions();
    }
    if (!Options.isEmpty()) {
        options << Options;
    }
    return options;
}

QStringList WpjEntry::segment() const {
    QStringList result;
    if (!Parent.isNull()) {
        result = Parent.toStrongRef()->segment();
    }
    if (nameDefinesSegment() && !Name.isEmpty()) {
        result << Name.value();
    }
    return result;
}

void WallsProjectParser::parseLine(QString line) {
    parseLine(Segment(line));
}

void WallsProjectParser::parseLine(Segment line) {
    reset(line);

    maybeWhitespace();

    if (ProjectRoot.isNull()) {
        oneOf([&]() { this->bookLine(); },
        [&]() { this->commentLine(); },
        [&]() { this->emptyLine(); });
    }
    else if (CurrentEntry.isNull()) {
        oneOf([&]() { this->commentLine(); },
        [&]() { this->emptyLine(); });
    }
    else {
        oneOf([&]() { this->bookLine(); },
        [&]() { this->endbookLine(); },
        [&]() { this->surveyLine(); },
        [&]() { this->nameLine(); },
        [&]() { this->pathLine(); },
        [&]() { this->refLine(); },
        [&]() { this->optionsLine(); },
        [&]() { this->statusLine(); },
        [&]() { this->commentLine(); },
        [&]() { this->emptyLine(); });
    }
}

void WallsProjectParser::emptyLine() {
    maybeWhitespace();
    endOfLine();
}

void WallsProjectParser::bookLine() {
    expect(".BOOK", Qt::CaseInsensitive);
    whitespace();
    QString title = remaining().value();
    WpjBookPtr parent = currentBook();
    WpjBookPtr newEntry(new WpjBook(parent, title));
    if (!parent.isNull()) {
        parent->Children << newEntry;
    }
    else {
        ProjectRoot = newEntry;
    }
    CurrentEntry = newEntry;
}

void WallsProjectParser::endbookLine() {
    expect(".ENDBOOK", Qt::CaseInsensitive);
    maybeWhitespace();
    endOfLine();
    CurrentEntry = currentBook()->Parent;
}

void WallsProjectParser::nameLine() {
    expect(".NAME", Qt::CaseInsensitive);
    whitespace();
    CurrentEntry->Name = remaining();
}

void WallsProjectParser::pathLine() {
    expect(".PATH", Qt::CaseInsensitive);
    whitespace();
    CurrentEntry->Path = remaining().value();
}

void WallsProjectParser::surveyLine() {
    expect(".SURVEY", Qt::CaseInsensitive);
    whitespace();
    QString title = remaining().value();
    WpjBookPtr book = currentBook();
    WpjEntryPtr newEntry(new WpjEntry(book, title));
    book->Children << newEntry;
    CurrentEntry = newEntry;
}

void WallsProjectParser::statusLine() {
    expect(".STATUS", Qt::CaseInsensitive);
    whitespace();
    CurrentEntry->Status = unsignedIntLiteral();
}

void WallsProjectParser::optionsLine() {
    expect(".OPTIONS", Qt::CaseInsensitive);
    whitespace();
    CurrentEntry->Options = remaining();
}

void WallsProjectParser::commentLine() {
    expect(";");
    remaining();
}

void WallsProjectParser::refLine() {
    expect(".REF", Qt::CaseInsensitive);
    whitespace();
    GeoReference ref;
    ref.northing = ULength(doubleLiteral(), Length::Meters);
    whitespace();
    ref.easting = ULength(doubleLiteral(), Length::Meters);
    whitespace();
    ref.zone = intLiteral();
    whitespace();
    ref.gridConvergence = UAngle(doubleLiteral(), Angle::Degrees);
    whitespace();
    ref.elevation = ULength(doubleLiteral(), Length::Meters);
    whitespace();
    doubleLiteral(); // don't know what this field represents
    whitespace();

    double degrees, minutes, seconds;

    degrees = intLiteral();
    whitespace();
    minutes = unsignedIntLiteral();
    whitespace();
    seconds = unsignedDoubleLiteral();
    whitespace();
    ref.latitude = UAngle(degrees + ((minutes + seconds / 60.0) / 60.0) * signum(degrees), Angle::Degrees);

    degrees = intLiteral();
    whitespace();
    minutes = unsignedIntLiteral();
    whitespace();
    seconds = unsignedDoubleLiteral();
    whitespace();
    ref.longitude = UAngle(degrees + ((minutes + seconds / 60.0) / 60.0) * signum(degrees), Angle::Degrees);

    ref.wallsDatumIndex = unsignedIntLiteral();
    whitespace();
    expect('"');
    ref.datumName = expect(QRegExp("[^\"]+"), {"<DATUM_NAME>"}).value();
    expect('"');
    maybeWhitespace();
    endOfLine();
    CurrentEntry->Reference = GeoReferencePtr(new GeoReference);
    *(CurrentEntry->Reference) = ref;
}

WpjBookPtr WallsProjectParser::parseFile(QString fileName) {
    QFile file(fileName);

    if (!file.open(QFile::ReadOnly))
    {
        QString msg = QString("I couldn't open %1").arg(fileName);
        emit message(WallsMessage("error", msg));
        return WpjBookPtr();
    }

    int lineNumber = 0;
    while (!file.atEnd() && (ProjectRoot.isNull() || !CurrentEntry.isNull()))
    {
        QString line = file.readLine();
        line = line.trimmed();
        if (file.error() != QFile::NoError)
        {
            emit message(WallsMessage("error",
                                      QString("failed to read from file: %1").arg(file.errorString()),
                                      fileName,
                                      lineNumber));
            file.close();
            return WpjBookPtr();
        }

        try {
            parseLine(Segment(line, fileName, lineNumber, 0));
        }
        catch (const SegmentParseException& ex) {
            emit message(WallsMessage(ex));
            file.close();
            return WpjBookPtr();
        }

        lineNumber++;
    }

    file.close();

    // not enough .ENDBOOKs at end?
    if (!CurrentEntry.isNull()) {
        emit message(WallsMessage("error",
                                  QString("unexpected end of file: %1").arg(fileName),
                                  fileName,
                                  lineNumber));
        return WpjBookPtr();
    }

    if (!ProjectRoot.isNull()) {
        ProjectRoot->Path = QFileInfo(fileName).dir().canonicalPath();
    }

    return ProjectRoot;
}

} // namespace dewalls