2008-06-04 05:22:13 +02:00
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
/* Copyright (C) 2008 Jonathan Moore Liles */
|
|
|
|
/* */
|
|
|
|
/* This program is free software; you can redistribute it and/or modify it */
|
|
|
|
/* under the terms of the GNU General Public License as published by the */
|
|
|
|
/* Free Software Foundation; either version 2 of the License, or (at your */
|
|
|
|
/* option) any later version. */
|
|
|
|
/* */
|
|
|
|
/* This program is distributed in the hope that it will be useful, but WITHOUT */
|
|
|
|
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
|
|
|
|
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
|
|
|
|
/* more details. */
|
|
|
|
/* */
|
|
|
|
/* You should have received a copy of the GNU General Public License along */
|
|
|
|
/* with This program; see the file COPYING. If not,write to the Free Software */
|
|
|
|
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
|
|
|
#include "Log_Entry.H"
|
|
|
|
|
|
|
|
Log_Entry::Log_Entry ( )
|
|
|
|
{
|
|
|
|
_sa = (char**)malloc( sizeof( char * ) );
|
|
|
|
*_sa = NULL;
|
|
|
|
_i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log_Entry::Log_Entry ( char **sa )
|
|
|
|
{
|
|
|
|
_sa = sa;
|
|
|
|
_i = 0;
|
|
|
|
|
|
|
|
if ( _sa )
|
|
|
|
while ( _sa[ _i ] ) ++_i;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-06-04 05:34:36 +02:00
|
|
|
Log_Entry::Log_Entry ( const char *s )
|
|
|
|
{
|
|
|
|
_i = 0;
|
|
|
|
_sa = s ? parse_alist( s ) : NULL;
|
|
|
|
|
|
|
|
if ( _sa )
|
|
|
|
while ( _sa[ _i ] ) ++_i;
|
|
|
|
}
|
|
|
|
|
2008-06-04 05:22:13 +02:00
|
|
|
Log_Entry::~Log_Entry ( )
|
|
|
|
{
|
|
|
|
if ( ! _sa )
|
|
|
|
return;
|
|
|
|
|
|
|
|
for ( _i = 0; _sa[ _i ]; ++_i )
|
|
|
|
{
|
|
|
|
free( _sa[ _i ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
free( _sa );
|
|
|
|
}
|
|
|
|
|
2008-06-04 05:34:36 +02:00
|
|
|
|
|
|
|
/** 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;
|
|
|
|
}
|
|
|
|
|
2008-06-04 05:22:13 +02:00
|
|
|
void
|
|
|
|
Log_Entry::grow ( )
|
|
|
|
{
|
|
|
|
_sa = (char**)realloc( _sa, sizeof( char * ) * (_i + 2) );
|
|
|
|
_sa[ _i + 1 ] = NULL;
|
|
|
|
}
|
|
|
|
\
|
|
|
|
int
|
|
|
|
Log_Entry::size ( void ) const
|
|
|
|
{
|
|
|
|
return _i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Log_Entry::get ( int n, const char **name, const char **value )
|
|
|
|
{
|
|
|
|
*name = _sa[ n ];
|
|
|
|
*value = *name + strlen( *name ) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char **
|
|
|
|
Log_Entry::sa ( void )
|
|
|
|
{
|
|
|
|
char **sa = _sa;
|
|
|
|
|
|
|
|
_sa = NULL;
|
|
|
|
|
|
|
|
return sa;
|
|
|
|
}
|