Update a configuration file: Difference between revisions

From Rosetta Code
Content added Content deleted
m (comments on parameter data)
(Added PicoLisp)
Line 42: Line 42:


[[Category:File handling]]
[[Category:File handling]]

=={{header|PicoLisp}}==
<lang PicoLisp>(let Data # Read all data
(in "config"
(make
(until (eof)
(link (trim (split (line) " "))) ) ) )
(setq Data # Fix comments
(mapcar
'((L)
(while (head '(";" ";") (car L))
(pop L) )
(if (= '(";") (car L))
L
(cons NIL L) ) )
Data ) )
(let (Need NIL Seed NIL NBan NIL NStr NIL Favo NIL)
(map
'((L)
(let D (mapcar uppc (cadar L))
(cond
((= '`(chop "NEEDSPEELING") D)
(if Need
(set L)
(on Need)
(unless (caar L)
(set (car L) '(";")) ) ) )
((= '`(chop "SEEDSREMOVED") D)
(if Seed
(set L)
(on Seed)
(when (caar L)
(set (car L)) ) ) )
((= '`(chop "NUMBEROFBANANAS") D)
(if NBan
(set L)
(on NBan)
(set (cddar L) 1024) ) )
((= '`(chop "NUMBEROFSTRAWBERRIES") D)
(if NStr
(set L)
(on NStr) ) )
((= '`(chop "FAVOURITEFRUIT") D)
(if Favo
(set L)
(on Favo) ) ) ) ) )
Data )
(unless Need
(conc Data (cons (list NIL "NEEDSPEELING"))) )
(unless Seed
(conc Data (cons (list NIL "SEEDSREMOVED"))) )
(unless NBan
(conc Data (cons (list NIL "NUMBEROFBANANAS" 1024))) )
(unless NStr
(conc Data (cons (list NIL "NUMBEROFSTRAWBERRIES" 62000))) ) )
(out "config"
(for L Data
(prinl (glue " " (if (car L) L (cdr L)))) ) ) )</lang>

Revision as of 13:06, 7 June 2011

Update a configuration file is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

We have a configuration file as follows:

# This is a configuration file in standard configuration file format
#
# Lines begininning with a hash or a semicolon are ignored by the application
# program. Blank lines are also ignored by the application program.

# Note that configuration option names are not case sensitive. However,
# configuration parameter data is case sensitive and the lettercase must
# be preserved.

# This is a favourite fruit
FAVOURITEFRUIT banana

# This is a boolean that should be set
NEEDSPEELING

# This boolean is commented out
; SEEDSREMOVED

# How many bananas we have
NUMBEROFBANANAS 48

The task is to manipulate the configuration file as follows:

  • Comment out the needspeeling option (using a semicolon prefix)
  • Uncomment the seedsremoved option by removing the semicolon and any leading whitespace
  • Change the numberofbananas parameter to 1024
  • Uncomment or create a new parameter for numberofstrawberries with a value of 62000

Note that configuration option names are not case sensitive. This means that changes should be effected, regardless of the case.

Option comments are made always using a semicolon. Lines beginning with hash symbols should not be manipulated and left unchanged in the revised file.

If a configuration option does not exist within the file (either commented or uncommented), it should be added during this update and duplicate configuration option names in the file should be removed, leaving just the first entry.

For the purpose of this task, the revised file should contain appropriate entries, whether commented or not for needspeeling,seedsremoved,numberofbananas and numberofstrawberries.)

The update may rewrite configuration entry in capital letters. However lines beginning with hashes and any parameter data must not be capitalized (eg the banana for favourite fruit must not become capitalized). The update process should also replace double semicolon prefixes with just a single semicolon (unless it is uncommenting the option, in which case it should remove all leading semicolons).

PicoLisp

<lang PicoLisp>(let Data # Read all data

  (in "config"
     (make
        (until (eof)
           (link (trim (split (line) " "))) ) ) )
  (setq Data  # Fix comments
     (mapcar
        '((L)
           (while (head '(";" ";") (car L))
              (pop L) )
           (if (= '(";") (car L))
              L
              (cons NIL L) ) )
        Data ) )
  (let (Need NIL  Seed NIL  NBan NIL  NStr NIL  Favo NIL)
     (map
        '((L)
           (let D (mapcar uppc (cadar L))
              (cond
                 ((= '`(chop "NEEDSPEELING") D)
                    (if Need
                       (set L)
                       (on Need)
                       (unless (caar L)
                          (set (car L) '(";")) ) ) )
                 ((= '`(chop "SEEDSREMOVED") D)
                    (if Seed
                       (set L)
                       (on Seed)
                       (when (caar L)
                          (set (car L)) ) ) )
                 ((= '`(chop "NUMBEROFBANANAS") D)
                    (if NBan
                       (set L)
                       (on NBan)
                       (set (cddar L) 1024) ) )
                 ((= '`(chop "NUMBEROFSTRAWBERRIES") D)
                    (if NStr
                       (set L)
                       (on NStr) ) )
                 ((= '`(chop "FAVOURITEFRUIT") D)
                    (if Favo
                       (set L)
                       (on Favo) ) ) ) ) )
        Data )
     (unless Need
        (conc Data (cons (list NIL "NEEDSPEELING"))) )
     (unless Seed
        (conc Data (cons (list NIL "SEEDSREMOVED"))) )
     (unless NBan
        (conc Data (cons (list NIL "NUMBEROFBANANAS" 1024))) )
     (unless NStr
        (conc Data (cons (list NIL "NUMBEROFSTRAWBERRIES" 62000))) ) )
  (out "config"
     (for L Data
        (prinl (glue " " (if (car L) L (cdr L)))) ) ) )</lang>