2008-03-10 04:08:33 +01:00
|
|
|
// Copyright (c) 2008, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
2008-03-13 22:28:22 +01:00
|
|
|
// AEGISUB/GORGONSUB
|
2008-03-10 04:08:33 +01:00
|
|
|
//
|
|
|
|
// Website: http://www.aegisub.net
|
|
|
|
// Contact: mailto:amz@aegisub.net
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "tokenizer.h"
|
|
|
|
#include "exception.h"
|
2008-03-15 07:21:11 +01:00
|
|
|
#include "utils.h"
|
2008-03-10 04:08:33 +01:00
|
|
|
#include <wx/tokenzr.h>
|
2008-03-13 22:28:22 +01:00
|
|
|
using namespace Gorgonsub;
|
2008-03-10 04:08:33 +01:00
|
|
|
|
|
|
|
///////////////
|
|
|
|
// Constructor
|
2008-03-15 07:21:11 +01:00
|
|
|
Tokenizer::Tokenizer(const String &_string,wxChar _token,size_t start)
|
|
|
|
: string(_string), pos(start), token(_token)
|
2008-03-10 04:08:33 +01:00
|
|
|
{
|
2008-03-15 07:21:11 +01:00
|
|
|
str = const_cast<wxChar*> (string.c_str());
|
2008-03-10 04:08:33 +01:00
|
|
|
}
|
|
|
|
Tokenizer::~Tokenizer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////
|
|
|
|
// Has more
|
2008-03-15 07:21:11 +01:00
|
|
|
bool Tokenizer::HasMoreTokens()
|
2008-03-10 04:08:33 +01:00
|
|
|
{
|
2008-03-15 07:21:11 +01:00
|
|
|
return pos < string.Length();
|
2008-03-10 04:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
// Get position
|
|
|
|
int Tokenizer::GetPosition()
|
|
|
|
{
|
2008-03-15 07:21:11 +01:00
|
|
|
return (int) pos;
|
2008-03-10 04:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////
|
|
|
|
// Get token
|
2008-03-12 20:28:24 +01:00
|
|
|
String Tokenizer::GetString(bool trim)
|
2008-03-10 04:08:33 +01:00
|
|
|
{
|
2008-03-15 07:21:11 +01:00
|
|
|
// Has any more?
|
2008-03-18 03:09:33 +01:00
|
|
|
if (!HasMoreTokens()) THROW_GORGON_EXCEPTION(Exception::Invalid_Token);
|
2008-03-15 07:21:11 +01:00
|
|
|
|
|
|
|
// Find token
|
|
|
|
size_t len = string.Length();
|
|
|
|
size_t oldPos = pos;
|
|
|
|
bool found = false;
|
|
|
|
for (size_t i=pos;i<len;i++) {
|
|
|
|
if (str[i] == token) {
|
|
|
|
found = true;
|
|
|
|
str[i] = 0;
|
|
|
|
pos = i+1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) pos = len;
|
|
|
|
|
|
|
|
// Trimmed
|
2008-03-12 20:28:24 +01:00
|
|
|
if (trim) {
|
2008-03-15 07:21:11 +01:00
|
|
|
return StringPtrTrim(str+oldPos,pos-oldPos-1,0);
|
2008-03-12 20:28:24 +01:00
|
|
|
}
|
2008-03-15 07:21:11 +01:00
|
|
|
|
|
|
|
// Untrimmed
|
|
|
|
return wxString(str+oldPos,pos-oldPos-1);
|
2008-03-10 04:08:33 +01:00
|
|
|
}
|
2008-03-15 07:21:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
//////////////////
|
|
|
|
// Get as integer
|
2008-03-10 04:08:33 +01:00
|
|
|
int Tokenizer::GetInt()
|
|
|
|
{
|
2008-03-15 07:21:11 +01:00
|
|
|
return StringToInt(GetString());
|
2008-03-10 04:08:33 +01:00
|
|
|
}
|
2008-03-15 07:21:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
// Get as float
|
2008-03-10 04:08:33 +01:00
|
|
|
float Tokenizer::GetFloat()
|
|
|
|
{
|
|
|
|
double value;
|
2008-03-15 07:21:11 +01:00
|
|
|
wxString temp = GetString();
|
2008-03-10 04:08:33 +01:00
|
|
|
temp.ToDouble(&value);
|
|
|
|
return (float) value;
|
|
|
|
}
|
2008-03-15 07:21:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
//////////////////
|
|
|
|
// Get as boolean
|
|
|
|
bool Tokenizer::GetBool()
|
2008-03-10 04:08:33 +01:00
|
|
|
{
|
2008-03-15 07:21:11 +01:00
|
|
|
return GetInt() != 0;
|
2008-03-10 04:08:33 +01:00
|
|
|
}
|
2008-03-15 07:21:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////
|
|
|
|
// Gets all remaining string data
|
|
|
|
String Tokenizer::GetTheRest()
|
2008-03-10 04:08:33 +01:00
|
|
|
{
|
2008-03-15 07:21:11 +01:00
|
|
|
// Make string
|
|
|
|
size_t size = string.Length()-pos;
|
|
|
|
size_t oldPos = pos;
|
|
|
|
pos += size;
|
|
|
|
return String(str+oldPos,size);
|
2008-03-10 04:08:33 +01:00
|
|
|
}
|