tools.c

来自「Magic C++!的源代码」· C语言 代码 · 共 972 行 · 第 1/2 页

C
972
字号
}/*get a section in configuation file*/int util_getinfo_str(char *section ,char *key , char *value , char * szFileName){ 	FILE *fd;     char buf[1024];        char cfile[100];    char line[100];    		char thekey[100];	char thesection[100];	char thevalue[100];	char cursection[100];	char *env;	memset( thekey  , 0 , sizeof( thekey ));	memset( thesection , 0 , sizeof( thesection ));	memset( cursection , 0 , sizeof( cursection ));	memset( thevalue , 0 , sizeof( thevalue ));    memset(cfile,0,sizeof(cfile));    memset(buf, 0 , sizeof( buf ));    memset(line , 0 , sizeof( line ));    /*    if( szInstallDir[ 0 ] != '/' )	{		fprintf(stderr,"Can not get install dir\n");		return -1;	}	sprintf( cfile , "%s/etc/magic.ini" , szInstallDir);	*/    if(( fd = fopen(szFileName,"r")) == NULL)    {    	util_err_log("open file error!" , __FILE__ , __LINE__ , errno );        return -1;    }	while( util_getline( fd , 1023 , line) != -1)	{		/*it is a section*/ 		memset( thekey , 0 ,sizeof(thekey));		memset( thevalue , 0 ,sizeof(thevalue));		memset( thesection , 0 , sizeof(thesection ));		util_trim( line );		if( line[0] == '#' )			continue;		if( util_getsection( line , thesection ) == 0 )		{  			memset( cursection , 0 , sizeof( cursection ) );			strcpy( cursection , thesection );			continue;		}		/*it is a key*/		else if( util_getkeyvalue( line , thekey , thevalue ) == 0 )		{			/*it's the same with query value*/			if( strcasecmp( cursection , section ) == 0 && strcasecmp( thekey , key) == 0 )			{				strcpy( value ,  thevalue );				fclose(fd );				return 0;			}					}		else if ( strlen( line ) != 0 )		{			util_log("an unrecognise format:%s \n" , line );			fclose(fd );			return -1;		}		memset( line , 0 ,sizeof(line));		}	if(debug)		util_log("can not find the key: %s section: %s " , key , section );	fclose(fd );	return -1;}/*pick out section name *//*return -1 for error format*/int util_getsection(char *line , char *section){	char *p1 , *p2;	p1 = strchr( line , '[' );	p2 = strchr( line , ']' );	if( p1 == 0 || p2 == 0 )	{		return -1;	}	else 	{		memset( section , 0 , sizeof( section ));		strncpy( section , p1 + 1 , p2 - p1 - 1);				util_trim( section );		return 0;	}	    }/*pick out section value *//*return -1 for error format*/int util_getkeyvalue(char *line ,char *thekey ,char *thevalue ){	char *p1;		p1 = strchr( line , '=' );	if( p1 == 0 )		return -1;		strncpy( thekey , line , p1 - line );	strcpy( thevalue , p1 + 1 );	util_trim( thekey );	util_trim( thevalue );	/*it's empty value*/	if( strlen( thekey ) == 0 || strlen( thevalue ) == 0 )		return -1;	return 0;}/*pick out the key of line string(with out delete blank space) *//*return -1 for error format*//*return 0 for success*/int util_getkeyvalue_notrim(char *line ,char *thekey ,char *thevalue ){	char *p1;		p1 = strchr( line , '=' );	if( p1 == 0 )		return -1;		strncpy( thekey , line , p1 - line );	strcpy( thevalue , p1 + 1 );	/*empty value*/	if( strlen( thekey ) == 0 || strlen( thevalue ) == 0 )		return -1;	return 0;}/*get one line in file/*done not check file formatreturn -1 for end of file*/int util_getline( FILE *fd , int nLineLen , char *line){	int c;	int i = 0;		memset( line , 0 , sizeof( line) );	c = fgetc( fd );	if( c == EOF )		return -1;	while( c == '\r' || c == '\n' )	{		c = fgetc( fd );		if( c == EOF )			return -1;	}	while( c != '\r' && c != '\n' && c != EOF && i < nLineLen )	{				line[ i++ ] = c;		c = fgetc(fd );	}	line[ i ] = '\0';	return i;}static const char *crlfs[] ={		"\x0d\x0a",			/*	DOS/Windows style*/	"\x0a\x0d",			/*	UNIX style*/	"\x0d\x0d\x0a",     /*UNIX style2*/	"\x0a"				/*Macintosh style*/};	/*enforced getline function , you can specify the type of file*/int util_getlineex( FILE *fd , int nLineLen , char *line ,int nstype){	int c;	int i = 0;		int nCrlfPtr = 0;	char *crlf;	memset( line , 0 , sizeof( line) );	c = fgetc( fd );	if( c == EOF )		return -1;	if( nstype < 0 || nstype > 3 )	{		util_err_log("util_getlineex nstype error!\n",__FILE__,__LINE__,errno );		return -1;	}	crlf = (char *)crlfs[ nstype ];	while(  c != EOF && i < nLineLen )	{		if ((char) c == crlf[nCrlfPtr])		{			nCrlfPtr ++;			/*到达了换行符的中止位置*/			if (crlf[nCrlfPtr] == 0)			{				nCrlfPtr = 0;				break;			}		}		else			nCrlfPtr = 0;		line[ i++ ] = c;		c = fgetc(fd );	}	line[ i ] = '\0';	return i;}/*delete control characters*/void util_trim(char *buf ){	char tmp[1024];	char *pbegin = buf;	int nlen = strlen(buf);		char *pend = buf + nlen - 1;	memset( tmp , 0 , sizeof( tmp ));	while( pbegin < buf + nlen && strchr( "\r\n\t " , *pbegin))		pbegin ++;	while( pend >= buf && strchr( "\r\n\t " , *pend))		pend --;	if( pend >= pbegin )		strncpy( tmp , pbegin , pend - pbegin + 1);	memset( buf , 0 , sizeof( buf ));	strcpy( buf , tmp );}/*get last modified time of a file*/int util_getfiletime(char *filename , char *time){		struct stat buf;	struct tm dc;	if( stat(filename , &buf ) == -1 )	{		util_err_log("stat error!" , __FILE__ , __LINE__ , errno );		return -1;	}	localtime_r(&buf.st_mtime,&dc);	sprintf(time,"%04d%02d%02d%02d%02d%02d",dc.tm_year+1900,dc.tm_mon+1,dc.tm_mday,dc.tm_hour,dc.tm_min,dc.tm_sec);	return 0;}int util_getfilesize(char *filename ){	struct stat buf;	if( stat(filename , &buf ) == -1 )	{		util_err_log("stat error!" , __FILE__ , __LINE__ , errno );		return -1;		}	return buf.st_size;}/*get last modified time and size of file*//*return size of file*//*-1 for failed*/long util_getfileinfo( char *filename , char *time ){	if( util_getfiletime(filename , time) == -1 )		return -1;	return  util_getfilesize(filename ) ;}void util_byte_copy(to,n,from)register char *to;register unsigned int n;register char *from;{  for (;;) {    if (!n) return; *to++ = *from++; --n;    if (!n) return; *to++ = *from++; --n;    if (!n) return; *to++ = *from++; --n;    if (!n) return; *to++ = *from++; --n;  }}/*test main program*//*main(){	util_log("%s%s%d\n","test","test",3);	util_err_log("testerr",__FILE__ , __LINE__,errno );	util_sem_init();	if( util_sem_lock( 1000 ) == -1)		printf("error to lock!\n");		if( util_sem_lock( 3000 ) == -1)		printf("error lock it's right!\n");	util_sem_unlock();	util_sem_unlock();	util_sem_unlock();	if( util_sem_lock( 1000 ) == -1)		printf("error to lock!\n");	if( util_sem_lock( 3000 ) == -1)		printf("error lock it's right!\n");	util_sem_unlock();		util_sem_remove();				char time[14];	int nsize = -1;	memset( time , 0 , sizeof( time ));	if( (nsize = util_getfileinfo( "/usr/ken/tsh_sun/tools.o" ,		time ) )== -1)		printf("getfileinfo failed!\n");	printf( "%s , %d " ,time , nsize );			char value[10];	int nvalue;	memset( value , 0 , sizeof( value ));		if( util_getinfo_int( "a" , "test" , &nvalue ) == -1 )		printf("error!\n");	else		printf("value=%d\n", nvalue);		return 0;	}*//*print a string with control character*/void util_checkline(char * line , int n ){ 	char dupline[102400]; 	int i ; 	int j;	memset( dupline , 0,sizeof(dupline));	for( i = 0,j=0 ; i < n ; i++ )	{		if( line[i] == '\r' )		{			dupline[ j++ ] = '\\';			dupline[ j++ ] = 'r';			}		else if( line[i] == '\n' )		{			dupline[ j++ ] = '\\';			dupline[ j++ ] = 'n';		}		else if(line[ i ]== '\0' )		{			dupline[ j++ ] = '\\';			dupline[ j++ ] = '0';			}					else			dupline[ j ++] = line[i];	}	dupline[ j ] = '\0';	util_log("%s\n",dupline);}/*setup alarm*/int util_alarm(int msec){	int z;	int usec = msec * 1000;	struct itimerval old_timer;	struct itimerval new_timer;	new_timer.it_interval.tv_sec = 0 ;	new_timer.it_interval.tv_usec = 0 ;	new_timer.it_value.tv_sec = (int)(usec / 1000000) ;	new_timer.it_value.tv_usec = usec % 1000000 ;		z = setitimer( ITIMER_REAL , &new_timer , &old_timer );	if( z )	{		return -1;	}	if( debug )	util_log("set timeval success:tv_sec=%d,tv_usec=%d\n",		new_timer.it_value.tv_sec,new_timer.it_value.tv_usec );	return 0;}int util_strupr(char *str){	int i = 0;	while( str[ i ] != '\0' )	{		str[ i ] = toupper( str[ i ] );			i++;	}	return 0;}int util_strlwr(char *str){	int i = 0;	while( str[ i ] != '\0' )	{		str[ i ] = tolower( str[ i ] );			i++;	}		return 0;} int util_getfilestyle(FILE *fd){	int nstyle = 0 ;	int I;	int nSize;	char buf[10240];	memset( buf , 0 , sizeof(buf));	if( ( nSize = fread( buf , 1 , sizeof(buf) , fd ) ) < 0 )	{		util_err_log("fread" , __FILE__,__LINE__,errno );		return -1;			}	for (I = 0; I < nSize ; I ++)	{		if (buf[I] == '\n')			break;	}	if (I == nSize)	{		/*	By default (or in the case of empty file), set DOS style*/		nstyle = CRLF_STYLE_DOS;	}	else	{		/*	Otherwise, analyse the first occurance of line-feed character*/		if (I > 0 && buf[I - 1] == '\r')   		{			if( I > 1 && buf[ I - 2 ] == '\r' )  /*"\r\r\n"*/				nstyle = CRLF_STYLE_UNIX2;			else				nstyle = CRLF_STYLE_DOS;		}		else		{			if (I < nSize - 1 && buf[I + 1] == '\r' )				nstyle = CRLF_STYLE_UNIX;			else				nstyle = CRLF_STYLE_MAC;						}	}	/*move file point*/	fseek( fd , 0 , SEEK_SET ); 	return nstyle;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?