home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWFiles / Sources / FWFileAc.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  8.3 KB  |  241 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWFileAc.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #include <Limits.h>
  13.  
  14. #ifndef FWFILEAC_H
  15. #include "FWFileAc.h"
  16. #endif
  17.  
  18. #ifndef FWFILREP_H
  19. #include "FWFilRep.h"
  20. #endif
  21.  
  22. #ifndef FWPRIDEB_H
  23. #include "FWPriDeb.h"
  24. #endif
  25.  
  26. #ifndef FWEXCDEF_H
  27. #include "FWExcDef.h"
  28. #endif
  29.  
  30. #if defined(FW_BUILD_MAC) && !defined(__FILES__)
  31. #include "Files.h"
  32. #endif
  33.  
  34. #if defined(FW_BUILD_WIN) && !defined(IO_H)
  35. #include "io.h"
  36. #endif
  37.  
  38. #if FW_LIB_EXPORT_PRAGMAS
  39. #pragma lib_export on
  40. #endif
  41.  
  42. #ifdef FW_BUILD_MAC
  43. #pragma segment File
  44. #endif
  45.  
  46. #ifdef FW_BUILD_WIN16
  47. extern "C" void FAR PASCAL DOS3Call();                // We use this instead of calling "Int 21h"
  48. #endif
  49.  
  50. //========================================================================================
  51. // CLASS FW_CFileSink
  52. //
  53. // This is a reference counted class.  It maintains a FW_TCountedPtr which points to a
  54. //   FW_CFileRep.  All copies of this class will reference the same rep and 
  55. //   therefore will share the same access permissions and file mark position.
  56. //========================================================================================
  57.  
  58. //----------------------------------------------------------------------------------------
  59. //    FW_CFileSink::FW_CFileSink
  60. //
  61. //  Copy constructor.  Does not copy the fRep itself, but only copies the pointer.
  62. //----------------------------------------------------------------------------------------
  63. FW_CFileSink::FW_CFileSink(const FW_CFileSink& other) :
  64.     FW_CRandomAccessSink(),
  65.     fRep(other.fRep)
  66. {
  67.     FW_END_CONSTRUCTOR
  68. }
  69.  
  70.  
  71. //----------------------------------------------------------------------------------------
  72. //    FW_CFileSink::FW_CFileSink
  73. //
  74. //  Internal method.  Clients should generally not use this constructor.
  75. //  Attaches the file access to the given access rep.  This routine is used mainly by
  76. //    exceptions to reference this FW_CFileSink during error handling.
  77. //----------------------------------------------------------------------------------------
  78. FW_CFileSink::FW_CFileSink(FW_PFile rep) :
  79.     FW_CRandomAccessSink(),
  80.     fRep(rep)
  81. {
  82.     FW_END_CONSTRUCTOR
  83. }
  84.  
  85.  
  86. //----------------------------------------------------------------------------------------
  87. //    FW_CFileSink::~FW_CFileSink
  88. //----------------------------------------------------------------------------------------
  89. FW_CFileSink::~FW_CFileSink()
  90. {
  91.     FW_START_DESTRUCTOR
  92. }
  93.  
  94.  
  95. //----------------------------------------------------------------------------------------
  96. //    FW_CFileSink::Read
  97. //
  98. //  Reads count bytes from the file into the buffer specified by destination.  The bytes
  99. //    will be read starting at the current file position.  The user is responsible for
  100. //    providing the buffer.
  101. //----------------------------------------------------------------------------------------
  102. void FW_CFileSink::Read(void* destination,
  103.                           long count)
  104. {
  105.     fRep->Read(destination, count);
  106. }
  107.  
  108.  
  109. //----------------------------------------------------------------------------------------
  110. //    FW_CFileSink::ReadPeek
  111. //
  112. //  Returns a pointer to a buffer full of data.  This data is for reading only and may be
  113. //    used as little or as much as necessary.  availableReadBytes has no meaning on input
  114. //    but on output returns the number of bytes that were just returned in the buffer.
  115. //  The user is not responsible for creating the returned buffer and should not try and
  116. //    destroy the buffer.
  117. //  This method does not advance the file pointer.  It may also return less data than the
  118. //    buffer size, if the user has already accepted some of the data by calling
  119. //    ReadPeekAdvance.
  120. //----------------------------------------------------------------------------------------
  121. const void* FW_CFileSink::ReadPeek(long& availableReadBytes)
  122. {
  123.     return fRep->ReadPeek(availableReadBytes);
  124. }
  125.  
  126.  
  127. //----------------------------------------------------------------------------------------
  128. //    FW_CFileSink::ReadPeekAdvance
  129. //
  130. //  This method should be called after ReadPeek to accept bytesRead bytes from the read
  131. //    buffer.  This will cause the file position to be advanced by bytesRead from the
  132. //    current position.
  133. //----------------------------------------------------------------------------------------
  134. void FW_CFileSink::ReadPeekAdvance(long bytesRead)
  135. {
  136.     fRep->ReadPeekAdvance(bytesRead);
  137. }
  138.  
  139.  
  140. //----------------------------------------------------------------------------------------
  141. //    FW_CFileSink::GetWritableBytes
  142. //
  143. //  Doesn't make much sense for Files since the file size is effectively unlimited.
  144. //----------------------------------------------------------------------------------------
  145. long FW_CFileSink::GetWritableBytes() const
  146. {
  147.     return LONG_MAX;
  148. }
  149.  
  150.  
  151. //----------------------------------------------------------------------------------------
  152. //    FW_CFileSink::Write
  153. //
  154. //  Write 'count' bytes to the file from the 'source' buffer.  The bytes will be written
  155. //    starting at the current file position.  The user is responsible for providing the
  156. //    source buffer.
  157. //----------------------------------------------------------------------------------------
  158. void FW_CFileSink::Write(const void* source,
  159.                            long count)
  160. {
  161.     fRep->Write(source, count);
  162. }
  163.  
  164.  
  165. //----------------------------------------------------------------------------------------
  166. // FW_CFileSink::WritePeek
  167. //
  168. //  Returns a pointer to an empty buffer.  The user may write data into this buffer as if
  169. //    she were writing the data directly into the file.  The data will not get written to
  170. //    the file though until WritePeekAdvance is called.  availableReadBytes has no meaning 
  171. //    on input but on output returns the space available in the buffer for writing to.
  172. //  The user is not responsible for creating the returned buffer and should not try and
  173. //    destroy the buffer.
  174. //  This method does not advance the file pointer.  It may also return a smaller buffer than the
  175. //    original buffer size, if the user has already flushed some of the data by calling
  176. //    WritePeekAdvance.
  177. //----------------------------------------------------------------------------------------
  178. void* FW_CFileSink::WritePeek(long& availableWriteBytes)
  179. {
  180.     return fRep->WritePeek(availableWriteBytes);
  181. }
  182.  
  183.  
  184. //----------------------------------------------------------------------------------------
  185. // FW_CFileSink::WritePeekAdvance
  186. //
  187. //  This method should be called after WritePeek to write bytesWritten bytes to the file
  188. //    from the WritePeek buffer.  This will cause the file position to be advanced by 
  189. //    bytesWritten bytes from current position.
  190. //----------------------------------------------------------------------------------------
  191. void FW_CFileSink::WritePeekAdvance(long bytesWritten)
  192. {
  193.     fRep->WritePeekAdvance(bytesWritten);
  194. }
  195.  
  196.  
  197. //----------------------------------------------------------------------------------------
  198. //    FW_CFileSink::GetLength
  199. //
  200. //  Returns the length of the file in bytes.  
  201. //----------------------------------------------------------------------------------------
  202. long FW_CFileSink::GetLength() const
  203. {
  204.     return fRep->GetLength();
  205. }
  206.  
  207.  
  208. //----------------------------------------------------------------------------------------
  209. //    FW_CFileSink::SetLength
  210. //
  211. //  Returns the length of the file in bytes.  
  212. //----------------------------------------------------------------------------------------
  213. void FW_CFileSink::SetLength(long length)
  214. {
  215.     fRep->SetLength(length);
  216. }
  217.  
  218.  
  219. //----------------------------------------------------------------------------------------
  220. //    FW_CFileSink::GetPosition
  221. //
  222. //  Returns the current file position as a byte offset from the beginning of the file.
  223. //----------------------------------------------------------------------------------------
  224. long FW_CFileSink::GetPosition() const
  225. {
  226.     return fRep->GetPosition();
  227. }
  228.  
  229.  
  230. //----------------------------------------------------------------------------------------
  231. //    FW_CFileSink::SetPosition
  232. // 
  233. //  Sets the current file position.  length is specified as a byte offset from the 
  234. //    beginning of the file.  Data will then be read from or written to the new file
  235. //    position.
  236. //----------------------------------------------------------------------------------------
  237. void FW_CFileSink::SetPosition(long length)
  238. {
  239.     fRep->SetPosition(FW_CFileSystem::kFromStart, length);
  240. }
  241.