forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Read Variables from Text File

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help
View previous topic :: View next topic  
Author Message
mcpaton
Newbie


Joined: 27 Jul 2005
Posts: 13

PostPosted: Wed Jul 27, 2005 9:28 am    Post subject: Read Variables from Text File Reply with quote

I want to store a heap of variables for different parts of my script in a text file (ini file to be exact) so that they can be edited at a later date, without recompiling.

Is there an easy way of opening a variable title, and then listing the contants of the variable under it, then opening another variable, etc etc... i'd like it to format like this:

Code:

[Variable1Title]
Variable 1
Variable 2
Variable 3

[Variable2Title]
Variable 1
Variable 2 etc etc


I just need a way of reading all those variables into the script into
%%Variable1Title and %%Variable2Title or like so....

i would appreciate any help with this!!

thanks
Back to top
View user's profile Send private message Visit poster's website
henrywood
Contributor
Contributor


Joined: 21 Sep 2004
Posts: 66
Location: Copenhagen, Denmark

PostPosted: Wed Jul 27, 2005 12:57 pm    Post subject: Re: Read variables Reply with quote

I have been trying to figure out a way to do this myself, but to no avail Sad

I wish we could get an @EVAL function (like in PHP) so that you might just do the following to solve your problem:

Code:


[code:1:8efd7d674e]
#define function,inivals
# %R = @inivals(<ini_file>,<ini_sect>)

 %R = @inivals(@windir()\win.ini,Mail)

 list create,2
 list assign,2,%R
 #info @count(2)
 
 %A = @fsep()
 option fieldsep,"="      
 repeat
     
     list seek,2,%C
      parse "%K;%V",@item(2,%C)
    
     # This does not work (YET ?)...
     if %K
    
         #%E = @eval(@chr(37)K=@chr(37)V)
    
         # The code between () would now be run -> thus setting a variable by the name contained in %K to the value contained in %V
    
     end
    
      %C = @succ(%C)
 
 until @equal(%C,@count(2))
 option fieldsep,%A
 
:inivals
  %R =
  LOADLIB KERNEL32
  if @ok()
    %B = @fill(2048,,Z)
    %N = @lib(KERNEL32,GetPrivateProfileSectionA,int:,str:%2,@addr("%B"),int:2048,str:%1)
    if @greater(%N,0)
      %I = 1
      repeat
        %C = @val(@substr(%B,%I))
        if @zero(%C)
          %R = %R@cr()
        else
          %R = %R@chr(%C)
        end
        %I = @succ(%I)
      until @greater(%I,%N)
    else
      %R =
    end
    FREELIB kernel32.dll
  end
  exit %R


Jules, is the @eval function a possibility ?
Back to top
View user's profile Send private message Send e-mail
Serge
Professional Member
Professional Member


Joined: 04 Mar 2002
Posts: 1480
Location: Australia

PostPosted: Wed Jul 27, 2005 3:55 pm    Post subject: Reply with quote

umh ... not sure if this will help but i will suggest it any way

why don't you use a INI file ... it will work the way you described above ... INI files can be used for many things and not just as the typical windows INI file

just a thought

serge

_________________
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Skit3000
Admin Team


Joined: 11 May 2002
Posts: 2166
Location: The Netherlands

PostPosted: Wed Jul 27, 2005 5:38 pm    Post subject: Reply with quote

You can use text elements to store variables if you want to create them dynamically... Smile

Code:
  DIALOG CREATE,New Dialog,-1,0,191,139
  DIALOG ADD,EDIT,eNAME,6,6,180,18,varname
  DIALOG ADD,EDIT,eVALUE,30,6,180,18,My test value1
  DIALOG ADD,BUTTON,bSTORE,54,120,66,24,Store
  DIALOG ADD,EDIT,eCALL,84,6,180,18,varname
  DIALOG ADD,BUTTON,bCALL,108,120,66,24,Call
  DIALOG SHOW

:Evloop
option errortrap,error
wait event
goto @event()

:bSTOREBUTTON
# "Save" the value of the variabel in a text element which
# is not visible on screen
%%eName = @dlgtext(eNAME)
%%eValue = @dlgtext(eVALUE)
# If an error occurs while setting the value of the variable, more then
# likely the element does not exist yet. In that case, create a new text
# element Smile
option errortrap,newvariable
dialog set,"var_"%%eName,%%eValue
goto Evloop
:newvariable
dialog add,text,"var_"%%eName,9999,9999,0,0,%%eValue
goto Evloop

:bCALLBUTTON
%%eCall = @dlgtext(eCALL)
info @dlgtext("var_"%%eCall)
goto Evloop

:Error
info Error @error(e) at line @error(n)
goto Evloop

:Close
exit

_________________
[ Add autocomplete functionality to your VDS IDE windows! ]
Voor Nederlandse beginners met VDS: bekijk ook eens deze tutorial!
Back to top
View user's profile Send private message
mcpaton
Newbie


Joined: 27 Jul 2005
Posts: 13

PostPosted: Wed Jul 27, 2005 10:21 pm    Post subject: Reply with quote

I want them to be static settings, but edit-able if needed. They are settings that must be remembered each time the program is used, but handy to have in the ini file to change at a later date if needed. I found the documentation on how to use INI files a little scarce, so am unsure how to use it like i have listed above...
Back to top
View user's profile Send private message Visit poster's website
Serge
Professional Member
Professional Member


Joined: 04 Mar 2002
Posts: 1480
Location: Australia

PostPosted: Thu Jul 28, 2005 2:34 am    Post subject: Reply with quote

1. if you only open 1 ini file with your code, then you only need to open it once at the start of your code

Code:
 INIFILE OPEN, @path(%0)your_ini_file_name


the @PATH(%0) allows for your ini file to be in the same folder as your program...otherwise it will look for it in the windows folder

2. to add entries use

Code:
INIFILE WRITE, Variable1Title , Variable, 1


this will automatically add the right entry to the ini file as per your first post

3. to overwrite an entry, use the same code as 2

Code:
INIFILE WRITE, Variable1Title , Variable, 1a


4. vds does not allow you to delete entries or sections but there are couple of free extensions (dll or dsu) you can download from www.vdsworld.com to be able to do that

5. to close your ini file at the end of your program...well there is no need to do that

hope this gets you going Very Happy

serge

_________________
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
mcpaton
Newbie


Joined: 27 Jul 2005
Posts: 13

PostPosted: Thu Jul 28, 2005 9:17 am    Post subject: Reply with quote

how to i read the initial values from the ini file? or are they somehow automatically loaded as variables? I want it to be a 2 way thing, mainly to read the settings into my script
Back to top
View user's profile Send private message Visit poster's website
DavidR
Contributor
Contributor


Joined: 05 Aug 2003
Posts: 83
Location: Bethel Pennsylvania U.S.A.

PostPosted: Thu Jul 28, 2005 10:42 am    Post subject: Reply with quote

mcpaton wrote:
how to i read the initial values from the ini file? or are they somehow automatically loaded as variables? I want it to be a 2 way thing, mainly to read the settings into my script


Here's a "snippet" of code that might help.
This is placed at the beginning of your program and sets the variables according to the ini file before your program executes.
Of course in a real application you would want to add some additional logic to set the variables to some default value in case the ini file doesn't exist or can't be found for some reason.
If the variables are modified by your program you will need to add logic to write them back to the ini file before the program exits.
Ini files are nice but I rarely use them anymore. It's cleaner to just stash this kind of stuff in the registry.
..........David

Code:
%P = @PATH(%0)
    INIFILE OPEN,%PEXAMPLE.INI
  %%Address = @INIREAD(Connect,Address)
  %%Port = @INIREAD(Connect,Port)
  %X = @INIREAD(Connect,Database)
  %Y = @INIREAD(Connect,Log)
  %%INTERVAL = @INIREAD(Connect,Interval)


Contents of ini file EXAMPLE.INI

[Connect]
Address=192.168.1.235
Port=21
Database=C:\mydb\
Log=Yes
Interval=600
Back to top
View user's profile Send private message
Serge
Professional Member
Professional Member


Joined: 04 Mar 2002
Posts: 1480
Location: Australia

PostPosted: Fri Jul 29, 2005 2:03 am    Post subject: Reply with quote

ooppss Embarassed forgot to tell you how to read the data ggrr!!!

thanks davic

serge

_________________
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum
You can attach files in this forum
You can download files in this forum

Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group