/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/utils/eh_status_bar.c

Go to the documentation of this file.
00001 #include <eh_utils.h>
00002 
00003 gpointer print_status( gpointer data );
00004 
00005 Eh_status_bar*
00006 eh_status_bar_new( double* cur , double* end )
00007 {
00008    Eh_status_bar* status_bar = eh_new( Eh_status_bar , 1 );
00009 
00010    status_bar->cur    = cur;
00011    status_bar->end    = end;
00012    status_bar->status = EH_STATUS_BAR_RUNNING;
00013    status_bar->timer  = g_timer_new();
00014    status_bar->mutex  = g_mutex_new();
00015 
00016    status_bar->t      = g_thread_create( print_status , status_bar , TRUE , NULL );
00017 
00018    return status_bar;
00019 }
00020 
00021 Eh_status_bar*
00022 eh_status_bar_stop( Eh_status_bar* b )
00023 {
00024    g_mutex_lock( b->mutex );
00025    b->status = EH_STATUS_BAR_STOPPED;
00026    g_mutex_unlock( b->mutex );
00027 
00028    return b;
00029 }
00030 
00031 Eh_status_bar*
00032 eh_status_bar_pause( Eh_status_bar* b )
00033 {
00034    g_mutex_lock( b->mutex );
00035    b->status = EH_STATUS_BAR_PAUSED;
00036    g_mutex_unlock( b->mutex );
00037    return b;
00038 }
00039 
00040 gboolean
00041 eh_status_bar_is_stopped( Eh_status_bar* b )
00042 {
00043    gboolean is_stopped;
00044 
00045    g_mutex_lock( b->mutex );
00046    is_stopped = (b->status == EH_STATUS_BAR_STOPPED);
00047    g_mutex_unlock( b->mutex );
00048 
00049    return is_stopped;
00050 }
00051 
00052 Eh_status_bar*
00053 eh_status_bar_destroy( Eh_status_bar* b )
00054 {
00055    eh_status_bar_stop( b );
00056 
00057    g_thread_join( b->t );
00058 
00059    g_timer_destroy( b->timer );
00060    g_mutex_free   ( b->mutex );
00061    eh_free( b );
00062 
00063    return NULL;
00064 }
00065 
00066 gpointer
00067 print_status( gpointer data )
00068 {
00069    Eh_status_bar* b = (Eh_status_bar*)data;
00070    double  t;
00071    double  eta;
00072    gchar*  t_str;
00073    gchar*  eta_str;
00074    gchar*  status_bar[] = { "." , "o" , "0" , "O" , NULL };
00075    gchar** p = status_bar;
00076 
00077    fprintf( stderr , "\n" );
00078    fprintf( stderr , " Current        |   Elapsed   |     ETA     \n" );
00079 
00080    for ( ; *(b->cur)<=0 && !eh_status_bar_is_stopped(b) ; );
00081 
00082    for ( ; !eh_status_bar_is_stopped(b) ; )
00083    {
00084       t   = g_timer_elapsed(b->timer,NULL);
00085       eta = t / *(b->cur) * ( *(b->end) - *(b->cur) );
00086 
00087       if ( *p==NULL )
00088          p = status_bar;
00089 
00090       if ( b->status==EH_STATUS_BAR_RUNNING )
00091       {
00092          t_str   = eh_render_time_str( t   );
00093          eta_str = eh_render_time_str( eta );
00094 
00095          fprintf( stderr , " %7g (%3.0f%%) | %s | %s" ,
00096                   *(b->cur) , *(b->cur) / *(b->end)*100. , t_str , eta_str );
00097 
00098          fprintf( stderr , "   (%s)" , *p );
00099          fprintf( stderr , "          \r" );
00100 
00101          eh_free( eta_str );
00102          eh_free( t_str   );
00103       }
00104 
00105       p++;
00106 
00107       g_usleep( 100000 );
00108    }
00109 
00110    t_str = eh_render_time_str( g_timer_elapsed(b->timer,NULL) );
00111 
00112    fprintf( stderr , "\n" );
00113 
00114    fprintf( stderr , "Elapsed time: %s\n" , t_str );
00115 
00116    eh_free( t_str );
00117 
00118    return data;
00119 }
00120 
00121 #define EH_SECONDS_PER_DAY    ( 86400. )
00122 #define EH_SECONDS_PER_HOUR   ( 3600. )
00123 #define EH_SECONDS_PER_MINUTE ( 60. )
00124 
00125 gchar*
00126 eh_render_time_str( double sec )
00127 {
00128    gchar* str = NULL;
00129 
00130    {
00131       gint d = sec / (gint)EH_SECONDS_PER_DAY;
00132       gint h = sec / (gint)EH_SECONDS_PER_HOUR;
00133       gint m = sec / (gint)EH_SECONDS_PER_MINUTE;
00134       gint s = fmod( sec , 60. );
00135 
00136       str = g_strdup_printf( "%02d:%02d:%02d:%02d" , d , h , m , s );
00137    }
00138 
00139    return str;
00140 }
00141 

Generated on Fri Jan 4 18:04:16 2008 for sedflux by  doxygen 1.5.2