Posts Tagged ‘FlexibleBinaryFormat’

Flexible Binary Format

Another project I worked on a long time ago but never released. The goal of Flexible Binary Format was to create a file format and library for dealing with (tree) structured binary data (in the same vein of what XML does for textual data). This was a reaction both to the verbose and non-binary-friendly nature of XML and to the ugly chunk-based structuring done in many binary file formats.

The code for the C++ library is now up.

I’ll do a high-level, quick-and-dirty tutorial in this post to show how things work.


include FBF.h

#include "FBF.h"


Build a data tree

FlexibleBinaryFormat::DataTree        dt;
FlexibleBinaryFormat::TreeBuilder    dataTreeBuilder(&dt);


Attach a node to the data tree

dataTreeBuilder.AttachNode("root/vertex");


Make a vector of nodes and sub-nodes

dataTreeBuilder.AttachNode("root/vertex");
dataTreeBuilder.AttachNode(
"root/vertex");
dataTreeBuilder.AttachNode("root/vertex[0]/xyz");
dataTreeBuilder.AttachNode(
"root/vertex[1]/abc");

Note: We created 2 nodes on the same path (root/vertex). We access them individually using an index number within brackets.


Attach data to nodes

dataTreeBuilder.AttachDouble("root/vertex[0]/xyz", 0.12345);
dataTreeBuilder.AttachDouble(
"root/vertex[1]/abc", 54321.0);

Once data is attached to a node, you can’t attach a sub-node to it.
(i.e. data is stored on the leaves of the data tree)

The following methods of FlexibleBinaryFormat::TreeBuilder are available to attach data to a node:

  • AttachString()
  • AttachBool()
  • AttachByte()
  • AttachSByte()
  • AttachDouble()
  • AttachFloat()
  • AttachShort()
  • AttachUShort()
  • AttachInt()
  • AttachUInt()
  • AttachInt()
  • AttachUInt()
  • AttachLong()
  • AttachULong()


Write the data tree out to a file

FlexibleBinaryFormat::FlexibleIO    flexio(&dt);
flexio.WriteToFile(
"test.fbf");


Read a data tree from a file

FlexibleBinaryFormat::DataTree        dt2;
FlexibleBinaryFormat::FlexibleIO    flexio2(&dt2);
flexio2.ReadFromFile(
"test.fbf");


Read data from a node

double v0xyz = dt2.GetData<double>("root/vertex[0]/xyz", 0);
// second arg is always 0;
// arg is a leftover from some deprecated stuff in older versions and will be removed