Added an example application that displays statistics about YAML
documents.
This commit is contained in:
parent
d5a063930d
commit
f726ff0b94
5
examples/yaml_stats/Makefile
Normal file
5
examples/yaml_stats/Makefile
Normal file
|
@ -0,0 +1,5 @@
|
|||
main:
|
||||
dmd -w -I../../ -L-L../../ -L-ldyaml yaml_stats.d
|
||||
|
||||
clean:
|
||||
rm yaml_stats myaml_stats.o
|
4
examples/yaml_stats/small.yaml
Normal file
4
examples/yaml_stats/small.yaml
Normal file
|
@ -0,0 +1,4 @@
|
|||
- 1
|
||||
- 2 : 'a'
|
||||
3 : 'b'
|
||||
- 4 : [1.0, 2.1, 3.2]
|
104
examples/yaml_stats/yaml_stats.d
Normal file
104
examples/yaml_stats/yaml_stats.d
Normal file
|
@ -0,0 +1,104 @@
|
|||
|
||||
///Example D:YAML application that displays statistics about YAML documents.
|
||||
|
||||
import std.stdio;
|
||||
import std.string;
|
||||
import yaml;
|
||||
|
||||
|
||||
///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];
|
||||
|
||||
if(root.isScalar)
|
||||
{
|
||||
++scalars;
|
||||
return;
|
||||
}
|
||||
if(root.isSequence)
|
||||
{
|
||||
++sequences;
|
||||
seqItems += root.length;
|
||||
foreach(ref Node node; root)
|
||||
{
|
||||
crawl(node);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(root.isMapping)
|
||||
{
|
||||
++mappings;
|
||||
mapPairs += root.length;
|
||||
foreach(ref Node key, ref Node value; root)
|
||||
{
|
||||
crawl(key);
|
||||
crawl(value);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
crawl(document);
|
||||
|
||||
string tagStats = "\nTag statistics:\n";
|
||||
foreach(tag, count; tags)
|
||||
{
|
||||
tagStats ~= format("\n", tag, " : ", count);
|
||||
}
|
||||
|
||||
return format( "\nNodes: ", nodes,
|
||||
"\n\nScalars: ", scalars,
|
||||
"\nSequences: ", sequences,
|
||||
"\nMappings: ", mappings,
|
||||
"\n\nAverage sequence length: ", cast(real)seqItems / sequences,
|
||||
"\nAverage mapping length: ", cast(real)mapPairs / mappings,
|
||||
"\n\n", tagStats);
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
auto loader = Loader(file);
|
||||
|
||||
size_t idx = 0;
|
||||
foreach(ref document; loader)
|
||||
{
|
||||
writeln("\nDocument ", idx++);
|
||||
writeln("----------------------------------------");
|
||||
writeln(statistics(document));
|
||||
}
|
||||
}
|
||||
catch(YAMLException e)
|
||||
{
|
||||
writeln("ERROR: ", e.msg);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue