代码拉取完成,页面将自动刷新
//---------------------------------------------------------------------------
#include<iostream>
#include<fstream>
#pragma hdrstop
#include "myconfig.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link"FileOpFun.lib"
TMyConfig::TMyConfig(AnsiString&filename,AnsiString&fieldNames,TXMLDocument*doc)
{
//检验扩展名是否为xml,若不是,抛出异常
if(!IsValidConfigFileName(filename))
{
throw new Exception("invalid filename");
}
if(!FileExists(filename))
{
CreateMyConfigFile(filename,fieldNames);
}
Doc=doc;
Doc->FileName=(WideString)filename.c_str();
Doc->Active=true;
}
AnsiString TMyConfig::GetValue(AnsiString fieldName)
{
if(!Doc->Active)
{
Doc->Active=true;
}
_di_IXMLNode Node=Doc->DocumentElement;
AnsiString v=Node->ChildNodes->Nodes[WideString(fieldName)]->Text;
return v;
}
void TMyConfig::SetValue(AnsiString fieldName,AnsiString value)
{
if(!Doc->Active)
{
Doc->Active=true;
}
_di_IXMLNode Node=Doc->DocumentElement;
Node->ChildNodes->Nodes[WideString(fieldName)]->Text=value;
}
bool TMyConfig::IsValidConfigFileName(AnsiString& filename)
{
int N=filename.Length();
int startpos=N-3;
AnsiString ext=filename.SubString(startpos,4);
if(ext.LowerCase()==".xml")
return true;
return false;
}
void TMyConfig::Save()
{
if(!Doc->Active)
Doc->Active=True;
Doc->SaveToFile(Doc->FileName);
}
void TMyConfig::CreateMyConfigFile(AnsiString& filename, AnsiString& fieldNames)
{
using namespace std;
fstream fout;
fout.open(filename.c_str(),ios_base::out);
fout<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>"<<endl;
fout<<"<Parameters>"<<endl;
char *fields=new char[fieldNames.Length()+1];
strcpy(fields,fieldNames.c_str());
char*p=strtok(fields,"|");
while(p!=NULL)
{
fout<<"<"<<p<<">"<<"</"<<p<<">"<<endl;
p=strtok(NULL,"|");
}
fout<<"</Parameters>"<<endl;
fout.close();
}
TMyConfig::~TMyConfig()
{
Save();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。