📄 textconfiguration.cpp
字号:
// ___ ___ ___ ___ ___ // /\__\ ___ /\__\ /\ \ /\__\ /\ \. // /::| | /\ \ /::| | /::\ \ /:/ / /::\ \. // /:|:| | \:\ \ /:|:| | /:/\:\ \ /:/ / /:/\:\ \. // /:/|:|__|__ /::\__\ /:/|:| |__ /:/ \:\ \ /:/ / /::\~\:\ \. // /:/ |::::\__\ __/:/\/__/ /:/ |:| /\__\ /:/__/_\:\__\ /:/__/ /:/\:\ \:\__\.// \/__/~~/:/ / /\/:/ / \/__|:|/:/ / \:\ /\ \/__/ \:\ \ \:\~\:\ \/__/// /:/ / \::/__/ |:/:/ / \:\ \:\__\ \:\ \ \:\ \:\__\. // /:/ / \:\__\ |::/ / \:\/:/ / \:\ \ \:\ \/__/ // /:/ / \/__/ /:/ / \::/ / \:\__\ \:\__\. // \/__/ \/__/ \/__/ \/__/ \/__/ // // =============================================================================// Minimalist OpenGL Environment// =============================================================================//// This file is part of Minimalist OpenGL Environment (MinGLE)//// Version: 0.2.0// Author: Balazs Domonkos// Filename: src/configuration/src/TextConfiguration.cpp// Creation Date: January 27th 2008// Revision Date: January 27th 2008////// The Minimalist OpenGL Environment 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.//// The Minimalist OpenGL Environment 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 Minimalist OpenGL Environment. If not, see // <http://www.gnu.org/licenses/>. See the COPYING file included // with this distribution file for license details.///// @file TextConfiguration.cpp/// Text configuration file support// Header that contains the declaration#include <TextConfiguration.h>// System headers#include <stack>namespace MinGLE {const std::string TextConfiguration::DEFAULT_COMMENT_CHARS = "#";const std::string TextConfiguration::DEFAULT_SEPARATOR_CHARS = "=";const std::string TextConfiguration::DEFAULT_BLOCK_BEGIN_CHARS = "{";const std::string TextConfiguration::DEFAULT_BLOCK_END_CHARS = "}";TextConfiguration::TextConfiguration(const std::string& filename, VirtualFileSystem *vfs) : mCommentChars(DEFAULT_COMMENT_CHARS), mSeparatorChars(DEFAULT_SEPARATOR_CHARS), mBeginChars(DEFAULT_BLOCK_BEGIN_CHARS), mEndChars(DEFAULT_BLOCK_END_CHARS) { // Parse textfile _parseTextFile(filename, vfs);}void TextConfiguration::_parseTextFile(const std::string& filename, VirtualFileSystem *vfs) { // Open configuration text file VirtualFileSystem::FileHandle f = vfs->open(filename, "rt"); // Stack of entries // (the topmost entry is the active entry) typedef std::stack<Entry *> EntryStack; EntryStack entryStack; entryStack.push(mRootEntry); // Parse the file while (!vfs->eof(f)) { // Read a line std::string line = vfs->readLine(f); // Variable needed for processing TokenType token; std::string beforeToken; // Variable for storing the name std::string name; while (_findToken(line, token, beforeToken)) { bool abort = false; switch (token) { case COMMENT: // Abort processing this line abort = true; break; case SEPARATOR: // Store the before token part as name name = beforeToken; break; case BEGIN: // If a name does not exist define it as the before-token if (!name.size()) name = beforeToken; // Add a new entry with the defined name entryStack.push(_addChild(entryStack.top(), name)); break; case END: // Root element cannot be dropped Assert(entryStack.size() > 1, "TextConfiguration::_parseTextFile"); // Step up one level entryStack.pop(); break; } // Stop processing line on abort if (abort) break; } // If a name is defined add the remainder of the line as value if (token == SEPARATOR && name.size()) (void) _addChild(entryStack.top(), name, line); // The stack should not be emtpy Assert(!entryStack.empty(), "TextConfiguration::_parseTextFile"); } // Close configuration text file vfs->close(f); // Only the root element should be in the stack Assert(entryStack.size() == 1 && entryStack.top() == mRootEntry, "TextConfiguration::_parseTextFile");}bool TextConfiguration::_findToken(std::string& line, TokenType& token, std::string& beforeToken) { // Find positions std::string::size_type commentPos = line.find_first_of(mCommentChars); std::string::size_type separatorPos = line.find_first_of(mSeparatorChars); std::string::size_type beginPos = line.find_first_of(mBeginChars); std::string::size_type endPos = line.find_first_of(mEndChars); // Comment ('#') if (commentPos != std::string::npos && commentPos < separatorPos && commentPos < beginPos && commentPos < endPos) { beforeToken = line.substr(0, commentPos); line = line.substr(commentPos+1); token = COMMENT; } // Separator ('=') else if (separatorPos != std::string::npos && separatorPos < commentPos && separatorPos < beginPos && separatorPos < endPos) { beforeToken = line.substr(0, separatorPos); line = line.substr(separatorPos+1); token = SEPARATOR; } // Begin ('{') else if (beginPos != std::string::npos && beginPos < commentPos && beginPos < separatorPos && beginPos < endPos) { beforeToken = line.substr(0, beginPos); line = line.substr(beginPos+1); token = BEGIN; } // End ('}') else if (endPos != std::string::npos && endPos < commentPos && endPos < separatorPos && endPos < beginPos) { beforeToken = line.substr(0, endPos); line = line.substr(endPos+1); token = END; } // Nothing is found else return false; // Trim the before-token-part and the after-token-part StringUtils::trim(beforeToken); StringUtils::trim(line); return true;}// =============================================================================const std::string TextConfigurationManager::FUNCTIONALITY_STRING = "text";TextConfigurationManager::~TextConfigurationManager() { // Nothing to do}void TextConfigurationManager::registerConfigurationManager() { ConfigurationManager::registerImplementation(TextConfigurationManager::FUNCTIONALITY_STRING, new TextConfigurationManager()); ConfigurationManager::registerFunctionality(FileFormatFunctionality("cfg", FileFormatFunctionality::READ), TextConfigurationManager::FUNCTIONALITY_STRING);}Configuration *TextConfigurationManager::_createConfiguration( const std::string& filename, VirtualFileSystem *vfs) { return new TextConfiguration(filename, vfs);}} // namespace MinGLE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -