00001
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include "bar.h"
00025
00029
00030 struct Bar_Progress_t_
00031 {
00032 int nItemsNext;
00033 int nItemsTotal;
00034 int posTotal;
00035 int posCur;
00036 FILE * pFile;
00037 };
00038
00039 static void Bar_ProgressShow( Bar_Progress_t * p, char * pString );
00040 static void Bar_ProgressClean( Bar_Progress_t * p );
00041
00045
00060 Bar_Progress_t * Bar_ProgressStart( FILE * pFile, int nItemsTotal )
00061 {
00062 Bar_Progress_t * p;
00063
00064
00065
00066 p = (Bar_Progress_t *) malloc(sizeof(Bar_Progress_t));
00067 memset( p, 0, sizeof(Bar_Progress_t) );
00068 p->pFile = pFile;
00069 p->nItemsTotal = nItemsTotal;
00070 p->posTotal = 78;
00071 p->posCur = 1;
00072 p->nItemsNext = (int)((7.0+p->posCur)*p->nItemsTotal/p->posTotal);
00073 Bar_ProgressShow( p, NULL );
00074 return p;
00075 }
00076
00088 void Bar_ProgressUpdate_int( Bar_Progress_t * p, int nItemsCur, char * pString )
00089 {
00090 if ( p == NULL ) return;
00091 if ( nItemsCur < p->nItemsNext )
00092 return;
00093 if ( nItemsCur >= p->nItemsTotal )
00094 {
00095 p->posCur = 78;
00096 p->nItemsNext = 0x7FFFFFFF;
00097 }
00098 else
00099 {
00100 p->posCur += 7;
00101 p->nItemsNext = (int)((7.0+p->posCur)*p->nItemsTotal/p->posTotal);
00102 }
00103 Bar_ProgressShow( p, pString );
00104 }
00105
00106
00118 void Bar_ProgressStop( Bar_Progress_t * p )
00119 {
00120 if ( p == NULL ) return;
00121 Bar_ProgressClean( p );
00122 free( p );
00123 }
00124
00136 void Bar_ProgressShow( Bar_Progress_t * p, char * pString )
00137 {
00138 int i;
00139 if ( p == NULL ) return;
00140 if ( pString )
00141 fprintf( p->pFile, "%s ", pString );
00142 for ( i = (pString? strlen(pString) + 1 : 0); i < p->posCur; i++ )
00143 fprintf( p->pFile, "-" );
00144 if ( i == p->posCur )
00145 fprintf( p->pFile, ">" );
00146 for ( i++ ; i <= p->posTotal; i++ )
00147 fprintf( p->pFile, " " );
00148 fprintf( p->pFile, "\r" );
00149 fflush( stdout );
00150 }
00151
00163 void Bar_ProgressClean( Bar_Progress_t * p )
00164 {
00165 int i;
00166 if ( p == NULL ) return;
00167 for ( i = 0; i <= p->posTotal; i++ )
00168 fprintf( p->pFile, " " );
00169 fprintf( p->pFile, "\r" );
00170 fflush( stdout );
00171 }
00172
00176
00177