Author Topic: WriteIni  (Read 3970 times)

0 Members and 1 Guest are viewing this topic.

Offline rhide

  • Major
  • *
  • Posts: 60
  • Coffee-addict
    • Vrastar-Hai soldat clan
WriteIni
« on: March 23, 2008, 02:16:26 pm »
Script Name: WriteINI
Script Description: I was not satisfied with the WriteIni procedures I could find using the search function here, so I decided to create my own.
Original Author(s): rhide
Core Version: 2.6.3
Requirements: xsplit

procedure WriteINI(FileName,Section,Key,Value: string);

Parameter Info:
  FileName (String): Name of the file (stored in the soldat folder)
  Section (String): Section to write to (specified as [...])
  Key (String): Key to assign
  Value (String): The value to write to the key

Description:
  The function is designed to look as much as the built-in ReadINI as possible
  http://enesce.com/help/html/Functions/ReadINI.html

  If the key exists, it is replaced by the new value, if not, a new key will be created.
  All ini files will be saved in the soldat/server folder.

Bugs:
  None known

Future changes:
  A lot of whitespaces is added quite unneccesarily, i'll remove that soon [Done]
« Last Edit: July 22, 2008, 06:42:07 pm by rhide »
Warning! Division by zero seriously injures yourself and the people in your surroundings.

Proud member of Vrastar-Hai:
http://www.guldheden.com/~erga050/vrastar-hai

Offline xmRipper

  • Soldat Beta Team
  • Flagrunner
  • ******
  • Posts: 742
    • Personal
Re: WriteIni
« Reply #1 on: March 23, 2008, 04:20:38 pm »
Yeah exactly what i needed =)
Co-Founder / CTO @ Macellan
Founder Turkish Soldat Community

Offline rhide

  • Major
  • *
  • Posts: 60
  • Coffee-addict
    • Vrastar-Hai soldat clan
Re: WriteIni
« Reply #2 on: March 23, 2008, 08:31:07 pm »
Ah, the annoying spaces are now gone. However, some newlines are still added to the end of the file, but I think it's more important to get the script up here than to worry about some non-important details..

Enjoy, and please report any bugs :)
(I havn't tested it properly yet :0)
Warning! Division by zero seriously injures yourself and the people in your surroundings.

Proud member of Vrastar-Hai:
http://www.guldheden.com/~erga050/vrastar-hai

Offline chutem

  • Veteran
  • *****
  • Posts: 1119
Re: WriteIni
« Reply #3 on: March 24, 2008, 04:32:32 am »
How about a Readini that you put in the section and key and it returns the value?
I'm not sure if there already is one of these :P
1NK3FbdNtH6jNH4dc1fzuvd4ruVdMQABvs

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: WriteIni
« Reply #4 on: March 24, 2008, 02:15:09 pm »
How about a Readini that you put in the section and key and it returns the value?
I'm not sure if there already is one of these :P
function ReadINI(FileName,Section,Key,DefaultValue: string): string;

Offline rhide

  • Major
  • *
  • Posts: 60
  • Coffee-addict
    • Vrastar-Hai soldat clan
Re: WriteIni
« Reply #5 on: March 24, 2008, 03:22:54 pm »
Description:
  The function is designed to look as much as the built-in ReadINI as possible
  http://enesce.com/help/html/Functions/ReadINI.html

and from my post above ;)
« Last Edit: March 24, 2008, 03:53:44 pm by rhide »
Warning! Division by zero seriously injures yourself and the people in your surroundings.

Proud member of Vrastar-Hai:
http://www.guldheden.com/~erga050/vrastar-hai

Offline chutem

  • Veteran
  • *****
  • Posts: 1119
Re: WriteIni
« Reply #6 on: March 24, 2008, 04:16:27 pm »
Oh ok lol, it was late when I posted...
1NK3FbdNtH6jNH4dc1fzuvd4ruVdMQABvs

Offline JFK

  • Camper
  • ***
  • Posts: 255
    • My TraxInSpace Account
Re: WriteIni
« Reply #7 on: April 18, 2008, 04:21:00 pm »
TrimINI gave me an Out Of Range error. I localized it and changed the following:

Code: [Select]
for i:=0 to strlen-1 do Begin

to this:

Code: [Select]
for i:=0 to strlen-2 do Begin

I have no idea what i exactly changed, but it seems to work now and im not losing any lines or anything.
I hope you can tell me i did the right thing ;)


Great thanks for these procedures btw, i first tried to use Avkon's WriteINI procedure, but for some reason it did not write the values in the right section, but it just changed the first fitting key it could find. Since all my sections have exactly the same key's this was pretty unwanted.
Come join: EliteCTF
Listen to: My Music

