/* iniread.p reads an ini file, does not use Parm table Written by Scott Auge Copyright (c) 2001 Amduus Information Works, Inc. scott_auge@yahoo.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 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 */