Newbie questions
[1/11] from: kpeters::mvinc::net at: 2-Oct-2003 9:40
Hi all ~
just stumbled upon REBOL yesterday and have started writing my first min-app
and the following questions
have come up thus far:
what is the most elegant way to read a value out of an ini file?
tried to find some docs on error handling but thus far have not succeeded -
is there anything out there?
how do i know that a read from ftp succeeds and how do i know which error
occured if it fails?
Thanks for all input
Kai
[2/11] from: Steven:White:ci:bloomington:mn:us at: 2-Oct-2003 12:16
>>> [kpeters--mvinc--net] 10/02/03 11:40AM >>>
>>>what is the most elegant way to read a value out of an ini file?
I have wondered about this myself. If the ini file is something that
exists from some other application, I have no idea, but if you are
writing an application yourself and want to provide some kind of
initialization parameter file, it seems like you should be able to do
this:
Make your "ini" file itself a REBOL script, that does nothing except
define words and their values. Then, as the first step in the
application, DO the "ini" script. This is based on the idea that REBOL
itself is somewhat readable. For example:
Your application:
REBOL []
...
do %init.ini
...
The contents of init.ini: (for example)
REBOL []
TEST-MODE: true
LOG-FILE-ID: %logfile
APPLICATION-TITLE: "REBOL Consultants, Inc."
...
whatever else is needed
Accept this idea with the realization that I am a bit of a newcomer
myself.
Steven White
City of Bloomington
1800 W Old Shakopee Rd
Bloomington MN 55431-3096
USA
952-563-4882 (voice)
952-563-4672 (fax)
[steven--white--ci--bloomington--mn--us]
[3/11] from: petr:krenzelok:trz:cz at: 2-Oct-2003 20:16
Kai Peters wrote:
>Hi all ~
>
>just stumbled upon REBOL yesterday and have started writing my first min-app
>and the following questions
>
just curious ... where have you heard of rebol, folks? Last weeks we can
see new ppl coming :-)
>have come up thus far:
>
>what is the most elegant way to read a value out of an ini file?
>
it depends upon the structure of ini file ... you can build parser for
it ... you can e.g. read/lines - that will return your file in a block
of lines, thru which you can traverse ...
>tried to find some docs on error handling but thus far have not succeeded -
>is there anything out there?
>
try [yourd code here]
attempt [your code here]
->> error? try [read %ble.bel]
== true
->> probe disarm try [read %ble.bel]
make object! [
code: 500
type: 'access
id: 'cannot-open
arg1: "/C/REBOL/View/ble.bel"
arg2: none
arg3: none
near: [read %ble.bel]
where: none
]
-pekr-
[4/11] from: apwing:zonnet:nl at: 2-Oct-2003 20:40
Hi Kay,
for error handling have a look at
http://www.rebol.com/docs/core23/rebolcore-17.html
Met vriendelijke groet / with kind regards,
Arie van Wingerden
[5/11] from: rebol:techscribe at: 2-Oct-2003 13:29
Hi Kai.
For "the most elegant way" I would direct you to Gabriele ;-).
A workable way would be:
given the ini file
name1="value1"
name2=640
name3=SVGA
Let's assume we are looking for the value of name3:
We need to convert the ini file into something that can be conveniently
processed under REBOL, for instance a block. The final expression could
look something like this:
select make block! replace/all read %IniFile.ini "=" " " 'name3
1. We read the contents of the file IniFile.ini
... read %IniFile.ini
The percent sign is needed to tell REBOL IniFile.ini is a file name.
'read returns a string.
2. Since we will want to use select (see help select for details) we
want to rid of the '=' signs and replace them by a space.
... replace/all ... "=" " "
(see help replace for details on replace/all)
3. We use the string returned by replace to create a block.
... make block! replace/all ...
4. Now we can retrieve the value associated with name3 by using select:
select make block! ... 'name3
Note that we must use the literal word 'name3 We cannot use name3,
since REBOL would believe it to be a word, and attempt to look up the
value of name3.
Hope this helps.
Elan
Kai Peters wrote:
[6/11] from: kpeters:mvinc at: 2-Oct-2003 13:25
Thanks, Steve. I will use your suggestion as it seems to be in the true
REBOL spirit.
(Am still interested in the ini solution if anyone has one, though).
Another question:
How do I typecast the URL below for use with banner or any other word
expecting a string?
REBOL []
ftpserver: ftp://server.somedomain.net
view layout [ banner ftpserver ]
Thanks,
Kai
[7/11] from: SunandaDH:aol at: 2-Oct-2003 16:51
Kai:
> ftpserver: ftp://server.somedomain.net
> view layout [ banner ftpserver ]
ftpserver: ftp://server.somedomain.net
view layout [ banner form ftpserver ]
Form -- converts a value to a string
Sunanda
[8/11] from: ingo:2b1 at: 2-Oct-2003 22:51
Welcome, Kai,
Kai Peters wrote:
<...>
> How do I typecast the URL below for use with banner or any other word
> expecting a string?
>
REBOL []
ftpserver: ftp://server.somedomain.net
view layout [ banner to-string ftpserver ]
Kind regards,
Ingo
[9/11] from: maximo:meteorstudios at: 2-Oct-2003 17:50
> Make your "ini" file itself a REBOL script, that does nothing except
> define words and their values. Then, as the first step in the
<<quoted lines omitted: 6>>
> do %init.ini
> ...
your example is a very common practice amongst rebol coders.
might I just suggest that you conserve the .r extension, in order to make sure windows
and your users, do not expect the content to be an actual windows ini file.
for example rebol has a user.r file which is executed before any rebol script.
you might want to provide the values in your script beforehand with default values, so
that if not all the values where defined in the init file, your application can fallback
on default values.
also, I often use an object to store configuration block and provide a load and save
method to it.
config: make object! [
language: 'francais
bg-image: %polished.png
config-path: %./
config-name: %config.r
word: 'config ;the name of the word which holds the config object
init: does [
print ["using language : " language]
print ["using image : " bg-image]
]
load: has [config-file][
config-file: append copy config-path config-name
either (exists? config-file) [
do system/words/load config-file
][
print rejoin ["There is no file named: %" config-file " ...using default values" ]
]
init
]
save: has [config-file data][
config-file: append copy config-path config-name
data: copy ""
; here we build a rebol script which sets the config for each item
;----------------------------------
append data rejoin [word "/language: '" config/language "^/"]
append data rejoin [word "/bg-image: %" config/bg-image "^/" ]
write config-file data
]
]
if you do:
config/save
a file (%./config.r ) with the following content will be created:
config/language: 'francais
config/bg-image: %polished.png
this is both obvious and easy to edit and has the benefit, that you can load it directly
within rebol.
this simple object can be extended with many new values, which only have to be mirrored
in the save function and has the added benefit that you can load up new configs at run
time
also, because init is separated from load, you can call init anytime to reset your work.
as all the scripts which are posted on this list, edit, use and improve at will.
HTH!
-MAx
[10/11] from: SunandaDH:aol at: 2-Oct-2003 19:22
> what is the most elegant way to read a value out of an ini file?
Actaully reading a windows ini file .....
This is not elegant, but it parses a standard ini file, with a reasonable
degree of error trapping -- there are some tricky bits as what goes into an INI
file may not correspond to what REBOL thinks of as a word. And doubtless
testing on other INI files will throw up other error conditions that need to be
handled.
;;;=================
ini-block: copy []
current-section: copy []
foreach ini-line read/lines %/c/windows/win.ini
[section-name: ini-line
error? try [section-name: first load/all ini-line]
either any [error? try [block? section-name]
not block? section-name
]
[parsed-line: parse/all ini-line "="
append last current-section parsed-line/1
append last current-section parsed-line/2
]
[append ini-block current-section
current-section: copy []
append current-section form section-name
append/only current-section copy []
]
]
;; samples from my win.ini
print select/skip select ini-block "ports" "com1:" 2
print select/skip select ini-block "intl" "iCountry" 2
print select/skip select ini-block "TrueType" "FontSmoothing" 2
;;;=================
Sunanda.
[11/11] from: SunandaDH:aol at: 3-Oct-2003 4:13
> > what is the most elegant way to read a value out of an ini file?
I've tidied up my offering and added it to the Script Library. It's now a
function to parse an ini file, plus another function to find keyword values:
http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=parse-ini.r
Ingo from another thread
<<Have a look at http://www.h-o-h.org/simtpop.r a little script that acts
like a look-a-like of an smtp and pop server to Outlook 97... soon to be
found on www.rebol.org the world famous Rebol library server!
>>
That's now in the Library too:
http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=simtpop.r
I'd like to encourage anyone here who posts a useful snippet of code to the
Mailing List to also consider taking a few moments to add it to the Script
Library.
The Library may not yet be world famous, but it is well-liked by Google and
is an excellent way of making it more likely that someone in the future will
find your work,
Sunanda.
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted