REXX Tips & Tricks, Version 2.80


Inf-HTML [About][Toc][Index] 0.9b (c) 1995 Peter Childs


Read a textfile using CharIn()





 
/* ------------------------------------------------------------------ */
/* Based on an idea & source code from John Wunderlin                 */

/* ------------------------------------------------------------------ */
/* function: Read a file using CharIn() and split it into lines by    */
/*           hand                                                     */
/*                                                                    */
/* call:     rxReadStem = stem_for_the_lines                          */
/*           call RxReadTextFile fileName  {, lineSep }               */
/*                                                                    */
/* where:    rxReadStem - name of the stem for the file contents      */
/*                        The name MUST end with a dot!               */
/*           fileName - name of the file to read                      */
/*           LineSep - line separator chars                           */
/*                     (def.: "0D0A"x)                                */
/*                                                                    */
/* returns:                                                           */
/*           0 -> ok                                                  */
/*           1 -> parameter missing                                   */
/*           2 -> file not found                                      */
/*           3 -> variable referenced in RxReadStem is invalid        */
/*                                                                    */
/* example:  To read the file \CONFIG.SYS into the stem 'CONFIG.' use */
/*                                                                    */
/*             rxReadStem = 'CONFIG.'                                 */
/*             thisRC = RxReadTextFile( '\CONFIG.SYS' )               */
/*                                                                    */
/*                                                                    */
RxReadTextFile: PROCEDURE expose (RxReadStem)

                    /* install local error handlers                   */
  SIGNAL ON  NOTREADY Name RxReadTextFileError
  SIGNAL ON  ERROR    Name RxReadTextFileError
  SIGNAL ON  FAILURE  Name RxReadTextFileError

                    /* init the return code                           */
  thisRC = 3

                    /* check the name of the variable for the         */
                    /* result                                         */
  if  symbol( rxReadStem ) = 'VAR' & right( rxReadStem,1 ) = '.' then
  do

                    /* get the parameter                              */
    parse arg fileName , lineSep

                    /* remove leading and trailing blanks from the    */
                    /* parameter                                      */
    fileName = strip( fileName )
    lineSep = strip( lineSep )

                    /* use default line separator if necessary        */
    if lineSep = "" then
      lineSep = d2c(13) || d2c(10)

                    /* set the return code                            */
    thisRC = 1

                    /* init the stem with the lines of the file       */
    interpret drop rxReadStem
    call value rxReadStem || '0', 0

    if fileName <> "" then
    do
      if stream( fileName, "c", "QUERY EXISTS" ) <> "" then
      do
                    /* read the complete file using Charin()          */
        fileContents = charIN( fileName, 1, chars( fileName ) )

                    /* close the file                                 */
        call stream fileName, "c", "CLOSE"

                    /* split the file into lines by hand              */
        startpos = 1
        do lineCount = 1 by 1

                    /* search the end of the current line             */
          curpos = pos( lineSep,filecontents,startpos )
          if curpos = 0 then
            leave lineCount

          lineLen = ( curpos - startpos )

                    /* save the line in the stem                      */
          call value rxReadStem || LineCount , ,
                       substr( fileContents,startpos,linelen )

          startpos = curpos + length( lineSep )
        end lineCount

                    /* save the no. of lines                          */
        call value rxReadStem || '0' , lineCount -1

                    /* set the return code                            */
        thisRC = 0

      end /* if stream( ... */
    end /* if filename <> "" then */
  end /* if */

RETURN thisRC

/* error exit for RxReadTextFileError                                 */

RxReadTextFileError:
                    /* turn off the condition that caused the error   */
  INTERPRET 'SIGNAL OFF ' condition( 'C' )

                    /* close the file                                 */
  call stream fileName, 'c', 'CLOSE'

return 3


  

Inf-HTML End Run - Successful