src/ldconfig.cpp

changeset 421
7d26db0be944
parent 406
5371baa17346
child 455
c5d14d112034
equal deleted inserted replaced
420:8d6af951b611 421:7d26db0be944
22 #include "misc.h" 22 #include "misc.h"
23 #include "colors.h" 23 #include "colors.h"
24 24
25 // ============================================================================= 25 // =============================================================================
26 // Helper function for parseLDConfig 26 // Helper function for parseLDConfig
27 static bool parseLDConfigTag( LDConfigParser& pars, char const* tag, str& val ) 27 static bool parseLDConfigTag (LDConfigParser& pars, char const* tag, str& val) {
28 {
29 short pos; 28 short pos;
30 29
31 // Try find the token and get its position 30 // Try find the token and get its position
32 if( !pars.findToken( pos, tag, 1 )) 31 if (!pars.findToken (pos, tag, 1))
33 return false; 32 return false;
34 33
35 // Get the token after it and store it into val 34 // Get the token after it and store it into val
36 return pars.getToken( val, pos + 1 ); 35 return pars.getToken (val, pos + 1);
37 } 36 }
38 37
39 // ============================================================================= 38 // =============================================================================
40 void parseLDConfig() 39 void parseLDConfig() {
41 { 40 File* f = openLDrawFile ("LDConfig.ldr", false);
42 File* f = openLDrawFile( "LDConfig.ldr", false );
43 41
44 if( !f ) 42 if (!f) {
45 { 43 critical (fmt (QObject::tr ("Unable to open LDConfig.ldr for parsing! (%1)"),
46 critical( fmt( QObject::tr( "Unable to open LDConfig.ldr for parsing! (%1)" ), 44 strerror (errno)));
47 strerror( errno )) );
48 delete f; 45 delete f;
49 return; 46 return;
50 } 47 }
51 48
52 // Read in the lines 49 // Read in the lines
53 for( str line : *f ) 50 for (str line : *f) {
54 { 51 if (line.length() == 0 || line[0] != '0')
55 if( line.length() == 0 || line[0] != '0' )
56 continue; // empty or illogical 52 continue; // empty or illogical
57 53
58 line.remove( '\r' ); 54 line.remove ('\r');
59 line.remove( '\n' ); 55 line.remove ('\n');
60 56
61 // Parse the line 57 // Parse the line
62 LDConfigParser pars( line, ' ' ); 58 LDConfigParser pars (line, ' ');
63 59
64 short code = 0, alpha = 255; 60 short code = 0, alpha = 255;
65 str name, facename, edgename, valuestr; 61 str name, facename, edgename, valuestr;
66 62
67 // Check 0 !COLOUR, parse the name 63 // Check 0 !COLOUR, parse the name
68 if( !pars.tokenCompare( 0, "0" ) || !pars.tokenCompare( 1, "!COLOUR" ) || !pars.getToken( name, 2 )) 64 if (!pars.tokenCompare (0, "0") || !pars.tokenCompare (1, "!COLOUR") || !pars.getToken (name, 2))
69 continue; 65 continue;
70 66
71 // Replace underscores in the name with spaces for readability 67 // Replace underscores in the name with spaces for readability
72 name.replace( "_", " " ); 68 name.replace ("_", " ");
73 69
74 // Get the CODE tag 70 // Get the CODE tag
75 if( !parseLDConfigTag( pars, "CODE", valuestr )) 71 if (!parseLDConfigTag (pars, "CODE", valuestr))
76 continue; 72 continue;
77 73
78 if( !isNumber( valuestr )) 74 if (!isNumber (valuestr))
79 continue; // not a number 75 continue; // not a number
80 76
81 // Ensure that the code is within [0 - 511] 77 // Ensure that the code is within [0 - 511]
82 bool ok; 78 bool ok;
83 code = valuestr.toShort( &ok ); 79 code = valuestr.toShort (&ok);
84 80
85 if( !ok || code < 0 || code >= 512 ) 81 if (!ok || code < 0 || code >= 512)
86 continue; 82 continue;
87 83
88 // VALUE and EDGE tags 84 // VALUE and EDGE tags
89 if( !parseLDConfigTag( pars, "VALUE", facename ) || !parseLDConfigTag( pars, "EDGE", edgename )) 85 if (!parseLDConfigTag (pars, "VALUE", facename) || !parseLDConfigTag (pars, "EDGE", edgename))
90 continue; 86 continue;
91 87
92 // Ensure that our colors are correct 88 // Ensure that our colors are correct
93 QColor faceColor( facename ), 89 QColor faceColor (facename),
94 edgeColor( edgename ); 90 edgeColor (edgename);
95 91
96 if( !faceColor.isValid() || !edgeColor.isValid() ) 92 if (!faceColor.isValid() || !edgeColor.isValid())
97 continue; 93 continue;
98 94
99 // Parse alpha if given. 95 // Parse alpha if given.
100 if( parseLDConfigTag( pars, "ALPHA", valuestr )) 96 if (parseLDConfigTag (pars, "ALPHA", valuestr))
101 alpha = clamp<short> ( valuestr.toShort(), 0, 255 ); 97 alpha = clamp<short> (valuestr.toShort(), 0, 255);
102 98
103 LDColor* col = new LDColor; 99 LDColor* col = new LDColor;
104 col->name = name; 100 col->name = name;
105 col->faceColor = faceColor; 101 col->faceColor = faceColor;
106 col->edgeColor = edgeColor; 102 col->edgeColor = edgeColor;
107 col->hexcode = facename; 103 col->hexcode = facename;
108 col->faceColor.setAlpha( alpha ); 104 col->faceColor.setAlpha (alpha);
109 col->index = code; 105 col->index = code;
110 setColor( code, col ); 106 setColor (code, col);
111 } 107 }
112 108
113 delete f; 109 delete f;
114 } 110 }
115 111
120 m_tokens = container_cast<QStringList, List<str>> (inText.split (sep, QString::SkipEmptyParts)); 116 m_tokens = container_cast<QStringList, List<str>> (inText.split (sep, QString::SkipEmptyParts));
121 m_pos = -1; 117 m_pos = -1;
122 } 118 }
123 119
124 // ----------------------------------------------------------------------------- 120 // -----------------------------------------------------------------------------
125 bool LDConfigParser::atBeginning () { 121 bool LDConfigParser::atBeginning() {
126 return (m_pos == -1); 122 return (m_pos == -1);
127 } 123 }
128 124
129 // ----------------------------------------------------------------------------- 125 // -----------------------------------------------------------------------------
130 bool LDConfigParser::atEnd () { 126 bool LDConfigParser::atEnd() {
131 return (m_pos == (signed) m_tokens.size () - 1); 127 return (m_pos == (signed) m_tokens.size() - 1);
132 } 128 }
133 129
134 // ----------------------------------------------------------------------------- 130 // -----------------------------------------------------------------------------
135 bool LDConfigParser::getToken (str& val, const ushort pos) { 131 bool LDConfigParser::getToken (str& val, const ushort pos) {
136 if (pos >= m_tokens.size()) 132 if (pos >= m_tokens.size())
150 return getToken (val, m_pos + 1); 146 return getToken (val, m_pos + 1);
151 } 147 }
152 148
153 // ----------------------------------------------------------------------------- 149 // -----------------------------------------------------------------------------
154 bool LDConfigParser::findToken (short& result, char const* needle, short args) { 150 bool LDConfigParser::findToken (short& result, char const* needle, short args) {
155 for (ushort i = 0; i < (m_tokens.size () - args); ++i) { 151 for (ushort i = 0; i < (m_tokens.size() - args); ++i) {
156 if (m_tokens[i] == needle) { 152 if (m_tokens[i] == needle) {
157 result = i; 153 result = i;
158 return true; 154 return true;
159 } 155 }
160 } 156 }
161 157
162 return false; 158 return false;
163 } 159 }
164 160
165 // ----------------------------------------------------------------------------- 161 // -----------------------------------------------------------------------------
166 void LDConfigParser::rewind () { 162 void LDConfigParser::rewind() {
167 m_pos = -1; 163 m_pos = -1;
168 } 164 }
169 165
170 // ----------------------------------------------------------------------------- 166 // -----------------------------------------------------------------------------
171 void LDConfigParser::seek (short amount, bool rel) { 167 void LDConfigParser::seek (short amount, bool rel) {
172 m_pos = (rel ? m_pos : 0) + amount; 168 m_pos = (rel ? m_pos : 0) + amount;
173 } 169 }
174 170
175 // ----------------------------------------------------------------------------- 171 // -----------------------------------------------------------------------------
176 size_t LDConfigParser::size () { 172 size_t LDConfigParser::size() {
177 return m_tokens.size(); 173 return m_tokens.size();
178 } 174 }
179 175
180 // ----------------------------------------------------------------------------- 176 // -----------------------------------------------------------------------------
181 bool LDConfigParser::tokenCompare (short inPos, const char* sOther) { 177 bool LDConfigParser::tokenCompare (short inPos, const char* sOther) {

mercurial