first ddata load attempt
Saaa
empty at needmail.com
Sun May 31 19:34:13 PDT 2009
Templated !!
This is the first time I use templates.
please comment
module ddata.ddata;
import std.stdarg;
import std.stdio;
import std.regexp;
import std2.string;
import std2.traits;
import std2.conv;
private char[][] _file;
private char[] _identifier;
private TypeInfo _type;
private int _row;
private char[] _token;
private int _tokenLen;
public bool Get(T)(in char[][] file, in char[] identifier, ref T var)
{
if (file.length == 0 || file.length > int.max)
{
return false;
}
if (identifier.length == 0 || identifier.length > ubyte.max)
{
return false;
}
RegExp myRegExp = new RegExp("_A-Za-z[_A-Za-z0-9]*");
if ( cast(bool) myRegExp.test(identifier) )
{
return false;
}
_file = file;
_identifier = identifier;
_type = typeid(T);
_row = getRow();
if (_row < 0 || _row >= _file.length)
{
return false;
throw new Exception("Identifier not found");
}
try
{
static if( std2.traits.isNumeric!(T))
{
Parse(var);
return true;
}
static if( typeid(T) is typeid(bool) )
{
parse_bool(var);
return true;
}
static if( std2.traits.isDynamicArray!(T) )
{
Parse_array(var);
return true;
}
return false;
}
catch
{
return false;
}
return true;
}
private int getRow()
{
_token = _type.toString() ~ ' ' ~ _identifier;
writefln(`Search token =`,_token);
_tokenLen = _token.length;
foreach(int row, char[] line; _file)
{
if(line.length > _token.length)
{
if(line[0.._tokenLen] == _token) return row;
}
}
return -1;
}
private void Parse(T)(ref T parsed)
{
uint begin = qcFind( _tokenLen, '=') + 1;
if( begin == -1) throw new Exception(` = not found`);
uint end = qcFind( begin, ';');
if( end == -1) throw new Exception(` ; not found`);
parsed = to!(T)( strip( _file[_row][begin..end]) );
writefln(parsed);
return;
}
private void parse_bool(ref bool parsed)
{
uint begin = qcFind( _tokenLen, '=') + 1;
if( begin == -1) throw new Exception(` = not found`);
uint end = qcFind( begin, ';');
if( end == -1) throw new Exception(` ; not found`);
parsed = (strip( _file[_row][begin..end]) == `true`);
writefln(parsed);
return;
}
private void Parse_array(T:U[],U)(ref T parsed)
{
uint begin = qcFind( _tokenLen, '[') + 1;
if( begin == -1) throw new Exception(` [ not found`);
uint end = qcFind( begin, ']');
if( end == -1) throw new Exception(`] not found`);
auto stringArray = std.string.split(_file[_row][begin..end], `,`);
T array;
array.length = stringArray.length;
foreach(int i, char[] string; stringArray)
{
array[i] = to!(U)(strip(string));
}
parsed = array;
return;
}
private int qcFind(in int start,in char c)
{
int location = std.string.find( _file[_row][start..$], c) + start;
if( location <= start || location >= _file[_row].length )
{
return -1;
}
return location;
}
More information about the Digitalmars-d-learn
mailing list