Pulling data from an external file

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Pulling data from an external file

Post by quailcreek » Tue Feb 20, 2007 4:22 pm

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

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Post by malte » Tue Feb 20, 2007 4:34 pm

Could you be a bit more specific what exactly you are trying to do?

Set object properties depending on data fetched from an external file? Which is the bit you are having problems with? Reading the file? Storing it? Setting the properties?

All the best,

Malte

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Post by quailcreek » Tue Feb 20, 2007 6: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

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Feb 21, 2007 1:31 pm

Took me 5 minutes. Let me know if there are any problems.

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

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Post by quailcreek » Wed Feb 21, 2007 4:18 pm

Very nice, Mark.
I'll make a test run later today and let you know. In the mean time, if you have another 5 minutes to spare. There is the other side of the coin. setIniVar(header,item,value,path) where the value is saved into the file.

;-)
Tom

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Post by quailcreek » Wed Feb 21, 2007 10:50 pm

Mark,
Please excuse my ignorance but should I do a "put it" to see the result? I have been doing a “put myIniDataâ€

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Feb 21, 2007 10:55 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
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

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Post by quailcreek » Wed Feb 21, 2007 11:06 pm

Sorry, Mark. I'm not seeing anything in the message box.

Tom

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Feb 21, 2007 11:23 pm

Tom,

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

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Post by quailcreek » Thu Feb 22, 2007 12:54 am

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:

Code: Select all

on mouseUp
  get inivar(short name of target,"Color","path to Entries.ini")
end mouseUp
Along with your script.

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

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Feb 22, 2007 1:09 am

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

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Post by quailcreek » Thu Feb 22, 2007 1:25 am

Mark,
The short name of the target would be "Sample", the name of the button. And the path is "E:\MyRevStacks\Entries.ini" but that wil be different on your machine, right?

Tom

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Feb 22, 2007 1:30 am

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:

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
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 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

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Post by quailcreek » Thu Feb 22, 2007 3:59 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

marielle
Livecode Opensource Backer
Livecode Opensource Backer

Post by marielle » Thu Feb 22, 2007 4:15 pm

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:

Code: Select all

put myIniData into URL ("file:" & tPath & tFileName) 
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.

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
Second case:

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
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).

Post Reply

Return to “Talking LiveCode”