📄 mes.c
字号:
/******************************************************************************* author : Frank N黚el filename : ghmm/ghmm/mes.c created : TIME: 17:28:12 DATE: Thu 25. January 1996 $Id: mes.c,v 1.5 2002/03/07 08:11:15 pipenb Exp $Copyright (C) 1998-2001, ZAIK/ZPR, Universit鋞 zu K鰈nThis program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*******************************************************************************/#include <errno.h>#include <string.h>#include <time.h>#ifdef WIN32# include <windows.h># include <io.h>#endif#include "mes.h"#include "mprintf.h"/* from sys.h */#if defined( _WINDOWS ) || defined( _DOS ) || defined(_PPC)# define strcasecmp( a, b ) strcmpi( a, b )# define PathSepChar ('\\')#else # define PathSepChar ('/')#endif#define MAX_MES_FILE_LENGTH 1400000static char* mes_err_txt[] = { "0 pointer", "negative value" , "value out of range" , "zero value", "boolean value FALSE"};typedef struct mes_t { int thread_id; char* std_path; char* logfile; int argc; char** argv; char win_enabled; char enabled; void (*win_fkt)(const char*);} mes_t;#define MES_PROCESS_MAX 0x10000static mes_t *mes_process[MES_PROCESS_MAX];static int mes_process_n=0;static char mes_default_logfile[] = "message.txt";static char mes_default_bak_logfile[] = "message.bak";/*static char* mes_logfile = NULL;static int mes_argc = 0;static char** mes_argv = NULL;static char mes_win_enabled = 1;static char mes_enabled = 1;static void(*mes_win_fkt)(char*) = NULL;*//*---------------------------------------------------------------------------*/static mes_t* mes_process_get(void) { int i, tid = getthreadid(); for(i=mes_process_n-1;i>=0;i--) if( mes_process[i] ) if( mes_process[i]->thread_id==tid ) return(mes_process[i]); return(NULL);} /* mes_process_get *//*---------------------------------------------------------------------------*/static int mes_process_enabled(void) { mes_t *mpc = mes_process_get(); if( mpc ) return mpc->enabled; return(1);} /* mes_process_enabled *//*---------------------------------------------------------------------------*/static int mes_process_win_enabled(void) { mes_t *mpc = mes_process_get(); if( mpc ) return mpc->win_enabled; return(1);} /* mes_process_win_enabled *//*---------------------------------------------------------------------------*/char* mes_get_std_path(void) { mes_t *mpc = mes_process_get(); if( mpc ) return mpc->std_path; return(NULL);} /* mes_get_std_path *//*---------------------------------------------------------------------------*/static void mes_process_alloc() { mes_t *mpi; int tid = getthreadid(); if( mes_process_get() ) return; if( mes_process_n>=MES_PROCESS_MAX ) return; if( !mes_process ) return; else { int i,j = -1; for(i=mes_process_n-1;i>=0;i--) if( !mes_process[i] ) { j=i; break; } if( i==-1 && mes_process_n<MES_PROCESS_MAX ) { mpi = (mes_t*) calloc( 1, sizeof(mes_t) ); if( !mpi ) return; mes_process[mes_process_n++] = mpi; } else if( j>=0 ) { mpi = (mes_t*) calloc( 1, sizeof(mes_t) ); if( !mpi ) return; mes_process[j] = mpi; } else return; } mpi->thread_id = tid; mpi->std_path = NULL; mpi->logfile = NULL; mpi->argc = 0; mpi->argv = NULL; mpi->win_enabled = 1; mpi->enabled = 1; mpi->win_fkt = NULL;} /* mes_process_alloc *//*---------------------------------------------------------------------------*/static void mes_aux_va( int flags, char* format, va_list args ) { char str[1024]; char* tmp; tmp = mprintf_va_dyn( str, sizeof(str), format, args ); if( !tmp ) return; mes_smart( flags, tmp, -1 ); if( tmp - str ) free( tmp ); return;} /* mes_aux_va *//*---------------------------------------------------------------------------*/static void mes_aux( int flags, char* format, ... ) { va_list args; va_start( args, format ); mes_aux_va( flags, format, args ); return;} /* mes_aux *//*---------------------------------------------------------------------------*/static int mes_filename_check(const char* filename ) { int len; if( !filename ) return(-1); len = strlen(filename); if( len <=0 ) return(-1); #if defined(_WINDOWS) /* WORK AROUND !! The following code line is included to catch an error of the network driver of Windows NT. */ if( filename[len-1] == '\\' ) return(-1); /* End of the work around */#endif return(0);} /* mes_filename_check *//*---------------------------------------------------------------------------*/static void mes_arg_free( void ) { mes_t *mpc = mes_process_get(); if( !mpc ) return; if( mpc->argv ) { while( mpc->argc > 0) { mpc->argc--; if( mpc->argv[mpc->argc] ) free( mpc->argv[mpc->argc] ); } free( mpc->argv ); mpc->argv = NULL; } mpc->argc = 0;} /* mes_arg_free *//*----------------------------------------------------------------------------*/static void mes_init_std_path( char* logfile ) { mes_t *mpc = mes_process_get(); int i; if( mpc ) { if( mpc->std_path ) free( mpc->std_path ); for( i = logfile?strlen(logfile):0; i && logfile[i-1] - PathSepChar; i-- ); mpc->std_path = malloc( i+1 ); if( mpc->std_path ) { memcpy( mpc->std_path, logfile, i ); mpc->std_path[i] = '\0'; } }} /* mes_init_std_path *//*============================================================================*/void mes_smart( int flags,const char* txt, int bytes ) { char tmp[2] = {0}; int slen; if( !mes_process_enabled() ) return; if( flags & MES_FLAG_TIME ) mes_time(); if( !txt ) return; if( bytes < 0 ) { slen = strlen( txt ); bytes = slen; } else { char* p = memchr( txt, 0, bytes ); if(p) slen = txt - p; else slen = bytes + 1; } if( bytes <= 0 ) return; if( slen > bytes ){ tmp[0] = txt[bytes-1]; tmp[bytes-1] = 0; } if( flags & ( MES_FLAG_FILE | MES_FLAG_TIME) ) { FILE* fmes; int size; char* logfile = NULL; mes_t *mpc = mes_process_get(); if( mpc ) logfile = mpc->logfile; if( !logfile ) logfile = mes_default_logfile; fmes = fopen( logfile, "rb" ); if( fmes ) { fseek( fmes, 0, SEEK_END); size = ftell( fmes); fclose( fmes ); if(size > MAX_MES_FILE_LENGTH ) { char bakfile[300]; if( mpc && mpc->std_path ) sprintf( bakfile, "%s%s", mpc->std_path, mes_default_bak_logfile ); else sprintf( bakfile, "%s", mes_default_bak_logfile ); remove( bakfile ); if( rename( logfile, bakfile ) ) { mes_aux( MES_FLAG_WIN, "\nFehler: Kann Datei %s nicht in %s umbenennen.\n\n", logfile, bakfile ); } } } fmes = fopen( logfile, "at" ); if( fmes ) { fputs( txt, fmes ); fputs( tmp, fmes ); fclose( fmes ); } } if( (flags & MES_FLAG_WIN) && mes_process_win_enabled() ) { mes_t *mpc=mes_process_get(); if( mpc && mpc->win_fkt ) { (*mpc->win_fkt)( txt ); (*mpc->win_fkt)( tmp ); } else { fputs( txt, stdout ); fputs( tmp, stdout ); fflush(stdout); } } if( slen > bytes ) ((char*)(txt))[bytes-1] = tmp[0];} /* mes_smart *//*============================================================================*/void mes_exit( void ) { int i, tid = getthreadid(); for(i=mes_process_n-1;i>=0;i--) { mes_t *mpi = mes_process[i]; if( mpi ) if( mpi->thread_id==tid ) { if( mpi->logfile ) free( mpi->logfile ); if( mpi->std_path ) free( mpi->std_path ); mes_arg_free(); free( mpi ); mes_process[i] = NULL; return; } }} /* mes_exit *//*============================================================================*/void mes_init_args( int argc, char* argv[] ) { mes_t *mpc = mes_process_get(); if( !argv || argc < 1 ) return; mes_arg_free(); mpc->argv = calloc( 1, argc * sizeof(*mpc->argv) ); if( mpc->argv ) while( mpc->argc < argc && *argv ) { int len = *argv ? strlen( *argv ) + 1 : 1; mpc->argv[mpc->argc] = malloc( len ); if( !mpc->argv[mpc->argc] ) break; if( *argv ) memcpy( mpc->argv[mpc->argc], *argv, len ); else mpc->argv[mpc->argc][0] = 0; argv++; mpc->argc++; } } /* mes_init_args */ /*============================================================================*/void mes_init_logfile( char* logfile ) { FILE* tst; mes_t *mpc = mes_process_get(); if( !logfile ) return; mes_init_std_path( logfile ); tst = fopen( logfile, "at" ); if( tst ) { fclose( tst ); if( mpc ) { int len = strlen(logfile); if( mpc->logfile ) free( mpc->logfile ); mpc->logfile = (char*) malloc( len+1 ); if( mpc->logfile ) { strcpy( mpc->logfile, logfile ); mpc->logfile[ len ] = '\0'; } return; } }} /* mes_init_logfile *//*============================================================================*/void mes_init_winfct( void(*winfct)(const char*) ) { mes_t *mpc = mes_process_get(); if( mpc && winfct ) mpc->win_fkt = winfct;} /* mes_init_winfct *//*============================================================================*/void mes_init( char* logfile, void(*winfct)(const char*), int argc, char* argv[] ) { mes_process_alloc(); mes_init_args( argc, argv ); mes_init_logfile( logfile ); mes_init_winfct( winfct );} /* mes_init *//*============================================================================*/int mes_win_ability( int on ){ mes_t *mpc = mes_process_get(); if( mpc ) { int res = mpc->win_enabled; mpc->win_enabled = !!on; return(res); } return(1);} /* mes_win_ability *//*============================================================================*/int mes_ability( int on ){ mes_t *mpc = mes_process_get(); if( mpc ) { int res = mpc->enabled; mpc->enabled = !!on; return(res); } return(1);} /* mes_ability *//*============================================================================*/void mes_time( void ) { mes_t* mpc = mes_process_get(); time_t now = time( NULL ); char* timestr = ctime( &now ); int len = strlen( timestr ); char txt[256]; if( !len ) return; if( timestr[len-1] == '\n' ) timestr[len-1] = ' '; mes_file( "\n***** " ); if( mpc ) { if( mpc->argc == 1 ) { mes_file( mpc->argv[0] ); mes_file( ":" ); } else if( mpc->argc > 1 ) { int i; mes_file( mpc->argv[0] ); mes_file( "(" ); mes_file( mpc->argv[1] ); for( i = 2; i < mpc->argc; i++ ) { mes_file( "," ); mes_file( mpc->argv[i] ); } mes_file( ")" ); mes_file( ":" ); } } mes_file( timestr ); sprintf( txt, "(%.2f sec)", clock()/(float)CLOCKS_PER_SEC); mes_file( txt ); mes_file( " *****:\n" );} /* mes_time *//*============================================================================*/void mes_va( int flags, int line, char* xproc, char* proc, char* format, va_list args ) { char digstr[32] = {0}; if( !format && !xproc && !proc ) return; if( line + 1 ) mprintf( digstr, sizeof(digstr), "(%u)", line ); if( flags & MES_FLAG_TIME ) { mes_time(); flags &= ~MES_FLAG_TIME; flags |= MES_FLAG_FILE; } if( !xproc ) xproc = proc; if( !proc ) proc = xproc; if( proc ) { if( flags & MES_FLAG_FILE ) mes_file( xproc ); if( flags & MES_FLAG_WIN ) mes_win( proc ); mes_smart( flags, digstr, -1 ); if( format ) mes_smart( flags, ": ", -1 ); } if( !format ) mes_smart( flags, "\n", -1 ); else mes_aux_va( flags, format, args );} /* mes_va *//*============================================================================*/void mes( int flags, int line, char* xproc, char* proc, char* format, ... ) { va_list args; va_start( args, format ); mes_va( flags, line, xproc, proc, format, args );} /* mes *//*============================================================================*/void mes_printf( int prot_flags, char* format, ... ) { va_list args; char* txt; if( !prot_flags ) return; if( !format ) { mes_time(); mes_file( "Call of mes_printf without format string" ); return; } va_start( args, format ); txt = mprintf_va( NULL, 0, format, args ); if( !txt ) { mes_time(); mes_file_win( "Call of mes_printf with format string\"" ); mes_file_win( format ); mes_file_win( "\" without success\n" ); return; } if( prot_flags & MES_FLAG_TIME ) mes_time(); mes_smart( prot_flags, txt, -1 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -