PtokaX forum

Development Section => Your Developing Problems => Topic started by: icculus on 21 December, 2003, 10:15:13

Title: Problem with WriteData() function
Post by: icculus on 21 December, 2003, 10:15:13
I have a problem with this function:

function WriteData()
   datafile = openfile("MyBot/settings.dat","w+")
   for i=1,DataTotal,1 do
      write(datafile, rawget(DataList,i).."\n")
   end
   closefile(datafile)
end

the problem is that is when it is called it duplicates all lines into new lines

ie:
(this is what it looks like before i call the function)
-----DO NOT EDIT THIS FILE---------
0.263
7
2
15
10
on
on
nowhere.no-ip.com
nowhere.no-ip.com
nowhere.no-ip.com
nowhere.no-ip.com
nowhere.no-ip.com

(this is what it looks like after function is called)
-----DO NOT EDIT THIS FILE---------
0.263
7
2
15
10
on
on
nowhere.no-ip.com
nowhere.no-ip.com
nowhere.no-ip.com
nowhere.no-ip.com
nowhere.no-ip.com
-----DO NOT EDIT THIS FILE---------
0.24
7
2
15
10
on
on
nowhere.no-ip.com
nowhere.no-ip.com
nowhere.no-ip.com
nowhere.no-ip.com
nowhere.no-ip.com

Please if some could help me out with this that would be great. :))
Title:
Post by: plop on 21 December, 2003, 14:57:42
give this a try.
function WriteData()
   file = "MyBot/settings.dat"
   remove(file)
   datafile = openfile(file,"w+")
   for i=1,DataTotal,1 do
      write(datafile, rawget(DataList,i).."\n")
   end
   closefile(datafile)
end
plop
Title: thanx plop that worked
Post by: icculus on 21 December, 2003, 22:29:12
thanx plop that worked
you the man
 :]
Title: It works with some but not all
Post by: icculus on 22 December, 2003, 08:42:44
That helps with some of the commands... but not all... it duplicates even more ... here is some more info:

function ReadData()
   readfrom("Mybot/settings.dat")
   while 1 do
      local line = read()
      if line == nil then
         break
      else
         DataTotal = DataTotal + 1
         rawset(DataList,DataTotal,line)
      end
   end
   readfrom()
      dcversion = tonumber(rawget(DataList,2))
      minshare = tonumber(rawget(DataList,3))
      slotsmin = tonumber(rawget(DataList,4))
      slotsmax = tonumber(rawget(DataList,5))
      maxhubs = tonumber(rawget(DataList,6))
      Redirect = rawget(DataList,7)
      AllowNonDCPP = rawget(DataList,8)
      RedirSlots = rawget(DataList,9)
      RedirShare = rawget(DataList,10)
      RedirMaxHubs = rawget(DataList,11)
      Redirdcv = rawget(DataList,12)
      Redirnondcpp = rawget(DataList,13)
end

function WriteData()
   datafile = openfile("MyBot/settings.dat","w+")
   for i=1,DataTotal,1 do
      write(datafile, rawget(DataList,i).."\n")
   end
   closefile(datafile)
end

--------sample of a function call:--------------
function dosetredirhubs(user)
   rawset(DataList,11,redirhubs)
   WriteData()
   ReadData()
   SendPmToNick(user.sName, Bot, "I have set the redirection of maximum hubs to: "..redirhubs)
end

function dosetredirdcppv(user)
   rawset(DataList,12,redirdcppv)
   WriteData()
   ReadData()
   SendPmToNick(user.sName, Bot, "I have set the redirection of minimum DC++ version to: "..redirdcppv)
end

function dosetredirnondcpp(user)
   rawset(DataList,13,redirnondcpp)
   WriteData()
   ReadData()
   SendPmToNick(user.sName, Bot, "I have set the redirection address for non-DC++ clients to: "..redirnondcpp)
end

-----------------------------------------------------------
I think the ReadData() and WriteData() are by Snowman the maker of mean machine ....hey snow how bout some help?
Title:
Post by: turkiye on 22 December, 2003, 09:58:47
THX
 8)
Title:
Post by: plop on 22 December, 2003, 11:57:46
would be maby handyer 2 change layout of the file.
redirhubs:nowhere.no-ip.com
slots:4
then convert this 2 an associative array on load, this way you can just save the table if something changes.
makes it a lot easyer and no doubles posible as you can everywhere just remove the old file and save the new.
also doesn't mather anymore on witch line something is stored.
settings[redirhubs]="nowhere.no-ip.com"
savetable()
1st line of savetable would remove the old file followed by the stuff 2 build a fresh new file.
also drops the need 2 load the file again after a save.

plop
Title: thanx
Post by: icculus on 22 December, 2003, 14:49:59
i am fairly new to LUA... but I will do some reading and see if I can figure that out and pull it off..
Thanx for your time plop.. :D
Title:
Post by: plop on 22 December, 2003, 19:15:09
no problem.

plop