1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/*
$Id: resource_tokenizer.cpp,v 1.5 2001/11/02 02:24:09 plasmoid Exp $
------------------------------------------------------------------------
ClanLib, the platform independent game SDK.
This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
version 2. See COPYING for details.
For a total list of contributers see CREDITS.
------------------------------------------------------------------------
*/
#include "Core/precomp.h"
#include "resource_tokenizer.h"
#include <API/Core/IOData/inputsource.h>
#include <API/Core/System/cl_assert.h>
#include <API/Core/System/clanstring.h>
CL_ResourceTokenizer::CL_ResourceTokenizer(std::string filename, CL_InputSource *input)
{
this->filename = filename;
cur_pos = 0;
cur_line = 1;
in_remark = false;
cl_assert(input != NULL);
total_size = input->size();
config_file_data = new unsigned char[total_size];
input->read(config_file_data, total_size);
}
CL_ResourceTokenizer::~CL_ResourceTokenizer()
{
delete[] config_file_data;
}
bool CL_ResourceTokenizer::is_operator(char c)
{
return
c == ',' ||
c == '=' ||
c == '(' ||
c == ')' ||
c == ';' ||
c == '{' ||
c == '}';
}
bool CL_ResourceTokenizer::is_space(char c)
{
return
c == ' ' ||
c == '\t' ||
c == 13 ||
c == 10;
}
bool CL_ResourceTokenizer::is_remark(char c, int position)
{
if (in_remark)
{
if (c == '\n')
{
in_remark = false;
}
return true;
}
else
{
if (c == '/')
{
if (
position+1 < total_size &&
config_file_data[position+1] == '/')
{
in_remark = true;
return true;
}
}
else if (c == '#')
{
in_remark = true;
return true;
}
}
return false;
}
std::string CL_ResourceTokenizer::get_next_token()
{
while (
cur_pos < total_size &&
(
is_remark(config_file_data[cur_pos], cur_pos) ||
is_space(config_file_data[cur_pos])
))
{
if (config_file_data[cur_pos] == '\n') cur_line++;
cur_pos++;
}
if (cur_pos == total_size) return "";
if (is_operator(config_file_data[cur_pos]))
{
char temp[2];
temp[0] = config_file_data[cur_pos];
temp[1] = 0;
std::string ret(temp);
cur_pos++;
return ret;
}
else
{
int temp_pos = cur_pos;
if (config_file_data[temp_pos] == '"')
{
int num_chars = 0;
temp_pos++;
while (temp_pos < total_size)
{
if (config_file_data[temp_pos] == '"')
{
if (temp_pos+1>=total_size || config_file_data[temp_pos+1] != '"') break;
else
{
temp_pos++;
}
}
temp_pos++;
num_chars++;
}
if (temp_pos == total_size)
{
write_error("Missing '""' following '""'-begin");
return "";
}
char *temp = new char[num_chars+1];
temp[num_chars] = 0;
int ofs = 1;
for (int i=0;i<num_chars;i++)
{
temp[i] = config_file_data[cur_pos+ofs+i];
if (config_file_data[cur_pos+ofs+i] == '"')
{
ofs++;
}
}
cur_pos = temp_pos+1;
std::string ret(temp);
delete [] temp;
return ret;
}
else
{
while (temp_pos < total_size &&
!is_space(config_file_data[temp_pos]) &&
!is_operator(config_file_data[temp_pos]) &&
!is_remark(config_file_data[temp_pos], cur_pos))
{
temp_pos++;
}
int tok_size = temp_pos-cur_pos;
char *temp = new char[tok_size+1];
temp[tok_size] = 0;
memcpy(temp, &config_file_data[cur_pos], tok_size);
cur_pos += tok_size;
std::string ret(temp);
delete [] temp;
return ret;
}
}
}
std::string CL_ResourceTokenizer::write_error(std::string err_msg)
{
CL_String err;
err << "Error occured during read of config file '" <<
filename.c_str() << "' (line " << cur_line << "): " << err_msg.c_str();
return err.get_string();
}
|