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

View File

@ -107,25 +107,46 @@ backwards_fgetc ( FILE *fp )
} }
char * char *
backwards_fgets ( char *s, int size, FILE *fp ) backwards_afgets ( FILE *fp )
{ {
if ( fseek( fp, -1, SEEK_CUR ) != 0 ) size_t size = 0;
return NULL;
char *s = NULL;
size_t i = 0;
int c; int c;
while ( ( c = backwards_fgetc( fp ) ) >= 0 ) while ( ( c = backwards_fgetc( fp ) ) >= 0 )
if ( '\n' == c ) {
if ( i > 0 && '\n' == c )
break; 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 ); fseek( fp, 1, SEEK_CUR );
char *r = fgets( s, size, fp ); return s;
fseek( fp, here, SEEK_SET );
return r;
} }

View File

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