nonlib/Loggable: Get rid of some line length limitations.

This commit is contained in:
Jonathan Moore Liles 2013-09-20 23:02:02 -07:00
parent 51bce6124e
commit b85ba3c2d9
3 changed files with 72 additions and 33 deletions

View File

@ -177,10 +177,13 @@ Loggable::load_unjournaled_state ( void )
}
unsigned int id;
char buf[BUFSIZ];
char *buf;
while ( fscanf( fp, "%X set %[^\n]\n", &id, buf ) == 2 )
while ( fscanf( fp, "%X set %a[^\n]\n", &id, &buf ) == 2 )
{
_loggables[ id ].unjournaled_state = new Log_Entry( buf );
free(buf);
}
fclose( fp );
@ -210,8 +213,7 @@ Loggable::replay ( const char *file )
bool
Loggable::replay ( FILE *fp )
{
/* FIXME: bogus */
char buf[BUFSIZ];
char *buf = NULL;
struct stat st;
fstat( fileno( fp ), &st );
@ -222,7 +224,7 @@ Loggable::replay ( FILE *fp )
if ( _progress_callback )
_progress_callback( 0, _progress_callback_arg );
while ( fscanf( fp, "%[^\n]\n", buf ) == 1 )
while ( fscanf( fp, "%a[^\n]\n", &buf ) == 1 )
{
if ( ! ( ! strcmp( buf, "{" ) || ! strcmp( buf, "}" ) ) )
{
@ -232,6 +234,8 @@ Loggable::replay ( FILE *fp )
do_this( buf, false );
}
free(buf);
current = ftell( fp );
if ( _progress_callback )
@ -480,8 +484,7 @@ Loggable::do_this ( const char *s, bool reverse )
void
Loggable::undo ( void )
{
const int bufsiz = 1024;
char buf[bufsiz];
char *buf;
block_start();
@ -489,26 +492,40 @@ Loggable::undo ( void )
fseek( _fp, _undo_offset, SEEK_SET );
backwards_fgets( buf, bufsiz, _fp );
if ( ! strcmp( buf, "}\n" ) )
if ( ( buf = backwards_afgets( _fp ) ) )
{
DMESSAGE( "undoing block" );
for ( ;; )
if ( ! strcmp( buf, "}\n" ) )
{
backwards_fgets( buf, bufsiz, _fp );
free( buf );
char *s = buf;
if ( *s != '\t' )
break;
else
++s;
DMESSAGE( "undoing block" );
for ( ;; )
{
if ( ( buf = backwards_afgets( _fp ) ) )
{
char *s = buf;
if ( *s != '\t' )
{
DMESSAGE( "done with block", s );
do_this( s, true );
break;
}
else
++s;
do_this( s, true );
free( buf );
}
}
}
else
{
do_this( buf, true );
free( buf );
}
}
else
do_this( buf, true );
off_t uo = ftell( _fp );
@ -517,6 +534,7 @@ Loggable::undo ( void )
block_end();
_undo_offset = uo;
}
/** write a snapshot of the current state of all loggable objects to

View File

@ -107,25 +107,46 @@ backwards_fgetc ( FILE *fp )
}
char *
backwards_fgets ( char *s, int size, FILE *fp )
backwards_afgets ( FILE *fp )
{
if ( fseek( fp, -1, SEEK_CUR ) != 0 )
return NULL;
size_t size = 0;
char *s = NULL;
size_t i = 0;
int c;
while ( ( c = backwards_fgetc( fp ) ) >= 0 )
if ( '\n' == c )
{
if ( i > 0 && '\n' == c )
break;
long here = ftell( fp );
if ( i >= size )
{
size += 256;
s = (char*)realloc( s, size );
}
s[i++] = c;
}
if ( s )
{
s[i] = 0;
int len = strlen(s) ;
int c, i, j;
for (i = 0, j = len - 1; i < j; i++, j--)
{
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
fseek( fp, 1, SEEK_CUR );
char *r = fgets( s, size, fp );
fseek( fp, here, SEEK_SET );
return r;
return s;
}

View File

@ -27,7 +27,7 @@ int exists ( const char *name );
bool acquire_lock ( int *lockfd, const char *filename );
void release_lock ( int *lockfd, const char *filename );
int backwards_fgetc ( FILE *fp );
char * backwards_fgets ( char *s, int size, FILE *fp );
char * backwards_afgets ( FILE *fp );
void touch ( int fd );
void write_line ( const char *dir, const char *name, const char *value );
char * read_line ( const char *dir, const char *name );