home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnusmalltalk / unixstream.st < prev    next >
Text File  |  1992-03-01  |  8KB  |  408 lines

  1. "======================================================================
  2. |
  3. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  4. | Written by Steve Byrne.
  5. |
  6. | This file is part of GNU Smalltalk.
  7. |
  8. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  9. | under the terms of the GNU General Public License as published by the Free
  10. | Software Foundation; either version 1, or (at your option) any later version.
  11. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  12. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  14. | details.
  15. | You should have received a copy of the GNU General Public License along with
  16. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  17. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  18. |
  19.  ======================================================================"
  20.  
  21. "
  22. |     Change Log
  23. | ============================================================================
  24. | Author       Date       Change 
  25. | sbb         23 Feb 92      Also added readShort and friends.
  26. |
  27. | sbb         23 Feb 92      Added direction constants
  28. |
  29. | sbb         17 Nov 90      added read:numBytes:
  30. |
  31. | sbb         17 Nov 90      Added skip: method.
  32. |
  33. | sbb         17 Nov 90      Installed as a built-in class.
  34. |
  35. "
  36.  
  37. PositionableStream subclass: #UnixStream
  38.            instanceVariableNames: 'fd'
  39.            classVariableNames: ''
  40.            poolDictionaries: ''
  41.            category: 'Unix integration'
  42. !
  43.     
  44. UnixStream comment:
  45. 'I provide an interface to Unix file descriptors, so that Smalltalk methods
  46. may perform most of the I/O operations that C programs can.  I am quite proud
  47. of my contribution to the GNU Smalltalk project, and look forward to serving
  48. the project better in the future.'
  49. !
  50.  
  51.  
  52. Behavior defineCFunc: 'open'
  53.          withSelectorArgs: 'open: aFileName flags: flagsInteger 
  54.                 mode: anInteger'
  55.      forClass: UnixStream
  56.      returning: #int
  57.      args: #(string int int)
  58. !
  59.  
  60. Behavior defineCFunc: 'close'
  61.      withSelectorArgs: 'close: fileDescriptor'
  62.      forClass: UnixStream
  63.      returning: #int
  64.      args: #(int)
  65. !
  66.  
  67. Behavior defineCFunc: 'read'
  68.      withSelectorArgs: 'read: fileDescriptor into: buf bytes: anInteger'
  69.      forClass: UnixStream
  70.      returning: #int
  71.      args: #(int byteArrayOut int)
  72. !
  73.  
  74. Behavior defineCFunc: 'read'
  75.      withSelectorArgs: 'read: fileDescriptor into: buf chars: anInteger'
  76.      forClass: UnixStream
  77.      returning: #int
  78.      args: #(int stringOut int)
  79. !
  80.  
  81. Behavior defineCFunc: 'write'
  82.      withSelectorArgs: 'write: fileDescriptor from: buf bytes: anInteger'
  83.      forClass: UnixStream
  84.      returning: #int
  85.      args: #(int byteArray int)
  86. !
  87.  
  88. Behavior defineCFunc: 'ioctl'
  89.      withSelectorArgs: 'ioctl: fileDescriptor request: anInteger 
  90.                 arg: cObject'
  91.      forClass: UnixStream
  92.      returning: #int
  93.      args: #(int cObject unknown)
  94. !
  95.  
  96. Behavior defineCFunc: 'lseek'
  97.      withSelectorArgs: 'lseek: fileDescriptor offset: anInteger 
  98.                 whence: cObject'
  99.      forClass: UnixStream
  100.      returning: #int
  101.      args: #(int int int)
  102. !
  103.  
  104. Behavior defineCFunc: 'tell'
  105.      withSelectorArgs: 'tell: fileDescriptor'
  106.      forClass: UnixStream
  107.      returning: #int
  108.      args: #(int)
  109. !
  110.  
  111. "======================================================================
  112. |
  113. |   Type specific I/O routines
  114. |
  115.  ======================================================================"
  116.  
  117. Behavior defineCFunc: 'readChar'
  118.      withSelectorArgs: 'readChar: fileDescriptor'
  119.      forClass: UnixStream
  120.      returning: #smalltalk
  121.      args: #(int)
  122. !
  123.  
  124. Behavior defineCFunc: 'readUChar'
  125.      withSelectorArgs: 'readUChar: fileDescriptor'
  126.      forClass: UnixStream
  127.      returning: #smalltalk
  128.      args: #(int)
  129. !
  130.  
  131. Behavior defineCFunc: 'readShort'
  132.      withSelectorArgs: 'readShort: fileDescriptor'
  133.      forClass: UnixStream
  134.      returning: #smalltalk
  135.      args: #(int)
  136. !
  137.  
  138. Behavior defineCFunc: 'readUShort'
  139.      withSelectorArgs: 'readUShort: fileDescriptor'
  140.      forClass: UnixStream
  141.      returning: #smalltalk
  142.      args: #(int)
  143. !
  144.  
  145. Behavior defineCFunc: 'readLong'
  146.      withSelectorArgs: 'readLong: fileDescriptor'
  147.      forClass: UnixStream
  148.      returning: #smalltalk
  149.      args: #(int)
  150. !
  151.  
  152. Behavior defineCFunc: 'readULong'
  153.      withSelectorArgs: 'readULong: fileDescriptor'
  154.      forClass: UnixStream
  155.      returning: #smalltalk
  156.      args: #(int)
  157. !
  158.  
  159. Behavior defineCFunc: 'readFloat'
  160.      withSelectorArgs: 'readFloat: fileDescriptor'
  161.      forClass: UnixStream
  162.      returning: #smalltalk
  163.      args: #(int)
  164. !
  165.  
  166. Behavior defineCFunc: 'readDouble'
  167.      withSelectorArgs: 'readDouble: fileDescriptor'
  168.      forClass: UnixStream
  169.      returning: #smalltalk
  170.      args: #(int)
  171. !
  172.  
  173.  
  174. Behavior defineCFunc: 'writeChar: aChar'
  175.      withSelectorArgs: 'write: fileDescriptor char: aChar'
  176.      forClass: UnixStream
  177.      returning: #void
  178.      args: #(int char)
  179. !
  180.  
  181. Behavior defineCFunc: 'writeShort: aShort'
  182.      withSelectorArgs: 'write: fileDescriptor short: aShort'
  183.      forClass: UnixStream
  184.      returning: #void
  185.      args: #(int int)
  186. !
  187.  
  188. Behavior defineCFunc: 'writeLong: aLong'
  189.      withSelectorArgs: 'write: fileDescriptor long: aLong'
  190.      forClass: UnixStream
  191.      returning: #void
  192.      args: #(int long)
  193. !
  194.  
  195. Behavior defineCFunc: 'writeFloat: aFloat'
  196.      withSelectorArgs: 'write: fileDescriptor float: aFloat'
  197.      forClass: UnixStream
  198.      returning: #void
  199.      args: #(int double)
  200. !
  201.  
  202. Behavior defineCFunc: 'writeDouble: aDouble'
  203.      withSelectorArgs: 'write: fileDescriptor double: aDouble'
  204.      forClass: UnixStream
  205.      returning: #void
  206.      args: #(int double)
  207. !
  208.  
  209.  
  210.  
  211.  
  212.  
  213. !UnixStream class methodsFor: 'instance creation'!
  214.  
  215. open: fileName dir: anInteger
  216.     ^self open: fileName dir: anInteger mode: 0
  217. !
  218.  
  219. open: fileName dir: anInteger mode: intMode
  220.     ^self new init: fileName dir: anInteger mode: intMode
  221. !
  222.  
  223. on: fd
  224.     ^self new initFd: fd
  225. !!
  226.  
  227.  
  228.  
  229. !UnixStream class methodsFor: 'constants'!
  230.  
  231. readOnly
  232.     ^0
  233. !
  234.  
  235. writeOnly
  236.     ^1
  237. !
  238.  
  239. readWrite
  240.     ^2
  241. !
  242.  
  243. "Other modifiers (like O_APPEND) may be added in the future as needs warrant"
  244. !
  245.  
  246.  
  247.  
  248. !UnixStream methodsFor: 'basic accessing'!
  249.  
  250. close
  251.     ^self close: fd
  252. !
  253.  
  254. read: byteArray
  255.     | val |
  256.     ^self read: fd into: byteArray bytes: byteArray size
  257. !
  258.  
  259. read: byteArray numBytes: anInteger
  260.     | val |
  261.     ^self read: fd into: byteArray bytes: anInteger
  262. !
  263.  
  264. read: string numChars: anInteger
  265.     ^self read: fd into: string chars: anInteger
  266. !
  267.  
  268. write: byteArray
  269.     ^self write: byteArray numBytes: byteArray size
  270. !
  271.  
  272. write: byteArray numBytes: anInteger
  273.     ^self write: fd from: byteArray bytes: anInteger
  274. !
  275.  
  276. tell
  277.     ^self tell: fd
  278. !
  279.  
  280. position: anInteger
  281.     ^self lseek: fd offset: anInteger whence: 0 "Set"
  282. !
  283.  
  284. ioctl: number arg: randomArg
  285.     ^self ioctl: fd request: number arg: randomArg
  286. !
  287.  
  288. readChar
  289.     ^self readChar: fd
  290. !
  291.  
  292. readUChar
  293.     ^self readUChar: fd
  294. !
  295.  
  296. readShort
  297.     ^self readShort: fd
  298. !
  299.  
  300. readUShort
  301.     ^self readUShort: fd
  302. !
  303.  
  304. readLong
  305.     ^self readLong: fd
  306. !
  307.  
  308. readULong
  309.     ^self readULong: fd
  310. !
  311.  
  312. readFloat
  313.     ^self readFloat: fd
  314. !
  315.  
  316. readDouble
  317.     ^self readDouble: fd
  318. !
  319.  
  320. writeChar: aChar
  321.     ^self write: fd char: aChar
  322. !
  323.  
  324. writeShort: aShort        "really an integer"
  325.     ^self write: fd short: aShort
  326. !
  327.  
  328. writeLong: aLong
  329.     ^self write: fd long: aLong
  330. !
  331.  
  332. writeFloat: aFloat
  333.     ^self write: fd float: aFloat
  334. !
  335.  
  336. writeDouble: aDouble
  337.     ^self write: fd double: aDouble
  338. ! !
  339.  
  340.  
  341.  
  342. !UnixStream methodsFor: 'accessing'!
  343.  
  344. readString: numChars
  345.     | byteArray numRead str |
  346.     byteArray _ ByteArray new: numChars.
  347.     (numRead _ self read: byteArray) <= 0 "failed for some reason"
  348.     ifTrue: [ ^nil ].
  349.     str _ String new: numRead.
  350.     1 to: numRead do:
  351.     [ :i | str at: i
  352.            put: (Character value: (byteArray at: i)) ].
  353.     ^str
  354. !
  355.  
  356. next
  357.     self notYetImplemented
  358. !
  359.  
  360. next: anInteger
  361.     self self notYetImplemented
  362. !
  363.  
  364.  
  365. nextPut: aValue
  366.     self notYetImplemented
  367. !
  368.  
  369. contents
  370.     self notYetImplemented
  371. !
  372.  
  373. atEnd
  374.     self notYetImplemented
  375. !
  376.  
  377. size
  378.     "Poor man's size function"
  379.     | curPos size |
  380.     curPos _ self tell.
  381.     self lseek: fd offset: 0 whence: 2. "To end"
  382.     size _ self tell.
  383.     self position: curPos.
  384.     ^size
  385. !
  386.  
  387. skip: anInteger
  388.     "Skip n bytes on the file.  N can be positive or negative"
  389.     ^self lseek: fd offset: anInteger whence: 1 "Cur"
  390. !!
  391.  
  392.  
  393.  
  394. !UnixStream methodsFor: 'private'!
  395.  
  396. init: fileName dir: anInteger mode: intMode
  397.     fd _ self open: fileName flags: anInteger mode: intMode.
  398.     fd < 0
  399.     ifTrue: [ ^nil ]
  400. !
  401.  
  402. initFd: anFd
  403.     fd _ anFd
  404. !!
  405.  
  406.