zoukankan      html  css  js  c++  java
  • 读写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行以内,对自己也是个锻炼.
    关注过程,不知不觉就发现了结果原来如此...
  • 相关阅读:
    图解机器学习读书笔记-CH3
    塑造职场影响力的五大法宝
    怎样培养独挡一面的能力
    数据结构
    [分享]恼人的设计模式
    Git使用总结
    设计师整理的系统开发流程-简洁又有重点
    JavaScript中的String对象
    python高效解析日志入库
    如何让js不产生冲突,避免全局变量的泛滥,合理运用命名空间
  • 原文地址:https://www.cnblogs.com/iicc/p/5043396.html
Copyright © 2011-2022 走看看