#include "version.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "common.h" #include "util.h" #include "configfile.h" #include #define IS_2BYTEWORD( _a_ ) ( (char)(0x80) <= (_a_) && (_a_) <= (char)(0xFF) ) /*----------------------------------------- 仃勾及荼垫毛仇欠允 娄醒“ 手午及 侬 [ 涛 凳今木月冗 windows , macintosh , unix及升及垫手覆杀匹五月[ windows : \r\n macintosh : \n\r unix : \n 分匀凶井卅 macintosh : \r 匹允[ 公木午仇及皿夫弘仿丞分午仃勾及 侬元扎卅仁化手 荼垫壅允仃升中中氏匹允井 -----------------------------------------*/ void chompex( char *buf ) { while( *buf ){ if( *buf == '\r' || *buf == '\n' ){ *buf='\0'; } buf++; } } /*---------------------------------------- * 仃勾及1 侬毛潸曰轮仁 * 娄醒 * src 凳允月 侬 ----------------------------------------*/ void chop( char* src ) { /* * 凶分凶氏卞 及 侬毛潸曰轮仁 */ int length = strlen( src ); if( length == 0 ) return; src[length-1] = '\0'; } /*---------------------------------------- * 仃勾及 侬毛潸月[公及 侬毛 醒隙烂匹五月[ * 娄醒 * src 葭及 侬 * delim 绰轮允月 侬及 [ 反NULL 侬匹蔽匀化中月仪 ----------------------------------------*/ void dchop( char* src , char* del) { int dellen, srclen; int i; BOOL delete=FALSE; /* * src及 欠井日譬屯化中仁[1 侬卞勾五 del 允屯化毛伙□皿仄化 * 域谯允木壬壅允[公仄化戚及 侬卞中仁[ */ srclen = strlen( src ); dellen = strlen( del ); if( srclen == 0 || dellen == 0 ) return; for( i = 0 ; i < dellen ; i ++ ){ if( src[srclen-1] == del[i] ){ delete = TRUE; break; } } /* 濠曰潸曰 */ if( delete )src[srclen - 1] = '\0'; } /*---------------------------------------- * dchop及菅[ 赓及 侬毛潸月[公及 侬毛 醒隙烂匹五月[ * 娄醒 * src 葭及 侬 * delim 绰轮允月 侬及 [ 反NULL 侬匹蔽匀化中月仪 ----------------------------------------*/ void pohcd( char* src , char* del) { int dellen, srclen; int i; BOOL delete=FALSE; /* * src及蟆井日譬屯化中仁[1 侬卞勾五 del 允屯化毛伙□皿仄化 * 域谯允木壬壅允[公仄化戚及 侬卞中仁[ */ srclen = strlen( src ); dellen = strlen( del ); if( srclen == 0 || dellen == 0 ) return; for( i = 0 ; i < dellen ; i ++ ){ if( src[0] == del[i] ){ delete = TRUE; break; } } /* 绰轮 */ if( delete ) /* 仇及戊疋□匹 NULL 侬手戊疋□今木月 */ for( i = 0 ; i < srclen ; i ++ ) src[i] = src[i+1]; } /*---------------------------------------- * 侬 卞 侬互殖引木化中月井升丹井譬屯月[ * 娄醒 * src char* 譬屯月 侬 * include char* 殖引木化中月井升丹井及 侬 [ * 忒曰袄 * 岳 赓卞殖引木化中凶src及桦赭 * 撩 -1 ----------------------------------------*/ int charInclude( char* src, char* include ) { int i,j; int srclen,includelen; srclen = strlen( src ); includelen = strlen( include ); for( i = 0 ; i < srclen ; i ++ ){ for( j = 0 ; j < includelen ; j ++ ) if( src[i] == include[j] ) return i; } return -1; } /*------------------------------------------------------------ * EUC及2田奶玄 分仃毛戊疋□仄卅中strncpy * freeBSD 及仿奶皮仿伉井日 strncpy 毛潸匀化五化} * 公木毛手午卞荼瞻[*dest++ 午井银中凶中仃升}瑛宁赐辆卞愤褥互 * 卅中及匹}银匀化中卅中[井歹曰卞 分仃升}禾奶件正手银匀化化 * 井卅曰 卅皿夫弘仿丞 * 娄醒 * dest char* 戊疋□燮 * src char* 末□旦 * n size_t 赢今 * 忒曰袄 * dest ------------------------------------------------------------*/ char* strncpy2( char* dest, const char* src, size_t n ) { if( n > 0 ){ char* d = dest; const char* s = src; int i; for( i=0; i=n ){ *(d+i-1)='\0'; break; } *(d+i) = *(s+i); }else *(d+i) = *(s+i); } } return dest; } /*---------------------------------------- * src 井日 dest 卞戊疋□允月[ 戊疋□燮及扔奶术毛譬屯月[ * 娄醒 * dest 戊疋□燮 * n 扔奶术 * src 葭及 侬 ----------------------------------------*/ void strcpysafe( char* dest ,size_t n ,const char* src ) { /* * src 井日 dest 尺戊疋□允月. * strcpy, strncpy 匹反 dest 方曰 戊疋□允月汹互 * 五中凛卞裟少午,丢乒伉陆失弁本旦互粟月. * 仇木毛 什啃卞, strncpy 互丐月互 strlen( src ) 互 n 方曰 * 五中凛卞反, dest 及 互 NULL 侬午反卅日卅中. * * 仄凶互匀化 dest 及 五今方曰 src 及幻丹互赢中凛卞反 * n-1 匹 strncpy 毛允月. 凝今中凛反公及引引戊疋□允月 * * n 互 及凛反云井仄仁卅月及匹 及凛反 窒手仄卅中[ * */ // Nuke +1 (08/25): Danger if src=0 if (!src) { *dest = '\0'; return; } if( n <= 0 ) /* 窒手仄卅中 */ return; /* 仇及凛鳔匹} n >= 1 动晓互瑁烂 */ /* NULL 侬毛哔 仄化 胜允月 */ else if( n < strlen( src ) + 1 ){ /* * 田永白央互箫曰卅中及匹 n - 1(NULL 侬) * 匹 strncpy 毛裟少 */ strncpy2( dest , src , n-1 ); dest[n-1]='\0'; }else strcpy( dest , src ); } /*---------------------------------------- * src 井日 length 掏允[ 戊疋□燮及 五今毛譬屯月[ * 娄醒 * dest 戊疋□燮 * n 戊疋□燮及扔奶术 * src 戊疋□葭 * length 窒 侬戊疋□允月井 ----------------------------------------*/ void strncpysafe( char* dest , const size_t n , const char* src ,const int length ) { /* * src 井日 dest 卞 length 戊疋□允月 * strcpy, strncpy 匹反 dest 方曰 戊疋□允月汹互 * 五中凛卞裟少午,丢乒伉陆失弁本旦互粟月. * 仇及楮醒匹反}strlen( src ) 午 length 及凝今中幻丹 * ( 端卞戊疋□允月汹) 午 dest 及扔奶术毛 屯化} * strcpysafe 午 元仪毛允月[ */ int Short; Short = min( strlen( src ) , length ); /* NULL 侬 毛哔 仄凶 胜 */ if( n < Short + 1 ){ /* * 田永白央互箫曰卅中及匹 n - 1(NULL 侬) * 匹 strncpy 毛裟少 */ strncpy2( dest , src , n-1 ); dest[n-1]='\0'; }else if( n <= 0 ){ return; }else{ /* * 田永白央反蜗坌卞丐月及匹 Short 匹strncpy毛裟少 * 卅云 src 卞反 Short 及赢今 卞 NULL 互卅中及匹} * dest 卞反 馨笛仄化云仁[ */ strncpy2( dest , src , Short ); dest[Short]= '\0'; } } /*------------------------------------------------------------ * strcat毛兜蝈卞 蜇允月[赞中及匹} 卅桦宁毛轮中化银歹卅中仪[ * 娄醒 * src char* 葭卞卅月 侬 * size int src 及扔奶术( 欠卞升木分仃酸匀化中月井匹反 * 卅仁化田永白央蝈 ) * ap char* 尥仃笛尹月 * 忒曰袄 * src ( 木阂迕) ------------------------------------------------------------*/ char* strcatsafe( char* src, int size, char* ap ) { strcpysafe( src + strlen( src ) , size - strlen(src), ap ); return src; } /*---------------------------------------- * 侬 及 毛 胜允月[ 元井升丹井仄井 烂仄卅中 by ringo * char *s1 , *s2 : 胜允月 侬 * 忒曰袄 * 0 “ 云卅元 * 1 : 仇午卅月 * 仇木反NPC及 平旦玄荸 卞支仁分勾[ ----------------------------------------*/ int strcmptail( char *s1 , char *s2 ) { int i; int len1 = strlen( s1 ); int len2 = strlen( s2 ); for(i=0;;i++){ int ind1 = len1 - 1 - i; int ind2 = len2 - 1 - i; if( ind1 < 0 || ind2 < 0 ) return 0; if( s1[ind1] != s2[ind2] ) return 1; } } /*---------------------------------------- * 侬 及 卞踏井木化中月帮醒毛必永玄允月[ * NPC及 平旦玄荸 卞支仁分勾[ * 凶分仄 卞中日氏 侬互丐月桦宁 骰允月[ * 耨痊互 切卅中失伙打伉术丞卞 凳[ * 娄醒 * hoge char* 荸 允月 侬 * 忒曰袄 * 荸 仄凶帮醒[ * * “ * "ringo ( 吧噗吧噗星人 ) : 100" * 及桦宁反100互井尹月[ * "ringo ( 吧噗吧噗星人 ) : 是100" * 手100卞卅月[幻氏引卞NPC迕卅曰友[ ----------------------------------------*/ #define DECIMALCHARNUM 10 #define FIGURECHARNUM 3 #define SPECIALCHARNUM 2 char *UTIL_decimalchar[DECIMALCHARNUM] = {"0","1","2", "3", "4","5", "6","7","8","9"}; char *UTIL_figurechar[FIGURECHARNUM] = {"","",""}; char *UTIL_specialchar[SPECIALCHARNUM] = {"",""}; /* * 侬 井日}醒袄毛 允午 歹木月 坌毛 五请允[燮 及幻丹井日 * 茧仄化 五请允[euc迕[ * * : in "这本书七万册300美金吗?" * out "7万" * 娄醒 * char *in: 侬 * char *out:请 侬 * int outsiz:请 侬 及田永白央扔奶术 * 忒曰袄 * 醒袄毛 允 坌互丐匀凶日公及 侬 及赢今}卅井匀凶日ㄟ */ static int findNumberString( char *in, char *out, int outsiz ) { int len,i,j,k, findflag, numstrflag; char tmp[3]; len = strlen(in); if( len == 0 ) return 0; numstrflag=0; findflag = 0; for( i=0,j=0;in[i]!='\0'&& j 0x7fffffff ) return 0x7fffffff; else return (int)val; } /* * 挚匹 仄凶醒袄毛帮醒卞 晶 * char *str 醒袄毛 允 侬 * 忒曰袄 醒袄 * * str互"一亿4千万?"及午五忒曰袄 140000000 */ int kanjistrToInt( char *str ) { double val,tmpval; char tmp[3]; int num,i,j,numflag; numflag = 0; num = 1; tmpval = 0; val = 0; for( i=0;str[i]!='\0';i++ ){ if( str[i] & 0x80 ){ tmp[0] = str[i]; tmp[1] = str[i+1]; tmp[2] = '\0'; i++; }else{ tmp[0] = str[i]; tmp[1] = '\0'; } for( j=0;j 0x7fffffff ) return 0x7fffffff; else return (int)val; } /*---------------------------------------- * 侬 及 卞踏井木化中月帮醒毛必永玄允月[ * NPC及 平旦玄荸 卞支仁分勾[ * 凶分仄 卞中日氏 侬互丐月桦宁 骰允月[ * 耨痊互 切卅中失伙打伉术丞卞 凳[ * 娄醒 * hoge char* 荸 允月 侬 * 忒曰袄 * 荸 仄凶帮醒[ * * “ * "ringo ( 吧噗吧噗星人 ) : 100" * 及桦宁反100互井尹月[ * "ringo ( 吧噗吧噗星人 ) : 是100" * 手100卞卅月[幻氏引卞NPC迕卅曰友[ ----------------------------------------*/ int texttoiTail( char *hoge) { return 0; /* 壬什匀化凶及匹仇丹仄凶[公及丹切楮醒仍午壅允[ */ } /*---------------------------------------- * 侬 毛醒侬井升丹井民尼永弁仄化}醒侬分匀凶日 * 娄醒卞 木化忒允 * 娄醒 * arg 晶葭 * number 晶 毛医 允月赭 * base 湘醒 * type 娄醒及滇( CTYPE 反 common.h 卞烛蜕今木化中月 ) * 忒曰袄 * TRUE(1) 岳 * FALSE(0) strtol互敦僬卞反岳 仄卅井匀凶 * 仇及凛及number反strtol及忒曰袄匹丐月 ----------------------------------------*/ BOOL strtolchecknum( char* arg , void* number,int base ,CTYPE type) { char* buf; int num; num = strtol( arg, &buf, base); switch( type ){ case CHAR: *(char*)number = (char)num; break; case SHORT: *(short*)number = (short)num; break; case INT: *(int*)number = num; break; case DOUBLE: *(double*)number = (double)num; break; default: break; } if( strlen( buf ) >= 1 ) /* * 酸曰 侬互丐月午中丹仪反 晶仄五木化卅中午中丹仪卅及分[ */ return FALSE; return TRUE; } /*---------------------------------------- * src 井日 dels 匹隙烂仄凶 侬毛潸曰轮仁 * 田永弁旦仿永扑亘反巨旦弗□皿匹丐月仪卞镗啦[ * 勾引曰田永弁旦仿永扑亘反绰轮匹五卅中[ * 娄醒 * src 凳今木月 侬 * char* 绰轮允月 侬( 侬 匹隙烂第) * 忒曰袄 * 卅仄 ----------------------------------------*/ void deleteCharFromString( char* src , char* dels ) { int index=0; /* 匹五丐互曰及 侬 匹及 index */ int delength; /* dels 及赢今毛涩烂允月(剂仄反簧蘸失永皿及啃 */ int i=0,j;/* i 反 src 毛伙□皿允月 醒 j 反 dels 毛伙□皿允月 醒 */ delength= strlen( dels ); while( src[i] != '\0' ){ if( src[i] == BACKSLASH ) /* * 田永弁旦仿永扑亘卅及匹巨旦弗□皿允月 * 勾引曰戚及 侬反 椭瘀卞啖 允月 */ src[index++] = src[++i]; /* substitute next char */ else{ for( j = 0 ; j < delength ; j ++ ){ if( src[i] == dels[j] ) /* * 绰轮允月 侬分[方匀化 i 卞 1 毛凶允 */ goto incrementi; } /* * 壅允 卞隙烂今木化中卅中及匹戊疋□允月[ */ src[index++] = src[i]; } incrementi: i++; } src[index] = '\0'; } /*---------------------------------------- * src 井日 dels 匹隙烂仄凶 侬毛潸曰轮仁 * 巨旦弗□皿反卅中 * 娄醒 * src 凳今木月 侬 * char* 绰轮允月 侬( 侬 匹隙烂第) * 忒曰袄 * 卅仄 ----------------------------------------*/ void deleteCharFromStringNoEscape( char* src , char* dels ) { int index=0; /* 匹五丐互曰及 侬 匹及 index */ int delength; /* dels 及赢今毛涩烂允月(剂仄反簧蘸失永皿及啃 */ int i=0,j;/* i 反 src 毛伙□皿允月 醒 j 反 dels 毛伙□皿允月 醒 */ delength= strlen( dels ); while( src[i] != '\0' ){ for( j = 0 ; j < delength ; j ++ ){ if( src[i] == dels[j] ) /* * 绰轮允月 侬分[方匀化 i 卞 1 毛凶允 */ goto incrementi; } /* * 壅允 卞隙烂今木化中卅中及匹戊疋□允月[ */ src[index++] = src[i]; incrementi: i++; } src[index] = '\0'; } /*------------------------------------------------------------ * 隙烂今木凶 侬毛隙烂今木凶 侬匹 五井尹月 * 娄醒 * src char* 凳今木月 侬 * oldc char 凳今木月 侬 * newc char 凳允月 侬 * 忒曰袄 * src ------------------------------------------------------------*/ char* replaceString( char* src, char oldc ,char newc ) { char* cp=src; do{ if( *cp == oldc ) *cp=newc; }while( *cp++ ); return src; } typedef struct tagEscapeChar { char escapechar; char escapedchar; }EscapeChar; static EscapeChar escapeChar[]= { { '\n', 'n' }, { ',', 'c' }, { '|', 'z' }, { '\\', 'y' }, }; char makeCharFromEscaped( char c )//add this function,because the second had it { int i; for( i = 0; i < sizeof( escapeChar )/sizeof( escapeChar[0] ); i++ ) { if( escapeChar[i].escapedchar == c ) { c = escapeChar[i].escapechar; break; } } return c; } /*---------------------------------------- * makeEscapeString匹综岳今木凶 侬 毛葭卞 允 * 娄醒 * src char* 葭卞卅月 侬 [仇木互踏五赘月[ * 忒曰袄 * src 毛忒允[(楮醒毛 木仇卞仄支允中方丹卞) ----------------------------------------*/ char *makeStringFromEscaped( char* src ) {//ttom this function all change,beside copy from the second int i; // CoolFish: Fix bug 2001/10/13 // int srclen = strlen( src ); int srclen = 0; int searchindex=0; // CoolFish: Fix bug 2001/10/13 if (!src) return NULL; srclen = strlen(src); for( i = 0; i < srclen; i ++ ) { // for 2Byte Word if( IS_2BYTEWORD( src[i] ) ){ src[searchindex++] = src[i++]; src[searchindex++] = src[i]; }else if( src[i] == '\\' ) { // 戚及 侬卞垫仁 i++; src[searchindex++] = makeCharFromEscaped( src[i] ); } else { src[searchindex++] = src[i]; } } src[searchindex] = '\0'; return src; } char* makeEscapeString( char* src , char* dest, int sizeofdest) { //ttom this function all change, copy from the second int i; int srclen = 0; int destindex=0; // CoolFish: Fix bug 2001/10/13 if (!src) return NULL; srclen = strlen(src); for( i = 0 ; i < srclen ; i ++ ){ BOOL dirty=FALSE; int j; char escapechar='\0'; if( destindex + 1 >= sizeofdest )break; if( IS_2BYTEWORD( src[i] ) ){ if( destindex + 2 >= sizeofdest )break; dest[destindex] = src[i]; dest[destindex+1] = src[i+1]; destindex += 2; i ++; continue; } for( j = 0; j= length ){ print( "*\n"); break; } src[index++]=src[i]; /* 粮互鞅匀凶仪卞允月 */ backchar = '\0'; continue; } if( src[i] == backchar ) /* * 绰轮允月手元分匀凶日窒手仄卅中[勾引曰绰轮卞卅月[ */ continue; /* * 粮及绰轮允月 侬互卅仁卅匀凶及匹 backchar 反 NULL 侬卞允月 * 仇木互井氏元氏 */ backchar = '\0'; for( j = 0 ; j < delength ; j ++ ){ if( src[i] == dels[j] ){ /* * 绰轮允月 侬 分[ 仇仇匹 continue 仄卅中仪卞方曰 * 域 侬反绰轮仄卅中仪互忡据匹五月[ */ backchar=src[i]; break; } } src[index++]=src[i]; } src[index++]='\0'; } /*---------------------------------------- * hash袄毛忒允[ 侬 卞及心银丹仇午 * 仇木反 公及引引卅及匹失伙打伉术丞反方丹歹井日氏互 * 中中失伙打伉术丞日仄中[ * 娄醒 * s 侬 * 忒曰袄 * int 甩永扑亘袄 *----------------------------------------*/ #define PRIME 211 int hashpjw ( char* s ) { char *p; unsigned int h= 0 ,g; for( p = s ; *p ; p ++ ){ h = ( h<< 4 ) + (*p); if( (g = h & 0xf0000000) != 0){ h = h ^ (g>>24); h = h ^ g; } } return h % PRIME; } /*---------------------------------------- * 夫□市伙卞田奶件玉仄凶末弗永玄毛忒允( TCP ) * 娄醒 * port 夫□市伙禾□玄 * 忒曰袄 * -1 巨仿□ * 1. socket扑旦 丞戊□伙及巨仿□ * 2. bind扑旦 丞戊□伙及巨仿□ * 3. listen扑旦 丞戊□伙及巨仿□ ----------------------------------------*/ int bindlocalhost( int port ) { struct sockaddr_in sin; /*夫□市伙失玉伊旦迕*/ int sfd; /*末弗永玄犯奴旦弁伉皿正*/ int rc; /*夫□市伙伉正□件戊□玉*/ /*AF_INET皿夫玄戊伙 及 SOCK_STREAM */ sfd = socket( AF_INET, SOCK_STREAM, 0 ); if( sfd == -1 ){ print( "%s\n" , strerror(errno ) ); return -1; } if( getReuseaddr() ) { int sendbuff; /* 夫□市伙失玉伊旦及疯厍迕 */ setsockopt( sfd, SOL_SOCKET, SO_REUSEADDR, (char *)&sendbuff, sizeof( sendbuff)); } /*0弁伉失仄化袄毛医 */ memset( &sin ,0, sizeof( struct sockaddr_in ) ); sin.sin_family=AF_INET; sin.sin_port = htons( port ); sin.sin_addr.s_addr = INADDR_ANY; rc = bind( sfd , (struct sockaddr*)&sin, sizeof(struct sockaddr_in)); if( rc == -1 ){ print( "%s\n" , strerror(errno ) ); return -1; } rc = listen( sfd , 5 ); if( rc == -1 ){ print( "%s\n" , strerror(errno ) ); return -1; } return sfd; } /*---------------------------------------- * 戊生弁玄允月[( TCP ) * 娄醒 * hostname 涛粮允月石旦玄 * (xx.xxx.xxx.xxx匹手中中仄xxx.co.jp午井匹手中中) * port 涛粮允月石旦玄及禾□玄 * 忒曰袄 * 犯奴旦弁伉皿正( -1 及凛反巨仿□ ) ----------------------------------------*/ int connectHost( char* hostname , unsigned short port ) { struct sockaddr_in sock; /*connect及凛卞银丹*/ struct hostent* hoste; /*hostname及hostent*/ int fd; /*戊生弁玄仄凶末弗永玄犯奴旦弁伉皿正*/ int lr; /*夫□市伙伉正□件戊□玉*/ memset( &sock , 0 , sizeof( struct sockaddr_in ) ); sock.sin_family = AF_INET; sock.sin_port = htons( port ); /* dot notation 井升丹井毛譬屯月 */ sock.sin_addr.s_addr = inet_addr( hostname ); if( sock.sin_addr.s_addr == -1 ){ /* *巨仿□ dot notation 匹反卅中[仄凶互匀化 骚及 蟆荸瑁互 邰分[ * dns毛夫井卅仃木壬卅日卅中凛反夫五卞中仁及手仇仇匹支匀化仁木月[ */ hoste = gethostbyname( hostname ); if( hoste == NULL ){ print( "获取主机名失败,请设置正确的DNS地址!\n" ); return -1; } memcpy((void*)&sock.sin_addr.s_addr , hoste->h_addr , sizeof(struct in_addr) ); } /*末弗永玄毛综月*/ fd = socket( AF_INET, SOCK_STREAM , 0 ); if( fd == -1 ){ print("Cannot Create Socket(%s errno:%d)\n" ,strerror( errno ) ,errno ); return -1; } /*戊生弁玄允月*/ lr = connect(fd,(struct sockaddr*)&sock,sizeof(struct sockaddr_in)); if( lr != 0 ){ print("Cannot connect. (%s errno:%d)\n" ,strerror( errno ) ,errno ); return -1; } return fd; } /*---------------------------------------- * 田永白央及 卞荼垫筏寞互丐月井升丹井[ * 剂仄镀中失伙打伉术丞卞 凳[ * 娄醒 * char* 譬屯月 侬 * 忒曰袄 * 赓卞苇勾井凶荼垫筏寞及匏 失弁本旦仄凶凛卞中中袄[ * 升丹中丹仪井午中丹午} * existsNewLinwCharacter( "aho\nhoge\n" ) * 分午}忒曰袄反 3 卞卅月[仇木卞镗啦允月仪[ * -1 卅井匀凶 * ----------------------------------------*/ int existsNewLineCharacter( char* line ) { #if 1 char* old = line; do{ if( *line == NEWLINE)return line-old; }while( *(line++) ); #else int i; /*伙□皿 醒*/ int length = strlen( line ); /*譬屯月 侬 及赢今*/ for( i = 0 ; i < length ; i ++ ){ if( line[i] == NEWLINE ) return i; } #endif return -1; } /*---------------------------------------- * 侬 卞 侬互 歹木月桦赭毛井尹允[窒 井毛瑁户木月[ * nindex( string , c , 1 ) 午 index( string , c ) 反 equivalent 匹丐月[ * number互 引凶反0及凛反忒曰袄反}string午 元匹丐月[ * * 娄醒 * string char* 譬屯月 侬 * c int 譬屯月 侬 * number int 歹月 寞 * 忒曰袄 * 域谯仄凶 侬及禾奶件正□ * NULL 心勾井日卅井匀凶[ ----------------------------------------*/ char* nindex( char* string, int c , int number ) { int i; /*伙□皿 醒*/ int num=0; int length = strlen( string ); /*譬屯月 侬 及赢今*/ if( number <= 0 )return string; for( i = 0 ; i < length ; i ++ ){ if( string[i] == c )num++; if( number == num ) return &string[i]; } return NULL; } BOOL rrd( char* dirname , STRING64* buf ,int bufsize, int* index) { DIR* d; char dirn[1024]; d = opendir( dirname ); if( d == NULL )return FALSE; while( 1 ){ struct dirent* dent; struct stat st; dent = readdir( d ); if( dent == NULL ){ if( errno == EBADF ){ errorprint; closedir(d); return FALSE; }else /* successful */ break; } /* . 匹铵引月白央奶伙反 引卅中 */ if( dent->d_name[0] == '.' )continue; snprintf(dirn, sizeof(dirn), "%s/%s" , dirname,dent->d_name ); if( stat( dirn , &st ) == -1 )continue; if( S_ISDIR( st.st_mode ) ){ if( rrd( dirn , buf ,bufsize, index) == FALSE ){ closedir(d); return FALSE; } }else{ if( *index >= bufsize ) break; strcpysafe( buf[*index].string , sizeof( buf[*index].string ), dirn ); (*index)++; } } closedir(d); return TRUE; } int rgetFileName( char* dirname , STRING64* string, int size) { int index=0; if( rrd( dirname , string ,size ,&index ) == FALSE )return -1; else return index; } BOOL checkStringsUnique( char** strings, int num ,int verbose) { int i,j; for( i = 0 ; i < num - 1; i ++ ){ for( j = i+1 ; j < num ; j ++){ if( !strcmp( strings[i] ,strings[j] )){ if( verbose ) print( "Overlapp string is %s\n", strings[i] ); return FALSE; } } } return TRUE; } BOOL PointInRect( RECT* rect, POINT* p ) { if( rect->x <= p->x && p->x <= rect->x + rect->width && rect->y <= p->y && p->y <= rect->y + rect->height ) return TRUE; return FALSE; } BOOL CoordinateInRect( RECT* rect, int x, int y) { POINT p={x,y}; return PointInRect(rect,&p); } int clipRect( RECT *rect1, RECT *rect2, RECT *ret ) { if( rect1->x > rect2->x + rect2->width -1 || rect2->x > rect1->x + rect1->width -1 || rect1->y > rect2->y + rect2->height -1 || rect2->y > rect1->y + rect1->height -1 ) return 0; ret->x = max( rect1->x, rect2->x ); ret->y = max( rect1->y, rect2->y ); ret->width = min( rect1->x+rect1->width, rect2->x+rect2->width ) - ret->x; ret->height = min( rect1->y+rect1->height, rect2->y+rect2->height ) - ret->y; return 1; } BOOL isstring1or0( char* string ) { if( strcasecmp(string,"TRUE" ) == 0 )return TRUE; if( strcasecmp(string,"FALSE") == 0 )return FALSE; if( strcasecmp(string,"1" ) == 0 )return TRUE; if( strcasecmp(string,"0" ) == 0 )return FALSE; if( strcasecmp(string,"ON" ) == 0 )return TRUE; if( strcasecmp(string,"OFF" ) == 0 )return FALSE; return FALSE; } void easyGetTokenFromString( char *src,int count,char*output,int len ) { int i; int counter = 0; if( len <= 0 )return; #define ISSPACETAB( c ) ( (c) == ' ' || (c) == '\t' ) for(i=0;;i++){ if( src[i]=='\0'){ output[0] = '\0'; return; } if( i > 0 && ! ISSPACETAB( src[i-1] ) && ! ISSPACETAB( src[i] ) ){ continue; } if( ! ISSPACETAB( src[i]) ){ counter++; if( counter == count){ /* copy it */ int j; for(j=0;j1---- * * ------------------------------------------------------------*/ float linearDiv( float val1 , float val2 , float d ) { return val1 + ( val2 - val1 ) * ( d ); } /*------------------------------------------------------------ * 侬 及卅井井日 啦卅 侬毛len 侬午曰分仄化仿件母丞卅 * 侬 毛勾仁月[NPC及仿件母丞由旦伐□玉戏岳卞勾井匀化月 * * char *cand : 仇仇井日蓟少[跳 及 侬[ * char *out : 请 田永白央 * int len : 请 及赢今 ------------------------------------------------------------*/ void makeRandomString( char *cand , char *out , int len ) { int i; int l = strlen( cand ); for(i=0;i= baselen; i ++ ) { tmp[i] = src % baselen; src /= baselen; } i--; if( minus ){ *out = '-'; *(out+1) = base[src]; for( j = 2; i >= 0; i --, j ++ ) { if( j > outlen - 2 ) return NULL; *(out+j) = base[tmp[i]]; } } else { *out = base[src]; for( j = 1; i >= 0; i --, j ++ ) { if( j > outlen - 2 ) return NULL; *(out+j) = base[tmp[i]]; } } *(out+j) = '\0'; return( out); #else /* 衙中 */ #define CNV_NUMBER 62 int i, j; char base[] = { "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"}; int tmp[64]; int src; if( a < 0 ) return( NULL); if( a < CNV_NUMBER) { *out = base[a]; *(out+1) = '\0'; return( out); } src = a; for( i = 0; src >= CNV_NUMBER; i ++ ) { tmp[i] = src % CNV_NUMBER; src /= CNV_NUMBER; } i--; *out = base[src]; for( j = 1; i >= 0; i --, j ++ ) { if( j > outlen - 2 ) return NULL; *(out+j) = base[tmp[i]]; } *(out+j) = '\0'; return( out); #undef CNV_NUMBER #endif } /* * 芨尹日木凶 及 匹]醒袄互褐 仄化中月井譬屯月楮醒 * -1 反覆擂陆午中丹 卅楮醒 * * 娄醒 * src *int 譬屯月葭及 * srclen int src 及 五今 * * 曰袄 褐 丐曰 TRUE * 卅仄 FALSE */ BOOL checkRedundancy( int *src, int srclen) { int i,j; int ret = FALSE; for( i = 0; i < srclen; i ++ ) { if( *(src + i) != -1 ) { for( j = i+1; j < srclen; j ++ ) { if( *(src +i) == *( src+j) ) { ret = TRUE; break; } } } } return ret; } void shuffle_ints( int num, int *a, int t ) { int i; for(i=0;i> 8 ) & 0xFF ] ^ ( crc << 8 ) ^ BitTable[ p[ i ] ] ); } return crc; } // Add Code By Shan 2001.06.16 INLINE double time_diff(struct timeval subtrahend, struct timeval subtractor) { return( (subtrahend.tv_sec - subtractor.tv_sec) + (subtrahend.tv_usec - subtractor.tv_usec ) / (double)1E6 ); } char *strcpy_s(char *des, size_t n, const char *src) { assert(des != NULL && src != NULL); if(des == NULL || src == NULL || n == 0) { return NULL; } char *TM_RetChar = des; int i = 0; while( (*des++ = *src++) ) { i++; if( i >= n ) { i--; break; } } TM_RetChar[i] = '\0'; return TM_RetChar; } char *replace(char *source,char *sub,char *rep) { char *result; /*pc1 是复制到结果result的扫描指针*/ /*pc2 是扫描 source 的辅助指针*/ /*pc3 寻找子串时,为检查变化中的source是否与子串相等,是指向sub的扫描指针 */ /*找到匹配后,为了复制到结果串,是指向rep的扫描指针*/ char *pc1, *pc2, *pc3; int isource, isub, irep; isub =strlen(sub); /*对比字符串的长度*/ irep =strlen(rep); /*替换字符串的长度*/ isource=strlen(source);/*源字符串的长度*/ if(NULL == *sub) return strdup(source); /*申请结果串需要的空间*/ result = (char *)malloc(( (irep > isub) ? (float)strlen(source) / isub* irep+ 1:isource ) * sizeof(char)); pc1 = result; /*为pc1依次复制结果串的每个字节作准备*/ while(*source != NULL) { /*为检查source与sub是否相等作准备,为pc2,pc3 赋初值*/ pc2 = source; pc3 = sub; /* 出循环的(任一)条件是: * *pc2不等于 *pc3(与子串不相等) * pc2 到源串结尾 * pc3 到源串结尾(此时,检查了全部子串,source处与sub相等) *****************************************************/ while(*pc2 == *pc3 && *pc3 != NULL && *pc2 != NULL) pc2++, pc3++; /* 如果找到了子串,进行以下处理工作*/ if(NULL == *pc3) { pc3 = rep; /*将替代串追加到结果串*/ while(*pc3 != NULL) *pc1++ = *pc3++; pc2--; source = pc2; /* 检查 source与sub相等的循环结束后, * pc2 对应的位置是在 sub 中串结束符处。该是源串中下一个位置。 * 将source 指向其前面一个字符。 */ } else /*如果没找到子串,下面复制source所指的字节到结果串*/ *pc1++ = *source; source++; /* 将source向后移一个字符*/ } *pc1 = NULL; return result; } int getHash ( char* s ) { int i; int h=0; for(i=0;; i++){ if( s[i] == 0 ){ break; } h += s[i]; } return h; } //int RAND (int x,int y) //{ // srand((int)time(0)); // return (x-1)+1+(int)( (double)(y-(x-1))*rand()/(RAND_MAX+1.0)); //}