Subject: Re: Re: [ast-developers] Allocate string space with sfio string 
streams?
--------

> see stk(3) and stkopen() that retruns an Sfio_t*
> call sfputr() sfputc() sfwrite() sfprintf() to add to the stack
> and stktell() to get the offset of each item appended to the stack
> and stkptr() to get the actual pointers
> ksh93 uses uses stk*() and stak*() throughout the code
> src/cmd/ksh93/*/*.c -- grep for stak[a-z]*(
> stak*() is a stk*() variant that write to one global stack
> 
> or see sfio.h and sfstropen() that returns an Sfio_t*
> the sfstr*() function macros are not defined in sfio(3) -- this will be fixed 
> th
> is summer
> libast and nmake use sfstr*() throughout the code
> src/cmd/nmake/*.c -- grep for sfstropen()...sfstrclose() pairs (across 
> multiple 
> lines)
> 
> stk*() is typically used for manipulating a collection of related items
> sfstr*() is typically used for manipulating one item, typically a path
> 
> 

Let me say a little more about stk(3).  As stated, stkopen() that returns an
Sfio_t*.  Objects can be built on the stack and then frozen.  Once
frozen their address can be used to reference the object.

Only the top object on the stack is relocatable and therefore must be
addressed by offset until frozen.

        sp = stkopen();         /* create a stack */
        sfprintf(sp,...);       /* use printf to write onto the stack */
        ptr = stkfreeze(sp,1);  /* freeze this object adding a 1 nul byte
                                   and return the address */
        ptr2 = stkalloc(sp,size);       /* return a pointer to a region 
        stkcopy(sp,"foo");
        stkcopy(sp,"bar");
        ptr3 = stkfreeze(sp,1); /* ptr3 will point to "foobar"
        stkclose(sp);
will remove all items from the stack and free up the space.

        Other stack operations get the current offset with the top object
        so that you can call stkptr(offset) to get a pointer the the
        current object and stkset() which allows you to free up all
        objects passed the base and offset which you specify.

Currently the stk() library is not thread safe. 

David Korn
d...@research.att.com
_______________________________________________
ast-developers mailing list
ast-developers@research.att.com
https://mailman.research.att.com/mailman/listinfo/ast-developers

Reply via email to