📄 config.cc
字号:
if (chBuff[iLength - 1] == '\\')
{
chBuff[iLength - 1] = '\0';
strcat(Value,chBuff);
}
else if (chBuff[iLength - 1] != '\\')
{
strcat(Value,chBuff);
break;
}
it++;
iCount++;
if (iCount == VRecord.size())
{
break;
}
}
}
SetLockStatus(fdt,F_UNLCK);
fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
close(fdt);
close(fd);
if('$' == Value[0])
{
pchPoint = Value;
pchPoint++;
if(((pchTemp = strchr(pchPoint,'.')) != NULL) && (strchr(pchPoint, '/') == NULL))
{
strncpy(chSection,pchPoint,pchTemp - pchPoint);
chSection[pchTemp - pchPoint] = '\0';
pchTemp++;
strcpy(chKey,pchTemp);
return GetParam(FileName,chSection,chKey,Value);
}
if(pchTemp = strchr(pchPoint,'/'))
{
strncpy(chTemp,pchPoint,pchTemp - pchPoint);
chTemp[pchTemp - pchPoint] = '\0';
memset(Value,0,sizeof(Value));
if((pchEnv = getenv(chTemp)) != NULL)
{
if(('/' == *(pchEnv + strlen(pchEnv) - 1))&&('/' == *pchTemp))
{
pchTemp++;
}
strcpy(chTemp,pchTemp);
sprintf(Value,"%s%s",pchEnv,chTemp);
unlink(chTempName);
return true;
}
else
{
unlink(chTempName);
return false;
}
}
}
unlink(chTempName);
return true;
}
/*
* Function Name : SetParam
* Description : 设置配置文件
* Input Param :
* Filename -----------> 配置文件名
* Section -----------> 建立的组名
* Key -----------> 建立的关键字名
* Value -----------> 建立的键值
* Returns :如果文件不存在,返回false
* 如果文件存在,设置完毕则返回true
* complete :2001/11/09
*/
bool C_Config::SetParam(const char *FileName,const char *Section,const char *Key,const char *Value)
{
int fdt,fd;
int iCount = 0;
char chTemp[LINELENGTH], chTempName[LINELENGTH];;
char chSection[LINELENGTH],chKey[LINELENGTH];
char *pchPoint = NULL,*pchTemp = NULL;
string sLine;
sprintf(chTempName,"%s.tmp",FileName);
if(-1 == (fdt = open(chTempName,O_RDWR|O_CREAT|O_EXCL)))
{
if(-1 == (fdt = open(chTempName,O_RDWR|O_TRUNC)))
{
unlink(chTempName);
return false;
}
}
//设置文件写锁
SetLockStatus(fdt,F_WRLCK);
fchmod(fdt,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
//设置文件写标志
iWriteFlag = 1;
//清空容器内容
VRecord.clear();
if(-1 == (fd = open(FileName,O_RDONLY)))
{
SetLockStatus(fdt,F_UNLCK);
close(fdt);
iWriteFlag = 0;
unlink(chTempName);
return false;
}
//将文件内容全部放入容器中
for(;;)
{
memset(chTemp, 0, sizeof(chTemp));
if(0 == GetLine(fd, chTemp))
{
break;
}
sLine = chTemp;
VRecord.push_back(sLine);
}
close(fd);
//防止文件空洞而将原文件长度截为0
if (-1 == (fd = open(FileName,O_RDWR|O_CREAT|O_TRUNC)))
{
SetLockStatus(fdt,F_UNLCK);
close(fdt);
iWriteFlag = 0;
unlink(chTempName);
return false;
}
//设置文件属性为只读,防止被错误修改
if (-1 == fchmod(fd,S_IRUSR|S_IRGRP))
{
SetLockStatus(fdt,F_UNLCK);
close(fdt);
iWriteFlag = 0;
unlink(chTempName);
return false;
}
iCount = 0;
vector<string>::iterator it;
it = VRecord.begin();
memset(chSection, 0, sizeof(chSection));
memset(chKey,0,sizeof(chKey));
//得到Section
sprintf(chSection,"[%s]",Section);
//郭亮2003-01-20修改
//如果文件为空,则新建所有选项,写入文件
if (VRecord.size() == 0)
{
sprintf(chKey, "\t%s = %s", Key, Value);
PutLine(fd, chSection);
PutLine(fd, chKey);
//释放锁并关闭文件
SetLockStatus(fdt,F_UNLCK);
fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
close(fdt);
close(fd);
unlink(chTempName);
return true;
}
while(it<VRecord.end())
{
memset(chTemp, 0, sizeof(chTemp));
strcpy(chTemp, (*it).c_str());
PutLine(fdt, chTemp);
it++;
iCount++;
Trim(chTemp);
if ((';' == chTemp[0]) || ('#' == chTemp[0]))
{
continue;
}
if (!strcmp(chTemp, chSection))
{
break;
}
//已经到文件末尾,没有该组名,则在文件末尾新建该组名并写入参数值
if (iCount == VRecord.size())
{
for(it = VRecord.begin();it<VRecord.end();it++)
{
memset(chTemp, 0, sizeof(chTemp));
strcpy(chTemp, (*it).c_str());
PutLine(fd, chTemp);
}
sprintf(chKey, "\t%s = %s", Key, Value);
PutLine(fd, chSection);
PutLine(fd, chKey);
//释放锁并关闭文件
SetLockStatus(fdt,F_UNLCK);
fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
close(fdt);
close(fd);
unlink(chTempName);
return true;
}
}
//郭亮2003-01-21修改
//找到了该组名,但是已经是文件最后一行,则在文件末尾写入参数值
if (iCount == VRecord.size())
{
for(it = VRecord.begin();it<VRecord.end();it++)
{
memset(chTemp, 0, sizeof(chTemp));
strcpy(chTemp, (*it).c_str());
PutLine(fd, chTemp);
}
sprintf(chKey, "\t%s = %s", Key, Value);
PutLine(fd, chKey);
//释放锁并关闭文件
SetLockStatus(fdt,F_UNLCK);
fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
close(fdt);
close(fd);
unlink(chTempName);
return true;
}
//得到Key
while(it<VRecord.end())
{
memset(chTemp, 0, sizeof(chTemp));
strcpy(chTemp, (*it).c_str());
Trim(chTemp);
//本组里没有该关键字,新建参数值并写入文件
if ('[' == chTemp[0])
{
sprintf(chKey, "\t%s = %s", Key, Value);
PutLine(fdt, chKey);
PutLine(fdt, chTemp);
it++;
iCount++;
break;
}
if ((';' == chTemp[0]) || ('#' == chTemp[0]))
{
it++;
iCount++;
PutLine(fdt, chTemp);
continue;
}
pchPoint = chTemp;
if ((pchTemp = strchr(pchPoint,'=')) != NULL)
{
strncpy(chKey, chTemp, pchTemp - pchPoint);
chKey[pchTemp - pchPoint] = '\0';
Trim(chKey);
}
if (!strcmp(chKey,Key))
{
it++;
iCount++;
sprintf(chKey, "\t%s = %s", Key, Value);
PutLine(fdt, chKey);
break;
}
sprintf(chTemp, "%s",(*it).c_str());
PutLine(fdt, chTemp);
it++;
iCount++;
//已经到文件末尾,则添加该参数值
if (iCount == VRecord.size())
{
for(it = VRecord.begin();it<VRecord.end();it++)
{
memset(chTemp, 0, sizeof(chTemp));
strcpy(chTemp, (*it).c_str());
PutLine(fd, chTemp);
}
sprintf(chKey, "\t%s = %s", Key, Value);
PutLine(fd, chKey);
//释放锁并关闭文件
SetLockStatus(fdt,F_UNLCK);
fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
close(fdt);
close(fd);
unlink(chTempName);
return true;
}
}
while(it<VRecord.end())
{
memset(chTemp, 0, sizeof(chTemp));
strcpy(chTemp, (*it).c_str());
PutLine(fdt, chTemp);
it++;
}
//将临时文件描述符移动到临时文件头,将内容写回原文件
lseek(fdt,0,SEEK_SET);
for(;;)
{
memset(chTemp, 0, sizeof(chTemp));
if(0 == GetLine(fdt, chTemp))
{
break;
}
PutLine(fd, chTemp);
}
sleep(10);
SetLockStatus(fdt,F_UNLCK);
fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
close(fdt);
close(fd);
unlink(chTempName);
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -