Pulling data from an external file
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
- 
				quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Pulling data from an external file
Hello once again,
Does Rev support an INI type external file structure? I want to pull information for an external file.
[objectName]
color=red
highlighted=false
Thanks
Tom
			
			
									
									
						Does Rev support an INI type external file structure? I want to pull information for an external file.
[objectName]
color=red
highlighted=false
Thanks
Tom
- 
				quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Malte,
Sorry for being vague. I case you’re not familiar, INI files allow you to store and retrieve information by calling the header and item data. The structure is getIniVar(header,item,path to the INI file). One of the things I use this for is to set the highlight of buttons. My script in toolbook looks like this: highlight of buttonx = getIniVar(name of buttonx,item1,path to file). This way I can store data in a file so that when the app is updated the user defined information i.e. whether a button has been highlighted, can be pulled from the ini file and doesn’t need to be stored in the app itself.
[header]
Item1=true
Item2=valueofsomething
Thanks
Tom
			
			
									
									
						Sorry for being vague. I case you’re not familiar, INI files allow you to store and retrieve information by calling the header and item data. The structure is getIniVar(header,item,path to the INI file). One of the things I use this for is to set the highlight of buttons. My script in toolbook looks like this: highlight of buttonx = getIniVar(name of buttonx,item1,path to file). This way I can store data in a file so that when the app is updated the user defined information i.e. whether a button has been highlighted, can be pulled from the ini file and doesn’t need to be stored in the app itself.
[header]
Item1=true
Item2=valueofsomething
Thanks
Tom
Took me 5 minutes. Let me know if there are any problems.
Usage:
get inivar(header,item,file)
Best,
Mark
			
			
									
									Code: Select all
function iniVar theHeader,theItem,thePath
  open file thePath
  read from file thePath until EOF
  close file thePath
  put it into myIniData
  filter myIniData without ";*"
  put offset("[" & theHeader & "]",myIniData) into myStartChar
  if myStartChar > 0 then
    if char (myStartChar + 1) to -1 of myIniData contains "[" then
      put offset("[",myIniData,myStartChar + 1) + myStartChar into myLastChar
      put char myStartChar to myLastChar of myIniData into myIniData
    else
      put char myStartChar to -1 of myIniData into myIniData
    end if
    delete line 1 of myIniData
    split myIniData by return and "="
    return myIniData[theItem]
  else
    return empty
  end if
end iniVar
Usage:
get inivar(header,item,file)
Best,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
						The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
- 
				quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
- 
				quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Hi Tom,
If I did it correctly, you can call the function like this:"
put inivar(header,item,file)
and you will see the result in the message box. If not, I might have to do some debugging.
Best,
Mark
			
			
									
									If I did it correctly, you can call the function like this:"
put inivar(header,item,file)
and you will see the result in the message box. If not, I might have to do some debugging.
Best,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
						The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
- 
				quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Tom,
What exactly are you entering in the message box, including all parameters, and can you post a brief sample ini file?
Mark
			
			
									
									What exactly are you entering in the message box, including all parameters, and can you post a brief sample ini file?
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
						The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
- 
				quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Mark,
Sorry, I guess I wan't very clear. I'm not entering anything in the message box. I was trying to do a "put" to see the result.
In a button named "Sample" I have:
Along with your script.
This goes into a file named Entries.ini
			
			
									
									
						Sorry, I guess I wan't very clear. I'm not entering anything in the message box. I was trying to do a "put" to see the result.
In a button named "Sample" I have:
Code: Select all
on mouseUp
  get inivar(short name of target,"Color","path to Entries.ini")
end mouseUp
This goes into a file named Entries.ini
Code: Select all
[Sample]
color=blue
highlighted=true
[Another Sample]
color=green
highlighted=false
[Yet Another Sample]
color=red
highlighted=false
theValue=325
What is the actual contents of the three parameters, i.e. the short name of the object and the path to the file?
			
			
									
									The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
						The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
- 
				quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Hi Tom,
The path would be "E:/MyRevStacks/Entries.ini", because Revolution always uses forward slashes.
The following script works fine, with your sample file:
I'll think about a script to write information to an INI file, but I need to find those 5 minutes 
Best regards,
Mark
			
			
									
									The path would be "E:/MyRevStacks/Entries.ini", because Revolution always uses forward slashes.
The following script works fine, with your sample file:
Code: Select all
on mouseUp
  answer file ""
  if it is not empty then
    put it into myFile
    open file myFile
    read from file myFile until EOF
    close file myFile
    put iniVar("Another Sample","color",myFile)
  end if
end mouseUp

Best regards,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
						The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
- 
				quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Thanks for your effort, Mark. But if RR can't handle reading and saving information to an external file better than this then, sadly, I guess I'm going to have to stay with Toolbook. I really like a lot of the things that RR has to offer but this is one I can't understand it not supporting. 
Thanks again for your effort
Tom
			
			
									
									
						Thanks again for your effort
Tom
- 
				marielle
- Livecode Opensource Backer 
Tom, 
It would help if you expressed your problem more clearly. Is it that you want to be able to read and write a full file? Is it that you want to be able to overwrite the value for a specific parameter?
First case:
This assumes that you know the value for tPath and the value for tFileName. If you don't, then use the approach proposed by Mark. "answer" will pop a dialog box asking you to specify a file. The file that is being specified is put into a it value. You then have. 
Second case: 
you first need to read the data, find the relevant parameter in the file, replace the value.
If sections are important and you can have the same parameter name appearing in different sections, then you need to add a bit of code to handle this, but that's only a few lines more.
(written on the fly, in 2 minutes, not tested).
			
			
									
									
						It would help if you expressed your problem more clearly. Is it that you want to be able to read and write a full file? Is it that you want to be able to overwrite the value for a specific parameter?
First case:
Code: Select all
put myIniData into URL ("file:" & tPath & tFileName) Code: Select all
answer file "select the ini file to save the data in:"
put it into tFilePath
if it is not empty then 
     -- if the user pressed "cancel" and didn't specify a file, 
     -- then the value returned is empty
   put myIniData into URL ("file:" & tFilePath) 
end if
you first need to read the data, find the relevant parameter in the file, replace the value.
Code: Select all
on iniFile.setValue pIniFile, pParameter, pNewValue
   if pIniFile is empty then exit iniFile.setValue
   if there is not a file pIniFile then exit iniFile.setValue
   if pParameter is empty then exit iniFile.setValue
   -----
   put URL ("file:" & pIniFile) into tIniData
   put lineoffset(cr & pParamater & "=", cr & tIniData) into tLineOffset
   put line tLineOffset of tIniData into tParamAndValue
   set the itemdel to "="
   put item 1 of tParamAndValue into tParam
   put tParam & "=" & pNewValue into line tLineOffset of tInitData
   put tIniData into URL ("file:" & pIniFile) 
end iniFile.setValue
(written on the fly, in 2 minutes, not tested).
