commonfunc.c
上传用户:bilang918
上传日期:2010-03-24
资源大小:558k
文件大小:3k
- void breakpoint(char *name,char *message){
printf("n---DEBUG---n%s:%sn",name,message);
getchar();
}
void monitor(char *logfilename, char *description, char *name){
time_t tempt;
char *temps;
int fd;
fd = open(logfilename, O_WRONLY|O_CREAT|O_APPEND);
temps = malloc(256);
tempt = time(&tempt);
strcpy(temps, (char*)ctime(&tempt));
strcat(temps, name);
strcat(temps, " --- ");
strcat(temps, description);
strcat(temps, "nn");
write(fd, temps, strlen(temps));
free(temps);
close(fd);
return;
}
int gettoken(unsigned char *buf,char token){
int length;
int i;
length = strlen(buf);
for(i = 0; i< length; i++){
if(buf[i] == token) break;
//if (dbgflag) printf("ngettoken():buf[i]:%c",buf[i]);
}
if(i >= length) return(-1);
return(i);
}
void trim(char *str,char token){ //trim the space before the string
int length;
int i;
length = strlen(str);
for(i = 0; i< length; i++){
if(str[i] != token) break;
}
strncpy(str,str+i,length-i);
str[length-i]=0;
//strncpy(str,str,i);
/*while(str[length] != token){
length--;
}*/
}
void trimb(char *str,char token){ //trim the space after the string
int length;
int i;
length = strlen(str);
while((str[length-1]==13) || (str[length-1]==10)){
str[length-1]=0;
length--;
}
if (strchr(str,token)!=NULL){
for(i = length; i >= 0; i--){
if (str[i-1] != token){
str[i]=0;
break;
}
}
}
}
void replace(char *str,char token,char replacement){
int length;
int i;
length = strlen(str);
for(i = 0; i < length; i++){
if (str[i] == token){
str[i]=replacement;
}
}
}
void split(struct variant *varPtr,char *str,char token){ //split string by token
int length,sublength;
int i;
bzero(varPtr,sizeof(struct variant));
//trim(str,' ');
varPtr->eleno=0;
sublength=0;
i=0;
length = strlen(str);
//if (dbgflag) printf("nsplit():length:%d",length);
while(strchr(str,token)!=NULL){
trim(str,token);
i=gettoken(str,token);
//if (dbgflag) printf("nsplit():i:%d",i);
//getchar();
if (i>0){
varPtr->eleno++;
strncpy(varPtr->element[varPtr->eleno].var,str,i);
varPtr->element[varPtr->eleno].var[i]=0;
varPtr->element[varPtr->eleno].length=i;
strncpy(str,str+i+1,length-i-1);
str[length-i-1]=0;
}
}
varPtr->eleno++;
if (varPtr->eleno >= 50) return;
strcpy(varPtr->element[varPtr->eleno].var,str);
varPtr->element[varPtr->eleno].length=strlen(str);
}
void getcurtime(char *curtime){
time_t tempt;
struct tm *tmp;
char strtime[128];
//tmp = malloc(sizeof(struct tm));
tempt = time(&tempt);
tmp = localtime(&tempt);
sprintf(strtime, "%d-%d-%d %d:%d:%d",
tmp->tm_year+1900, tmp->tm_mon+1, tmp->tm_mday,
tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
strcpy(curtime, strtime);
//free(tmp);
}
void deletetime(char *deltime,long ttl){
time_t tempt;
struct tm *tmp;
char strtime[128];
//tmp = malloc(sizeof(struct tm));
tempt = time(&tempt);
tempt = tempt+ttl*3600;
tmp = localtime(&tempt);
sprintf(strtime, "%d-%d-%d %d:%d:%d",
tmp->tm_year+1900, tmp->tm_mon+1, tmp->tm_mday,
tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
strcpy(deltime, strtime);
//free(tmp);
}
void log_file(char *filepath,char* content){
- FILE * fd;
- int pid;
- if((fd=fopen(filepath,"r+"))==NULL)
- {
- fd=fopen(filepath,"wr+") ;
- }
- fseek(fd,0,2);
- fputs(content,fd);
- fputc(10,fd);
- fclose(fd);
- }