Sat, 16 Mar 2013 00:05:39 +0200
Color gibberish red. Check for line code length for gibberish (must be 1 to be valid)
0 | 1 | #include <vector> |
2 | ||
3 | #include "common.h" | |
4 | #include "io.h" | |
5 | #include "ldtypes.h" | |
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 | 9 | |
10 | // ============================================================================= | |
11 | // IO_FindLoadedFile (str) | |
12 | // | |
13 | // Returns a pointer to the first found open file with the given name. | |
14 | // ============================================================================= | |
15 | OpenFile* IO_FindLoadedFile (str name) { | |
16 | OpenFile* file; | |
17 | ||
18 | for (uint i = 0; i < g_LoadedFiles.size(); i++) { | |
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 | 21 | return file; |
22 | } | |
23 | ||
24 | return NULL; | |
25 | } | |
26 | ||
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 | 29 | // |
30 | // Opens the given file and parses the LDraw code within. | |
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 | 33 | FILE* fp = fopen (path.chars (), "r"); |
34 | ||
35 | if (!fp) { | |
36 | printf ("Couldn't open %s!\n", path.chars ()); | |
37 | return NULL; | |
38 | } | |
39 | ||
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 | 42 | |
43 | vector<str> lines; | |
44 | ||
45 | { | |
46 | char line[1024]; | |
47 | while (fgets (line, sizeof line, fp)) { | |
48 | // Trim the trailing newline | |
49 | str zLine = line; | |
50 | while (zLine[~zLine - 1] == '\n' || zLine[~zLine - 1] == '\r') | |
51 | zLine -= 1; | |
52 | ||
53 | lines.push_back (zLine); | |
54 | } | |
55 | } | |
56 | ||
57 | fclose (fp); | |
58 | ||
59 | for (ulong i = 0; i < lines.size(); ++i) | |
60 | load->objects.push_back (ParseLine (lines[i])); | |
61 | ||
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 | 73 | } |
74 | ||
75 | // ============================================================================= | |
76 | // ParseLine (str) | |
77 | // | |
78 | // Parses a string line containing an LDraw object and returns the object parsed. | |
79 | // ============================================================================= | |
80 | LDObject* ParseLine (str zLine) { | |
81 | str zNoWhitespace = zLine; | |
82 | StripWhitespace (zNoWhitespace); | |
83 | if (!~zNoWhitespace) { | |
84 | // Line was empty, or only consisted of whitespace | |
85 | return new LDEmpty; | |
86 | } | |
87 | ||
88 | char c = zLine[0]; | |
89 | vector<str> tokens = zLine / " "; | |
90 | ||
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
|
91 | 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
|
92 | 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
|
93 | |
0 | 94 | switch (c - '0') { |
95 | case 0: | |
96 | { | |
97 | // Comment | |
98 | LDComment* obj = new LDComment; | |
99 | obj->zText = zLine.substr (1, -1); | |
100 | return obj; | |
101 | } | |
102 | ||
103 | case 1: | |
104 | { | |
105 | // Subfile | |
106 | LDSubfile* obj = new LDSubfile; | |
107 | obj->dColor = atoi (tokens[1]); | |
108 | obj->vPosition = ParseVertex (zLine, 2); // 2 - 4 | |
109 | ||
110 | for (short i = 0; i < 9; ++i) | |
111 | obj->faMatrix[i] = atof (tokens[i + 5]); // 5 - 13 | |
112 | ||
113 | obj->zFileName = tokens[14]; | |
114 | return obj; | |
115 | } | |
116 | ||
117 | case 2: | |
118 | { | |
119 | // Line | |
120 | LDLine* obj = new LDLine; | |
121 | obj->dColor = GetWordInt (zLine, 1); | |
122 | for (short i = 0; i < 2; ++i) | |
123 | obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 7 | |
124 | return obj; | |
125 | } | |
126 | ||
127 | case 3: | |
128 | { | |
129 | // Triangle | |
130 | LDTriangle* obj = new LDTriangle; | |
131 | obj->dColor = GetWordInt (zLine, 1); | |
132 | ||
133 | for (short i = 0; i < 3; ++i) | |
134 | obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 10 | |
135 | ||
136 | return obj; | |
137 | } | |
138 | ||
139 | case 4: | |
140 | { | |
141 | // Quadrilateral | |
142 | LDQuad* obj = new LDQuad; | |
143 | obj->dColor = GetWordInt (zLine, 1); | |
144 | ||
145 | for (short i = 0; i < 4; ++i) | |
146 | obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 13 | |
147 | ||
148 | return obj; | |
149 | } | |
150 | ||
151 | case 5: | |
152 | { | |
153 | // Conditional line | |
154 | LDCondLine* obj = new LDCondLine; | |
155 | obj->dColor = GetWordInt (zLine, 1); | |
156 | ||
157 | for (short i = 0; i < 2; ++i) | |
158 | obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 7 | |
159 | ||
160 | for (short i = 0; i < 2; ++i) | |
161 | obj->vaControl[i] = ParseVertex (zLine, 8 + (i * 3)); // 8 - 13 | |
162 | return obj; | |
163 | } | |
164 | ||
165 | default: | |
166 | { | |
167 | // Strange line we couldn't parse | |
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
|
168 | return new LDGibberish (zLine, "Unknown line code number"); |
0 | 169 | } |
170 | } | |
171 | } |