/*
* StringTokenizer.h
* Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// SOL++2000
#pragma once
#include <sol\Analyzer.h>
#include <sol\String.h>
#include <sol\StringBuffer.h>
namespace SOL {
class StringTokenizer :public Analyzer {
const TCHAR* string;
int num;
TCHAR separator[5];
private:
void defaultSeparator()
{
this->num = 0;
this->separator[num++] = (TCHAR)' ';
this->separator[num++] = (TCHAR)'\t';
this->separator[num++] = (TCHAR)',';
}
public:
StringTokenizer()
:num(0), string(null)
{
defaultSeparator();
}
public:
StringTokenizer(const TCHAR* str)
:num(0), string(str)
{
defaultSeparator();
}
public:
StringTokenizer(const String& str)
:num(0), string((const TCHAR*)str)
{
defaultSeparator();
}
public:
StringTokenizer(const StringBuffer& buffer)
:num(0), string((const TCHAR*)buffer)
{
defaultSeparator();
}
public:
bool addSeparator(TCHAR s)
{
bool rc = false;
if(num < sizeof(separator)) {
separator[num++] = s;
rc = true;
}
return rc;
}
public:
bool hasMoreToken()
{
bool rc = false;
// 1999.09.23 Added *string != Zero
if (string != null && *string != (TCHAR)0) {
rc = true;
}
return rc;
}
public:
void clearSeparator()
{
num = 0;
}
public:
bool isSeparator(TCHAR s)
{
bool rc = false;
for(int i = 0; i< num; i++) {
if(separator[i] == s) {
rc = true;
break;
}
}
return rc;
}
public:
const TCHAR* getToken(String& token)
{
if (string != null) {
StringBuffer buffer;
string = getToken(buffer);
token = buffer.getBuffer();
}
return string;
}
public:
const TCHAR* getToken(StringBuffer& buffer)
{
int quotation = 0;
const TCHAR* p = this->string;
if(p == null || *p == (TCHAR)Zero) {
return null;
}
while(*p !=Zero && isSeparator(*p) == true) {
p++;
}
while(*p != Zero) {
if(*p == (TCHAR)'"' && *(p+1) == (TCHAR)'"') {
p += 2;
break;
}
if(*p == (TCHAR)'"') {
p++;
quotation++;
}
if(isSeparator(*p) == true && quotation == 2) {
break;
}
if(isSeparator(*p) == true && quotation == 0) {
break;
}
buffer.append(*p++);
}
return p;
}
public:
const TCHAR* getToken(TCHAR* token, int size)
{
if (this->string != null) {
this->string = getToken(this->string, token, size);
}
return string;
}
public:
const TCHAR* getToken(const TCHAR* p, TCHAR* first, int size)
{
if(*p == Zero) {
return NULL;
}
while(*p !=Zero && isSeparator(*p) == true) {
p++;
}
int len = 1;
int quotation = 0;
while(*p != (TCHAR)Zero) {
if(*p == '"' && *(p+1) == '"') {
p += 2;
break;
}
if(*p == (TCHAR)'"') {
p++;
quotation++;
}
if(isSeparator(*p) == true && quotation == 2) {
break;
}
if(isSeparator(*p) == true && quotation == 0) {
break;
}
if(len < size) {
*first++ = *p++;
len++;
}
else {
break;
}
}
*first = Zero;
return p;
}
public:
const TCHAR* getInteger(const TCHAR* line, int* value)
{
TCHAR word[10];
const TCHAR* ptr = getToken(line, word, sizeof(word));
*value = atoi(word);
return ptr;
}
public:
int hexStringToInt(const TCHAR* str)
{
int n =0;
if(*str++ =='0') {
if (*str == (TCHAR)'x') {
_stscanf_s(++str, _T("%x"), &n);
}
else if (*str == (TCHAR)'X') {
_stscanf_s(++str, _T("%X"), &n);
}
}
return n;
}
};
}
|