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

3.5.56 ‘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, See section string-table-add-ref’ - Add an entry to a string table, get reference, and see section emit-string-table’ - output a string table) will insert text and emit the populated table.

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) ;; redirect output to temporary
   (define ct 1)  +][+

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

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.

If you utilize this in your programming, it is recommended that you prevent printf format usage warnings with the GCC option -Wno-format-contains-nul

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


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

This document was generated by Bruce Korb on August 21, 2015 using texi2html 1.82.