From d4548cfb987a8846d0d5ddb99a99a9dc840ea90e Mon Sep 17 00:00:00 2001 From: scottmk Date: Tue, 6 Jan 2009 16:01:41 +0000 Subject: [PATCH] Tinkering with macros. 1. In OSRF_BUFFER_ADD, OSRF_BUFFER_ADD_CHAR, and OSRF_BUFFER_RESET: eliminated multiple evaluations of macro arguments. 2. In OSRF_BUFFER_ADD: renamed local variable __tl to _tl, since identifiers beginning with two underscores are reserved. 3. In OSRF_BUFFER_RESET: applied the do/while(0) trick so that the macro will be work as intended when subject to an "if". 4. Added new macro OSRF_BUFFER_C_STR to return a const pointer to the internal buffer of a growing_buffer. This new macro will enable safe and direct access to the buffer contents without violating encapsulation and without incurring a malloc and free. git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1572 9efc2488-bf62-4759-914b-345cdb29e865 --- include/opensrf/utils.h | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/include/opensrf/utils.h b/include/opensrf/utils.h index dd2be90..cb639a0 100644 --- a/include/opensrf/utils.h +++ b/include/opensrf/utils.h @@ -56,13 +56,15 @@ GNU General Public License for more details. #define OSRF_BUFFER_ADD(gb, data) \ do {\ - int __tl; \ - if(gb && data) {\ - __tl = strlen(data) + gb->n_used;\ - if( __tl < gb->size ) {\ - strcpy( gb->buf + gb->n_used, data ); \ - gb->n_used = __tl; \ - } else { buffer_add(gb, data); }\ + int _tl; \ + growing_buffer* _gb = gb; \ + const char* _data = data; \ + if(_gb && _data) {\ + _tl = strlen(_data) + _gb->n_used;\ + if( _tl < _gb->size ) {\ + strcpy( _gb->buf + _gb->n_used, _data ); \ + _gb->n_used = _tl; \ + } else { buffer_add(_gb, _data); }\ }\ } while(0) @@ -83,21 +85,26 @@ GNU General Public License for more details. #define OSRF_BUFFER_ADD_CHAR(gb, c)\ do {\ - if(gb) {\ - if(gb->n_used < gb->size - 1) {\ - gb->buf[gb->n_used++] = c;\ - gb->buf[gb->n_used] = '\0';\ + growing_buffer* _gb = gb;\ + char _c = c;\ + if(_gb) {\ + if(_gb->n_used < _gb->size - 1) {\ + _gb->buf[_gb->n_used++] = _c;\ + _gb->buf[_gb->n_used] = '\0';\ }\ else\ - buffer_add_char(gb, c);\ + buffer_add_char(_gb, _c);\ }\ }while(0) #define OSRF_BUFFER_RESET(gb) \ - memset(gb->buf, 0, gb->size);\ - gb->n_used = 0; + do {\ + growing_buffer* _gb = gb;\ + memset(_gb->buf, 0, _gb->size);\ + _gb->n_used = 0;\ + }while(0) - +#define OSRF_BUFFER_C_STR( x ) ((const char *) (x)->buf) /* turns a va_list into a string */ -- 2.11.0