home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Language / Compiler / Builtins / real_OutStream.m < prev    next >
Encoding:
Text File  |  1990-08-16  |  2.2 KB  |  89 lines

  1. % @(#)real_OutStream.mX    1.2  3/16/88
  2. %
  3. import _BufferObject from "Builtins"
  4. export _OutStreamObject to "Builtins"
  5.  
  6.  
  7. const _OutStreamObject == immutable object _OutStreamObject
  8.   export getSignature, create
  9.  
  10.   const OutStreamType == type OutStreamType
  11.     operation putChar [Character]
  12.     operation putInt [Integer, Integer]
  13.     operation putReal [ Real ]
  14.     operation putString [ String ]
  15.     operation flush
  16.     operation close
  17.   end OutStreamType
  18.  
  19.   function getSignature -> [ r : Signature ]
  20.     r <- OutStreamType
  21.   end getSignature
  22.  
  23.   operation create [ fd : Integer ] -> [r : OutStreamType]
  24.     r <- object aUnixOutStream
  25.       export putChar, putInt, putReal, putString, flush, close
  26.       const myfd : Integer == fd
  27.       monitor
  28.     var isBroken :    Boolean <- false
  29.     var isClosed :       Boolean <- false
  30.     const buffer == _BufferObject.create[myfd]
  31.  
  32.     operation putChar [r : Character]
  33.       if isBroken or isClosed then returnAndFail end if
  34.       buffer.addChar[r]
  35.       on failure returnAndFail end failure
  36.     end putChar
  37.  
  38.     operation putInt [number : Integer, width : Integer]
  39.       const chars == number.asString
  40.       var theWidth : Integer
  41.       var pad : Character
  42.       
  43.       if isBroken or isClosed then returnAndFail end if
  44.       if width < 0 then
  45.         pad <- '0'
  46.         theWidth <- ~width
  47.       else
  48.         pad <- ' '
  49.         theWidth <- width
  50.       end if
  51.       buffer.pad[pad, theWidth - chars.length]
  52.       buffer.addString[chars]
  53.       on failure returnAndFail end failure
  54.     end putInt
  55.  
  56.     operation putReal [r : Real]
  57.       const chars == r.asString
  58.  
  59.       if isBroken or isClosed then returnAndFail end if
  60.       buffer.addString[chars]
  61.       on failure returnAndFail end failure
  62.     end putReal
  63.  
  64.     operation putString [r : String]
  65.       if isBroken or isClosed then returnAndFail end if
  66.       buffer.addString[r]
  67.       on failure returnAndFail end failure
  68.     end putString
  69.  
  70.     operation flush
  71.       if isBroken or isClosed then returnAndFail end if
  72.       buffer.write
  73.     end flush
  74.  
  75.     operation close
  76.       if isBroken then returnAndFail end if
  77.       buffer.write
  78.       if myfd != 1 then
  79.         isClosed <- true
  80.         primitive 117 [] <- [myfd]
  81.       end if
  82.       on failure returnAndFail end failure
  83.     end close
  84.       end monitor
  85.     end aUnixOutStream
  86.   end create
  87. end _OutStreamObject
  88.