Offline NTxC

  • Major(1)
  • Posts: 34
  • Scripter
Re: WriteIni
« Reply #8 on: June 15, 2008, 01:55:18 pm »
Awesome function. Thanks very much :) As for the newlines at the end of the file, I wrote a function to remove them. Here is it:

Code: [Select]
function TrimNewlines(EntireFile: TStringArray) : TStringArray;
var
   rest    : TStringArray;
   i, filelength : integer;
   resu: string;
Begin
filelength := ArrayHigh(EntireFile);
SetArrayLength(rest, filelength+1);
for i:=0 to filelength do begin
if(EntireFile[i]='') then begin
if(i<>0) then begin
if(EntireFile[i-1]='') then begin
rest[i]:='-';
continue;
end;
end;
end;
rest[i]:=EntireFile[i];
end;
Result:=rest;
end;

To use it, call TrimNewlines instead of TrimINI in the WriteINI function. So the end of it looks like that:

Code: [Select]
   { Remove all annoying whitespace }
   EntireFile := TrimNewlines(EntireFile);
   
   WriteFile(filename,''); { Make sure the file is empty }
   for i:=0 to ArrayHigh(EntireFile) do
begin
      if(EntireFile[i]='-') then continue;
      WriteLnFile(filename, EntireFile[i]);
end;

No more annoying newlines at the end ;)

Cheers!
NT
:[ NT-Online Server Mod Coming Up! ]:
________________________________
Featuring:
- Admin System with dynamic ranks and privileges...
- Duel System...
- Reward System - earn rewards for your frags...
- CVARs - configuration variables - to make the server administering easier...

... and more! Coming soon!

Offline freestyler

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 326
Re: WriteIni
« Reply #9 on: June 26, 2008, 02:25:40 pm »
NTxC, are you sure it works?... It doesn't seem to cut the newlines; moreover, you declare resu as string and you never use it.

Offline NTxC

  • Major(1)
  • Posts: 34
  • Scripter
Re: WriteIni
« Reply #10 on: June 28, 2008, 01:31:16 pm »
Yes, it works :)
It leaves two newlines at the end of a file, instead of always adding one.

And yeah, 'resu' is just a leftover of experiments ;) Forgot to cut it off.

_________
EDIT: I have attached Core.pas for WriteINI with ability to remove additional newlines. I can take it down on OP's request :)
« Last Edit: June 28, 2008, 01:40:48 pm by NTxC »
:[ NT-Online Server Mod Coming Up! ]:
________________________________
Featuring:
- Admin System with dynamic ranks and privileges...
- Duel System...
- Reward System - earn rewards for your frags...
- CVARs - configuration variables - to make the server administering easier...

... and more! Coming soon!

Offline amb2010

  • Camper
  • ***
  • Posts: 264
  • Fear the dot ...
Re: WriteIni
« Reply #11 on: June 28, 2008, 03:10:22 pm »
Awesome function. Thanks very much :) As for the newlines at the end of the file, I wrote a function to remove them. Here is it:

-snip-

No more annoying newlines at the end ;)

Cheers!
NT

You said it was suppose to remove the newlines not add 2 lines instead of one...
And as the lyrics go in the United State's national anthem: "America, f**k YEAH!".

Offline NTxC

  • Major(1)
  • Posts: 34
  • Scripter
Re: WriteIni
« Reply #12 on: June 29, 2008, 06:57:52 am »
It forces only two newlines at the end of file. So if you had 29 newlines at the end, it forces 2. If you had 10, it forces 2. If you had 1, it forces 2. I guess 2 newlines look better than 20 at the end ;)
:[ NT-Online Server Mod Coming Up! ]:
________________________________
Featuring:
- Admin System with dynamic ranks and privileges...
- Duel System...
- Reward System - earn rewards for your frags...
- CVARs - configuration variables - to make the server administering easier...

... and more! Coming soon!

Offline KEEN

  • Major
  • *
  • Posts: 95
  • soldat champagne
    • Soldat Argentina
Re: WriteIni
« Reply #13 on: August 26, 2008, 08:32:59 am »
hi!!
Im returning to the scripts for soldat... makes enough that had left them... xD

Very useful the function, but it would like to annotate the following:

When the function writes in some [section] of the .ini, the key and the value turns out to be repeated... I want to know if to someone the same was happening.

-------------------
Example:
[Section]
key1=1
key2=0
key2=1
key1=0
-------------------

When it should be:
[Section]
key1=0
key2=0

And modified:
[Section]
key1=1
key2=1

thanks for the work, really it has facilitated the things!