global.cpp
来自「基于linux下的故障录波前置程序」· C++ 代码 · 共 598 行
CPP
598 行
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<errno.h>
#include"Global.h"
///////////////////////////////////////////////////////////////////////
//
// 函数名 : GB_CheckMySlef
// 功能描述 : 检查进程唯一性
// 参数 : char *Killname
// 返回值 : void
//
///////////////////////////////////////////////////////////////////////
void GB_CheckMySlef(char *Killname)
{
FILE * f_c;
int mypid;
char buffer[100];
char name[20];
char name1[20];
char name2[20];
char name3[20];
char fotbuffer[100];
char szBuffer[100];
pid_t pd;
char *killname;
killname=Killname;
pd =getpid();
if(system("ps -a> search_file")==-1)
{
#ifdef _DEBUG
printf("system ps -a>search_file cmd error!\n");
#endif
return;
}
f_c=fopen("search_file", "r");
if(!f_c)
{
#ifdef _DEBUG
printf("open search_file error!\n");
#endif
return;
}
fgets(buffer,100,f_c);
while(!feof(f_c))
{
fgets(buffer,100,f_c);
sscanf(buffer,"%d %s %s %s",&mypid,name,name1,name2);
if(!strcmp(killname,name2))
{
if(mypid!=pd)
{
sprintf(fotbuffer,"kill -9 %d",mypid);
system(fotbuffer );
sprintf(szBuffer,"%d process already killed,%dprocess start!",pd,mypid);
#ifdef _DEBUG
printf("%s\n",szBuffer);
#endif
}
else
continue;
}
}
fclose(f_c);
if(system("rm -f search_file")==-1)
{
return;
}
return;
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : GB_trim
// 功能描述 : 去字符串空格
// 参数 : char *buf
// 返回值 : void
//
///////////////////////////////////////////////////////////////////////
void GB_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 );
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : GB_getline
// 功能描述 : 读行字符
// 参数 : FILE *fd
// 参数 : int nLineLen
// 参数 : char *line
// 返回值 : int
//
///////////////////////////////////////////////////////////////////////
int GB_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;
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : GB_SetIniString
// 功能描述 : 写string类型的ini文件
// 参数 : char *fp
// 参数 : const char *pSection
// 参数 : const char *pIdent
// 参数 : const char *pValue
// 返回值 : int
//
///////////////////////////////////////////////////////////////////////
int GB_SetIniString(char *fp,const char *pSection,const char *pIdent,const char *pValue)
{
int i=0;
int j=0;
int k=0;
int iBufferMore = 0;
int bKeyFound=0,bIdentFound=0;
int iKeyPos=0,iIdentPos=0;
struct stat statbuf;
stat(fp,&statbuf);
iBufferLen = 0; bBufferChanged = 0;
if ((fp1=fopen(fp, "r+")) == NULL) return -1;
fread(szBuffer,statbuf.st_size,1,fp1);
iBufferLen = statbuf.st_size;
if (!strlen(pSection) || !strlen(pIdent) || (fp1 == NULL)) return -1;
while (i<iBufferLen)
{
while ((i<iBufferLen) &&
((szBuffer[i]==' ') || (szBuffer[i]=='\t'))) i++;
if (i>=iBufferLen)
break;
if (szBuffer[i]=='#')
{
while ((i<iBufferLen) && (szBuffer[i] != '\n')) i++;
if (i>=iBufferLen) break;
i++;
} else
{
if (szBuffer[i]=='[')
{
i++;
while ((i<iBufferLen) &&
((szBuffer[i]==' ') || (szBuffer[i]=='\t'))) i++;
if (i>=iBufferLen)
break;
if (strncmp(szBuffer+i, pSection, strlen(pSection))==0)
{
i += strlen(pSection);
while ((i<iBufferLen) &&
((szBuffer[i]==' ') || (szBuffer[i]=='\t')))
i++;
if (i>=iBufferLen)
break;
if (szBuffer[i]==']')
bKeyFound=1;
i++;
while ((i<iBufferLen) && (szBuffer[i]!='\n')) i++;
if (i>=iBufferLen) break;
i++;
iKeyPos = i;
} else
{
while ((i<iBufferLen) && (szBuffer[i]!='\n')) i++;
if (i>=iBufferLen)
break;
i++;
}
} else
{
if (!bKeyFound)
{
while ((i<iBufferLen) && (szBuffer[i] != '\n')) i++;
if (i>=iBufferLen) break;
i++;
}
else
{
if (strncmp(szBuffer+i, pIdent, strlen(pIdent))==0)
{
i += strlen(pIdent);
if (i>=iBufferLen)
break;
while ((i<iBufferLen) &&
((szBuffer[i]=='\t') || (szBuffer[i]==' ')))
i++;
if (szBuffer[i] == '=')
{
i++; iIdentPos=i;
bIdentFound=1;
break;
} else
{
while ((i<iBufferLen) && (szBuffer[i]!='\n'))
i++;
if (i>=iBufferLen) break;
i++;
}
} else
{
while ((i<iBufferLen) && (szBuffer[i]!='\n'))
i++;
if (i>=iBufferLen)
break;
i++;
}
}
}
}
}
if (bKeyFound)
{
if (bIdentFound)
{
i=iIdentPos; j=0;
while ((i<iBufferLen) && (szBuffer[i] != '\n'))
{
i++; j++;
}
if (strlen(pValue)<=j)
{
strncpy(szBuffer+iIdentPos, pValue, strlen(pValue));
for (i=strlen(pValue); i<j; i++) szBuffer[iIdentPos+i]=' ';
iBufferMore = 0;
}
else
{
k = strlen(pValue);
for (i=iBufferLen; i>=iIdentPos+j; i--)
szBuffer[i+k-j] = szBuffer[i];
strncpy(szBuffer+iIdentPos, pValue, strlen(pValue));
iBufferMore = k-j;
}
}
else
{
i = strlen(" = \n")+strlen(pIdent)+strlen(pValue);
for (j=iBufferLen-1; j>=iKeyPos; j--)
szBuffer[j+i]=szBuffer[j];
sprintf(szBuffer+iKeyPos, "%s = %s\n", pIdent, pValue);
iBufferMore = i;
}
bBufferChanged = 1;
}
else
{
sprintf(szBuffer+iBufferLen,
"\n[%s]\n%s = %s", pSection, pIdent, pValue);
iBufferMore = strlen("\n[]\n = ")+strlen(pSection)
+strlen(pIdent)+strlen(pValue);
bBufferChanged = 1;
}
iBufferLen += iBufferMore;
if (bBufferChanged)
{
rewind(fp1);
fwrite(szBuffer, iBufferLen, 1, fp1);
}
if (!fclose(fp1))
return 0;
else
return -1;
return 0;
}
int GB_SetIniInteger(char *fp,const char *pSection, const char *pIdent, const int pValue)
{
char szTempBuffer[MAX_VALUE_BUFFER_SIZE];
sprintf(szTempBuffer, "%d", pValue);
if ( GB_SetIniString(fp,pSection, pIdent, szTempBuffer) == 0 )
{
return 0;
}
else
return -1;
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : GB_SetIniLong
// 功能描述 : 写long类型的ini文件
// 参数 : char *fp
// 参数 : const char *pSection
// 参数 : const char *pIdent
// 参数 : const long pValue
// 返回值 : int
//
///////////////////////////////////////////////////////////////////////
int GB_SetIniLong(char *fp,const char *pSection, const char *pIdent, const long pValue)
{
char szTempBuffer[MAX_VALUE_BUFFER_SIZE];
sprintf(szTempBuffer, "%ld", pValue);
if ( GB_SetIniString(fp,pSection, pIdent, szTempBuffer) == 0 )
{
return 0;
}
else
{
return -1;
}
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : GB_SetIniDouble
// 功能描述 : 写double类型的ini文件
// 参数 : char *fp
// 参数 : const char *pSection
// 参数 : const char *pIdent
// 参数 : const double pValue
// 返回值 : int
//
///////////////////////////////////////////////////////////////////////
int GB_SetIniDouble(char *fp,const char *pSection, const char *pIdent, const double pValue)
{
char szTempBuffer[MAX_VALUE_BUFFER_SIZE];
sprintf(szTempBuffer, "%g", pValue);
if ( GB_SetIniString(fp,pSection, pIdent, szTempBuffer) == 0 )
{
return 0;
}
else
{
return -1;
}
}
///////////////////////////////////////////////////////////////////////////////////
int GB_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);
GB_trim( section );
return 0;
}
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : GB_getkeyvalue
// 功能描述 : 获得关键字的值
// 参数 : char *line
// 参数 : char *thekey
// 参数 : char *thevalue
// 返回值 : int
//
///////////////////////////////////////////////////////////////////////
int GB_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 );
GB_trim( thekey );
GB_trim( thevalue );
if( strlen( thekey ) == 0 || strlen( thevalue ) == 0 )
return -1;
return 0;
}
int GB_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 );
if( strlen( thekey ) == 0 || strlen( thevalue ) == 0 )
return -1;
return 0;
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : GB_GetInfo_Str
// 功能描述 : 从ini文件中获得string类型的值
// 参数 : char *section
// 参数 : char *key
// 参数 : char *value
// 参数 : char *inifile
// 返回值 : int
//
///////////////////////////////////////////////////////////////////////
int GB_GetInfo_Str(char *section ,char *key , char *value , char *inifile)
{
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(( fd = fopen(inifile,"r")) == NULL)
{
return -1;
}
while( GB_getline( fd , 1023 , line) != -1)
{
memset( thekey , 0 ,sizeof(thekey));
memset( thevalue , 0 ,sizeof(thevalue));
memset( thesection , 0 , sizeof(thesection ));
GB_trim( line );
if( line[0] =='#' )
continue;
if( GB_getsection( line , thesection ) == 0 )
{
memset( cursection , 0 , sizeof( cursection ) );
strcpy( cursection , thesection );
continue;
}
else if( GB_getkeyvalue( line , thekey , thevalue ) == 0 )
{
if( strcasecmp( cursection , section ) == 0 && strcasecmp( thekey , key) == 0 )
{
strcpy( value , thevalue );
fclose(fd );
return 0;
}
}
memset( line , 0 ,sizeof(line));
}
fclose(fd );
return -1;
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : GB_GetInfo_Int
// 功能描述 : 从ini文件中获得int类型的值
// 参数 : char *section
// 参数 : char *key
// 参数 : int *nvalue
// 参数 : char *inifile
// 返回值 : int
//
///////////////////////////////////////////////////////////////////////
int GB_GetInfo_Int(char *section ,char *key , int *nvalue , char *inifile)
{
char szvalue[100];
memset( szvalue , 0 , sizeof( szvalue));
if( GB_GetInfo_Str( section , key , szvalue , inifile) == -1 )
{
*nvalue = 0;
return -1;
}
*nvalue = atoi( szvalue );
if( errno == EINVAL || errno == ERANGE)
return -1;
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?