Data Realms Fan Forums
http://45.55.195.193/

INI File Reading and Writing
http://45.55.195.193/viewtopic.php?f=73&t=16490
Page 1 of 1

Author:  halo2pac [ Thu Sep 10, 2009 9:09 am ]
Post subject:  INI File Reading and Writing

Hello, I am having trouble finding a lua based INI reader/writer.
I need it all done via LUA, which means the use of io.open ect.

Does anyone have, know wher to get, or can build one for me.

I would greatly appreciate it.

I do have one that works a little.. but it is Extremely buggy:
Code:
function WriteINI(File, Section, KeyName, Value)
   -- Installation:
   --   1: Place this function at the end of your luaplugin.lua
   --   2: Example code >  WriteINI("ssgm.ini", "Plugins", "01", "LuaPlugin.dll")
   --       This would write ssgm.ini, in the [Plugins] Section and change "01" keyname
   --   3: Example Code 2 >  WriteINI("LuaPlugins/readini.ini", "Source", "Keyname", "Value")
   --       This would read from the LuaPlugins Directory.


   -- Here are the errors that are returned if you wish to change them.   

   ArgError = "None"    -- Passed invalid arguments to function

   blnSection = false
   intAction = 0
   strBracket = [[[]]
   strCloseBracket = [[].]]
   strCloseBracket = string.sub(strCloseBracket, 1, 1)
   Save = ""
   
   if File ~= nil and Section ~= nil and KeyName ~= nil and Value ~= nil then
      if File ~= "" and Section ~= "" and KeyName ~= "" and Value ~= "" then
         i = io.open(File, "r")
         if i ~= nil then
            while true do
               local Line = i:read()   -- Reads a line
               if Line == nil then
                  break
               else
                  if intAction == 0 then
                     if blnSection == false then
                        Found = string.sub(Line, 0, 1)
                        if Found == strBracket then -- Found Header
                           Header = string.sub(Line, 2, -2)
                           if Header == Section then
                              blnSection = true
                           end
                        end
                     else
                        Header = string.sub(Line, 0, 1)
                        if Header == strBracket then
                           Line = KeyName .. "=" .. Value .. "\n" .. Line
                           intAction = 1
                        elseif Header == ";" then
                           -- Ignor Comments
                        elseif Line == "" then
                           -- Ignor Blank Lines
                        else
                           strFindEqual = string.find(Line, "=")
                           if strFindEqual ~= nil then
                              strKeyname = string.sub(Line, 0, strFindEqual - 1)
                              if strKeyname == KeyName then
                                 Line = KeyName .. "=" .. Value
                                 intAction = 1
                              end   
                           end
                        end
                     end
                  end
                     
                  Save = Save .. Line .. "\n"
               end
            end
           
            i:close()
     
            if intAction == 1 then
               --
            else
               Save = Save .. strBracket .. Section .. strCloseBracket .. "\n" .. KeyName .. "=" .. Value
            end

            i = io.open(File, "w")
               i:write(Save)
            i:close()
         else
            i = io.open(File, "w")
               i:write(strBracket .. Section .. strCloseBracket .. "\n" .. KeyName .. "=" .. Value)
            i:close()
         end
      else
         return ArgError
      end
   else
      return ArgError
   end
end

function ReadINI(File, Section, KeyName)
   -- Installation:
   --   1: Place this function at the end of your luaplugin.lua
   --   2: Example code >  Read = ReadINI("ssgm.ini", "Plugins", "01")
   --       This would read ssgm.ini, in the [Plugins] Section, And most likly will return LuaPlugin.dll
   --   3: Example Code 2 >  Read = ReadINI("LuaPlugins/readini.ini", "Source", "Keyname")
   --       This would read from the LuaPlugins Directory.

   -- By Default this function will return "None" if nothing is found, And "NoFile" if the file you are trying
   -- to read does not exist.

   -- Here are the errors that are returned if you wish to change them.   

   NoneError = "None"    -- Didnt Find Keyname in file
   FileError = "NoFile"    -- No file exists
   ArgError = "None"    -- Passed invalid arguments to function

   blnSection = false
   intAction = 0
   strBracket = [[[]]
   
   if File ~= nil and Section ~= nil and KeyName ~= nil then
      if File ~= "" and Section ~= "" and KeyName ~= "" then
         i = io.open(File, "r")
         if i ~= nil then
            while true do
               local Line = i:read()   -- Reads a line
               if Line == nil or intAction ~= 0 then
                  break
               else
                  if blnSection == false then
                     Found = string.sub(Line, 0, 1)
                     if Found == strBracket then -- Found Header
                        Header = string.sub(Line, 2, -2)
                        if Header == Section then
                           blnSection = true
                        end
                     end
                  else
                     Header = string.sub(Line, 0, 1)
                     if Header == strBracket then
                        intAction = 2
                     elseif Header == ";" then
                        -- Ignor Comments
                     elseif Line == "" then
                        -- Ignor Blank Lines
                     else
                        strFindEqual = string.find(Line, "=")
                        if strFindEqual ~= nil then
                           strKeyname = string.sub(Line, 0, strFindEqual - 1)
                           if strKeyname == KeyName then
                              intAction = 1
                              Value = string.sub(Line, strFindEqual + 1)
                           end   
                        end
                     end
                  end
               end
            end
           
            i:close()
     
            if intAction == 1 then
               return Value
            elseif intAction == 2 then
               return NoneError
            else
               return NoneError
            end   
         else
            return FileError
         end
      else
         return ArgError
      end
   else
      return ArgError
   end
end
   

Author:  ProjektTHOR [ Thu Sep 10, 2009 1:32 pm ]
Post subject:  Re: INI File Reading and Writing

Here's a good one

Author:  Grif [ Thu Sep 10, 2009 2:46 pm ]
Post subject:  Re: INI File Reading and Writing

The CC engine's lua implementation cannot do I/O anyways.

Unless you want to make an external reader (debugger?) and for some godforsaken reason want to do it in Lua I really can't see why you're doing this.

Author:  halo2pac [ Thu Sep 10, 2009 3:31 pm ]
Post subject:  Re: INI File Reading and Writing

It is very hard to find any forum or community that does LUA scripting at all.
I did not know your community's use of LUA did not include IO operations.
It should though since LUA v5 supports it.

But thanks for your help.

Author:  Duh102 [ Thu Sep 10, 2009 3:33 pm ]
Post subject:  Re: INI File Reading and Writing

CC's Lua implementation is... lacking at best. Hopefully Data will make it better in future versions, but so far this is not the case.

Author:  halo2pac [ Thu Sep 10, 2009 3:37 pm ]
Post subject:  Re: INI File Reading and Writing

Our community at DCOM Productions has implemented LUA into the game Command and Conquer: Renegade vastly. It is open source, and created by a total pro who knows his stuff.

You should check it out, you may be able to learn something from it.
Our LUA forums.

We don't support CC, though, But we may be able to help with lua implementation.

Thanks for all your help.

Author:  Grif [ Thu Sep 10, 2009 11:19 pm ]
Post subject:  Re: INI File Reading and Writing

Hey uh halotupac or whatever the ♥♥♥♥

you're sounding more and more like a spambot

I/O is not included with CC's lua on purpose so that people can't write a program to do dumb ♥♥♥♥

also considering that lua project that you guys did you sure are unclear on how to actually type lua

Author:  mail2345 [ Fri Sep 11, 2009 5:56 am ]
Post subject:  Re: INI File Reading and Writing

Grif wrote:
I/O is not included with CC's lua on purpose so that people can't write a program to do dumb ♥♥♥♥

Included libaries, IRClands style.
That said, file I/O should be allowed to the CC folder, imho.

Author:  Geti [ Fri Sep 11, 2009 11:13 am ]
Post subject:  Re: INI File Reading and Writing

thats just going to lead to "im in your settings.ini, stealing your key" bull♥♥♥♥.

Author:  mail2345 [ Sat Sep 12, 2009 5:49 am ]
Post subject:  Re: INI File Reading and Writing

With that and other things as exceptions. Anyway, I'm sure a key stealing mod will result in a ban.

As I said, file libs.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/