[Opensrf-commits] r1572 - trunk/include/opensrf
svn at svn.open-ils.org
svn at svn.open-ils.org
Tue Jan 6 11:01:43 EST 2009
Author: scottmk
Date: 2009-01-06 11:01:41 -0500 (Tue, 06 Jan 2009)
New Revision: 1572
Modified:
trunk/include/opensrf/utils.h
Log:
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.
Modified: trunk/include/opensrf/utils.h
===================================================================
--- trunk/include/opensrf/utils.h 2009-01-05 17:50:15 UTC (rev 1571)
+++ trunk/include/opensrf/utils.h 2009-01-06 16:01:41 UTC (rev 1572)
@@ -56,13 +56,15 @@
#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 @@
#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 */
More information about the opensrf-commits
mailing list