读写INI的通用函数

转自前辈的博客,反复搜索了一下,可能是这里原创.

http://blog.csdn.net/chinazhd/article/details/6540250

在写到INI文件读写的时候,发现好多重复的语句,原代码类似这样的好多:

  IniGameConf := Tinifile.Create(sIniFile + M2SERVERCONFIGFILE);
  IniGameConf.WriteString(M2SERVERSECTIONNAME1, 'ServerName', g_sGameName);
  IniGameConf.WriteString(M2SERVERSECTIONNAME1, 'DBName', g_sHeroDBName);
  IniGameConf.WriteString(M2SERVERSECTIONNAME1, 'GateAddr', sAllIPaddr);
  IniGameConf.WriteInteger(M2SERVERSECTIONNAME1, 'GatePort', g_Config.M2Server.GatePort);
  IniGameConf.WriteString(M2SERVERSECTIONNAME1, 'DBAddr', sLocalIPaddr);
  IniGameConf.WriteInteger(M2SERVERSECTIONNAME1, 'DBPort', g_Config.DBServer.ServerPort);
  IniGameConf.WriteString(M2SERVERSECTIONNAME1, 'IDSAddr', sLocalIPaddr);
  IniGameConf.WriteInteger(M2SERVERSECTIONNAME1, 'IDSPort', g_Config.LoginSrv.ServerPort);
  IniGameConf.WriteString(M2SERVERSECTIONNAME1, 'MsgSrvAddr', sAllIPaddr);
  IniGameConf.WriteInteger(M2SERVERSECTIONNAME1, 'MsgSrvPort', g_Config.M2Server.MsgSrvPort);
  IniGameConf.WriteString(M2SERVERSECTIONNAME1, 'LogServerAddr', sLocalIPaddr);
  IniGameConf.WriteInteger(M2SERVERSECTIONNAME1, 'LogServerPort', g_Config.LogServer.Port);
  IniGameConf.WriteBool(M2SERVERSECTIONNAME1, 'CloseWuXin', g_boCloseWuXin);

  IniGameConf.WriteString(M2SERVERSECTIONNAME2, 'GuildDir', '.\GuildBase\Guilds\');
  IniGameConf.WriteString(M2SERVERSECTIONNAME2, 'GuildFile', '.\GuildBase\GuildList.txt');
  IniGameConf.WriteString(M2SERVERSECTIONNAME2, 'ConLogDir', '.\ConLog\');
  IniGameConf.WriteString(M2SERVERSECTIONNAME2, 'CastleDir', '.\Castle\');
  IniGameConf.WriteString(M2SERVERSECTIONNAME2, 'CastleFile', '.\Castle\List.txt');
  IniGameConf.WriteString(M2SERVERSECTIONNAME2, 'GameDataDir', '.\Envir\');
  IniGameConf.WriteString(M2SERVERSECTIONNAME2, 'EnvirDir', '.\Envir\');
  IniGameConf.WriteString(M2SERVERSECTIONNAME2, 'MapDir', '.\Map\');
  IniGameConf.WriteString(M2SERVERSECTIONNAME2, 'NoticeDir', '.\Notice\');
  IniGameConf.WriteString(M2SERVERSECTIONNAME2, 'LogDir', '.\Log\');
  IniGameConf.WriteString(M2SERVERSECTIONNAME2, 'EMailDir', '.\EMail\');
  IniGameConf.Free;

  sIniFile := g_sGameDirectory + 'Mir200\' + 'GuildBase\';
  if not DirectoryExists(sIniFile) then begin
    CreateDir(sIniFile);
  end;

  sIniFile := g_sGameDirectory + 'Mir200\' + 'GuildBase\Guilds\';
  if not DirectoryExists(sIniFile) then begin
    CreateDir(sIniFile);
  end;

  sIniFile := g_sGameDirectory + 'Mir200\' + 'ConLog\';
  if not DirectoryExists(sIniFile) then begin
    CreateDir(sIniFile);
  end;
  sIniFile := g_sGameDirectory + 'Mir200\' + 'Castle\';
  if not DirectoryExists(sIniFile) then begin
    CreateDir(sIniFile);
  end;
  sIniFile := g_sGameDirectory + 'Mir200\' + 'Envir\';
  if not DirectoryExists(sIniFile) then begin
    CreateDir(sIniFile);
  end;
  sIniFile := g_sGameDirectory + 'Mir200\' + 'Map\';
  if not DirectoryExists(sIniFile) then begin
    CreateDir(sIniFile);
  end;
  sIniFile := g_sGameDirectory + 'Mir200\' + 'Notice\';
  if not DirectoryExists(sIniFile) then begin
    CreateDir(sIniFile);
  end;
  sIniFile := g_sGameDirectory + 'Mir200\' + 'Log\';
  if not DirectoryExists(sIniFile) then begin
    CreateDir(sIniFile);
  end;
  sIniFile := g_sGameDirectory + 'Mir200\' + 'EMail\';
  if not DirectoryExists(sIniFile) then begin
    CreateDir(sIniFile);
  end;

很明显,重复的比较多,INI文件读写的时候数据类型不同,写代码时也太慢了,找了一个通用读写INI文件的函数,只取了自己需要的部分,并稍作修改,根据传入的参数类型自动判断数据类型,听说用variant变量会占资源,先用用看:

//读取INI
function ReadIniValue(const FileName, Section, Name: string; DefaultValue: Variant): Variant;
begin
  with TIniFile.Create(FileName) do
  try
    if VarType(DefaultValue)=varStrArg then
      Result := ReadString(Section, Name, DefaultValue)
    else if VarType(DefaultValue)=varInteger then
      Result := ReadInteger(Section, Name, DefaultValue)
    else if VarType(DefaultValue)=varDouble then
      Result := ReadFloat(Section, Name, DefaultValue)
    else if VarType(DefaultValue)=varBoolean then
      Result := ReadBool(Section, Name, DefaultValue);
  finally
    Free;
  end;
end;

//写入INI
procedure WriteIniValue(const FileName, Section, Name: string;Value: Variant);
begin
  with TIniFile.Create(FileName) do
  try
    if VarType(Value)=varStrArg then
      WriteString(Section, Name, VarToStr(Value))
    else if VarType(Value)=varInteger then
      WriteInteger(Section, Name, Value)
    else if VarType(Value)=varDouble then
      WriteFloat(Section, Name, Value)
    else if VarType(Value)=varBoolean then
      WriteBool(Section, Name, Value);
  finally
    Free;
  end;
end;

//检测目录不存在就创建
procedure IfNoDirThenCreate(const DirPath:string);
begin
  if not DirectoryExists(DirPath) then CreateDir(DirPath);
end;
对这些基本不变的东西,占用的代码空间太大了,原先是准备用静态数组声明,然后用FOR循环一块处理,后来觉得那样还是不直观,还是准备整理一下写到数据库或者dll文件里边,里边用到的函数尽量都缩小到50行以内,对自己也是个锻炼.
posted @ 2015-12-13 19:26  陨落流星  阅读(1409)  评论(0编辑  收藏  举报