www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - first ddata load attempt

reply "Saaa" <empty needmail.com> writes:
Attached my first attempt to load int and int[] from a D-styled data file.
Adding the functions to support the other types is trivial (hope to be using 
templates :)
but supporting an arbitrary deep array isn't that obvious to me yet.

Please comment :)

ps. it uses std2 because of the possible template use.
dmd 1.041 


begin 666 ddata.zip
M4$L#!!0````(`)L$OSI9=HHW,P```#D````(````9&%T82YD873CY<K,*U'(
M5+!5T#4TLK3FY0(+1,<J)!85`06C%0QU%(P4=(R!T$3'% C-=!3,=2QCK0%0
M2P,$" ``````"0B_. ````````````````8```!D9&%T82]02P,$% ```` `
M#P>_.CB-FW2#`P``N H```T```!D9&%T82]D9&%T82YDG59=;],P%'T>TO[#
M!2$UABX,WE#()(1 0N(!\?%"%*5NXK06F1U<9V-\_7;NM9,XJ;JQ(;51>FV?
M>WSNN7;/==4U`JJ*6QZ[9W)\[_B>/&^UL;"S58Q?;C;)?DSJ6> 9QHQ4F[U 
MJ=6%`VR-O.!60+GE)LNS'(I:-B*9#U!85D)964MA)H.?KEKQ5M4:"HMODP&I
M+!1&7QX`LOJK4/.I??"=BSM6W;J1):RU;F C;"15($C\EC!&(!!;0AS'[/C>
M3X(XDC5$-#=NA-K8+:0IG,*O7S"-G5'V^)Q_9[C )WZ/C+"=45#S9B<2"CQY
M8K>X$5#B$EY_+T5KI5;1 U>8'+ Q_ JLUK"6&]#&O>ZVJ/(#1HM_CT0"R7TZ
M8220ZM975OP7K;<C&G T1ZG1ZAIZ1_BIM1&\W$:D)Y3)A-&87M81W)?J/;=6
M&!5!N815P4]^O#SY<GKR?,6 `4VCN0>XWH*LTF "0$1 2$!,SQ ICC2=,2&E
M^CG8B2,Q.K4G32?*!?9'=X[Q77::D^!D4EFAF^PC-N[-61>F Z,X`G<`-P-E
M^4&H,#P#NVLY/ZM=UU+7BLJ!SNI6T(*4^N.#OHS<2.\V-_+"^\N]GZ503'Q_
MO:UN4Z=:=ZJ:,;'F:N*47H69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D(OSH`````````
M```````&````````````$" ``%D```!D9&%T82]02P$"% `4````" `/![\Z
M.(V;=(,#``"Z" ``#0```````````"  ``!]````9&1A=&$O9&1A=&$N9%!+
M`0(4`!0````(`)($OSKC>CV1! $``.P!```&````````````(" ``"L$``!M
;86EN+F102P4&``````0`! #9````4P4`````
`
end
May 30 2009
parent reply "Saaa" <empty needmail.com> writes:
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;
} 
May 31 2009
parent reply "nobody" <somebody somewhere.com> writes:
Looks interesting, but unfortunately it's still useless to me since it 
doesn't seem to support arrays of varying depth, nor boolean arrays I 
believe.

ps. Isn't this throw statement unreachable? (throw new Exception("Identifier 
not found");)
pps. Since you're going for D-styled, shouldn't booleans be matched not only 
for 'true' but for 'false' as well?

"Saaa" <empty needmail.com> wrote in message 
news:gvveal$107p$1 digitalmars.com...
 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;
 }
 
Jun 01 2009
parent "Saaa" <empty needmail.com> writes:
 Looks interesting, but unfortunately it's still useless to me since it
 doesn't seem to support arrays of varying depth, nor boolean arrays I 
 believe.

 ps. Isn't this throw statement unreachable? (throw new 
 Exception("Identifier not found")
erm.. yes.. should have been commented out.
 pps. Since you're going for D-styled, shouldn't booleans be matched not 
 only for 'true' but for 'false' as well?
But the speed! Maybe I'll add this when I'll add the bool arrays. Why doesn't std2.conv.to parse booleans anyways?
Jun 02 2009