536973dda469a9cb559b640d2cc2006ab58cfcb2 tdreszer Mon Aug 20 12:44:55 2012 -0700 Converted a couple of macros to INLINEs at Mark's request. diff --git src/lib/dyOut.c src/lib/dyOut.c index 8955a3c..2f2bcaa 100644 --- src/lib/dyOut.c +++ src/lib/dyOut.c @@ -1,68 +1,96 @@ // dyOut - DYnamic string stack based OUTput // // This module relies upon a dyString based stack of buffers which accumulate output // for later printing. As a stack, only the top buffer can be filled and flushed out // (duOutFlush). When flushing, the top buffer's content is appended to the next // lower buffer, unless explicitly requested otherwise (dyOutDirectly). // If the stack is empty, dyOutPrintf will be equivalent to printf. // // Output goes to STDOUT unless a single alternative out file is registered. #include "common.h" #include "dyOut.h" // The stack of dyString buffers is only accessible locally static struct dyString *dyOutStack = NULL; -// It is desirable to return a unique token but NOT give access to the dyString it is based on -#define dyOutPointerToToken(pointer) (int)((size_t)(pointer) & 0xffffffff) #define dyOutAssertToken(token) assert(dyOutPointerToToken(dyOutStack) == token) // This module defaults to STDOUT but an alternative can be registered static FILE *dyOutStream = NULL; -#define dyOutStreamInitialize() {if (dyOutStream == NULL) dyOutStream = stdout;} #define dyOutStreamAssertUnregistered() assert(dyOutStream == stdout || dyOutStream == NULL) #define dyOutStreamAssertRegistered(out) assert(dyOutStream == out) +INLINE void dyOutStreamInitialize(void) +// Must initialize the static dyOutStream before use. +{ +if (dyOutStream == NULL) + dyOutStream = stdout; +} + + +INLINE int dyOutPointerToToken(struct dyString *pointer) +// It is desirable to return a unique token but NOT give access to the dyString it is based on +{ +return (int)((size_t)(pointer) & 0xffffffff); +} + + +static struct dyString *dyOutPointerFromToken(int token) +// Return the stack member corresponding to the token. +// This ix can be used to walk up the stack from a given point +{ +struct dyString *ds = dyOutStack; +int ix = 0; + +for (; ds != NULL; ds = ds->next, ++ix) + { + if (dyOutPointerToToken(ds) == token) + return ds; + } +return NULL; +} + + int dyOutOpen(int initialBufSize) // Allocate dynamic string and puts at top of stack // returns token to be used for later stack accounting asserts { if (initialBufSize <= 0) initialBufSize = 2048; // presume large struct dyString *ds = dyStringNew(initialBufSize); slAddHead(&dyOutStack,ds); return dyOutPointerToToken(ds); } int dyOutPrintf(char *format, ...) // Prints into end of the top ds buffer, and return resulting string length // If there is no current buffer, this acts like a simple printf and returns -1 { int len = -1; // caller could assert returned length > 0 to ensure dyOut is open! va_list args; va_start(args, format); if (dyOutStack != NULL) { dyStringVaPrintf(dyOutStack, format, args); len = dyStringLen(dyOutStack); } else { - dyOutStreamInitialize() + dyOutStreamInitialize(); vfprintf(dyOutStream,format, args); } va_end(args); return len; } int dyOutPrintDirectly(int token,FILE *file) // Prints the contents of the top buffer directly to a file. // Will leave the filled buffer at top of stack // Returns the length printed. { dyOutAssertToken(token); int len = dyStringLen(dyOutStack); @@ -153,46 +181,30 @@ return dyStringContents(dyOutStack); } int dyOutEmpty(int token) // Empties the top buffer in stack (abandoning content), but leave buffer in place // Returns the length abandoned. { dyOutAssertToken(token); int len = dyStringLen(dyOutStack); dyStringClear(dyOutStack); return len; } -static struct dyString *dyOutPointerFromToken(int token) -// Return the stack member corresponding to the token. -// This ix can be used to walk up the stack from a given point -{ -struct dyString *ds = dyOutStack; -int ix = 0; - -for (; ds != NULL; ds = ds->next, ++ix) - { - if (dyOutPointerToToken(ds) == token) - return ds; - } -return NULL; -} - - int dyOutSize(int token) // Returns the current length of the buffer starting at the level corresponding to token. // Unlike other cases, the token does not have to correspond to the top of the stack! { struct dyString *dsTarget = dyOutPointerFromToken(token); assert(dsTarget != NULL); int len = dyStringLen(dsTarget); struct dyString *ds = dyOutStack; for (; ds != dsTarget && ds != NULL; ds = ds->next) // assertable != NULL, but still len += dyStringLen(ds); return len; }