home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / SREFPRC1 / OPENREAD.SRF < prev    next >
Text File  |  1996-10-17  |  2KB  |  74 lines

  1. /* ----------------------------------------------------------------------- */
  2. /* OPEN_READ: keep trying to open a file (for msec seconds
  3. . Argumentes:
  4.         afile == file to open
  5.         msec == quit trying after msec seconds
  6.         howopen = open mode (READ WRITE BOTH READ ) -- default is READ
  7.   Returns
  8.     Status, with values
  9.         -1 = no such file
  10.         -2 = error opening (say, NEW specified but file exists), or timeout
  11.         >0 = seconds it took to open
  12. */
  13. /* ----------------------------------------------------------------------- */
  14.  
  15. sref_open_read:
  16. parse upper arg afile , msec , howopen .
  17. debug=0
  18.  
  19. howopen=translate(howopen)
  20.  
  21. if afile=0 | afile="" then do
  22.    if debug=1 then   say "OPEN_READ: No file name provided "
  23.    return -1             /*no such file flat */
  24. end
  25.  
  26. /* DISALLOW wildcarded files (they cause trouble below */
  27. if pos('*',afile)>0 | pos('?',afile)>0 then do
  28.     if debug=1 then  say "OPEN_READ: No wildcards allowed "
  29.     return -1
  30. end
  31.  
  32. isfile=stream(afile,'c','query exists') ;
  33. if howopen="NEW"  then do
  34.     if isfile="" then
  35.         isfile=afile
  36.     else do
  37.         if debug=1 then  say "OPEN_READ: NEW file already exists "
  38.        return -1
  39.     end  /* Do */
  40. end
  41. else do
  42.    if isfile=""  then do
  43.        if debug=1 then  say 'OPEN_READ: Could not find ' afile
  44.       return -1             /*no such file */
  45.    end 
  46. end
  47.  
  48. sec1=time('RESET')
  49. foy=time('ELAPSED')
  50.  
  51. do until time('ELAPSED') > msec
  52.     select
  53.     when howopen='BOTH' then
  54.        inuse=stream(isfile,'c','open')
  55.     when howopen='WRITE'| howopen="NEW" then do
  56.          inuse=stream(isfile,'c','open write')
  57.     end
  58.     otherwise do
  59.         inuse=stream(isfile,'c','open read')
  60.     end
  61.     end
  62.     if inuse<>'READY:' then do
  63.         foo=syssleep(1)                  /* wait a second, and try again */
  64.         iterate
  65.       end
  66. /* Else, it's openable */
  67.     gog=time('ELAPSED')
  68.     return gog+0.01
  69. end
  70.  if debug=1 then  say " OPEN_READ: no time "
  71. return -2                /* could not open in alloted time */
  72.  
  73.  
  74.