View unanswered posts | View active topics It is currently Fri Dec 27, 2024 11:14 pm



Reply to topic  [ 10 posts ] 
 INI File Reading and Writing 
Author Message
User avatar

Joined: Thu Sep 10, 2009 9:03 am
Posts: 3
Reply with quote
Post 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
   


Thu Sep 10, 2009 9:09 am
Profile WWW
Banned
User avatar

Joined: Tue Feb 27, 2007 4:05 pm
Posts: 2527
Reply with quote
Post Re: INI File Reading and Writing
Here's a good one


Thu Sep 10, 2009 1:32 pm
Profile YIM
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post 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.


Thu Sep 10, 2009 2:46 pm
Profile
User avatar

Joined: Thu Sep 10, 2009 9:03 am
Posts: 3
Reply with quote
Post 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.


Thu Sep 10, 2009 3:31 pm
Profile WWW
happy carebear mom
User avatar

Joined: Tue Mar 04, 2008 1:40 am
Posts: 7096
Location: b8bbd5
Reply with quote
Post 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.


Thu Sep 10, 2009 3:33 pm
Profile
User avatar

Joined: Thu Sep 10, 2009 9:03 am
Posts: 3
Reply with quote
Post 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.


Thu Sep 10, 2009 3:37 pm
Profile WWW
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post 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


Thu Sep 10, 2009 11:19 pm
Profile
User avatar

Joined: Tue Nov 06, 2007 6:58 am
Posts: 2054
Reply with quote
Post 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.


Fri Sep 11, 2009 5:56 am
Profile
User avatar

Joined: Sun Jul 13, 2008 9:57 am
Posts: 4886
Location: some compy
Reply with quote
Post Re: INI File Reading and Writing
thats just going to lead to "im in your settings.ini, stealing your key" bull♥♥♥♥.


Fri Sep 11, 2009 11:13 am
Profile WWW
User avatar

Joined: Tue Nov 06, 2007 6:58 am
Posts: 2054
Reply with quote
Post 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.


Sat Sep 12, 2009 5:49 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 10 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.022s | 15 Queries | GZIP : Off ]