Fast, easy serialization in C
You can use tpl to serialize your C data quickly and easily.
Compared to using XML, tpl is simpler, faster and "more natural"
to use in C programs.
Storing an integer array | Reloading an integer array |
---|---|
#include "tpl.h" int main() { tpl_node *tn; int i; tn = tpl_map( "A(i)", &i ); for( i=0; i<10; i++ ) { tpl_pack( tn, 1 ); } tpl_dump( tn, TPL_FILE, "demo.tpl" ); tpl_free( tn ); } |
#include "tpl.h" int main() { tpl_node *tn; int i; tn = tpl_map( "A(i)", &i ); tpl_load( tn, TPL_FILE, "demo.tpl" ); while (tpl_unpack( tn, 1 ) > 0) { printf("%d ", i); } tpl_free( tn ); } |
Your data, your data format
Just express the type of data you are working with as a tpl format string. For
example, suppose you're writing a CAD program. A 2D polygon could be expressed
A(ff); a list of polygons could be expressed A(A(ff)). Map
these format characters to your C variables, then pack or unpack your data.
Full native binary efficiency
Using tpl, you get the full efficiency of using your data in its native binary
representation. This makes it feasible to deal with very large tpl images (up
to 4GB) efficiently. But portability is not sacrified; byte order conversion is
transparent and automatic when needed. Tpl can serialize most C data types
including structures:
Serializing a structure | Deserializing a structure |
---|---|
#include "tpl.h" struct ms_t { int i; char c[3]; double f; }; int main() { tpl_node *tn; struct ms_t ms = {1, {'a','b','c'}, 3.14}; tn = tpl_map( "S(ic#f)", &ms, 3); tpl_pack( tn, 0 ); tpl_dump( tn, TPL_FILE, "struct.tpl" ); tpl_free( tn ); } |
#include "tpl.h" struct ms_t { int i; char c[3]; double f; }; int main() { tpl_node *tn; struct ms_t ms; tn = tpl_map( "S(*)", &ms); tpl_load( tn, TPL_FILE, "struct.tpl" ); tpl_unpack( tn, 0 ); tpl_free( tn ); } |
Tpl groks XML
Tpl can even be reversibly converted to XML:
% tplxml struct.tpl > struct.xml % tplxml struct.xml > struct.tpl
Tpl speaks Perl
Besides the C API, tpl has a
Perl API to make it easy to share data between C and
Perl or write client-server programs.
No library dependencies
Tpl does not make your software dependent on any libraries. You can compile its
source code (one file) right into your program.
Is it a file format?
Yes and no. A tpl image is a binary entity that can be written into a file, a
memory buffer or to a file descriptor. Your program can use tpl as its file
format. You can also use tpl as a message format for message-passing programs.
Unix, Mac, Windows
Tpl works on Linux, Solaris, OpenBSD, Mac OS X and on Windows using MinGW or Cygwin.
For more information
For a more thorough explanation and more examples, please read the
User Guide.