Tue, 30 Jul 2013 18:01:53 +0300
Separated LDConfig.ldr-specific stuff to ldconfig.cpp/h
src/colors.cpp | file | annotate | diff | comparison | revisions | |
src/colors.h | file | annotate | diff | comparison | revisions | |
src/ldconfig.cpp | file | annotate | diff | comparison | revisions | |
src/ldconfig.h | file | annotate | diff | comparison | revisions | |
src/misc.cpp | file | annotate | diff | comparison | revisions | |
src/misc.h | file | annotate | diff | comparison | revisions |
--- a/src/colors.cpp Tue Jul 30 17:38:29 2013 +0300 +++ b/src/colors.cpp Tue Jul 30 18:01:53 2013 +0300 @@ -21,12 +21,12 @@ #include "file.h" #include "misc.h" #include "gui.h" -#include <qcolor.h> +#include "ldconfig.h" +#include <QColor> static LDColor* g_LDColors[MAX_COLORS]; -void initColors() -{ +void initColors() { print( "%1: initializing color information.\n", __func__ ); LDColor* col; @@ -47,10 +47,8 @@ } // ============================================================================= -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -// ============================================================================= -LDColor* getColor( short colnum ) -{ +// ----------------------------------------------------------------------------- +LDColor* getColor( short colnum ) { // Check bounds if( colnum < 0 || colnum >= MAX_COLORS ) return null; @@ -59,99 +57,18 @@ } // ============================================================================= +// ----------------------------------------------------------------------------- +void setColor( short colnum, LDColor* col ) { + if( colnum < 0 || colnum >= MAX_COLORS ) + return; + + g_LDColors[colnum] = col; +} + +// ============================================================================= uchar luma( QColor& col ) { return ( 0.2126f * col.red()) + ( 0.7152f * col.green()) + ( 0.0722f * col.blue() ); -} - -// ============================================================================= -// Helper function for parseLDConfig -static bool parseLDConfigTag( StringParser& pars, char const* tag, str& val ) -{ - short pos; - - // Try find the token and get its position - if( !pars.findToken( pos, tag, 1 )) - return false; - - // Get the token after it and store it into val - return pars.getToken( val, pos + 1 ); -} - -// ============================================================================= -void parseLDConfig() -{ - File* f = openLDrawFile( "LDConfig.ldr", false ); - - if( !f ) - { - critical( fmt( QObject::tr( "Unable to open LDConfig.ldr for parsing! (%1)" ), strerror( errno )) ); - delete f; - return; - } - - // Read in the lines - for( str line : *f ) - { - if( line.length() == 0 || line[0] != '0' ) - continue; // empty or illogical - - line.remove( '\r' ); - line.remove( '\n' ); - - // Parse the line - StringParser pars( line, ' ' ); - - short code = 0, alpha = 255; - str name, facename, edgename, valuestr; - - // Check 0 !COLOUR, parse the name - if( !pars.tokenCompare( 0, "0" ) || !pars.tokenCompare( 1, "!COLOUR" ) || !pars.getToken( name, 2 )) - continue; - - // Replace underscores in the name with spaces for readability - name.replace( "_", " " ); - - // Get the CODE tag - if( !parseLDConfigTag( pars, "CODE", valuestr )) - continue; - - if( !isNumber( valuestr )) - continue; // not a number - - // Ensure that the code is within [0 - 511] - bool ok; - code = valuestr.toShort( &ok ); - - if( !ok || code < 0 || code >= 512 ) - continue; - - // VALUE and EDGE tags - if( !parseLDConfigTag( pars, "VALUE", facename ) || !parseLDConfigTag( pars, "EDGE", edgename )) - continue; - - // Ensure that our colors are correct - QColor faceColor( facename ), - edgeColor( edgename ); - - if( !faceColor.isValid() || !edgeColor.isValid() ) - continue; - - // Parse alpha if given. - if( parseLDConfigTag( pars, "ALPHA", valuestr )) - alpha = clamp<short> ( valuestr.toShort(), 0, 255 ); - - LDColor* col = new LDColor; - col->name = name; - col->faceColor = faceColor; - col->edgeColor = edgeColor; - col->hexcode = facename; - col->faceColor.setAlpha( alpha ); - col->index = code; - g_LDColors[code] = col; - } - - delete f; -} +} \ No newline at end of file
--- a/src/colors.h Tue Jul 30 17:38:29 2013 +0300 +++ b/src/colors.h Tue Jul 30 18:01:53 2013 +0300 @@ -32,11 +32,11 @@ }; void initColors(); -void parseLDConfig(); uchar luma (QColor& col); // Safely gets a color with the given number or null if no such color. LDColor* getColor (short colnum); +void setColor( short colnum, LDColor* col ); // Main and edge color identifiers static const short maincolor = 16;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ldconfig.cpp Tue Jul 30 18:01:53 2013 +0300 @@ -0,0 +1,187 @@ +/* + * LDForge: LDraw parts authoring CAD + * Copyright (C) 2013 Santeri Piippo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "file.h" +#include "ldconfig.h" +#include "gui.h" +#include "misc.h" +#include "colors.h" + +// ============================================================================= +// Helper function for parseLDConfig +static bool parseLDConfigTag( LDConfigParser& pars, char const* tag, str& val ) +{ + short pos; + + // Try find the token and get its position + if( !pars.findToken( pos, tag, 1 )) + return false; + + // Get the token after it and store it into val + return pars.getToken( val, pos + 1 ); +} + +// ============================================================================= +void parseLDConfig() +{ + File* f = openLDrawFile( "LDConfig.ldr", false ); + + if( !f ) + { + critical( fmt( QObject::tr( "Unable to open LDConfig.ldr for parsing! (%1)" ), + strerror( errno )) ); + delete f; + return; + } + + // Read in the lines + for( str line : *f ) + { + if( line.length() == 0 || line[0] != '0' ) + continue; // empty or illogical + + line.remove( '\r' ); + line.remove( '\n' ); + + // Parse the line + LDConfigParser pars( line, ' ' ); + + short code = 0, alpha = 255; + str name, facename, edgename, valuestr; + + // Check 0 !COLOUR, parse the name + if( !pars.tokenCompare( 0, "0" ) || !pars.tokenCompare( 1, "!COLOUR" ) || !pars.getToken( name, 2 )) + continue; + + // Replace underscores in the name with spaces for readability + name.replace( "_", " " ); + + // Get the CODE tag + if( !parseLDConfigTag( pars, "CODE", valuestr )) + continue; + + if( !isNumber( valuestr )) + continue; // not a number + + // Ensure that the code is within [0 - 511] + bool ok; + code = valuestr.toShort( &ok ); + + if( !ok || code < 0 || code >= 512 ) + continue; + + // VALUE and EDGE tags + if( !parseLDConfigTag( pars, "VALUE", facename ) || !parseLDConfigTag( pars, "EDGE", edgename )) + continue; + + // Ensure that our colors are correct + QColor faceColor( facename ), + edgeColor( edgename ); + + if( !faceColor.isValid() || !edgeColor.isValid() ) + continue; + + // Parse alpha if given. + if( parseLDConfigTag( pars, "ALPHA", valuestr )) + alpha = clamp<short> ( valuestr.toShort(), 0, 255 ); + + LDColor* col = new LDColor; + col->name = name; + col->faceColor = faceColor; + col->edgeColor = edgeColor; + col->hexcode = facename; + col->faceColor.setAlpha( alpha ); + col->index = code; + setColor( code, col ); + } + + delete f; +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +LDConfigParser::LDConfigParser (str inText, char sep) { + m_tokens = container_cast<QStringList, List<str>> (inText.split (sep, QString::SkipEmptyParts)); + m_pos = -1; +} + +// ----------------------------------------------------------------------------- +bool LDConfigParser::atBeginning () { + return (m_pos == -1); +} + +// ----------------------------------------------------------------------------- +bool LDConfigParser::atEnd () { + return (m_pos == (signed) m_tokens.size () - 1); +} + +// ----------------------------------------------------------------------------- +bool LDConfigParser::getToken (str& val, const ushort pos) { + if (pos >= m_tokens.size()) + return false; + + val = m_tokens[pos]; + return true; +} + +// ----------------------------------------------------------------------------- +bool LDConfigParser::next (str& val) { + return getToken (val, ++m_pos); +} + +// ----------------------------------------------------------------------------- +bool LDConfigParser::peekNext (str& val) { + return getToken (val, m_pos + 1); +} + +// ----------------------------------------------------------------------------- +bool LDConfigParser::findToken (short& result, char const* needle, short args) { + for (ushort i = 0; i < (m_tokens.size () - args); ++i) { + if (m_tokens[i] == needle) { + result = i; + return true; + } + } + + return false; +} + +// ----------------------------------------------------------------------------- +void LDConfigParser::rewind () { + m_pos = -1; +} + +// ----------------------------------------------------------------------------- +void LDConfigParser::seek (short amount, bool rel) { + m_pos = (rel ? m_pos : 0) + amount; +} + +// ----------------------------------------------------------------------------- +size_t LDConfigParser::size () { + return m_tokens.size(); +} + +// ----------------------------------------------------------------------------- +bool LDConfigParser::tokenCompare (short inPos, const char* sOther) { + str tok; + if (!getToken (tok, inPos)) + return false; + + return (tok == sOther); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ldconfig.h Tue Jul 30 18:01:53 2013 +0300 @@ -0,0 +1,55 @@ +/* + * LDForge: LDraw parts authoring CAD + * Copyright (C) 2013 Santeri Piippo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef LDCONFIG_H +#define LDCONFIG_H + +#include "types.h" + +// ============================================================================= +// StringParser +// +// String parsing utility +// ============================================================================= +class LDConfigParser { +public: + LDConfigParser (str inText, char sep); + + bool atEnd (); + bool atBeginning (); + bool next (str& val); + bool peekNext (str& val); + bool getToken (str& val, const ushort pos); + bool findToken (short& result, char const* needle, short args); + size_t size (); + void rewind (); + void seek (short amount, bool rel); + bool tokenCompare (short inPos, const char* sOther); + + str operator[] (const size_t idx) { + return m_tokens[idx]; + } + +private: + List<str> m_tokens; + short m_pos; +}; + +void parseLDConfig(); + +#endif // LDCONFIG_H \ No newline at end of file
--- a/src/misc.cpp Tue Jul 30 17:38:29 2013 +0300 +++ b/src/misc.cpp Tue Jul 30 18:01:53 2013 +0300 @@ -296,77 +296,4 @@ double fval = atof( buf ); delete[] buf; return fval; -} - -// ============================================================================= -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -// ============================================================================= -StringParser::StringParser (str inText, char sep) { - m_tokens = container_cast<QStringList, List<str>> (inText.split (sep, QString::SkipEmptyParts)); - m_pos = -1; -} - -// ----------------------------------------------------------------------------- -bool StringParser::atBeginning () { - return (m_pos == -1); -} - -// ----------------------------------------------------------------------------- -bool StringParser::atEnd () { - return (m_pos == (signed) m_tokens.size () - 1); -} - -// ----------------------------------------------------------------------------- -bool StringParser::getToken (str& val, const ushort pos) { - if (pos >= m_tokens.size()) - return false; - - val = m_tokens[pos]; - return true; -} - -// ----------------------------------------------------------------------------- -bool StringParser::next (str& val) { - return getToken (val, ++m_pos); -} - -// ----------------------------------------------------------------------------- -bool StringParser::peekNext (str& val) { - return getToken (val, m_pos + 1); -} - -// ----------------------------------------------------------------------------- -bool StringParser::findToken (short& result, char const* needle, short args) { - for (ushort i = 0; i < (m_tokens.size () - args); ++i) { - if (m_tokens[i] == needle) { - result = i; - return true; - } - } - - return false; -} - -// ----------------------------------------------------------------------------- -void StringParser::rewind () { - m_pos = -1; -} - -// ----------------------------------------------------------------------------- -void StringParser::seek (short amount, bool rel) { - m_pos = (rel ? m_pos : 0) + amount; -} - -// ----------------------------------------------------------------------------- -size_t StringParser::size () { - return m_tokens.size(); -} - -// ----------------------------------------------------------------------------- -bool StringParser::tokenCompare (short inPos, const char* sOther) { - str tok; - if (!getToken (tok, inPos)) - return false; - - return (tok == sOther); } \ No newline at end of file
--- a/src/misc.h Tue Jul 30 17:38:29 2013 +0300 +++ b/src/misc.h Tue Jul 30 18:01:53 2013 +0300 @@ -103,35 +103,6 @@ b = c; } -// ============================================================================= -// StringParser -// -// String parsing utility -// ============================================================================= -class StringParser { -public: - StringParser (str inText, char sep); - - bool atEnd (); - bool atBeginning (); - bool next (str& val); - bool peekNext (str& val); - bool getToken (str& val, const ushort pos); - bool findToken (short& result, char const* needle, short args); - size_t size (); - void rewind (); - void seek (short amount, bool rel); - bool tokenCompare (short inPos, const char* sOther); - - str operator[] (const size_t idx) { - return m_tokens[idx]; - } - -private: - List<str> m_tokens; - short m_pos; -}; - // ----------------------------------------------------------------------------- // Plural expression template<class T> static inline const char* plural (T n) {