Update a configuration file: Difference between revisions

m
→‎version 1: added/changed comments, indentations, and whitespace.
m (added whitespace before the TOC (table of contents), added a ;Task: (bold) header, added other whitespace to the task's preamble, corrected some misspellings.)
m (→‎version 1: added/changed comments, indentations, and whitespace.)
Line 1,763:
 
Programming note:   not all REXXes support the closing of files using the   '''lineout'''   BIF with a single argument.
<lang rexx>/*REXX pgmprogram showsdemonstrates how to update a configuration file (4four specific tasks).*/
parse arg iFID oFID . /*obtain optional inputarguments from file─id.the CL*/
if iFID=='' | iFID=='",'" then iFID= 'UPDATECF.TXT' /*useNot defaultgiven? Then use default.*/
if oFID=='' | oFID=='",'" then oFID='\TEMP\UPDATECF.$$$' /*use default?" " " " " */
call lineout iFID; call lineout oFID /*close the input &and the output files.*/
$.=0 /*placeholder of the options founddetected. */
call dos 'ERASE' oFID /*erase a file (with no errerror MSGsmessage).*/
changed=0 /*nothing changed in the file (so far).*/
/* [↓] read the entire cfg config file. */
do rec=0 while lines(iFID)\==0 /*read a record; bump the record cntcount.*/
z=linein(iFID); zz=space(z) /*get recrecord; del elide extraneous blanks.*/
say '───────── record:' z /*echo the record just read──►conread ──► console*/
a=left(zz,1); _=space( translate(zz, ,';') ) /*_: is used to elide multimultiple ";" */
if zz=='' | a=='#' then do; call cpy z; iterate; end /*blank| or a comment.*/
if _=='' then do; changed=1; iterate; end /*elide any semicolons; empty records.*/
parse upper var z op . /*obtain the option from the recrecord. */
/* [↓] OP option may have leading or ···*/
if a==';' then do; parse upper var z 2 op . /*trailing blanks.*/
if op='SEEDSREMOVED' then call new space( substr(z, 2) )
call cpy z; $.op=1 /*write the Z record to the output. file*/
iterate /*rec*/ /* ··· and then go read the next record*/
end
if $.op then do; changed=1; iterate; end /*is the option already defined? */
$.op=1 /* [↑] Yes? DeleteThen delete it. */
if op=='NEEDSPEELING' then call new '";'" z
if op=='NUMBEROFBANANAS' then call new op 1024
if op=='NUMBEROFSTRAWBERRIES' then call new op 62000
call cpy z /*write the Z record to the output. file*/
end /*rec*/
 
nos='NUMBEROFSTRAWBERRIES' /* [↓] Does NOS option need updating? */
if \$.nos then do; call new nos 62000; call cpy z; end /*update optoption.*/
call lineout iFID; call lineout oFID /*close the input &and the output files.*/
if rec==0 then do; say "ERROR: input file wasn't found:" iFID; exit; end
if changed then do /*possibly overwrite the input file. */
call dos 'XCOPY' oFID iFID '/y /q',">nul" /*quietly*/
say; say center('output file', 79, "▒") /*title. */
call dos 'TYPE' oFID /*display content of the output file's content. */
end
call dos 'ERASE' oFID /*erase a file (with no errerror msgmessage).*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────one─line subroutines────────────────*/
cpy: call lineout oFID, arg(1); return /*write one line of text───►oFIDtext ───► oFID. */
dos: ''arg(1) word(arg(2) "2>nul",1); return /*execute a DOS command (quietly). */
new: z=arg(1); changed=1; return /*use new Z, indicate changed recrecord. */</lang>
'''output''' &nbsp; when using the default input file (which has additional removable statements) and input options:
<pre>
───────── record: # This is a configuration file in standard configuration file format
───────── record: #
───────── record: # Lines begininningbeginning with a hash or a semicolon are ignored by the application
───────── record: # program. Blank lines are also ignored by the application program.
───────── record: ;