[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.5.52 `string-table-new' - create a string table

Usage: (string-table-new st-name)
This function will create an array of characters. The companion functions, (See section `string-table-add' - Add an entry to a string table, and see section `emit-string-table' - output a string table) will insert text and emit the populated table, respectively.

With these functions, it should be much easier to construct structures containing string offsets instead of string pointers. That can be very useful when transmitting, storing or sharing data with different address spaces.

Here is a brief example copied from the strtable.test test:

 
[+ (string-table-new "scribble")
   (out-push-new)
   (define ix 0)
   (define ct 1)  +][+

FOR str IN that was the week that was +][+
  (set! ct (+ ct 1))
  (set! ix (string-table-add "scribble" (get "str")))
+]
    scribble + [+ (. ix) +],[+
ENDFOR  +]
    NULL };
[+ (out-suspend "main")
   (emit-string-table "scribble")
   (ag-fprintf 0 "\nchar const *ap[%d] = {" ct)
   (out-resume "main")
   (out-pop #t) +]

Some explanation:

I added the (out-push-new) because the string table text is diverted into an output stream named, "scribble" and I want to have the string table emitted before the string table references. The string table references are also emitted inside the FOR loop. So, when the loop is done, the current output is suspended under the name, "main" and the "scribble" table is then emitted into the primary output. (emit-string-table inserts its output directly into the current output stream. It does not need to be the last function in an AutoGen macro block.) Next I ag-fprintf the array-of-pointer declaration directly into the current output. Finally I restore the "main" output stream and (out-pop #t)-it into the main output stream.

Here is the result. Note that duplicate strings are not repeated in the string table:

 
static char const scribble[18] =
    "that\0" "was\0"  "the\0"  "week\0";

char const *ap[7] = {
    scribble + 0,
    scribble + 5,
    scribble + 9,
    scribble + 13,
    scribble + 0,
    scribble + 5,
    NULL };

These functions use the global name space stt-* in addition to the function names.

Arguments:
st-name - the name of the array of characters


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Bruce Korb on September, 30 2006 using texi2html 1.76.

Viewable With Any Browser   autogen Home