From: <juv...@us...> - 2009-07-27 20:56:28
|
Revision: 2257 http://paintown.svn.sourceforge.net/paintown/?rev=2257&view=rev Author: juvinious Date: 2009-07-27 20:56:17 +0000 (Mon, 27 Jul 2009) Log Message: ----------- Cleaned up MugenReader, modularizing line reads and added ability to read in from a vector of text. Removed copied code in Mugen::Util and made font use MugenReader. Modified Paths: -------------- trunk/src/mugen/mugen_font.cpp trunk/src/mugen/mugen_reader.cpp trunk/src/mugen/mugen_reader.h trunk/src/mugen/mugen_util.cpp trunk/src/mugen/mugen_util.h Modified: trunk/src/mugen/mugen_font.cpp =================================================================== --- trunk/src/mugen/mugen_font.cpp 2009-07-27 20:41:14 UTC (rev 2256) +++ trunk/src/mugen/mugen_font.cpp 2009-07-27 20:56:17 UTC (rev 2257) @@ -285,7 +285,9 @@ ourText.push_back(line); } - std::vector< MugenSection * > collection = Mugen::Util::configReader( ourText ); + MugenReader reader( ourText ); + + std::vector< MugenSection * > collection = reader.getCollection(); for( unsigned int i = 0; i < collection.size(); ++i ){ const std::string &head = collection[i]->getHeader(); Modified: trunk/src/mugen/mugen_reader.cpp =================================================================== --- trunk/src/mugen/mugen_reader.cpp 2009-07-27 20:41:14 UTC (rev 2256) +++ trunk/src/mugen/mugen_reader.cpp 2009-07-27 20:56:17 UTC (rev 2257) @@ -11,12 +11,8 @@ using namespace std; -// Check for Clsn -static bool checkClsn( const std::string &clsn ){ - return ( clsn.find("Clsn") != std::string::npos ); -} - -MugenReader::MugenReader( const char * file ){ +MugenReader::MugenReader( const char * file ): +usingText(false){ Global::debug(1) << "[mugen reader] Opening file '" << file << "'" << endl; ifile.open( file ); if (!ifile){ @@ -25,7 +21,8 @@ myfile = string( file ); } -MugenReader::MugenReader( const string & file ){ +MugenReader::MugenReader( const string & file ): +usingText(false){ Global::debug(1) << "[mugen reader] Opening file '" << file << "'" << endl; ifile.open( file.c_str() ); if (!ifile){ @@ -34,8 +31,15 @@ myfile = file; } +MugenReader::MugenReader( const std::vector<std::string> &text ): +usingText(false){ + configData = text; +} + MugenReader::~MugenReader(){ - ifile.close(); + if(!usingText){ + ifile.close(); + } for( std::vector< MugenSection * >::iterator i = collection.begin() ; i != collection.end() ; ++i ){ if(*i)delete (*i); @@ -44,143 +48,189 @@ } const std::vector< MugenSection * > & MugenReader::getCollection() throw(MugenException){ - - if ( !ifile ){ - throw MugenException( std::string("Could not open file: ") + myfile ); - } - - std::string line; - - const char openbracket = '['; - const char closebracket = ']'; - const char comment = ';'; - const char seperator = ','; - const char colon = ':'; - const char equal = '='; - const char quote = '"'; - const char * ignored = "\r\n"; - - SearchState state = Section; - // Our section and items - MugenSection * sectionHolder = 0; - // Marker for areas - bool beginSection = false; - - while( !ifile.eof() ){ - getline( ifile, line ); - // Use to hold section or content in the options - std::string contentHolder = ""; - // Place holder to put back all the grabbed content - MugenItemContent *itemHolder = new MugenItemContent(); - // Needed to kill loop - bool breakLoop = false; - bool inQuote = false; - bool getExpression = false; - bool startComment = false; - - Global::debug(0) << line << endl; - - for( unsigned int i = 0; i < line.size(); ++i ){ - // Go to work - switch( state ){ - case Section:{ - // Done with this - if( line[i] == comment ){ - breakLoop = true; - break; - } - else if ( line[i] == ' ' && !beginSection )continue; - //Start grabbing our section - else if( line[i] == openbracket){ - beginSection = true; - } - //End of our section store and go to ContentGet - else if( line[i] == closebracket){ - sectionHolder = new MugenSection(); - sectionHolder->setHeader( contentHolder ); - state = ContentGet; - beginSection = false; - breakLoop = true; - break; - } - else if( beginSection )contentHolder += line[i]; - } - break; - case ContentGet: - default:{ - // Done with this - if( line[i] == comment ){ - startComment = true; - } - // Check if we are near the end to kill it - if( i+1 == line.size() && !contentHolder.empty() ){ - if (line[i] != ' ' && !strchr(ignored, line[i])){ - contentHolder += line[i]; - } - *itemHolder << contentHolder; - *sectionHolder << itemHolder; - breakLoop = true; - break; - } - // Buh bye spaces - if( line[i] == ' ' && !inQuote ){ - continue; - } - // Start getting expression - if( line[i] != ' ' && getExpression ){ - inQuote = true; - } - // Check if this section is done, push it on the stack and reset everything - if( line[i] == openbracket && !checkClsn( contentHolder ) && !itemHolder->hasItems() ){ - addSection( sectionHolder ); - beginSection = true; - state = Section; - contentHolder = ""; - break; - } - // We got one push back the other and reset the holder to get the next - else if( line[i] == colon || line[i] == seperator ){ - if( !contentHolder.empty() && !strchr(ignored, contentHolder[0]) ){ - *itemHolder << contentHolder; - } - contentHolder = ""; - } - // Equal - else if(line[i] == equal && !inQuote ){ - if( !contentHolder.empty() ) *itemHolder << contentHolder; - getExpression = true; - contentHolder = ""; - } - // We has a quote begin or end it - else if( line[i] == quote ){ - inQuote = !inQuote; - } - //Start grabbing our item - else if (!startComment && !strchr(ignored, line[i])){ - contentHolder += line[i]; - } - } - break; - } - if( breakLoop )break; - } - if (state == ContentGet){ - if( itemHolder->hasItems() ){ - if( !contentHolder.empty() && !strchr(ignored, contentHolder[0]) ){ - *itemHolder << contentHolder; - } - *sectionHolder << itemHolder; - } + if ( configData.empty() && !ifile ){ + throw MugenException( std::string("Could not open file: \"") + myfile + std::string("\" or Read Text!") ); + } + + // get lines + std::vector<std::string> temp; + if (ifile && !usingText){ + std::string line; + while( !ifile.eof() ){ + std::string line; + getline( ifile, line ); + temp.push_back(line); } - } - // Add in last section - addSection( sectionHolder ); - - return collection; + } else { + temp = configData; + } + + for( std::vector<std::string>::const_iterator i = temp.begin(); i != temp.end(); ++i ){ + // get line + const std::string line = *i; + + if (isSection(line)){ + // Grab section and add it to the stack + addSection(extractSection(line)); + } else if (hasContent(line)){ + // Extract the content add it to the stack + addContent(extractContent(line)); + } + } + return collection; } void MugenReader::addSection( MugenSection * section ){ - collection.push_back ( section ); + collection.push_back ( section ); } +void MugenReader::addContent( MugenItemContent *content ){ + *collection.back() << content; +} +bool MugenReader::isSection( const std::string &line ){ + const char comment = ';'; + const char openbracket = '['; + const char closebracket = ']'; + bool foundSection = false; + int charCount = 0; + + for (unsigned int i = 0; i < line.size(); ++i){ + if ( line[i] == ' ' && !foundSection ){ + continue; + } else if ( line[i] == openbracket){ + // Check if we have characters if so we might have clsn + if (charCount){ + return false; + } + // Found start of a section + foundSection = true; + } else if ( foundSection && line[i] == closebracket){ + // Found close of section it's a section + return true; + } else if ( line[i] == comment ){ + // oops comment before a closebracket, there is no section + return false; + } else { + // Found something other than a bracket this isn't a section + charCount++; + } + } + // No section here + return false; +} + +bool MugenReader::hasContent( const std::string &line ){ + const char comment = ';'; + const char * eol = "\r\n"; + bool foundContent = false; + for (unsigned int i = 0; i < line.size(); ++i){ + if ( line[i] == ' ' ){ + continue; + } else if ( !foundContent && line[i] == comment ){ + // oops comment before content, there is no content + return false; + } else if ( !foundContent && strchr(eol, line[i]) ){ + // Found end of line and nothing else, no content + return false; + } else { + // There's something usefull allow to be parsed + return true; + } + } + // No content here + return false; +} + +MugenSection *MugenReader::extractSection( const std::string &line ){ + Global::debug(1) << line << endl; + const char comment = ';'; + const char openbracket = '['; + const char closebracket = ']'; + bool foundSection = false; + std::string contentHolder = ""; + + for (unsigned int i = 0; i < line.size(); ++i){ + if ( line[i] == ' ' && !foundSection ){ + continue; + } else if ( line[i] == openbracket){ + // Found start of a section + foundSection = true; + } else if ( foundSection && line[i] == closebracket){ + // Found close of section it's a section make a new one and return it + MugenSection *temp = new MugenSection(); + temp->setHeader( contentHolder ); + return temp; + } else if ( line[i] == comment ){ + // oops shouldn't happen, but return 0 anyhow + return 0; + } else { + // Grab our section name + contentHolder += line[i]; + } + } + + return 0; +} + +MugenItemContent *MugenReader::extractContent( const std::string &line ){ + Global::debug(1) << line << endl; + const char openbracket = '['; + const char closebracket = ']'; + const char comment = ';'; + const char seperator = ','; + const char colon = ':'; + const char equal = '='; + const char quote = '"'; + const char * ignored = "\r\n"; + + // Use to hold content + std::string contentHolder = ""; + // Place holder to put grabbed content + MugenItemContent *itemHolder = new MugenItemContent(); + + bool inQuote = false; + + for (unsigned int i = 0; i < line.size(); ++i){ + if (line[i] == ' ' && !inQuote){ + continue; + } else if (line[i] == comment){ + // found comment... store what remains and get out + if( !contentHolder.empty() ){ + *itemHolder << contentHolder; + } + return itemHolder; + } else if ( strchr(ignored, line[i]) ){ + // Found end of line.. as above store what remains and get out + if( !contentHolder.empty() ){ + *itemHolder << contentHolder; + } + return itemHolder; + } else if( line[i] == equal && !inQuote ){ + if( !contentHolder.empty() ){ + *itemHolder << contentHolder; + } + contentHolder = ""; + } else if( line[i] == quote ){ + inQuote = !inQuote; + } else if( line[i] == colon || line[i] == seperator ){ + // Grab an item + if( !contentHolder.empty() ){ + *itemHolder << contentHolder; + } + contentHolder = ""; + } else { + contentHolder+=line[i]; + } + } + + if (!contentHolder.empty()){ + *itemHolder << contentHolder; + } + + if (itemHolder->hasItems()){ + return itemHolder; + } + + return 0; +} Modified: trunk/src/mugen/mugen_reader.h =================================================================== --- trunk/src/mugen/mugen_reader.h 2009-07-27 20:41:14 UTC (rev 2256) +++ trunk/src/mugen/mugen_reader.h 2009-07-27 20:56:17 UTC (rev 2257) @@ -7,11 +7,13 @@ #include "mugen_exception.h" class MugenSection; +class MugenItemContent; class MugenReader{ public: MugenReader( const std::string & file ); MugenReader( const char * file ); + MugenReader( const std::vector<std::string> &text ); ~MugenReader(); @@ -22,6 +24,11 @@ std::ifstream ifile; std::string myfile; + // in case we are using text + std::vector<std::string> configData; + + bool usingText; + std::vector< MugenSection * >collection; enum SearchState{ @@ -30,6 +37,17 @@ }; void addSection( MugenSection * section ); + + void addContent( MugenItemContent *content ); + + bool isSection( const std::string &line ); + + bool hasContent( const std::string &line ); + + MugenSection *extractSection( const std::string &line ); + + MugenItemContent *extractContent( const std::string &line ); + }; #endif Modified: trunk/src/mugen/mugen_util.cpp =================================================================== --- trunk/src/mugen/mugen_util.cpp 2009-07-27 20:41:14 UTC (rev 2256) +++ trunk/src/mugen/mugen_util.cpp 2009-07-27 20:56:17 UTC (rev 2257) @@ -500,144 +500,6 @@ ifile.close(); } -// Check for Clsn -static bool checkClsn( const std::string &clsn ){ - return ( clsn.find("Clsn") != std::string::npos ); -} - -enum SearchState{ - Section = 0, - ContentGet -}; - -std::vector< MugenSection * > Mugen::Util::configReader( const std::vector<std::string> &configdata ){ - - std::vector< MugenSection * > collection; - std::string line; - - const char openbracket = '['; - const char closebracket = ']'; - const char comment = ';'; - const char seperator = ','; - const char colon = ':'; - const char equal = '='; - const char quote = '"'; - const char * ignored = "\r\n"; - - SearchState state = Section; - // Our section and items - MugenSection * sectionHolder = 0; - // Marker for areas - bool beginSection = false; - - for( std::vector<std::string>::const_iterator i = configdata.begin(); i != configdata.end(); ++i ){ - line = *i; - // Use to hold section or content in the options - std::string contentHolder = ""; - // Place holder to put back all the grabbed content - MugenItemContent *itemHolder = new MugenItemContent(); - // Needed to kill loop - bool breakLoop = false; - bool inQuote = false; - bool getExpression = false; - - for( unsigned int i = 0; i < line.size(); ++i ){ - // Go to work - switch( state ){ - case Section:{ - // Done with this - if( line[i] == comment ){ - breakLoop = true; - break; - } - else if ( line[i] == ' ' && !beginSection )continue; - //Start grabbing our section - else if( line[i] == openbracket){ - beginSection = true; - } - //End of our section store and go to ContentGet - else if( line[i] == closebracket){ - sectionHolder = new MugenSection(); - sectionHolder->setHeader( contentHolder ); - state = ContentGet; - beginSection = false; - breakLoop = true; - break; - } - else if( beginSection )contentHolder += line[i]; - } - break; - case ContentGet: - default:{ - // Done with this - if( line[i] == comment ){ - if( itemHolder->hasItems() ){ - if( !contentHolder.empty() ){ - *itemHolder << contentHolder; - } - *sectionHolder << itemHolder; - } - breakLoop = true; - break; - } - // Check if we are near the end to kill it - if( i+1 == line.size() && !contentHolder.empty() ){ - if (line[i] != ' ' && !strchr(ignored, line[i])){ - contentHolder += line[i]; - } - *itemHolder << contentHolder; - *sectionHolder << itemHolder; - breakLoop = true; - break; - } - // Buh bye spaces - if( line[i] == ' ' && !inQuote ){ - continue; - } - // Start getting expression - if( line[i] != ' ' && getExpression ){ - inQuote = true; - } - // Check if this section is done, push it on the stack and reset everything - if( line[i] == openbracket && !checkClsn( contentHolder ) && !itemHolder->hasItems() ){ - if( sectionHolder->hasItems() )collection.push_back( sectionHolder ); - beginSection = true; - state = Section; - contentHolder = ""; - break; - } - // We got one push back the other and reset the holder to get the next - else if( line[i] == colon || line[i] == seperator ){ - if( !contentHolder.empty() ) *itemHolder << contentHolder; - contentHolder = ""; - } - // Equal - else if(line[i] == equal && !inQuote ){ - if( !contentHolder.empty() ) *itemHolder << contentHolder; - getExpression = true; - contentHolder = ""; - } - // We has a quote begin or end it - else if( line[i] == quote ){ - inQuote = !inQuote; - } - //Start grabbing our item - else if (! strchr(ignored, line[i])){ - contentHolder += line[i]; - } - } - break; - } - if( breakLoop )break; - } - } - // Add in last section - if( sectionHolder->hasItems() )collection.push_back( sectionHolder ); - - return collection; - -} - MugenBackground *Mugen::Util::getBackground( const unsigned long int &ticker, MugenSection *section, std::map< unsigned int, std::map< unsigned int, MugenSprite * > > &sprites ){ MugenBackground *temp = new MugenBackground(ticker); std::string head = section->getHeader(); Modified: trunk/src/mugen/mugen_util.h =================================================================== --- trunk/src/mugen/mugen_util.h 2009-07-27 20:41:14 UTC (rev 2256) +++ trunk/src/mugen/mugen_util.h 2009-07-27 20:56:17 UTC (rev 2257) @@ -28,8 +28,6 @@ bool readPalette(const std::string &filename, unsigned char *pal); void readSprites(const std::string & filename, const std::string & palette, std::map<unsigned int, std::map<unsigned int, MugenSprite *> > & sprites) throw (MugenException); void readSounds(const std::string & filename, std::map<unsigned int,std::map<unsigned int, MugenSound *> > & sounds) throw (MugenException); - // Reads and compiles collections from a string rather than using a file ( You will need to delete the MugenSection's independently if you use this - std::vector< MugenSection * > configReader( const std::vector<std::string> &configdata ); // Get background: The background must be deleted if used outside of stage/menus (Note: we give the background a ticker to whatever is running it) MugenBackground *getBackground( const unsigned long int &ticker, MugenSection *section, std::map< unsigned int, std::map< unsigned int, MugenSprite * > > &sprites ); // Get animation: The animation must be deleted if used outside of stage/animation (stage and character do the deletion in this case) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |