io.cpp

Sat, 16 Mar 2013 13:08:24 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sat, 16 Mar 2013 13:08:24 +0200
changeset 18
a6732098fed8
parent 17
5606eebd0b90
child 19
6c5977e43e73
permissions
-rw-r--r--

Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code

0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 #include <vector>
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 #include "common.h"
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 #include "io.h"
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 #include "ldtypes.h"
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 #include "misc.h"
4
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
7 #include "gui.h"
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
8 #include "bbox.h"
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 // =============================================================================
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 // IO_FindLoadedFile (str)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 //
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 // Returns a pointer to the first found open file with the given name.
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 // =============================================================================
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 OpenFile* IO_FindLoadedFile (str name) {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 OpenFile* file;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 for (uint i = 0; i < g_LoadedFiles.size(); i++) {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 file = g_LoadedFiles[i];
7
098e3c4949c6 Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents: 4
diff changeset
20 if (!file->zFileName.icompare (name))
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 return file;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 return NULL;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 // =============================================================================
4
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
28 // IO_OpenLDrawFile (str)
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 //
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 // Opens the given file and parses the LDraw code within.
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 // =============================================================================
4
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
32 OpenFile* IO_OpenLDrawFile (str path) {
13
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
33 logf ("Opening %s...\n", path.chars());
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
34
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 FILE* fp = fopen (path.chars (), "r");
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 if (!fp) {
13
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
38 logf (LOG_Error, "Couldn't open %s: %s\n", path.chars (), strerror (errno));
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 return NULL;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 OpenFile* load = new OpenFile;
13
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
43 ulong numWarnings = 0;
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
44
7
098e3c4949c6 Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents: 4
diff changeset
45 load->zFileName = path;
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 vector<str> lines;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 char line[1024];
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 while (fgets (line, sizeof line, fp)) {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 // Trim the trailing newline
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 str zLine = line;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 while (zLine[~zLine - 1] == '\n' || zLine[~zLine - 1] == '\r')
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 zLine -= 1;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 lines.push_back (zLine);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 fclose (fp);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62
13
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
63 for (ulong i = 0; i < lines.size(); ++i) {
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
64 LDObject* obj = ParseLine (lines[i]);
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
65 load->objects.push_back (obj);
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
66
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
67 // Check for warnings
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
68 if (obj->getType() == OBJ_Gibberish) {
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
69 logf (LOG_Warning, "Couldn't parse line #%lu: %s\n",
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
70 i, static_cast<LDGibberish*> (obj)->zReason.chars());
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
71 logf (LOG_Warning, "- Line was: %s\n", lines[i].chars());
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
72 numWarnings++;
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
73 }
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
74 }
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 g_LoadedFiles.push_back (load);
4
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
77 g_CurrentFile = g_LoadedFiles[g_LoadedFiles.size() - 1];
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
78
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
79 // Recalculate the bounding box
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
80 g_BBox.calculate();
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
81
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
82 // Rebuild the object tree view now.
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
83 g_qWindow->buildObjList ();
7
098e3c4949c6 Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents: 4
diff changeset
84 g_qWindow->setTitle ();
4
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
85
14
6d9d8efae2f8 this thing got its own reinterpret_cast now. :P Added SetContents action for altering an object by contents and reinterpreting it.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 13
diff changeset
86 logf (LOG_Success, "File %s parsed successfully (%lu warning%s).\n",
6d9d8efae2f8 this thing got its own reinterpret_cast now. :P Added SetContents action for altering an object by contents and reinterpreting it.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 13
diff changeset
87 path.chars(), numWarnings, PLURAL (numWarnings));
13
3955ff2a7d72 Added logf function to write to message log. Write warnings of unparsable files into the message log.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
88
4
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
89 return g_CurrentFile;
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 // =============================================================================
12
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
93 // isNumber (char*)
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
94 //
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
95 // Returns whether a given string represents a floating point number
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
96 // TODO: Does LDraw support scientific notation?
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
97 // =============================================================================
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
98 static bool isNumber (char* sToken) {
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
99 char* sPointer = &sToken[0];
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
100 bool bGotDot = false;
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
101
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
102 // Allow leading hyphen for negatives
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
103 if (*sPointer == '-')
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
104 sPointer++;
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
105
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
106 while (*sPointer != '\0') {
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
107 if (*sPointer == '.' && !bGotDot) {
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
108 // Decimal point
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
109 bGotDot = true;
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
110 sPointer++;
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
111 continue;
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
112 }
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
113
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
114 if (*sPointer >= '0' && *sPointer <= '9') {
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
115 sPointer++;
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
116 continue; // Digit
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
117 }
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
118
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
119 // If the above cases didn't catch this character, it was
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
120 // illegal and this is therefore not a number.
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
121 return false;
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
122 }
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
123
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
124 return true;
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
125 }
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
126
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
127 // =============================================================================
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 // ParseLine (str)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 //
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 // Parses a string line containing an LDraw object and returns the object parsed.
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 // =============================================================================
12
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
132 #define CHECK_TOKEN_COUNT(N) \
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
133 if (tokens.size() != N) \
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
134 return new LDGibberish (zLine, "Bad amount of tokens");
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
135
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
136 #define CHECK_TOKEN_NUMBERS(MIN,MAX) \
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
137 for (ushort i = MIN; i <= MAX; ++i) \
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
138 if (!isNumber (tokens[i])) \
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
139 return new LDGibberish (zLine, str::mkfmt ("Token #%u was `%s`, expected a number", \
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
140 (i + 1), tokens[i].chars()));
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
141
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 LDObject* ParseLine (str zLine) {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 str zNoWhitespace = zLine;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 StripWhitespace (zNoWhitespace);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 if (!~zNoWhitespace) {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 // Line was empty, or only consisted of whitespace
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 return new LDEmpty;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 char c = zLine[0];
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 vector<str> tokens = zLine / " ";
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152
11
323390a03294 Color gibberish red. Check for line code length for gibberish (must be 1 to be valid)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 7
diff changeset
153 if (~tokens[0] != 1)
323390a03294 Color gibberish red. Check for line code length for gibberish (must be 1 to be valid)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 7
diff changeset
154 return new LDGibberish (zLine, "Illogical line code");
323390a03294 Color gibberish red. Check for line code length for gibberish (must be 1 to be valid)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 7
diff changeset
155
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 switch (c - '0') {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 case 0:
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 // Comment
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160 LDComment* obj = new LDComment;
17
5606eebd0b90 Allow addition of dummy lines..
Santeri Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
161 obj->zText = zLine.substr (2, -1);
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 return obj;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 case 1:
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 {
12
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
167 CHECK_TOKEN_COUNT (15)
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
168 CHECK_TOKEN_NUMBERS (1, 13)
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
169
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 // Subfile
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 LDSubfile* obj = new LDSubfile;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 obj->dColor = atoi (tokens[1]);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173 obj->vPosition = ParseVertex (zLine, 2); // 2 - 4
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 for (short i = 0; i < 9; ++i)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 obj->faMatrix[i] = atof (tokens[i + 5]); // 5 - 13
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 obj->zFileName = tokens[14];
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 return obj;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 case 2:
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 {
12
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
184 CHECK_TOKEN_COUNT (8)
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
185 CHECK_TOKEN_NUMBERS (1, 7)
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
186
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 // Line
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 LDLine* obj = new LDLine;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 obj->dColor = GetWordInt (zLine, 1);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 for (short i = 0; i < 2; ++i)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 7
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 return obj;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 case 3:
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 {
12
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
197 CHECK_TOKEN_COUNT (11)
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
198 CHECK_TOKEN_NUMBERS (1, 10)
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
199
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 // Triangle
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 LDTriangle* obj = new LDTriangle;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 obj->dColor = GetWordInt (zLine, 1);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 for (short i = 0; i < 3; ++i)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205 obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 10
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207 return obj;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210 case 4:
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 {
12
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
212 CHECK_TOKEN_COUNT (14)
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
213 CHECK_TOKEN_NUMBERS (1, 13)
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
214
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215 // Quadrilateral
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216 LDQuad* obj = new LDQuad;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217 obj->dColor = GetWordInt (zLine, 1);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219 for (short i = 0; i < 4; ++i)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
220 obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 13
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
221
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
222 return obj;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
223 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
225 case 5:
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226 {
12
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
227 CHECK_TOKEN_COUNT (14)
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
228 CHECK_TOKEN_NUMBERS (1, 13)
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
229
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 // Conditional line
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231 LDCondLine* obj = new LDCondLine;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232 obj->dColor = GetWordInt (zLine, 1);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
234 for (short i = 0; i < 2; ++i)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
235 obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 7
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237 for (short i = 0; i < 2; ++i)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238 obj->vaControl[i] = ParseVertex (zLine, 8 + (i * 3)); // 8 - 13
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239 return obj;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241
12
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
242 default: // Strange line we couldn't parse
8f6de46a27e2 Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish
Santeri Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
243 return new LDGibberish (zLine, "Unknown line code number");
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 }

mercurial