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


M2P,$" ``````"0B_. ````````````````8```!D9&%T82]02P,$% ```` `
M#P

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>

M8K>X$5#B
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*47H69HBS8 W=6`PJ*U"\X*7O?%\2IW%K#O&<>^2DM
M-SM1X(0H&"+4)60* M^<R\_Y9[;"M78T-V');;D])%V08 A;TU'4'0"SHVXH
MES^FCOS!!ZD_0&.K/[K3.F+P!Q:PP&<Q=_:ED5;4C8I6'P4WY19Z -720SG&
MPW%*P/Z]+WPR:7<B \5>#N=H(Y5(O$VF?8_A<%S.T8+0P\3L-(['Y*Y3H*<%

M!H?<O8(TN'K%DB&'4%7(X& (/5FP'IDFW(R;[.%.W*&=#UD$=#FWD5<]HU;-
M,Y<JCA$^9\ .RI+E^TZ]M3S9G>7)[B!/?GMY\GU8_+AV]?]77M*N,,.N;:2-

MS% RM_FP[&!5!S6EFX=IN;%+_.'OY;[ --+HDI.([C)4U=P^;ED</T1Q2BRU
MA^F+$E:^2/T`WA A>)9.D0;AQCV'5J5-A<-N!/"[^ M02P,$% ```` `D 2_
M.N-Z/9$$`0``[ $```8```!M86EN+F1E3TMJPS 07<? .PQ9N!(8+[J,Z:KT




M&*A>'K$>KMVH%>NWCX;G]%*1*J$ "47Q0&$(B40IHW;N- 5BTUHG $I0$%G<
MFAL[FH<]=1#JUBN.7U!+`0(4`!0````(`)L$OSI9=HHW,P```#D````(````
M````````(" ```````!D871A+F1A=%!+`0(4``H```````D(OSH`````````
M```````&````````````$" ``%D```!D9&%T82]02P$"% `4````" `/![\Z

M`0(4`!0````(`)($OSKC>CV1! $``.P!```&````````````(" ``"L$``!M

`
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