D-styled data file
Saaa
empty at needmail.com
Mon Apr 27 17:01:19 PDT 2009
I would like to be able to read and write variables which are human
readable.
It should be as fast as possible and easy to use.
Something like this:
-- file.dat
char[] header = `test header`;
int[6] numbers = [ 1, 2, 3, 4, 5, 6];
// not sure about the array layout yet
// the following layout makes human reading a lot easier if you'd had
something like float[10][10][10]
// also comments, or any line not starting with a type is ignored
bool[][][] map =
[
[
[true, true],
[false, false],
[false, false]
],
[true, true],
[false, false],
[false, false]
]
]
bool map = bool; // this is ignored to speed up finding a variable
--
-- main.d
module main;
import std.stdio;
import std.file;
void main()
{
char[][] file = read (`file.dat`);
//I'm not sure how this part should work :)
TypeFile file_TF = getTF (file);
// a TypeFile holds known information about the file
// list of : name, type, line number
// It also holds a copy of the file but maybe there can be a less safe
version
// getTF_unsafe(file);
// which only keeps a reference to the file.
char[] name = file_TF.header; //probably needs a better function name
//parses only the requested variable
//a lot of room for optimizations (indexing until found etc.)
writefln(file_TFnumbers.length); // prints 6
int[] numbers = file_TF.numbers;
numbers[3] = 30;
file_TF.numbers = numbers;
//updates the copy of the char[][]
write(file.dat, file_TF.stringof); // the only keyword
//comments in the original file would be preserved this way
}
--
Any comments, questions or pointers?
Maybe all this is already available?
I would think that the parsing code is already available somewhere.
More information about the Digitalmars-d-learn
mailing list