Newbie config. file question

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kpeters
Posts: 112
Joined: Wed Mar 21, 2007 9:32 pm

Newbie config. file question

Post by kpeters » Mon Apr 30, 2007 4:04 am

Being new to both Mac OS X and Revolution:

What does Rev offer in the way of reading from/writing to configuration files ?

What is the 'official' destination for such files? Just some thing in the user's home directory?

TIA,
Kai

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

Post by Mark » Mon Apr 30, 2007 8:20 am

Dear Kai,

I assume you mean to ask where to put preferences files for your application. Rev doesn't offer anything, and no development environment does, as far as I know.

All it takes it a simple script that writes info to a file in the preferences folder.

Code: Select all

global gPrefsVar
on savePrefs
  put gPrefsVar & cr & the cPrefsProp of stack "Your Stack" & cr & "Whatever information you want" into myPrefs
  put specialfolderpath("preferences") & "/Your Stack.prefs" into myFilePath
  open file myFilePath
  write myPrefs to file myFilePath
  close file myFilePath
end savePrefs
You'd read these preferences with the following script:

Code: Select all

global gPrefsVar
on savePrefs
  put specialfolderpath("preferences") & "/Your Stack.prefs" into myFilePath
  open file myFilePath
  read from file myFilePath until EOF
  close file myFilePath
  put line 1 of it into gPrefsVar
  set the cPrefsProp of stack "Your Stack" to line 2 of it
  return line 3 of it -- "Whatever information...."
end savePrefs
You can make this as complex as you like. For example, I use a script that takes variable names as parameters and saves the contents of these variables to a preferences file when called.

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

xApple
Posts: 113
Joined: Wed Nov 29, 2006 10:21 pm

Post by xApple » Mon Apr 30, 2007 10:17 am

For OS X, I like to store the pref file inside the application bundle. If it intrests anyone here are my script for handeling preference file generation and retrival:

Code: Select all

on preOpenStack
  determinePrefPath
  readPrefFile
  insertPrefValues
end preOpenStack


on closeStack
  determinePrefPath
  generatePrefs
  writePrefFile
end closeStack


on determinePrefPath
  global myPrefPath
  ----------
  set the itemDel to "."
  switch the platform
  case "MacOS"
    if (item 1 of the systemVersion < 10) or (the environment is "development") then
      put specialFolderPath("Preferences") into myPrefPath
    else
      put getApplicationPath() & "/Preferences" into myPrefPath
    end if
    break
  case "Win32"
    put specialFolderPath(baseConvert("0x001a",16,10)) into myPrefPath -- CSIDL_APPDATA
    break
  default
    put the defaultFolder into myPrefPath
  end switch
  -----------
  if the result is "folder not found" then answer error "Could not find the preferences folder" with "Damit"
  if myPrefPath is empty then
    answer error "Error while getting the preferences folder path." with "Damit"
    put the defaultFolder into myPrefPath
  end if
  -----------
  put myPrefPath & "/" & (the short name of this stack) & "Prefs.ini" into myPrefPath
end determinePrefPath


on readPrefFile
  global myPrefPath, thePrefs
  ---------- "
  open file myPrefPath for text read
  if the result is "can't open that file" then -- file doesn't exist yet (1st time)
    do merge(format("put [[relativeRef(\"initialPrefs\")]] into thePrefs"))
    mustCreatePrefFolder
  else
    read from file myPrefPath until EOF
    put it into thePrefs
    close file myPrefPath
  end if
  -----------
  split thePrefs by return and "="
end readPrefFile


on insertPrefValues
  global thePrefs
  ----------
  put the keys of thePrefs into myKeys
  repeat with x = 1 to the number of lines of myKeys
    put line x of myKeys into keyName
    do format("put \"%s\" into %s",thePrefs[keyName],relativeRef(keyName))
  end repeat
end insertPrefValues


on generatePrefs
  global thePrefs
  ----------
  put the keys of thePrefs into myKeys
  repeat with x = 1 to the number of lines of myKeys
    put line x of myKeys into keyName
    do format("put %s into thePrefs[keyName]",relativeRef(keyName))
  end repeat
  combine thePrefs using return and "="
end generatePrefs


on writePrefFile
  global myPrefPath, thePrefs
  ----------
  set the fileType to ((field "creatorCode" of card "dataCard") & "INI ")
  open file myPrefPath for text write
  if the result is not empty then answerError the result
  write thePrefs to file myPrefPath
  if the result is not empty then answerError the result
  close file myPrefPath
end writePrefFile


on answerError theMsg
  global myPrefPath
  ----------
  answer error "The following error was encountered while trying to open the preference file:" & \
      return & theMsg & return & "The path was:" & return & myPrefPath with "Damit"
end answerError


function getApplicationPath
  put the filename of this stack into myPath
  set the itemDel to "/"
  repeat 2
    put empty into last item of myPath
    put empty into last char of myPath
  end repeat
  return myPath
end getApplicationPath


on mustCreatePrefFolder
  if (the platform is "MacOS") and \
      (the systemVersion >= 10) and \
      (the environment is "standalone application") then
    -----------
    put (getApplicationPath() & "/Preferences") into myPath
    create folder myPath
  end if
end mustCreatePrefFolder


function relativeRef theName
  repeat with x = 1 to the number of cards of this stack
    if exists(control theName of card x) then return "control" && quote & theName & quote && "of card" && x
  end repeat
  return "no relative referance"
end relativeRef
It uses the standard INI format type and for each line of type "a=b" it will store the value "b" into the field of name "a".

kpeters
Posts: 112
Joined: Wed Mar 21, 2007 9:32 pm

Post by kpeters » Mon Apr 30, 2007 7:18 pm

Thanks - your help is - as always - very much appreciated.

Regards,
Kai

Post Reply