📄 common.cpp
字号:
static const char* sccsid = "@(#)common.cpp v1.0.0 2000-08-22";
/* Copyright(C) 1999, 2001 by JiangSu Bell Software CO.,LTD. */
/*
Name: common.cpp Version: 1.0.0
Created by HanBing Date: 2000-08-22
Comment: Common Functions
Modified:
2) 2000-09-18 HanBing - Add functions for Print: PrintText;
1) 2000-08-22 HanBing - Create;
*/
#include "common.h"
/* ---------------------------------------------------------------------- */
#ifdef _WINDOWS
short WinAnnounce( const char* exp, const char* file, unsigned line, const char *str )
{
char pc_str[1024];
const char* msg = str ? str : "";
sprintf( pc_str, "\nfile: %s line: %u\n%s\n%s\n", file, line, msg, exp );
MessageBox( NULL, pc_str, "Fatal Error", MB_OK|MB_ICONSTOP );
ExitProcess( 0 );
return 0;
};
//cao 02.6.16
short StdAnnounce( const char* exp, const char* file, unsigned line, const char *str )
{
return WinAnnounce( exp,file,line,str ) ;
}
#endif
#ifndef _WINDOWS
short StdAnnounce( const char* exp, const char* file, unsigned line, const char *str )
{
cout << endl << "file: " << file << " line: " << line << endl;
cout << str << endl << exp << endl << flush;
exit( 0 );
return 0;
};
#endif
/* ---------------------------------------------------------------------- */
char* Rtrim( char *str )
{
int j = ( int )strlen( str ) - 1;
for ( int k = j ; k >= 0 ; k-- )
{
if ( str[k] == ' ' )
str[ k ] = 0 ;
else
k = -1 ;
}
return str;
}
/* ---------------------------------------------------------------------- */
char* Ltrim( char *str )
{
char *pcS = str;
char *pcD = str;
int i = 0;
while ( *pcS )
{
if ( *pcS == ' ' && i == 0 )
pcS++;
else
{
if ( *pcS != ' ' )
i = 1;
*pcD++ = *pcS++;
}
}
*pcD = 0;
return str;
}
/* ---------------------------------------------------------------------- */
char* PackString( char *str )
{
char *pcS = str;
char *pcD = str;
while ( *pcS )
{
if ( *pcS != ' ' )
*pcD++ = *pcS;
pcS++;
}
*pcD = 0;
return str;
}
/* ---------------------------------------------------------------------- */
char* StrnCpy( char* dest, const char* src, int len )
{
if ( !dest )
return NULL;
if ( !src )
{
*dest = 0;
return dest;
}
int i = strlen( src );
if ( len > i )
len = i;
char *d = dest;
while ( len-- && ( *d++ = *src++ ) );
*d = 0;
return dest;
}
bool IsNumber(char* src)
{
bool status=true;
char * desc=new char[strlen(src)+1];
char * p;
strcpy(desc, src);
PackString(desc);
p = desc;
if(*p)
{
while(*p)
{
if(*p >= '0' && *p <= '9')
p++;
else
{
status = false;
break;
}
}
}
else
status = false;
delete desc;
return status;
}
//Add by June 2003-06-10
//根据车牌号尾位获取驾驶员的上一次年检时间
char *GetNjTime( char *s_ch_code )
{
char s_nj_time[20];
s_nj_time [0] = 0;
char s_cur_date[9];
s_cur_date [0] = 0;
OTime clTime;
clTime.GetLocalTime();
strcpy( s_cur_date, clTime.GetDate() );
char s_tmp [2];
int i_tmp = 0;
int i_year = 0, i_month = 0, i_day = 0;
memcpy( s_tmp, s_ch_code+7, 1 );
s_tmp[1] = 0;
sscanf( s_tmp, "%d", &i_tmp );
if( i_tmp == 1 || i_tmp == 2 )
i_tmp = i_tmp + 10;
sscanf( s_cur_date, "%04d%02d%02d", &i_year, &i_month, &i_day );
if( i_month > i_tmp )
sprintf( s_nj_time, "%04d%02d00000000", i_year, i_tmp );
else
sprintf( s_nj_time, "%04d%02d00000000", i_year-1, i_tmp );
return s_nj_time;
}
/* ---------------------------------------------------------------------- */
//Add by June 2003-06-10
char* TrimChar( char *str, int flag )
{
char *pcS = str;
char *pcD = str;
while ( *pcS )
{
if( flag == 1 )
{
if ( *pcS <= '9' && *pcS >= '0' )
*pcD++ = *pcS;
}
else if( flag == 2 )
{
if ( *pcS != '+' && *pcS != '-' && *pcS != '?' )
*pcD++ = *pcS;
}
pcS++;
}
*pcD = 0;
return str;
}
const short encrypt_len = 10;
const char* Encrypt( const char* pass_word, const char* salt, char* encrypted )
{
int n_salt = 0;
static char pwd_in[ encrypt_len+1 ];
static char pwd_out[ encrypt_len*2+1 ];
static char pwd_salt[ encrypt_len+1 ];
memset( pwd_in, '\0', encrypt_len );
memset( pwd_out, '\0', encrypt_len*2 );
memset( pwd_salt, '\0', encrypt_len );
StrnCpy( pwd_in, pass_word, encrypt_len );
StrnCpy( pwd_salt, salt, encrypt_len );
int i = strlen( pwd_salt );
for ( int n = i; n < encrypt_len; n++ )
{
pwd_salt[n] = pwd_salt[n-i];
}
for ( i = 0; i < encrypt_len; i++ )
{
srand( n_salt * i + pwd_salt[i] + pwd_in[i] );
n_salt = rand() % 32;
if ( pwd_in[i] < 32 || pwd_salt[i] < 32 )
{
srand ( n_salt );
n_salt += ( rand() % 2 + 1 ) * 32;
}
char c_salt = n_salt;
pwd_out[i*2] = pwd_in[i] ^ c_salt;
pwd_out[i*2+1] = pwd_salt[i] ^ c_salt;
}
if ( encrypted )
strcpy( encrypted, pwd_out );
return pwd_out;
}
const char* Decrypt( const char* pass_word, const char* salt, char* decrypted )
{
static char pwd_in[ encrypt_len*2+1 ];
static char pwd_out[ encrypt_len+1 ];
static char pwd_salt[ encrypt_len+1 ];
memset( pwd_in, '\0', encrypt_len*2 );
memset( pwd_out, '\0', encrypt_len );
memset( pwd_salt, '\0', encrypt_len );
StrnCpy( pwd_in, pass_word, encrypt_len*2 );
StrnCpy( pwd_salt, salt, encrypt_len );
int i = strlen( pwd_salt );
for ( int n = i; n < encrypt_len; n++ )
{
pwd_salt[n] = pwd_salt[n-i];
}
for ( i = 0; i < encrypt_len; i++ )
{
char c_salt = pwd_in[i*2+1] ^ pwd_salt[i];
pwd_out[i] = pwd_in[i*2] ^ c_salt;
}
if ( decrypted )
strcpy( decrypted, pwd_out );
return pwd_out;
}
/* ---------------------------------------------------------------------- */
extern "C" bool FormatText( const char *p_file, char **ppc_text, char *pc_text )
{
bool ret = true;
FILE *hfile = fopen( p_file, "r" );
if ( hfile == NULL )
{
char pc_str[ 200 ];
sprintf( pc_str, "Can not open the File: %s", p_file );
Debug( 1, pc_str );
ret = false;
}
else
{
char *pc_tmp = pc_text;
while ( fscanf( hfile, "%c", pc_tmp ) != EOF )
pc_tmp++;
pc_tmp = pc_text;
unsigned num = 0, len = 0;
while ( *pc_tmp )
{
if ( *pc_tmp > '9' || *pc_tmp < '0' )
pc_tmp++;
else if ( sscanf( pc_tmp, "%u/%u:", &num, &len ) != 2 || num == 0 || len == 0 )
pc_tmp++;
else
{
unsigned n = strlen( ppc_text[ num-1 ] );
for ( unsigned i = 0; i < len; i++ )
{
if ( i < n )
*pc_tmp++ = ppc_text[ num-1 ][i];
else
*pc_tmp++ = ' ';
}
}
}
fclose( hfile );
}
return ret;
}
#if defined( UNIX ) || defined( VMS )
extern "C" void Printon()
{
printf("%c[5i", '\033'); // for VT220 term order is ESC(033)[5i for on
}
extern "C" void Printoff()
{
printf("%c[4i", '\033'); // for VT220 term order is ESC(033)[4i for off
}
extern "C" bool OutputPrint( const char* p_file, const char* pc_text )
{
bool ret = true;
FILE *hfile = ( p_file && strlen( p_file ) ) ? fopen( p_file, "a" ) : stdout;
if ( hfile )
{
if ( hfile == stdout )
PrintOn();
fprintf( hfile, "%s", pc_text );
if ( hfile == stdout )
PrintOff();
else
fclose( hfile );
}
else
{
char pc_str[ 200 ];
sprintf( pc_str, "Can not open the Output File: %s", p_file );
Debug( 1, pc_str );
ret = false;
}
return ret;
}
#else // defined( MSDOS ) || defined( WINDOWS )
extern "C" bool OutputPrint( const char* p_file, const char* pc_text )
{
bool ret = true;
const char *p_name = ( p_file && strlen( p_file ) ) ? p_file : "PRN";
FILE *hfile = fopen( p_name, "a" );
if ( hfile )
{
fprintf( hfile, "%s", pc_text );
fclose( hfile );
}
else
{
char pc_str[ 200 ];
sprintf( pc_str, "Can not open the Output File: %s", p_file );
Debug( 1, pc_str );
ret = false;
}
return ret;
}
#endif
const unsigned int MaxFileLen = 65536;
bool PrintText( char **ppcData, const char *pFileInput, const char *pFileOutput )
{
bool ret = false;
char *pc_text = new char [ MaxFileLen ];
memset( pc_text, 0, MaxFileLen );
if ( FormatText( pFileInput, ppcData, pc_text ) )
ret = OutputPrint( pFileOutput, pc_text );
delete pc_text;
return ret;
}
/* ---------------------------------------------------------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -