From 451399b3445f2a8f929bfe8893df9b3e931ac2b1 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Tue, 27 Dec 2011 00:06:07 -0800 Subject: [PATCH] Migrate to std::vector in event class --- common.h | 1 + env.cpp | 35 ++++++++++++++++------------- event.cpp | 67 ++++++++++++++++++++++--------------------------------- event.h | 3 ++- proc.cpp | 21 ++++++----------- 5 files changed, 56 insertions(+), 71 deletions(-) diff --git a/common.h b/common.h index 07eb1217f..d45667f45 100644 --- a/common.h +++ b/common.h @@ -21,6 +21,7 @@ /* Common string type */ typedef std::wstring wcstring; +typedef std::vector wcstring_list_t; /** Maximum number of bytes used by a single utf-8 character diff --git a/env.cpp b/env.cpp index 01062ee50..f603e7b8d 100644 --- a/env.cpp +++ b/env.cpp @@ -411,12 +411,13 @@ static void universal_callback( int type, ev.param1.variable=name; ev.function_name=0; - al_init( &ev.arguments ); - al_push( &ev.arguments, L"VARIABLE" ); - al_push( &ev.arguments, str ); - al_push( &ev.arguments, name ); - event_fire( &ev ); - al_destroy( &ev.arguments ); + ev.arguments = new wcstring_list_t(); + ev.arguments->push_back(L"VARIABLE"); + ev.arguments->push_back(str); + ev.arguments->push_back(name); + event_fire( &ev ); + delete ev.arguments; + ev.arguments = NULL; } } @@ -1005,15 +1006,16 @@ int env_set( const wchar_t *key, ev.param1.variable = key; ev.function_name = 0; - al_init( &ev.arguments ); - al_push( &ev.arguments, L"VARIABLE" ); - al_push( &ev.arguments, L"SET" ); - al_push( &ev.arguments, key ); + ev.arguments = new wcstring_list_t; + ev.arguments->push_back(L"VARIABLE"); + ev.arguments->push_back(L"SET"); + ev.arguments->push_back(key); // debug( 1, L"env_set: fire events on variable %ls", key ); event_fire( &ev ); // debug( 1, L"env_set: return from event firing" ); - al_destroy( &ev.arguments ); + delete ev.arguments; + ev.arguments = NULL; } if( is_locale( key ) ) @@ -1110,14 +1112,15 @@ int env_remove( const wchar_t *key, int var_mode ) ev.param1.variable=key; ev.function_name=0; - al_init( &ev.arguments ); - al_push( &ev.arguments, L"VARIABLE" ); - al_push( &ev.arguments, L"ERASE" ); - al_push( &ev.arguments, key ); + ev.arguments = new wcstring_list_t; + ev.arguments->push_back(L"VARIABLE"); + ev.arguments->push_back(L"ERASE"); + ev.arguments->push_back(key); event_fire( &ev ); - al_destroy( &ev.arguments ); + delete ev.arguments; + ev.arguments = NULL; erased = 1; } } diff --git a/event.cpp b/event.cpp index 7860ad373..40fd410b9 100644 --- a/event.cpp +++ b/event.cpp @@ -156,16 +156,11 @@ static event_t *event_copy( event_t *event, int copy_arguments ) e->param1.variable = wcsdup( e->param1.variable ); else if( e->type == EVENT_GENERIC ) e->param1.param = wcsdup( e->param1.param ); - - al_init( &e->arguments ); - if( copy_arguments ) + + e->arguments = new wcstring_list_t; + if( copy_arguments && event->arguments ) { - int i; - for( i=0; iarguments ); i++ ) - { - al_push( &e->arguments, wcsdup( (wchar_t *)al_get( &event->arguments, i ) ) ); - } - + *(e->arguments) = *(event->arguments); } return e; @@ -389,8 +384,7 @@ static int event_is_killed( event_t *e ) */ static void event_fire_internal( event_t *event ) { - int i, j; - string_buffer_t *b=0; + size_t i, j; event_list_t fire; /* @@ -445,20 +439,17 @@ static void event_fire_internal( event_t *event ) /* Fire event */ - if( !b ) - b = sb_new(); - else - sb_clear( b ); + wcstring buffer = criterion->function_name; - sb_append( b, criterion->function_name ); - - for( j=0; jarguments); j++ ) - { - wchar_t *arg_esc = escape( (wchar_t *)al_get( &event->arguments, j), 1 ); - sb_append( b, L" " ); - sb_append( b, arg_esc ); - free( arg_esc ); - } + if (event->arguments) + { + for( j=0; j< event->arguments->size(); j++ ) + { + wcstring arg_esc = escape_string( event->arguments->at(j), 1 ); + buffer += L" "; + buffer += arg_esc; + } + } // debug( 1, L"Event handler fires command '%ls'", (wchar_t *)b->buff ); @@ -470,18 +461,12 @@ static void event_fire_internal( event_t *event ) prev_status = proc_get_last_status(); parser_push_block( EVENT ); current_block->param1.event = event; - eval( (wchar_t *)b->buff, 0, TOP ); + eval( buffer.c_str(), 0, TOP ); parser_pop_block(); proc_pop_interactive(); proc_set_last_status( prev_status ); } - if( b ) - { - sb_destroy( b ); - free( b ); - } - /* Free killed events */ @@ -495,7 +480,7 @@ static void event_fire_internal( event_t *event ) static void event_fire_delayed() { - int i; + size_t i; /* If is_event is one, we are running the event-handler non-recursively. @@ -528,7 +513,7 @@ static void event_fire_delayed() { signal_list_t *lst; event_t e; - al_init( &e.arguments ); + e.arguments = new wcstring_list_t(1); //one element /* Switch signal lists @@ -553,10 +538,10 @@ static void event_fire_delayed() /* Send all signals in our private list */ - for( i=0; icount; i++ ) + for( i=0; i<(size_t)lst->count; i++ ) { e.param1.signal = lst->signal[i]; - al_set( &e.arguments, 0, sig2wcs( e.param1.signal ) ); + e.arguments->at(0) = sig2wcs( e.param1.signal ); if( event_is_blocked( &e ) ) { blocked.push_back(event_copy(&e, 1)); @@ -567,7 +552,7 @@ static void event_fire_delayed() } } - al_destroy( &e.arguments ); + delete e.arguments; } } @@ -635,8 +620,8 @@ void event_free( event_t *e ) /* When apropriate, we clear the argument vector */ - al_foreach( &e->arguments, &free ); - al_destroy( &e->arguments ); + if (e->arguments) delete e->arguments; + e->arguments = NULL; free( (void *)e->function_name ); if( e->type == EVENT_VARIABLE ) @@ -663,15 +648,17 @@ void event_fire_generic_internal(const wchar_t *name, ...) ev.param1.param = name; ev.function_name=0; - al_init( &ev.arguments ); + ev.arguments = new wcstring_list_t; va_start( va, name ); while( (arg=va_arg(va, wchar_t *) )!= 0 ) { - al_push( &ev.arguments, arg ); + ev.arguments->push_back(arg); } va_end( va ); event_fire( &ev ); + delete ev.arguments; + ev.arguments = NULL; } diff --git a/event.h b/event.h index 6d0f91df9..fe235da6d 100644 --- a/event.h +++ b/event.h @@ -12,6 +12,7 @@ #ifndef FISH_EVENT_H #define FISH_EVENT_H +#include "common.h" /** The signal number that is used to match any signal @@ -91,7 +92,7 @@ typedef struct event_fire. In all other situations, the value of this variable is ignored. */ - array_list_t arguments; + wcstring_list_t *arguments; } event_t; diff --git a/proc.cpp b/proc.cpp index 86c31b52a..356e136b3 100644 --- a/proc.cpp +++ b/proc.cpp @@ -132,7 +132,7 @@ void proc_init() { interactive_stack = al_halloc( global_context ); proc_push_interactive( 0 ); - al_init( &event.arguments ); + event.arguments = new wcstring_list_t; sb_init( &event_pid ); sb_init( &event_status ); } @@ -177,7 +177,8 @@ void job_free( job_t * j ) void proc_destroy() { - al_destroy( &event.arguments ); + delete event.arguments; + event.arguments = NULL; sb_destroy( &event_pid ); sb_destroy( &event_status ); while( first_job ) @@ -535,19 +536,11 @@ void proc_fire_event( const wchar_t *msg, int type, pid_t pid, int status ) event.type=type; event.param1.pid = pid; - al_push( &event.arguments, msg ); - - sb_printf( &event_pid, L"%d", pid ); - al_push( &event.arguments, event_pid.buff ); - - sb_printf( &event_status, L"%d", status ); - al_push( &event.arguments, event_status.buff ); - + event.arguments->push_back(msg); + event.arguments->push_back(format_val(pid)); + event.arguments->push_back(format_val(status)); event_fire( &event ); - - al_truncate( &event.arguments, 0 ); - sb_clear( &event_pid ); - sb_clear( &event_status ); + event.arguments->resize(0); } int job_reap( int interactive )