/* iniread.p reads an ini file, does not use Parm table */ /* * Written by Scott Auge scott_auge@yahoo.com sauge@amduus.com * Copyright (c) 2001 Amduus Information Works, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Amduus Information Works * and its contributors. * 4. Neither the name of Amduus nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY AMDUUS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AMDUUS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ /* Allowable formats: # anywhere in line represents a comment - best to have these at the beginning for readability. [] represents a stanza parm=value represents the value under that stanza Example: # Example Ini File [Stanza1] Parm1=this Parm2=that [Stanza2] Parm1=that Parm2=this Returns the value of the parm under the stanza or ? if not found. */ DEF VAR RCSVersion AS CHARACTER INIT "$Header: /home/appl/opensrc/misc/RCS/inird.p,v 1.1 2001/08/05 01:06:26 sauge Exp $" NO-UNDO. DEF INPUT PARAMETER pFileName AS CHARACTER NO-UNDO. DEF INPUT PARAMETER pStanza AS CHARACTER NO-UNDO. DEF INPUT PARAMETER pParm AS CHARACTER NO-UNDO. DEF OUTPUT PARAMETER pValue AS CHARACTER NO-UNDO. DEF VAR lLine AS CHARACTER NO-UNDO. DEF VAR lFoundStanza AS LOGICAL NO-UNDO. DEF VAR lIsComment AS LOGICAL NO-UNDO. DEF VAR lStanza AS CHARACTER NO-UNDO. /* Default so that we are looking through the file */ ASSIGN lFoundStanza = FALSE. INPUT FROM VALUE(pFileName). REPEAT: IMPORT UNFORMATTED lLine. /* Determine if it is a comment - if so, move on */ RUN IsComment (INPUT lLine, OUTPUT lIsComment). IF lIsComment THEN NEXT. /* If we have not found are stanza, examine the line to see if */ /* if contains the stanza we are looking for. If so, then take */ /* note we have found it and move onto the next line to look */ /* for the parm. If not, then skip on to the next line. */ IF NOT lFoundStanza THEN DO: RUN PullStanza (INPUT lLine, INPUT pStanza, OUTPUT lStanza). IF lStanza = ? THEN NEXT. MESSAGE "Found Stanza". ASSIGN lFoundStanza = TRUE. NEXT. END. /* NOT lFoundStanza */ /* If we have gotten this far, then we have found the stanza and */ /* are looking for the parameter and it's associated value. */ /* Here we have found the parameter, so close the file and pass */ /* it's value back to the calling program. */ RUN PullParmName (INPUT lLine, INPUT pParm, OUTPUT pValue). IF pValue <> ? THEN DO: INPUT CLOSE. RETURN. END. END. /* REPEAT */ /* IF we have not found the parameter in the file, then close up */ /* the file and return a ? to inform the caller that we could */ /* not find it. */ INPUT CLOSE. ASSIGN pValue = ?. /* ------------------- Internal Procedures ----------------------- */ /* The parameter name and the parameter value are adjacent to each */ /* other seperated by an ='s sign. Cannot do a BEGINS because an- */ /* other parameter might begin with the same name. This pulls the */ /* parameter out if it is one. */ PROCEDURE PullParmName: DEF INPUT PARAMETER pLine AS CHARACTER NO-UNDO. DEF INPUT PARAMETER pParm AS CHARACTER NO-UNDO. DEF OUTPUT PARAMETER pValue AS CHARACTER NO-UNDO. /* First TRIM the line so that we eliminate any beginning and */ /* ending spaces. */ ASSIGN pLine = TRIM (pLine). /* If the line does not begins the parameter name as well the */ /* ='s sign then we have no use going on. */ IF NOT pLine BEGINS pParm + "=" THEN DO: ASSIGN pValue = ?. RETURN. END. /* Not our parameter line */ /* We want the string at the end of the ='s sign, so cut that */ /* portion out and pass it along to the caller program. */ pValue = SUBSTRING(pLine, LENGTH(pParm + "=") + 1). END. /* PROCEDURE PullParmName */ /* Determine if the line we are examining has the stanza we are */ /* looking for. If so, then return the stanza, else ?. */ PROCEDURE PullStanza: DEF INPUT PARAMETER pLine AS CHARACTER NO-UNDO. DEF INPUT PARAMETER pStanza AS CHARACTER NO-UNDO. DEF OUTPUT PARAMETER pStanzaReturn AS CHARACTER NO-UNDO. ASSIGN pStanza = "[" + pStanza + "]". IF INDEX (pLine, pStanza) <> 0 THEN ASSIGN pStanzaReturn = pStanza. ELSE ASSIGN pStanzaReturn = ?. END. /* PROCEDURE PullStanza */ /* Determine if the line is a comment - later on will prune out the */ /* comment text, but since a comment symbol could be a parm value, */ /* comments are restricted to one line for the while. */ PROCEDURE IsComment: DEF INPUT PARAMETER pLine AS CHARACTER NO-UNDO. DEF OUTPUT PARAMETER pValue AS LOGICAL NO-UNDO. DEF VAR i AS INTEGER NO-UNDO. DEF VAR l AS INTEGER NO-UNDO. ASSIGN l = LENGTH (pLine). DO i = 1 TO l: IF SUBSTRING(pLine, i) = "#" THEN DO: ASSIGN pValue = YES. RETURN. END. /* Found a # in the line */ END. /* DO */ ASSIGN pValue = NO. END. /* PROCEDURE IsComment */