Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

StringTokenizer.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * ストリングトークナイザ実装
00022  * @author Junpee
00023  */
00024 
00025 #include "LampBasic.h"
00026 #include "Core/Utility/StringTokenizer.h"
00027 #include "Core/System/StringMethod.h"
00028 
00029 namespace Lamp{
00030 
00031 // デフォルトデリミタ
00032 const String StringTokenizer::defaultDelimiter(" \t\n\r\f");
00033 
00034 //------------------------------------------------------------------------------
00035 // コンストラクタ
00036 StringTokenizer::StringTokenizer() :
00037     base_(NULL), current_(NULL), tokenOffset_(0), delimiterOutput_(false){
00038 }
00039 //------------------------------------------------------------------------------
00040 // 初期化
00041 void StringTokenizer::initialize(const char* target,
00042      const String& delimiter, bool delimiterOutput){
00043      base_ = current_ = target;
00044      tokenOffset_ = 0;
00045      delimiter_ = delimiter;
00046      delimiterOutput_ = delimiterOutput;
00047      nowToken_ = "";
00048      previousToken_ = "";
00049 }
00050 //------------------------------------------------------------------------------
00051 // 初期化
00052 void StringTokenizer::initialize(const String& target,
00053     const String& delimiter, bool delimiterOutput){
00054     targetString_ = target;
00055     initialize(target.getBytes(), delimiter, delimiterOutput);
00056 }
00057 //------------------------------------------------------------------------------
00058 // デストラクタ
00059 StringTokenizer::~StringTokenizer(){
00060 }
00061 //------------------------------------------------------------------------------
00062 // トークンがまだあるかどうか
00063 bool StringTokenizer::hasMoreTokens(){
00064     if(current_ == NULL){ return false; }
00065     if(delimiterOutput_){ return ((*current_) != '\0'); }
00066     while(true){
00067         if((*current_) == '\0'){ return false; }
00068         int index = (int)StdStrcspn(current_, delimiter_.getBytes());
00069         if(index != 0){ break; }
00070         current_ = StdStrinc(current_);
00071     }
00072     return true;
00073 }
00074 //------------------------------------------------------------------------------
00075 // 次のトークン取得
00076 String StringTokenizer::getNextToken(){
00077     Assert((*current_) != '\0');
00078     tokenOffset_ = (int)(current_ - base_);
00079     // デリミタ出力
00080     int index = (int)StdStrcspn(current_, delimiter_.getBytes());
00081     if(delimiterOutput_ && (index == 0)){
00082         const char* prevCurrent = current_;
00083         current_ = StdStrinc(current_);
00084         int length = (int)(current_ - prevCurrent);
00085         char* newString = new char[length + 1];
00086         StdStrncpy(newString, prevCurrent, length);
00087         newString[length] = '\0';
00088         previousToken_ = nowToken_;
00089         nowToken_ = newString;
00090         delete[] newString;
00091         return nowToken_;
00092     }
00093     char* newString = new char[index + 1];
00094     StdStrncpy(newString, current_, index);
00095     newString[index] = '\0';
00096     previousToken_ = nowToken_;
00097     nowToken_ = newString;
00098     delete[] newString;
00099     current_ += index;
00100     return nowToken_;
00101 }
00102 //------------------------------------------------------------------------------
00103 } // End of namespace Lamp
00104 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:36 2005 for Lamp by doxygen 1.3.2