2011-10-20 21:20:02 +00:00
|
|
|
|
|
|
|
///Example D:YAML application that displays statistics about YAML documents.
|
|
|
|
|
|
|
|
import std.stdio;
|
|
|
|
import std.string;
|
2018-04-27 03:17:09 +00:00
|
|
|
import dyaml;
|
2011-10-20 21:20:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
///Collects statistics about a YAML document and returns them as string.
|
|
|
|
string statistics(ref Node document)
|
|
|
|
{
|
|
|
|
size_t nodes;
|
|
|
|
size_t scalars, sequences, mappings;
|
|
|
|
size_t seqItems, mapPairs;
|
|
|
|
|
|
|
|
size_t[string] tags;
|
|
|
|
|
|
|
|
void crawl(ref Node root)
|
|
|
|
{
|
|
|
|
++nodes;
|
|
|
|
if((root.tag in tags) is null)
|
|
|
|
{
|
|
|
|
tags[root.tag] = 0;
|
|
|
|
}
|
|
|
|
++tags[root.tag];
|
2019-01-28 01:56:00 +00:00
|
|
|
final switch (root.nodeID)
|
2011-10-20 21:20:02 +00:00
|
|
|
{
|
2019-01-28 01:56:00 +00:00
|
|
|
case NodeID.scalar:
|
|
|
|
++scalars;
|
|
|
|
return;
|
|
|
|
case NodeID.sequence:
|
|
|
|
++sequences;
|
|
|
|
seqItems += root.length;
|
|
|
|
foreach(ref Node node; root)
|
|
|
|
{
|
|
|
|
crawl(node);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case NodeID.mapping:
|
|
|
|
++mappings;
|
|
|
|
mapPairs += root.length;
|
|
|
|
foreach(ref Node key, ref Node value; root)
|
|
|
|
{
|
|
|
|
crawl(key);
|
|
|
|
crawl(value);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case NodeID.invalid:
|
|
|
|
assert(0);
|
2011-10-20 21:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
crawl(document);
|
|
|
|
|
|
|
|
string tagStats = "\nTag statistics:\n";
|
|
|
|
foreach(tag, count; tags)
|
|
|
|
{
|
2012-12-27 21:20:45 +00:00
|
|
|
tagStats ~= format("\n%s : %s", tag, count);
|
2011-10-20 21:20:02 +00:00
|
|
|
}
|
|
|
|
|
2012-12-27 21:20:45 +00:00
|
|
|
return format( "\nNodes: %s" ~
|
|
|
|
"\n\nScalars: %s" ~
|
|
|
|
"\nSequences: %s" ~
|
|
|
|
"\nMappings: %s" ~
|
|
|
|
"\n\nAverage sequence length: %s" ~
|
|
|
|
"\nAverage mapping length: %s" ~
|
2018-04-27 03:17:09 +00:00
|
|
|
"\n\n%s",
|
|
|
|
nodes, scalars, sequences, mappings,
|
2012-12-27 21:20:45 +00:00
|
|
|
sequences == 0.0 ? 0.0 : cast(real)seqItems / sequences,
|
|
|
|
mappings == 0.0 ? 0.0 : cast(real)mapPairs / mappings,
|
|
|
|
tagStats);
|
2011-10-20 21:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void main(string[] args)
|
|
|
|
{
|
|
|
|
//Help message
|
|
|
|
if(args.length == 1)
|
|
|
|
{
|
|
|
|
writeln("Usage: yaml_stats [YAML_FILE ...]\n");
|
|
|
|
writeln("Analyzes YAML files with provided filenames and displays statistics.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Print stats about every document in every file.
|
|
|
|
foreach(file; args[1 .. $])
|
|
|
|
{
|
|
|
|
writeln("\nFile ", file);
|
|
|
|
writeln("------------------------------------------------------------");
|
|
|
|
try
|
|
|
|
{
|
2018-05-31 07:02:21 +00:00
|
|
|
auto loader = Loader.fromFile(file);
|
2011-10-20 21:20:02 +00:00
|
|
|
|
|
|
|
size_t idx = 0;
|
|
|
|
foreach(ref document; loader)
|
|
|
|
{
|
|
|
|
writeln("\nDocument ", idx++);
|
|
|
|
writeln("----------------------------------------");
|
|
|
|
writeln(statistics(document));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(YAMLException e)
|
|
|
|
{
|
|
|
|
writeln("ERROR: ", e.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|