[Opensrf-commits] r1519 - in trunk: include/opensrf src/libopensrf
svn at svn.open-ils.org
svn at svn.open-ils.org
Fri Dec 5 14:29:30 EST 2008
Author: miker
Date: 2008-12-05 14:29:27 -0500 (Fri, 05 Dec 2008)
New Revision: 1519
Modified:
trunk/include/opensrf/utils.h
trunk/src/libopensrf/utils.c
Log:
Patch from Scott McKellar:
-- Adds a new macro buffer_length to return the n_used member of a
growing buffer.
-- Changed buffer_chomp() to return the character removed instead of
the new string length.
-- Removed the newly obsolete buffer_append_uescape function.
-- In utils.c: removed a commented-out version of the
OSRF_BUFFER_ADD_CHAR macro, which has long resided in utils.h.
-- In the OSRF_BUFFER_ADD macro: replaced strcat() with strcpy(), as
I had done earlier in the corresponding function buffer_add().
Modified: trunk/include/opensrf/utils.h
===================================================================
--- trunk/include/opensrf/utils.h 2008-12-04 17:59:07 UTC (rev 1518)
+++ trunk/include/opensrf/utils.h 2008-12-05 19:29:27 UTC (rev 1519)
@@ -60,7 +60,7 @@
if(gb && data) {\
__tl = strlen(data) + gb->n_used;\
if( __tl < gb->size ) {\
- strcat(gb->buf, data);\
+ strcpy( gb->buf + gb->n_used, data ); \
gb->n_used = __tl; \
} else { buffer_add(gb, data); }\
}\
@@ -185,6 +185,8 @@
};
typedef struct growing_buffer_struct growing_buffer;
+#define buffer_length(x) (x)->n_used
+
growing_buffer* buffer_init( int initial_num_bytes);
// XXX This isn't defined in utils.c!! removing for now...
@@ -197,7 +199,7 @@
char* buffer_release( growing_buffer* gb );
int buffer_free( growing_buffer* gb );
int buffer_add_char(growing_buffer* gb, char c);
-int buffer_chomp(growing_buffer* gb); // removes the last character from the buffer
+char buffer_chomp(growing_buffer* gb); // removes the last character from the buffer
/* returns the size needed to fill in the vsnprintf buffer.
* ! this calls va_end on the va_list argument*
Modified: trunk/src/libopensrf/utils.c
===================================================================
--- trunk/src/libopensrf/utils.c 2008-12-04 17:59:07 UTC (rev 1518)
+++ trunk/src/libopensrf/utils.c 2008-12-05 19:29:27 UTC (rev 1519)
@@ -16,9 +16,6 @@
#include <opensrf/log.h>
#include <errno.h>
-static const char hex_chars[] = "0123456789abcdef";
-static unsigned char hex_code[7] = "\\u00";
-
inline void* safe_malloc( int size ) {
void* ptr = (void*) malloc( size );
if( ptr == NULL ) {
@@ -284,28 +281,17 @@
return strdup( gb->buf );
}
-int buffer_chomp(growing_buffer* gb) {
- if( gb == NULL ) { return -1; }
- if(gb->n_used > 0) {
+char buffer_chomp(growing_buffer* gb) {
+ char c = '\0';
+ if(gb && gb->n_used > 0) {
gb->n_used--;
+ c = gb->buf[gb->n_used];
gb->buf[gb->n_used] = '\0';
}
- return gb->n_used;
+ return c;
}
-/*
-#define OSRF_BUFFER_ADD_CHAR(gb, c)\
- do {\
- if(gb) {\
- if(gb->n_used < gb->size - 1)\
- gb->buf[gb->n_used++] = c;\
- else\
- buffer_add_char(gb, c);\
- }\
- }while(0)
- */
-
int buffer_add_char(growing_buffer* gb, char c ) {
if(gb && gb->buf) {
@@ -427,113 +413,7 @@
return buffer_release(buf);
}
-int buffer_append_uescape( growing_buffer* buf, const char* string ) {
- if(NULL == string)
- return 0; // Nothing to add? Nothing to do
-
- if( NULL == buf )
- return -1; // Nothing to add to
-
- int idx = 0;
- unsigned long int c;
-
- while (string[idx]) {
-
- c = 0x0;
-
- if ((unsigned char)string[idx] >= 0x80) { // not ASCII
-
- if ((unsigned char)string[idx] >= 0xC0 && (unsigned char)string[idx] <= 0xF4) { // starts a UTF8 string
-
- int clen = 1;
- if (((unsigned char)string[idx] & 0xF0) == 0xF0) {
- clen = 3;
- c = (unsigned char)string[idx] ^ 0xF0;
-
- } else if (((unsigned char)string[idx] & 0xE0) == 0xE0) {
- clen = 2;
- c = (unsigned char)string[idx] ^ 0xE0;
-
- } else if (((unsigned char)string[idx] & 0xC0) == 0xC0) {
- clen = 1;
- c = (unsigned char)string[idx] ^ 0xC0;
- }
-
- for (;clen;clen--) {
-
- idx++; // look at the next byte
- c = (c << 6) | ((unsigned char)string[idx] & 0x3F); // add this byte worth
- }
-
- buffer_fadd(buf, "\\u%04x", c);
-
- } else {
- return idx + 1;
- }
-
- } else if(string[idx] >= ' ' ) { // printable ASCII character
-
- c = string[idx];
- switch(c) {
- case '"':
- case '\\':
- OSRF_BUFFER_ADD_CHAR(buf, '\\');
- OSRF_BUFFER_ADD_CHAR(buf, c);
- break;
-
- default:
- OSRF_BUFFER_ADD_CHAR(buf, c);
- }
-
- } else {
- c = string[idx];
-
- /* escape the usual suspects */
- switch(c) {
- case '\b':
- OSRF_BUFFER_ADD_CHAR(buf, '\\');
- OSRF_BUFFER_ADD_CHAR(buf, 'b');
- break;
-
- case '\f':
- OSRF_BUFFER_ADD_CHAR(buf, '\\');
- OSRF_BUFFER_ADD_CHAR(buf, 'f');
- break;
-
- case '\t':
- OSRF_BUFFER_ADD_CHAR(buf, '\\');
- OSRF_BUFFER_ADD_CHAR(buf, 't');
- break;
-
- case '\n':
- OSRF_BUFFER_ADD_CHAR(buf, '\\');
- OSRF_BUFFER_ADD_CHAR(buf, 'n');
- break;
-
- case '\r':
- OSRF_BUFFER_ADD_CHAR(buf, '\\');
- OSRF_BUFFER_ADD_CHAR(buf, 'r');
- break;
-
- default:
- {
- // Represent as \u followed by four hex characters
- hex_code[ 4 ] = hex_chars[ c >> 4 ]; // high nybble
- hex_code[ 5 ] = hex_chars[ c & 0x0F ]; // low nybble
- hex_code[ 6 ] = '\0';
- OSRF_BUFFER_ADD(buf, (char *) hex_code);
- break;
- }
- }
- }
-
- idx++;
- }
-
- return 0;
-}
-
// A function to turn a process into a daemon
int daemonize( void ) {
pid_t f = fork();
More information about the opensrf-commits
mailing list