| |
| Posted by Federico"lox" Lucignano | PermalinkReply |
|
Federico"lox" Lucignano
| Hi guys,
I was doing some experiments in D and while experimenting a wrote down this
little xml generator, but when i launch the resulting executable it gives
"Error: Access Violation" at the end of execution (it seems just after return 0
in main() function).
Still I don't know the language very well so I don't know if I'm doing something wrong, can someone help me?
Thnx in advance.
/*
small XML test (compile with the -debug compiler option)
Author: Federico "Lox" Lucignano (federico@alchimiedigitali.net)
Homepage:
Started: Sat, 16 April 2005 12.37.45 GMT
Last Update: Sat, 16 April 2005 12.50.45 GMT
*/
debug import std.stdio;
debug import std.string;
debug import std.process;
debug import std.perf;
debug import std.file;
extern (C)
{
struct Attribute
{
char[] name;
char[] value;
Node* owner;
char[] toString()
{
return name ~ "=\"" ~ value ~ "\"";
}
void dispose()
{
owner = null;
delete name;
name = null;
delete value;
value = null;
return;
}
}
struct Node
{
Attribute*[] attributes;
Node*[] childNodes;
Node* parent;
char[] name;
char[] value;
uint depth()
{
Node* current = parent;
uint result = 0;
while(!(current is null))
{
result++;
current = current.parent;
}
return result;
}
bit appendAttribute(Attribute* a)
{
try
{
a.owner = this;
attributes.length = attributes.length + 1;
attributes[length - 1] = a;
}
catch(Error e)
{
return false;
}
return true;
}
bit appendChild(Node* n)
{
try
{
n.parent = this;
childNodes.length = childNodes.length + 1;
childNodes[length - 1] = n;
}
catch(Error e)
{
return false;
}
return true;
}
char[] print()
{
char[] indentation;
char[] result;
for(uint x = 0; x < depth; x++)
{
indentation ~= "\t";
}
result ~= indentation;
result ~= "<" ~ name;
foreach(Attribute* a; attributes)
{
result ~= " ";
result ~= a.toString();
}
result ~= ">\n";
foreach(Node* n; childNodes)
{
result ~= n.print();
result ~= "\n";
}
if(value.length > 0)
result ~= indentation ~ "\t" ~ value ~ "\n";
result ~= indentation;
result ~= "</" ~ name ~ ">";
return result;
}
void dispose()
{
foreach(Attribute* a; attributes)
{
a.dispose();
delete a;
}
attributes.length = 0;
attributes = null;
foreach(Node* n; childNodes)
{
n.dispose();
delete n;
}
childNodes.length = 0;
childNodes = null;
parent = null;
delete name;
name = null;
delete value;
value = null;
return;
}
}
}
debug
{
int main(char[][] args)
{
uint nodeCount = 0;
uint attCount = 0;
writefln("Generating tree");
PerformanceCounter chrono = new PerformanceCounter();
chrono.start();
Node* n = new Node;
nodeCount++;
n.name = "root";
for(uint x = 0; x < 4; x++)
{
Attribute* a = new Attribute;
a.name = "att" ~ toString(x);
a.value = "val";
n.appendAttribute(a);
a = null;
attCount++;
}
for(uint x = 0; x < 100; x++)
{
Node* nn = new Node;
nn.name = "cNode";
nn.value = "nodeValue";
for(uint y = 0; y < 4; y++)
{
Attribute* a = new Attribute;
a.name = "att" ~ toString(y);
a.value = "val";
nn.appendAttribute(a);
a = null;
attCount++;
}
for(uint y = 0; y < 100; y++)
{
Node* nnn = new Node;
nnn.name = "ccNode";
nnn.value = "Prova prova prova prova prova prova prova prova prova prova prova
prova prova prova";
for(uint z = 0; z < 4; z++)
{
Attribute* a = new Attribute;
a.name = "att" ~ toString(z);
a.value = "val";
nnn.appendAttribute(a);
a = null;
attCount++;
}
nn.appendChild(nnn);
nnn = null;
nodeCount++;
}
n.appendChild(nn);
nn = null;
nodeCount++;
}
chrono.stop();
float nodesPerSec = (cast(float)nodeCount / chrono.microseconds) * 1000000.00;
float attsPerSec = (cast(float)attCount / chrono.microseconds) * 1000000.00;
float entitiesPerSec = (cast(float)nodeCount / chrono.microseconds) *
1000000.00;
float millisecs = cast(float)chrono.microseconds / 1000.00;
float kb = cast(float)n.print().length / 1024.00;
float kbs = (kb / chrono.microseconds) * 1000000.00;
writefln("\nGenerated:");
writefln("\n\t- %d nodes in %f milliseconds (%f nodes/s)", nodeCount, millisecs,
nodesPerSec);
writefln("\n\t- %d attributes in %f milliseconds (%f attributes/s)", attCount,
millisecs, attsPerSec);
writefln("\n\t- %d total entities in %f milliseconds (%f entities/s)", nodeCount
+ attCount, millisecs, nodesPerSec + attsPerSec);
writefln("\n\t- %f Kbytes in %f milliseconds (%f Kb/s)", kb, millisecs, kbs);
writefln("\nWriting result on disk.");
std.file.write("C:\\result.xml", n.print());
n.dispose();
delete n;
delete chrono;
system("PAUSE");
return 0;
}
}
|