Tinkering with macros.
authorscottmk <scottmk@9efc2488-bf62-4759-914b-345cdb29e865>
Tue, 6 Jan 2009 16:01:41 +0000 (16:01 +0000)
committerscottmk <scottmk@9efc2488-bf62-4759-914b-345cdb29e865>
Tue, 6 Jan 2009 16:01:41 +0000 (16:01 +0000)
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

index dd2be90..cb639a0 100644 (file)
@@ -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 */