home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / EODEV.Z / FlatFileColumn.m < prev    next >
Encoding:
Text File  |  1996-08-23  |  3.3 KB  |  149 lines

  1. /*
  2.         Copyright (c) 1996, NeXT Software, Inc.
  3.         All rights reserved.
  4.  
  5.         You may freely copy, distribute and reuse the code in this example.
  6.         NeXT disclaims any warranty of any kind, expressed or implied,
  7.         as to its fitness for any particular use.
  8. */
  9. #import "FlatFileColumn.h"
  10. #import <EOAccess/EOAccess.h>
  11.  
  12. #define DataBegin '<'
  13. #define DataEnd   '>'
  14.  
  15. @implementation FlatFileColumn
  16.  
  17. + (Class)columnClassForAttribute:(EOAttribute *)attr
  18. {
  19.     switch ([attr adaptorValueType]) {
  20.         case EOAdaptorNumberType : return [FlatFileNumberColumn class];
  21.         case EOAdaptorCharactersType : return [FlatFileStringColumn class];
  22.         case EOAdaptorBytesType : return [FlatFileDataColumn class];
  23.         case EOAdaptorDateType : return [FlatFileDateColumn class];
  24.     }
  25.  
  26.     return nil;
  27. }
  28.  
  29. + columnForAttribute:(EOAttribute *)attr
  30. {
  31.     return [[[[self columnClassForAttribute:attr] alloc] initWithAttribute:attr] autorelease];
  32. }
  33.  
  34. - initWithAttribute:(EOAttribute *)attr
  35. {
  36.     [super init];
  37.     attribute = [attr retain];
  38.  
  39.     return self;
  40. }
  41.  
  42. - (void)dealloc
  43. {
  44.     [attribute release];
  45.     [super dealloc];
  46. }
  47.  
  48. - (void)bindAtIndex:(int)index
  49. {
  50.     bindingIndex = index;
  51. }
  52.  
  53. - (int)bindingIndex
  54. {
  55.     return bindingIndex;
  56. }
  57.  
  58. - (id)newValueForBytes:(void *)bytes length:(int)length
  59. {
  60.     return nil;
  61. }
  62.  
  63. @end
  64.  
  65. @implementation FlatFileNumberColumn
  66.  
  67. - initWithAttribute:(EOAttribute *)attr
  68. {
  69.     [super initWithAttribute:attr];
  70.     useIntValue = [[attribute valueType] isEqualToString:@"i"];
  71.  
  72.     return self;
  73. }
  74.  
  75. - (id)newValueForBytes:(void *)bytes length:(int)length
  76. {
  77.     NSString *helper = [[NSString alloc] initWithCString:(char *)bytes length:length];
  78.     NSDecimalNumber *value = [NSDecimalNumber decimalNumberWithString:helper];
  79.  
  80.     [helper release];
  81.     return value;
  82. }
  83.  
  84. @end
  85.  
  86. @implementation FlatFileStringColumn
  87.  
  88. - initWithAttribute:(EOAttribute *)attr
  89. {
  90.     [super initWithAttribute:attr];
  91.     encoding = [NSString defaultCStringEncoding];
  92.  
  93.     return self;
  94. }
  95.  
  96. - (id)newValueForBytes:(void *)bytes length:(int)length
  97. {
  98.     return [attribute newValueForBytes:bytes length:length encoding:encoding];
  99. }
  100.  
  101. @end
  102.  
  103. @implementation FlatFileDataColumn
  104.  
  105. - (id)newValueForBytes:(void *)bytes length:(int)length
  106. {
  107.     char *valueBytes = (char *)malloc(length * sizeof(char)), *currentValueByte = valueBytes, byte = 0, nibble;
  108.     int i = 0, valueLength = 0;
  109.     id value;
  110.     BOOL haveFirstNibble = NO;
  111.     
  112.     if (*(char *)bytes == DataBegin)
  113.         bytes++;
  114.  
  115.     while ((i++ < length) && ((nibble = *(char *)bytes++) != DataEnd)) {
  116.         if ((nibble >= '0') && (nibble <= '9'))
  117.             nibble -= '0';
  118.         else if ((nibble >= 'a') && (nibble <= 'z'))
  119.             nibble -= ('a' - 10);
  120.         else
  121.             continue;
  122.  
  123.         if (haveFirstNibble) {
  124.             byte += nibble;
  125.             *currentValueByte++ = byte;
  126.             valueLength++;
  127.         } else
  128.             byte = nibble << 4;
  129.  
  130.         haveFirstNibble = !haveFirstNibble;
  131.     }
  132.  
  133.     value = [attribute newValueForBytes:valueBytes length:valueLength];
  134.     free(valueBytes);
  135.     return value;
  136. }
  137.  
  138. @end
  139.  
  140. @implementation FlatFileDateColumn
  141.  
  142. - (id)newValueForBytes:(void *)bytes length:(int)length
  143. {
  144.     NSString *helper = [NSString stringWithCString:bytes length:length];
  145.     return [NSCalendarDate dateWithString:helper calendarFormat:FlatFileCalendarFormat];
  146. }
  147.  
  148. @end
  149.