io.cpp

Sat, 16 Mar 2013 00:35:36 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sat, 16 Mar 2013 00:35:36 +0200
changeset 12
8f6de46a27e2
parent 11
323390a03294
child 13
3955ff2a7d72
permissions
-rw-r--r--

Check whether the numeric arguments of lines really are numeric, and treat lines that don't pass this check as gibberish

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) {
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 FILE* fp = fopen (path.chars (), "r");
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 if (!fp) {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 printf ("Couldn't open %s!\n", path.chars ());
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 return NULL;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 OpenFile* load = new OpenFile;
7
098e3c4949c6 Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents: 4
diff changeset
41 load->zFileName = path;
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 vector<str> lines;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 char line[1024];
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 while (fgets (line, sizeof line, fp)) {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 // Trim the trailing newline
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 str zLine = line;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 while (zLine[~zLine - 1] == '\n' || zLine[~zLine - 1] == '\r')
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 zLine -= 1;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 lines.push_back (zLine);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 fclose (fp);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 for (ulong i = 0; i < lines.size(); ++i)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 load->objects.push_back (ParseLine (lines[i]));
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 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
63 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
64
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
65 // Recalculate the bounding box
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
66 g_BBox.calculate();
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
67
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
68 // 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
69 g_qWindow->buildObjList ();
7
098e3c4949c6 Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents: 4
diff changeset
70 g_qWindow->setTitle ();
4
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
71
758302636564 improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
72 return g_CurrentFile;
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 // =============================================================================
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
76 // 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
77 //
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
78 // 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
79 // 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
80 // =============================================================================
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
81 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
82 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
83 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
84
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
85 // 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
86 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
87 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
88
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
89 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
90 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
91 // 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
92 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
93 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
94 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
95 }
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
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 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
98 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
99 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
100 }
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 // 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
103 // 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
104 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
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
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 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
108 }
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
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 // =============================================================================
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 // ParseLine (str)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 //
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 // Parses a string line containing an LDraw object and returns the object parsed.
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 // =============================================================================
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
115 #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
116 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
117 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
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 #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
120 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
121 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
122 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
123 (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
124
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 LDObject* ParseLine (str zLine) {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 str zNoWhitespace = zLine;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 StripWhitespace (zNoWhitespace);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 if (!~zNoWhitespace) {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 // Line was empty, or only consisted of whitespace
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 return new LDEmpty;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 char c = zLine[0];
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 vector<str> tokens = zLine / " ";
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135
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
136 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
137 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
138
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 switch (c - '0') {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 case 0:
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 // Comment
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 LDComment* obj = new LDComment;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 obj->zText = zLine.substr (1, -1);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 return obj;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 case 1:
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 {
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
150 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
151 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
152
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153 // Subfile
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 LDSubfile* obj = new LDSubfile;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 obj->dColor = atoi (tokens[1]);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 obj->vPosition = ParseVertex (zLine, 2); // 2 - 4
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 for (short i = 0; i < 9; ++i)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 obj->faMatrix[i] = atof (tokens[i + 5]); // 5 - 13
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 obj->zFileName = tokens[14];
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 2:
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 (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
168 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
169
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 // Line
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 LDLine* obj = new LDLine;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 obj->dColor = GetWordInt (zLine, 1);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173 for (short i = 0; i < 2; ++i)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 7
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 return obj;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 case 3:
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 {
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
180 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
181 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
182
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 // Triangle
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 LDTriangle* obj = new LDTriangle;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 obj->dColor = GetWordInt (zLine, 1);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 for (short i = 0; i < 3; ++i)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 10
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 return obj;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 case 4:
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 {
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
195 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
196 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
197
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 // Quadrilateral
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 LDQuad* obj = new LDQuad;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 obj->dColor = GetWordInt (zLine, 1);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 for (short i = 0; i < 4; ++i)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 13
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205 return obj;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 case 5:
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 {
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
210 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
211 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
212
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 // Conditional line
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 LDCondLine* obj = new LDCondLine;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215 obj->dColor = GetWordInt (zLine, 1);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217 for (short i = 0; i < 2; ++i)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218 obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 7
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
220 for (short i = 0; i < 2; ++i)
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
221 obj->vaControl[i] = ParseVertex (zLine, 8 + (i * 3)); // 8 - 13
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
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
225 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
226 return new LDGibberish (zLine, "Unknown line code number");
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228 }

mercurial