home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / visbuild / vbsample / irecord.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-16  |  21.9 KB  |  533 lines

  1. /*******************************************************************************
  2. * FILE NAME: irecord.cpp                                                       *
  3. *                                                                              *
  4. * DESCRIPTION:                                                                 *
  5. *   Functions to implement the class(es):                                      *
  6. *     IRecord - Base class for all record classes.                             *
  7. *                                                                              *
  8. * COPYRIGHT:                                                                   *
  9. *   IBM(R) VisualAge(TM) for C++                                               *
  10. *   (C) Copyright International Business Machines Corporation 1991, 1996       *
  11. *   Licensed Material - Program-Property of IBM - All Rights Reserved.         *
  12. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  13. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  14. *                                                                              *
  15. *   This program will not run in DOS mode.                                     *
  16. *                                                                              *
  17. * DISCLAIMER OF WARRANTIES:                                                    *
  18. *   The following [enclosed] code is sample code created by IBM                *
  19. *   Corporation.  This sample code is not part of any standard IBM product     *
  20. *   and is provided to you solely for the purpose of assisting you in the      *
  21. *   development of your applications.  The code is provided "AS IS",           *
  22. *   without warranty of any kind.  IBM shall not be liable for any damages     *
  23. *   arising out of your use of the sample code, even if they have been         *
  24. *   advised of the possibility of such damages.                                *
  25. *******************************************************************************/
  26. #include <irecord.hpp>
  27.  
  28. #include <string.h>
  29.  
  30. // Define the functions and static data members to be exported.
  31. // Ordinals 350 through 399 are reserved for use by IRecord.
  32. #pragma export(IRecord::asString() const,, 350)
  33. #pragma export(IRecord::field(IString&,unsigned long,unsigned long,    \
  34.                    unsigned char) const,, 351)
  35. #pragma export(IRecord::field(char*,unsigned long,unsigned long,       \
  36.                    unsigned char) const,, 352)
  37. #pragma export(IRecord::field(char,unsigned long,unsigned char) const,, 353)
  38. #pragma export(IRecord::field(double,unsigned long,unsigned char) const,, 354)
  39. #pragma export(IRecord::field(float,unsigned long,unsigned char) const,, 355)
  40. #pragma export(IRecord::field(long double,unsigned long,unsigned char) const,, 356)
  41. #pragma export(IRecord::field(unsigned long,unsigned long,unsigned char) const,, 357)
  42. #pragma export(IRecord::field(unsigned short,unsigned long,unsigned char) const,, 358)
  43. #pragma export(IRecord::field(void*,unsigned long,unsigned long,       \
  44.                    unsigned char) const,, 359)
  45. #pragma export(IRecord::IRecord(),, 360)
  46. #pragma export(IRecord::IRecord(const IRecord&),, 361)
  47. #pragma export(IRecord::IRecord(const IString&),, 362)
  48. #pragma export(IRecord::isOrdered() const,, 363)
  49. #pragma export(IRecord::operator=(const IRecord&),, 364)
  50. #pragma export(IRecord::operator=(const IString&),, 365)
  51. #pragma export(IRecord::setField(const IString&,unsigned long,         \
  52.                    unsigned long, unsigned char),, 366)
  53. #pragma export(IRecord::setField(const char*,unsigned long,            \
  54.                    unsigned long,unsigned char),, 367)
  55. #pragma export(IRecord::setField(const char,unsigned long),, 368)
  56. #pragma export(IRecord::setField(const double,unsigned long),, 369)
  57. #pragma export(IRecord::setField(const float,unsigned long),, 370)
  58. #pragma export(IRecord::setField(const long double,unsigned long),, 371)
  59. #pragma export(IRecord::setField(const unsigned long,unsigned long),, 372)
  60. #pragma export(IRecord::setField(const unsigned short,unsigned long),, 373)
  61. #pragma export(IRecord::setField(const void*,unsigned long,unsigned long),, 374)
  62. #pragma export(IRecord::setSize(unsigned long),, 375)
  63. #pragma export(IRecord::size() const,, 376)
  64. #pragma export(IRecord::~IRecord(),, 377)
  65.  
  66. // It's possible for the caller of the external entry points to have a
  67. // different C library environment.  Make sure the exception handler for
  68. // our library environment is registered on entry and deregistered on exit.
  69. #ifdef __OS2__
  70. #pragma handler(IRecord::asString())
  71. #pragma handler(IRecord::field(IString&,unsigned long,unsigned long,   \
  72.                     unsigned char))
  73. #pragma handler(IRecord::field(char*,unsigned long,unsigned long,unsigned char))
  74. #pragma handler(IRecord::field(char,unsigned long,unsigned char))
  75. #pragma handler(IRecord::field(double,unsigned long,unsigned char))
  76. #pragma handler(IRecord::field(float,unsigned long,unsigned char))
  77. #pragma handler(IRecord::field(long double,unsigned long,unsigned char))
  78. #pragma handler(IRecord::field(unsigned long,unsigned long,unsigned char))
  79. #pragma handler(IRecord::field(unsigned short,unsigned long,unsigned char))
  80. #pragma handler(IRecord::field(void*,unsigned long,unsigned long,unsigned char))
  81. #pragma handler(IRecord::IRecord())
  82. #pragma handler(IRecord::IRecord(const IRecord&))
  83. #pragma handler(IRecord::IRecord(const IString&))
  84. #pragma handler(IRecord::isOrdered())
  85. #pragma handler(IRecord::operator=(const IRecord&))
  86. #pragma handler(IRecord::operator=(const IString&))
  87. #pragma handler(IRecord::setField(const IString&,unsigned long,        \
  88.                     unsigned long, unsigned char))
  89. #pragma handler(IRecord::setField(const char*,unsigned long,           \
  90.                     unsigned long,unsigned char))
  91. #pragma handler(IRecord::setField(const char,unsigned long))
  92. #pragma handler(IRecord::setField(const double,unsigned long))
  93. #pragma handler(IRecord::setField(const float,unsigned long))
  94. #pragma handler(IRecord::setField(const long double,unsigned long))
  95. #pragma handler(IRecord::setField(const unsigned long,unsigned long))
  96. #pragma handler(IRecord::setField(const unsigned short,unsigned long))
  97. #pragma handler(IRecord::setField(const void*,unsigned long,unsigned long))
  98. #pragma handler(IRecord::setSize(unsigned long))
  99. #pragma handler(IRecord::size())
  100. #pragma handler(IRecord::~IRecord())
  101. #endif
  102.  
  103.  
  104. /*------------------------------------------------------------------------------
  105. | Function Name: IRecord :: IRecord
  106. |
  107. | Implementation:
  108. |   Default initialization of the record data IString is okay.
  109. |-----------------------------------------------------------------------------*/
  110. IRecord :: IRecord()
  111. {
  112.   ;
  113. }
  114.  
  115. /*------------------------------------------------------------------------------
  116. | Function Name: IRecord :: IRecord
  117. |
  118. | Implementation:
  119. |   Copy the passed in record data.
  120. |-----------------------------------------------------------------------------*/
  121. IRecord :: IRecord ( const IString & recordData ) :
  122.                    sRecordData( recordData )
  123. {
  124.   ;
  125. }
  126.  
  127. /*------------------------------------------------------------------------------
  128. | Function Name: IRecord :: IRecord
  129. |
  130. | Implementation:
  131. |   Copy the passed in record data.
  132. |-----------------------------------------------------------------------------*/
  133. IRecord :: IRecord ( const IRecord & aRecord ) :
  134.                    sRecordData( aRecord.sRecordData )
  135. {
  136.   ;
  137. }
  138.  
  139. /*------------------------------------------------------------------------------
  140. | Function Name: IRecord :: ~IRecord
  141. |
  142. | Implementation:
  143. |   The implementation IString is automatically deleted.
  144. |-----------------------------------------------------------------------------*/
  145. IRecord :: ~IRecord()
  146. {
  147.   ;
  148. }
  149.  
  150. /*------------------------------------------------------------------------------
  151. | Function Name: IRecord :: isOrdered
  152. |
  153. | Implementation:
  154. |   By default, IRecord objects are not ordered (see IOrderedRecord class).
  155. ------------------------------------------------------------------------------*/
  156. IBoolean IRecord :: isOrdered ( ) const
  157. {
  158.   return( false );
  159. }
  160.  
  161. /*------------------------------------------------------------------------------
  162. | Function Name: IRecord :: asString
  163. |
  164. | Implementation:
  165. |   Return a copy of the implementation string.
  166. ------------------------------------------------------------------------------*/
  167. IString IRecord :: asString ( ) const
  168. {
  169.   return( sRecordData );
  170. }
  171.  
  172. /*------------------------------------------------------------------------------
  173. | Function Name: IRecord :: operator =
  174. |
  175. | Implementation:
  176. |   Replace our data with the source IRecord data.
  177. ------------------------------------------------------------------------------*/
  178. IRecord & IRecord :: operator = ( const IRecord & aRecord )
  179. {
  180.   // Check for assignment to self.
  181.   if ( this != &aRecord )
  182.     sRecordData = aRecord.sRecordData;
  183.   return( *this );
  184. }
  185.  
  186. /*------------------------------------------------------------------------------
  187. | Function Name: IRecord :: operator =
  188. |
  189. | Implementation:
  190. |   Replace our data with the source IString data.
  191. ------------------------------------------------------------------------------*/
  192. IRecord & IRecord :: operator = ( const IString & aRecord )
  193. {
  194.   sRecordData = aRecord;
  195.   return( *this );
  196. }
  197.  
  198. /*------------------------------------------------------------------------------
  199. | Function Name: IRecord :: size
  200. |
  201. | Implementation:
  202. |   The IString size is the IRecord size.
  203. ------------------------------------------------------------------------------*/
  204. unsigned long IRecord :: size ( ) const
  205. {
  206.   return( sRecordData.size( ) );
  207. }
  208.  
  209. /*------------------------------------------------------------------------------
  210. | Function Name: IRecord :: setSize
  211. |
  212. | Implementation:
  213. |   Set the size of the IString to the requested size.
  214. ------------------------------------------------------------------------------*/
  215. unsigned long IRecord :: setSize ( unsigned long size )
  216. {
  217.   if ( sRecordData.size( ) > size )
  218.     sRecordData.remove( size + 1 );
  219.   else if ( sRecordData.size( ) < size )
  220.     sRecordData.insert( "", size, '\x00' );
  221.  
  222.   return( size );
  223. }
  224.  
  225. /*------------------------------------------------------------------------------
  226. | Function Name: IRecord :: field
  227. |
  228. | Implementation:
  229. |   Extract the field into the caller's buffer and return the buffer.
  230. ------------------------------------------------------------------------------*/
  231. void * IRecord :: field ( void * pBuffer,
  232.                           unsigned long recordOffset,
  233.                           unsigned long size,
  234.                           unsigned char padByte ) const
  235. {
  236.   IString theField = sRecordData.subString( recordOffset, size, padByte );
  237.   memcpy( pBuffer, (unsigned char *)theField, size );
  238.   return( pBuffer );
  239. }
  240.  
  241. /*------------------------------------------------------------------------------
  242. | Function Name: IRecord :: field
  243. |
  244. | Implementation:
  245. |   Extract the field into the caller's IString and return the IString.
  246. ------------------------------------------------------------------------------*/
  247. IString & IRecord :: field ( IString & buffer,
  248.                              unsigned long recordOffset,
  249.                              unsigned long size,
  250.                              unsigned char padByte ) const
  251. {
  252.   buffer = sRecordData.subString( recordOffset, size, padByte );
  253.   return( buffer );
  254. }
  255.  
  256. /*------------------------------------------------------------------------------
  257. | Function Name: IRecord :: field
  258. |
  259. | Implementation:
  260. |   Extract the field into the caller's null terminated character string
  261. |   and return the string.
  262. ------------------------------------------------------------------------------*/
  263. char * IRecord :: field ( char * pBuffer,
  264.                           unsigned long recordOffset,
  265.                           unsigned long size,
  266.                           unsigned char padByte ) const
  267. {
  268.   IString theField = sRecordData.subString( recordOffset, size, padByte );
  269.   memcpy( pBuffer, (unsigned char *)theField, size );
  270.   pBuffer[size] = '\x00';
  271.   return( pBuffer );
  272. }
  273.  
  274. /*------------------------------------------------------------------------------
  275. | Function Name: IRecord :: field
  276. |
  277. | Implementation:
  278. |   Extract a character and return it to the caller.
  279. ------------------------------------------------------------------------------*/
  280. char IRecord :: field ( char buffer,
  281.                         unsigned long recordOffset,
  282.                         unsigned char padByte ) const
  283. {
  284.   if ( recordOffset <= sRecordData.size() )
  285.     buffer = sRecordData[recordOffset];
  286.   else
  287.     buffer = padByte;
  288.  
  289.   return( buffer );
  290. }
  291.  
  292. /*------------------------------------------------------------------------------
  293. | Function Name: IRecord :: field
  294. |
  295. | Implementation:
  296. |   Extract a short integer and return it to the caller.
  297. ------------------------------------------------------------------------------*/
  298. unsigned short IRecord :: field ( unsigned short buffer,
  299.                                   unsigned long recordOffset,
  300.                                   unsigned char padByte ) const
  301. {
  302.   IString theField = sRecordData.subString( recordOffset,
  303.                                             sizeof(buffer),
  304.                                             padByte );
  305.   memcpy( &buffer, (unsigned char *)theField, sizeof(buffer) );
  306.   return( buffer );
  307. }
  308.  
  309. /*------------------------------------------------------------------------------
  310. | Function Name: IRecord :: field
  311. |
  312. | Implementation:
  313. |   Extract a long integer and return it to the caller.
  314. ------------------------------------------------------------------------------*/
  315. unsigned long IRecord :: field ( unsigned long buffer,
  316.                                  unsigned long recordOffset,
  317.                                  unsigned char padByte ) const
  318. {
  319.   IString theField = sRecordData.subString( recordOffset,
  320.                                             sizeof(buffer),
  321.                                             padByte );
  322.   memcpy( &buffer, (unsigned char *)theField, sizeof(buffer) );
  323.   return( buffer );
  324. }
  325.  
  326. /*------------------------------------------------------------------------------
  327. | Function Name: IRecord :: field
  328. |
  329. | Implementation:
  330. |   Extract a float and return it to the caller.
  331. ------------------------------------------------------------------------------*/
  332. float IRecord :: field ( float buffer,
  333.                          unsigned long recordOffset,
  334.                          unsigned char padByte ) const
  335. {
  336.   IString theField = sRecordData.subString( recordOffset,
  337.                                             sizeof(buffer),
  338.                                             padByte );
  339.   memcpy( &buffer, (unsigned char *)theField, sizeof(buffer) );
  340.   return( buffer );
  341. }
  342.  
  343. /*------------------------------------------------------------------------------
  344. | Function Name: IRecord :: field
  345. |
  346. | Implementation:
  347. |   Extract a double and return it to the caller.
  348. ------------------------------------------------------------------------------*/
  349. double IRecord :: field ( double buffer,
  350.                           unsigned long recordOffset,
  351.                           unsigned char padByte ) const
  352. {
  353.   IString theField = sRecordData.subString( recordOffset,
  354.                                             sizeof(buffer),
  355.                                             padByte );
  356.   memcpy( &buffer, (unsigned char *)theField, sizeof(buffer) );
  357.   return( buffer );
  358. }
  359.  
  360. /*------------------------------------------------------------------------------
  361. | Function Name: IRecord :: field
  362. |
  363. | Implementation:
  364. |   Extract a long double and return it to the caller.
  365. ------------------------------------------------------------------------------*/
  366. long double IRecord :: field ( long double buffer,
  367.                                unsigned long recordOffset,
  368.                                unsigned char padByte ) const
  369. {
  370.   IString theField = sRecordData.subString( recordOffset,
  371.                                             sizeof(buffer),
  372.                                             padByte );
  373.   memcpy( &buffer, (unsigned char *)theField, sizeof(buffer) );
  374.   return( buffer );
  375. }
  376.  
  377. /*------------------------------------------------------------------------------
  378. | Function Name: IRecord :: setField
  379. |
  380. | Implementation:
  381. |   Overlay the field with the caller's buffer and return the passed buffer.
  382. ------------------------------------------------------------------------------*/
  383. const void * IRecord :: setField ( const void * pData,
  384.                                    unsigned long recordOffset,
  385.                                    unsigned long size )
  386. {
  387.   IString theData( pData, size );
  388.   sRecordData.overlayWith( theData, recordOffset, '\x00' );
  389.   return( pData );
  390. }
  391.  
  392. /*------------------------------------------------------------------------------
  393. | Function Name: IRecord :: setField
  394. |
  395. | Implementation:
  396. |   Overlay the field with the caller's IString and return the passed IString.
  397. ------------------------------------------------------------------------------*/
  398. const IString & IRecord :: setField ( const IString & data,
  399.                                       unsigned long recordOffset,
  400.                                       unsigned long size,
  401.                                       unsigned char padByte )
  402. {
  403.   unsigned long copySize = size;
  404.   unsigned long padSize  = 0;
  405.   if ( data.size() < size )
  406.   {
  407.     copySize = data.size();
  408.     padSize  = size - data.size();
  409.   }
  410.   IString theData( (unsigned char *)data,
  411.                    copySize,
  412.                    NULL,
  413.                    padSize,
  414.                    padByte );
  415.   sRecordData.overlayWith( theData, recordOffset, '\x00' );
  416.   return( data );
  417. }
  418.  
  419. /*------------------------------------------------------------------------------
  420. | Function Name: IRecord :: setField
  421. |
  422. | Implementation:
  423. |   Overlay the field with the caller's null terminated character string and
  424. |   return the pointer to the passed character string.
  425. ------------------------------------------------------------------------------*/
  426. const char * IRecord :: setField ( const char * pData,
  427.                                    unsigned long recordOffset,
  428.                                    unsigned long size,
  429.                                    unsigned char padByte )
  430. {
  431.   unsigned long copySize = size;
  432.   unsigned long padSize  = 0;
  433.   if ( strlen( pData ) < size )
  434.   {
  435.     copySize = strlen( pData );
  436.     padSize  = size - strlen( pData );
  437.   }
  438.   IString theData( pData,
  439.                    copySize,
  440.                    NULL,
  441.                    padSize,
  442.                    padByte );
  443.   sRecordData.overlayWith( theData, recordOffset, '\x00' );
  444.   return( pData );
  445. }
  446.  
  447. /*------------------------------------------------------------------------------
  448. | Function Name: IRecord :: setField
  449. |
  450. | Implementation:
  451. |   Overlay the field with the caller's character and return passed character.
  452. ------------------------------------------------------------------------------*/
  453. const char IRecord :: setField ( const char data,
  454.                                  unsigned long recordOffset )
  455. {
  456.   IString theData( &data, sizeof(data) );
  457.   sRecordData.overlayWith( theData, recordOffset, '\x00' );
  458.   return( data );
  459. }
  460.  
  461. /*------------------------------------------------------------------------------
  462. | Function Name: IRecord :: setField
  463. |
  464. | Implementation:
  465. |   Overlay the field with the caller's short integer and return passed
  466. |   short integer.
  467. ------------------------------------------------------------------------------*/
  468. const unsigned short IRecord :: setField ( const unsigned short data,
  469.                                            unsigned long recordOffset )
  470. {
  471.   IString theData( &data, sizeof(data) );
  472.   sRecordData.overlayWith( theData, recordOffset, '\x00' );
  473.   return( data );
  474. }
  475.  
  476. /*------------------------------------------------------------------------------
  477. | Function Name: IRecord :: setField
  478. |
  479. | Implementation:
  480. |   Overlay the field with the caller's long integer and return passed
  481. |   long integer.
  482. ------------------------------------------------------------------------------*/
  483. const unsigned long IRecord :: setField ( const unsigned long data,
  484.                                           unsigned long recordOffset )
  485. {
  486.   IString theData( &data, sizeof(data) );
  487.   sRecordData.overlayWith( theData, recordOffset, '\x00' );
  488.   return( data );
  489. }
  490.  
  491. /*------------------------------------------------------------------------------
  492. | Function Name: IRecord :: setField
  493. |
  494. | Implementation:
  495. |   Overlay the field with the caller's float and return passed float.
  496. ------------------------------------------------------------------------------*/
  497. const float IRecord :: setField ( const float data,
  498.                                   unsigned long recordOffset )
  499. {
  500.   IString theData( &data, sizeof(data) );
  501.   sRecordData.overlayWith( theData, recordOffset, '\x00' );
  502.   return( data );
  503. }
  504.  
  505. /*------------------------------------------------------------------------------
  506. | Function Name: IRecord :: setField
  507. |
  508. | Implementation:
  509. |   Overlay the field with the caller's double and return passed double.
  510. ------------------------------------------------------------------------------*/
  511. const double IRecord :: setField ( const double data,
  512.                                    unsigned long recordOffset )
  513. {
  514.   IString theData( &data, sizeof(data) );
  515.   sRecordData.overlayWith( theData, recordOffset, '\x00' );
  516.   return( data );
  517. }
  518.  
  519. /*------------------------------------------------------------------------------
  520. | Function Name: IRecord :: setField
  521. |
  522. | Implementation:
  523. |   Overlay the field with the caller's long double and return passed
  524. |   long double.
  525. ------------------------------------------------------------------------------*/
  526. const long double IRecord :: setField ( const long double data,
  527.                                         unsigned long recordOffset )
  528. {
  529.   IString theData( &data, sizeof(data) );
  530.   sRecordData.overlayWith( theData, recordOffset, '\x00' );
  531.   return( data );
  532. }
  533.