Split more of Log_Entry out.

This commit is contained in:
Jonathan Moore Liles 2008-06-03 22:34:36 -05:00
parent 6e3e41f9ba
commit 6a324b45c3
3 changed files with 119 additions and 115 deletions

View File

@ -36,6 +36,15 @@ Log_Entry::Log_Entry ( char **sa )
} }
Log_Entry::Log_Entry ( const char *s )
{
_i = 0;
_sa = s ? parse_alist( s ) : NULL;
if ( _sa )
while ( _sa[ _i ] ) ++_i;
}
Log_Entry::~Log_Entry ( ) Log_Entry::~Log_Entry ( )
{ {
if ( ! _sa ) if ( ! _sa )
@ -49,6 +58,111 @@ Log_Entry::~Log_Entry ( )
free( _sa ); free( _sa );
} }
/** remove escapes from string /s/ in-place */
static void
unescape ( char *s )
{
char *r = s;
for ( ; *s; s++, r++ )
{
if ( '\\' == *s )
{
switch ( *(++s) )
{
case 'n':
*r = '\n';
break;
case '"':
*r = '"';
break;
default:
break;
}
}
else
*r = *s;
}
*r = '\0';
}
/** sigh. parse a string of ":name value :name value" pairs into an
* array of strings, one per pair */
// FIXME: doesn't handle the case of :name ":foo bar", nested quotes
// or other things it should.
char **
Log_Entry::parse_alist( const char *s )
{
// FIXME: bogus over allocation...
int tl = strlen( s );
char **r = (char**)malloc( sizeof( char* ) * tl );
// const char *e = s + tl;
const char *c = NULL;
int i = 0;
for ( ; ; s++ )
{
/* if ( *s == '\n' ) */
/* break; */
// if ( *s == ':' || s == e )
if ( *s == ':' || *s == '\0' )
{
if ( c )
{
int l = s - c;
char *pair = (char*)malloc( l + 1 );
/* remove trailing space */
if ( c[ l - 1 ] == ' ' )
--l;
strncpy( pair, c, l );
pair[ l ] = '\0';
r[ i++ ] = pair;
/* split */
strtok( pair, " " );
/* remove quotes */
char *v = pair + strlen( pair ) + 1;
unescape( v );
if ( *v == '"' )
{
// v++;
if ( v[ strlen( v ) - 1 ] != '"' )
WARNING( "invalid quoting in log entry!" );
else
{
v[ strlen( v ) - 1 ] = '\0';
memmove( v, v + 1, strlen( v ) + 1 );
}
}
}
c = s;
if ( *s == '\0' )
break;
}
}
r[ i ] = NULL;
return r;
}
void void
Log_Entry::grow ( ) Log_Entry::grow ( )
{ {

View File

@ -32,10 +32,13 @@ class Log_Entry
Log_Entry ( const Log_Entry &rhs ); Log_Entry ( const Log_Entry &rhs );
Log_Entry & operator= ( const Log_Entry &rhs ); Log_Entry & operator= ( const Log_Entry &rhs );
static char ** parse_alist ( const char *s );
public: public:
Log_Entry ( ); Log_Entry ( );
Log_Entry ( char **sa ); Log_Entry ( char **sa );
Log_Entry ( const char *s );
~Log_Entry ( ); ~Log_Entry ( );
/****************/ /****************/

View File

@ -18,9 +18,7 @@
/*******************************************************************************/ /*******************************************************************************/
#define _LOGGABLE_C
#include "Loggable.H" #include "Loggable.H"
#undef _LOGABLE_C
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -160,110 +158,7 @@ Loggable::escape ( const char *s )
return r; return r;
} }
/** remove escapes from string /s/ in-place */
static void
unescape ( char *s )
{
char *r = s;
for ( ; *s; s++, r++ )
{
if ( '\\' == *s )
{
switch ( *(++s) )
{
case 'n':
*r = '\n';
break;
case '"':
*r = '"';
break;
default:
break;
}
}
else
*r = *s;
}
*r = '\0';
}
/** sigh. parse a string of ":name value :name value" pairs into an
* array of strings, one per pair */
// FIXME: doesn't handle the case of :name ":foo bar", nested quotes
// or other things it should.
static
char **
parse_alist( const char *s )
{
// FIXME: bogus over allocation...
int tl = strlen( s );
char **r = (char**)malloc( sizeof( char* ) * tl );
// const char *e = s + tl;
const char *c = NULL;
int i = 0;
for ( ; ; s++ )
{
/* if ( *s == '\n' ) */
/* break; */
// if ( *s == ':' || s == e )
if ( *s == ':' || *s == '\0' )
{
if ( c )
{
int l = s - c;
char *pair = (char*)malloc( l + 1 );
/* remove trailing space */
if ( c[ l - 1 ] == ' ' )
--l;
strncpy( pair, c, l );
pair[ l ] = '\0';
r[ i++ ] = pair;
/* split */
strtok( pair, " " );
/* remove quotes */
char *v = pair + strlen( pair ) + 1;
unescape( v );
if ( *v == '"' )
{
// v++;
if ( v[ strlen( v ) - 1 ] != '"' )
WARNING( "invalid quoting in log entry!" );
else
{
v[ strlen( v ) - 1 ] = '\0';
memmove( v, v + 1, strlen( v ) + 1 );
}
}
}
c = s;
if ( *s == '\0' )
break;
}
}
r[ i ] = NULL;
return r;
}
static static
@ -322,9 +217,7 @@ Loggable::do_this ( const char *s, bool reverse )
{ {
// printf( "got set command (%s).\n", arguments ); // printf( "got set command (%s).\n", arguments );
char **sa = parse_alist( arguments ); Log_Entry e( arguments );
Log_Entry e( sa );
l->log_start(); l->log_start();
l->set( e ); l->set( e );
@ -332,13 +225,7 @@ Loggable::do_this ( const char *s, bool reverse )
} }
else if ( ! strcmp( command, create ) ) else if ( ! strcmp( command, create ) )
{ {
char **sa = NULL; Log_Entry e( arguments );
if ( arguments )
sa = parse_alist( arguments );
Log_Entry e( sa );
ASSERT( _class_map[ std::string( classname ) ], "Journal contains an object of class \"%s\", but I don't know how to create such objects.", classname ); ASSERT( _class_map[ std::string( classname ) ], "Journal contains an object of class \"%s\", but I don't know how to create such objects.", classname );