home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWStream / SLStrmRW.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  15.4 KB  |  557 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLStrmRW.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef SLSTRMRW_H
  13. #include "SLStrmRW.h"
  14. #endif
  15.  
  16. #ifndef FWSTRMRW_H
  17. #include "FWStrmRW.h"
  18. #endif
  19.  
  20. #ifndef FWPRIDEB_H
  21. #include "FWPriDeb.h"
  22. #endif
  23.  
  24. #ifndef FWEXCLIB_H
  25. #include "FWExcLib.h"
  26. #endif
  27.  
  28. #ifndef FWODFEXC_H
  29. #include "FWODFExc.h"
  30. #endif
  31.  
  32. #include "SLASinks.xh"
  33. #include "SLObjReg.xh"
  34.  
  35. #ifdef FW_BUILD_MAC
  36. #pragma segment FWStream
  37. #endif
  38.  
  39.  
  40. //----------------------------------------------------------------------------------------
  41. // FW_NATIVE_BIGENDIAN, FW_NATIVE_LITTLEENDIAN
  42. //    One and only one of these macros is true to define the native format
  43. //    for storing integral values
  44. //----------------------------------------------------------------------------------------
  45. #ifdef FW_BUILD_MAC
  46. #define    FW_NATIVE_BIGENDIAN            1
  47. #define    FW_NATIVE_LITTLEENDIAN        0
  48. #endif
  49.  
  50. #ifdef FW_BUILD_WIN
  51. #define    FW_NATIVE_BIGENDIAN            0
  52. #define    FW_NATIVE_LITTLEENDIAN        1
  53. #endif
  54.  
  55. #if !FW_NATIVE_BIGENDIAN && !FW_NATIVE_LITTLEENDIAN
  56. #error "Failed to define an endianness"
  57. #endif
  58.  
  59. //----------------------------------------------------------------------------------------
  60. // swapShort
  61. //----------------------------------------------------------------------------------------
  62.  
  63. static short swapShort(short aShort)
  64. {
  65.     union
  66.     {
  67.         short temp;
  68.         char s[2];
  69.     };
  70.     temp = aShort;
  71.     char c = s[0];
  72.     s[0] = s[1];
  73.     s[1] = c;
  74.     return temp;
  75. }
  76.  
  77. //----------------------------------------------------------------------------------------
  78. // swapLong
  79. //----------------------------------------------------------------------------------------
  80.  
  81. static long swapLong(long aLong)
  82. {
  83.     union
  84.     {
  85.         long temp;
  86.         char s[4];
  87.     };
  88.     temp = aLong;
  89.     char c = s[0];
  90.     s[0] = s[3];
  91.     s[3] = c;
  92.     c = s[1];
  93.     s[1] = s[2];
  94.     s[2] = c;
  95.     return temp;
  96. }
  97.  
  98. //----------------------------------------------------------------------------------------
  99. // readableIdentity
  100. //----------------------------------------------------------------------------------------
  101.  
  102. static void readableIdentity(FW_HReadableStream stream,
  103.                              void* destination,
  104.                              long count,
  105.                              long itemSize)
  106. {
  107.     stream->fSink->Read(&stream->fev, destination, count*itemSize);
  108. }
  109.  
  110. //----------------------------------------------------------------------------------------
  111. // writableIdentity
  112. //----------------------------------------------------------------------------------------
  113.  
  114. static void writableIdentity(FW_HWritableStream stream,
  115.                              const void* source,
  116.                              long count,
  117.                              long itemSize)
  118. {
  119.     stream->fSink->Write(&stream->fev, (void*)source, count*itemSize);
  120. }
  121.  
  122.  
  123. //----------------------------------------------------------------------------------------
  124. // readReturnError
  125. //----------------------------------------------------------------------------------------
  126.  
  127. static FW_PlatformError readReturnError(FW_HReadableStream stream,
  128.                                         void* destination,
  129.                                         long count,
  130.                                         long itemSize)
  131. {
  132.     FW_RETURN_ERR_TRY
  133.     {
  134.         readableIdentity(stream, destination, count, itemSize);
  135.     }
  136.     FW_RETURN_ERR_CATCH
  137. }
  138.  
  139.  
  140. //----------------------------------------------------------------------------------------
  141. // writeReturnError
  142. //----------------------------------------------------------------------------------------
  143.  
  144. static FW_PlatformError writeReturnError(FW_HWritableStream stream,
  145.                                          const void* source,
  146.                                          long count,
  147.                                          long itemSize)
  148. {
  149.     FW_RETURN_ERR_TRY
  150.     {
  151.         writableIdentity(stream, source, count, itemSize);
  152.     }
  153.     FW_RETURN_ERR_CATCH
  154. }
  155.  
  156.  
  157. //----------------------------------------------------------------------------------------
  158. // FW_PrivReadableStream_Construct
  159. //----------------------------------------------------------------------------------------
  160.  
  161. FW_PlatformError    SL_API    
  162. FW_PrivReadableStream_Construct(FW_HReadableStream stream, 
  163.                                 FW_OSink* sink,
  164.                                 FW_OObjectRegistry* objectRegistry,
  165.                                 int numberFormat)
  166. {
  167.     FW_RETURN_ERR_TRY
  168.     {
  169.         // Make safe for destruction
  170.         stream->fArchiveCreatedObjectRegistry = (objectRegistry == 0);
  171.  
  172.         stream->fSink = sink;
  173.         stream->fObjectRegistry = objectRegistry;
  174.         stream->fReserved = 0;
  175.         FW_PrimitiveSetMemory(&stream->fev, sizeof(stream->fev), 0);
  176.  
  177.         stream->fNumberFormat = numberFormat;
  178. #if FW_NATIVE_BIGENDIAN
  179.         if (stream->fNumberFormat == FW_kStream_BigEndian)
  180.             stream->fNumberFormat = FW_kStream_NativeEndian;
  181. #endif
  182. #if FW_NATIVE_LITTLEENDIAN
  183.         if (stream->fNumberFormat == FW_kStream_LittleEndian)
  184.             stream->fNumberFormat = FW_kStream_NativeEndian;
  185. #endif
  186.  
  187.         if (stream->fArchiveCreatedObjectRegistry)
  188.             stream->fObjectRegistry = new FW_OBasicObjectRegistry;
  189.     } 
  190.     FW_RETURN_ERR_CATCH
  191. }
  192.  
  193.  
  194. //----------------------------------------------------------------------------------------
  195. // FW_PrivReadableStream_CopyConstruct
  196. //----------------------------------------------------------------------------------------
  197.  
  198. FW_PlatformError    SL_API    
  199. FW_PrivReadableStream_CopyConstruct(FW_HReadableStream stream, 
  200.                                     FW_HReadableStream original)
  201. {    
  202.     stream->fSink                         = original->fSink;
  203.     stream->fObjectRegistry               = original->fObjectRegistry;
  204.     stream->fev                              = original->fev;
  205.     stream->fNumberFormat                  = original->fNumberFormat;
  206.     stream->fArchiveCreatedObjectRegistry = FALSE;
  207.     return FW_xNoError;
  208. }
  209.  
  210. //----------------------------------------------------------------------------------------
  211. // FW_PrivReadableStream_Destruct
  212. //----------------------------------------------------------------------------------------
  213.  
  214. FW_PlatformError    SL_API    
  215. FW_PrivReadableStream_Destruct(FW_HReadableStream stream)
  216. {
  217.     FW_RETURN_ERR_TRY
  218.     {
  219.         if (stream->fArchiveCreatedObjectRegistry)
  220.             delete stream->fObjectRegistry;
  221.     } 
  222.     FW_RETURN_ERR_CATCH
  223. }
  224.  
  225.  
  226. //----------------------------------------------------------------------------------------
  227. // FW_PrivReadableStream_ReadBytes
  228. //----------------------------------------------------------------------------------------
  229.  
  230. FW_PlatformError    SL_API    
  231. FW_PrivReadableStream_ReadBytes(FW_HReadableStream stream, 
  232.                                 void* buffer, 
  233.                                 long count)
  234. {
  235.     return readReturnError(stream, buffer, count, 1);
  236. }
  237.  
  238.  
  239. //----------------------------------------------------------------------------------------
  240. // FW_PrivReadableStream_ReadChars
  241. //----------------------------------------------------------------------------------------
  242.  
  243. FW_PlatformError    SL_API    
  244. FW_PrivReadableStream_ReadChars(FW_HReadableStream stream, 
  245.                                 char* buffer, 
  246.                                 long count)
  247. {
  248.     return readReturnError(stream, buffer, count, sizeof(buffer[0]));
  249. }
  250.  
  251.  
  252. //----------------------------------------------------------------------------------------
  253. // FW_PrivReadableStream_ReadNullTerminatedString
  254. //----------------------------------------------------------------------------------------
  255.  
  256. FW_PlatformError    SL_API    
  257. FW_PrivReadableStream_ReadNullTerminatedString(FW_HReadableStream stream, 
  258.                                                char* nullTerminatedString)
  259. {
  260.     Environment* const ev = &stream->fev;
  261.     FW_OSink* const sink = stream->fSink;
  262.  
  263.     FW_RETURN_ERR_TRY
  264.     {
  265.         long availableReadBytes;
  266.         FW_Boolean foundTerminator = FALSE;
  267.     
  268.         availableReadBytes = sink->GetReadableBytes(ev);
  269.         while (!foundTerminator && availableReadBytes > 0)
  270.         {
  271.             sink->Read(ev, nullTerminatedString, 1);
  272.             --availableReadBytes;
  273.             foundTerminator = (*nullTerminatedString++ == '\0');
  274.         }
  275.  
  276.         if (!foundTerminator)
  277.             FW_Failure(FW_xReadableStream);
  278.     }
  279.     FW_RETURN_ERR_CATCH
  280. }
  281.  
  282.  
  283. //----------------------------------------------------------------------------------------
  284. // FW_PrivReadableStream_ReadShorts
  285. //----------------------------------------------------------------------------------------
  286.  
  287. FW_PlatformError    SL_API    
  288. FW_PrivReadableStream_ReadShorts(FW_HReadableStream stream, 
  289.                                  short* buffer, 
  290.                                  long count)
  291. {
  292.     if (stream->fNumberFormat == FW_kStream_NativeEndian)
  293.     {
  294.         return readReturnError(stream, buffer, count, sizeof(buffer[0]));
  295.     }
  296.     else
  297.     {
  298.         FW_PlatformError anError = readReturnError(stream, buffer, count, sizeof(buffer[0]));
  299.         
  300.         if (anError == FW_xNoError)
  301.         {
  302.             for (; count > 0; --count, ++buffer)
  303.                 *buffer = swapShort(*buffer);
  304.         }
  305.         
  306.         return anError;
  307.     }
  308. }
  309.  
  310.  
  311. //----------------------------------------------------------------------------------------
  312. // FW_PrivReadableStream_ReadLongs
  313. //----------------------------------------------------------------------------------------
  314.  
  315. FW_PlatformError    SL_API    
  316. FW_PrivReadableStream_ReadLongs(FW_HReadableStream stream, 
  317.                                 long* buffer, 
  318.                                 long count)
  319. {
  320.     if (stream->fNumberFormat == FW_kStream_NativeEndian)
  321.     {
  322.         return readReturnError(stream, buffer, count, sizeof(buffer[0]));
  323.     }
  324.     else
  325.     {
  326.         FW_PlatformError anError = readReturnError(stream, buffer, count, sizeof(buffer[0]));
  327.         
  328.         if (anError == FW_xNoError)
  329.         {
  330.             for (; count > 0; --count, ++buffer)
  331.                 *buffer = swapLong(*buffer);
  332.         }
  333.         
  334.         return anError;
  335.     }
  336. }
  337.  
  338.  
  339. //----------------------------------------------------------------------------------------
  340. // FW_PrivReadableStream_ReadFloats
  341. //----------------------------------------------------------------------------------------
  342.  
  343. FW_PlatformError    SL_API    
  344. FW_PrivReadableStream_ReadFloats(FW_HReadableStream stream, 
  345.                                  float* buffer, 
  346.                                  long count)
  347. {
  348.     return readReturnError(stream, buffer, count, sizeof(buffer[0]));
  349. }
  350.  
  351.  
  352. //----------------------------------------------------------------------------------------
  353. // FW_PrivReadableStream_ReadDoubles
  354. //----------------------------------------------------------------------------------------
  355.  
  356. FW_PlatformError    SL_API    
  357. FW_PrivReadableStream_ReadDoubles(FW_HReadableStream stream, 
  358.                                   double* buffer, 
  359.                                   long count)
  360. {
  361.     return readReturnError(stream, buffer, count, sizeof(buffer[0]));
  362. }
  363.  
  364.  
  365. //----------------------------------------------------------------------------------------
  366. // FW_PrivWritableStream_Construct
  367. //----------------------------------------------------------------------------------------
  368.  
  369. FW_PlatformError    SL_API    
  370. FW_PrivWritableStream_Construct(FW_HWritableStream stream, 
  371.                                 FW_OSink* sink,
  372.                                 FW_OObjectRegistry* objectRegistry,
  373.                                 int numberFormat)
  374. {
  375.  
  376.     FW_RETURN_ERR_TRY
  377.     {
  378.         // Make safe for destruction
  379.         stream->fArchiveCreatedObjectRegistry = (objectRegistry == 0);
  380.  
  381.         stream->fSink = sink;
  382.         stream->fObjectRegistry = objectRegistry;
  383.         stream->fReserved = 0;
  384.         FW_PrimitiveSetMemory(&stream->fev, sizeof(stream->fev), 0);
  385.  
  386.         stream->fNumberFormat = numberFormat;
  387. #if FW_NATIVE_BIGENDIAN
  388.         if (stream->fNumberFormat == FW_kStream_BigEndian)
  389.             stream->fNumberFormat = FW_kStream_NativeEndian;
  390. #endif
  391. #if FW_NATIVE_LITTLEENDIAN
  392.         if (stream->fNumberFormat == FW_kStream_LittleEndian)
  393.             stream->fNumberFormat = FW_kStream_NativeEndian;
  394. #endif
  395.  
  396.         if (stream->fArchiveCreatedObjectRegistry)
  397.             stream->fObjectRegistry = new FW_OBasicObjectRegistry;
  398.     }
  399.     FW_RETURN_ERR_CATCH
  400. }
  401.  
  402.  
  403. //----------------------------------------------------------------------------------------
  404. // FW_PrivWritableStream_CopyConstruct
  405. //----------------------------------------------------------------------------------------
  406.  
  407. FW_PlatformError    SL_API    
  408. FW_PrivWritableStream_CopyConstruct(FW_HWritableStream stream, 
  409.                                     FW_HWritableStream original)
  410. {
  411.     stream->fSink                         = original->fSink;
  412.     stream->fObjectRegistry               = original->fObjectRegistry;
  413.     stream->fev                              = original->fev;
  414.     stream->fNumberFormat                  = original->fNumberFormat;
  415.     stream->fArchiveCreatedObjectRegistry = FALSE;
  416.     return FW_xNoError;
  417. }
  418.  
  419. //----------------------------------------------------------------------------------------
  420. // FW_PrivWritableStream_Destruct
  421. //----------------------------------------------------------------------------------------
  422.  
  423. FW_PlatformError    SL_API    
  424. FW_PrivWritableStream_Destruct(FW_HWritableStream stream)
  425. {
  426.     FW_RETURN_ERR_TRY
  427.     {
  428.         if (stream->fArchiveCreatedObjectRegistry)
  429.             delete stream->fObjectRegistry;
  430.     } 
  431.     FW_RETURN_ERR_CATCH
  432. }
  433.  
  434.  
  435. //----------------------------------------------------------------------------------------
  436. // FW_PrivWritableStream_WriteBytes
  437. //----------------------------------------------------------------------------------------
  438.  
  439. FW_PlatformError    SL_API    
  440. FW_PrivWritableStream_WriteBytes(FW_HWritableStream stream, 
  441.                                  const void* buffer, 
  442.                                  long count)
  443. {
  444.     return writeReturnError(stream, buffer, count, 1);
  445. }
  446.  
  447.  
  448. //----------------------------------------------------------------------------------------
  449. // FW_PrivWritableStream_WriteChars
  450. //----------------------------------------------------------------------------------------
  451.  
  452. FW_PlatformError    SL_API    
  453. FW_PrivWritableStream_WriteChars(FW_HWritableStream stream, 
  454.                                  const char* buffer, 
  455.                                  long count)
  456. {
  457.     return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
  458. }
  459.  
  460.  
  461. //----------------------------------------------------------------------------------------
  462. // FW_PrivWritableStream_WriteNullTerminatedString
  463. //----------------------------------------------------------------------------------------
  464.  
  465. FW_PlatformError    SL_API    
  466. FW_PrivWritableStream_WriteNullTerminatedString(FW_HWritableStream stream, 
  467.                                                 const char* nullTerminatedString)
  468. {
  469.     return writeReturnError(stream, 
  470.                             nullTerminatedString, 
  471.                             FW_PrimitiveStringLength(nullTerminatedString) + 1, 
  472.                             sizeof(nullTerminatedString[0]));
  473. }
  474.  
  475. //----------------------------------------------------------------------------------------
  476. // FW_PrivWritableStream_WriteShorts
  477. //----------------------------------------------------------------------------------------
  478.  
  479. FW_PlatformError    SL_API    
  480. FW_PrivWritableStream_WriteShorts(FW_HWritableStream stream, 
  481.                                   const short* buffer, 
  482.                                   long count)
  483. {
  484.     if (stream->fNumberFormat == FW_kStream_NativeEndian)
  485.     {
  486.         return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
  487.     }
  488.     else
  489.     {
  490.         FW_RETURN_ERR_TRY
  491.         {
  492.             for (; count > 0; --count, ++buffer)
  493.             {
  494.                 short temp = swapShort(*buffer);
  495.                 writableIdentity(stream, &temp, 1, sizeof(temp));
  496.             }
  497.         }
  498.         FW_RETURN_ERR_CATCH
  499.     }
  500. }
  501.  
  502.  
  503. //----------------------------------------------------------------------------------------
  504. // FW_PrivWritableStream_WriteLongs
  505. //----------------------------------------------------------------------------------------
  506.  
  507. FW_PlatformError    SL_API    
  508. FW_PrivWritableStream_WriteLongs(FW_HWritableStream stream, 
  509.                                  const long* buffer, 
  510.                                  long count)
  511. {
  512.     if (stream->fNumberFormat == FW_kStream_NativeEndian)
  513.     {
  514.         return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
  515.     }
  516.     else
  517.     {
  518.         FW_RETURN_ERR_TRY
  519.         {
  520.             for (; count > 0; --count, ++buffer)
  521.             {
  522.                 long temp = swapLong(*buffer);
  523.                 writableIdentity(stream, &temp, 1, sizeof(temp));
  524.             }
  525.         }
  526.         FW_RETURN_ERR_CATCH
  527.     }
  528. }
  529.  
  530.  
  531. //----------------------------------------------------------------------------------------
  532. // FW_PrivWritableStream_WriteFloats
  533. //----------------------------------------------------------------------------------------
  534.  
  535. FW_PlatformError    SL_API    
  536. FW_PrivWritableStream_WriteFloats(FW_HWritableStream stream, 
  537.                                   const float* buffer, 
  538.                                   long count)
  539. {
  540.     return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
  541. }
  542.  
  543.  
  544. //----------------------------------------------------------------------------------------
  545. // FW_PrivWritableStream_WriteDoubles
  546. //----------------------------------------------------------------------------------------
  547.  
  548. FW_PlatformError    SL_API    
  549. FW_PrivWritableStream_WriteDoubles(FW_HWritableStream stream, 
  550.                                    const double* buffer, 
  551.                                    long count)
  552. {
  553.     return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
  554. }
  555.  
  556.  
  557.