[BACK]Return to MyDecoder.m CVS log [TXT][DIR] Up to [local] / OpenXM / src / cfep

Annotation of OpenXM/src/cfep/MyDecoder.m, Revision 1.1.1.1

1.1       takayama    1: //
                      2: //  MyDecoder.m
                      3: //  cfep
                      4: //
                      5: //  Created by Nobuki Takayama on 06/01/29.
                      6: //  Copyright 2006 OpenXM.org. All rights reserved.
                      7: //
                      8:
                      9: #import "MyDecoder.h"
                     10:
                     11:
                     12: @implementation MyDecoder
                     13: -(void) dealloc {
                     14:   if (data) {[data autorelease];  data=nil;}
                     15:   [super dealloc];
                     16: }
                     17:
                     18: -(NSString *)myDecoder: (int) c from: (id) myDocument {
                     19:   Byte b[1];
                     20:   NSString *s;
                     21:   // NSLog(@"state=%d\n",state);
                     22:   @synchronized(self) {
                     23:     switch(state) {
                     24:        case 0:
                     25:          if (c == OX_PACKET_START)  {state = 1; channel=0;}
                     26:          break;
                     27:     case 1:
                     28:          if (c == OX_DATA_CMO_DATA_START) {
                     29:            size = 0; state = 2;
                     30:          }else if ((c < '0') || (c > '9')) {
                     31:            [self reportError: @"myDecoder: invalid channel.\n" to: myDocument];
                     32:                [self reset]; return nil;
                     33:          } else { channel = channel*10+c-'0'; }
                     34:          break;
                     35:        case 2:
                     36:          if (c == OX_DATA_CMO_DATA_END_SIZE) {
                     37:            state = 3;
                     38:                if (size > MYDECODER_MAX) {
                     39:                  [self reportError: @"myDecoder: too big size.\n" to: myDocument];
                     40:                  [self reset];
                     41:                  return nil;
                     42:                }
                     43:                data = [NSMutableData dataWithCapacity: (size+1)]; [data retain];
                     44:          }else if ((c < '0') || (c > '9')) {
                     45:            [self reportError: @"myDecoder: invalid size.\n" to: myDocument];
                     46:                [self reset]; return nil;
                     47:          } else { size = size*10+c-'0'; }
                     48:          break;
                     49:        case 3:
                     50:          if (size <= 0) {
                     51:            state = 4;
                     52:                if (c != OX_DATA_CMO_DATA_END) {
                     53:                  [self reportError: @"myDecoder: unexpected end of ox_data_cmo_data.\n" to: myDocument];
                     54:                  [self reset];
                     55:                  return nil;
                     56:                }
                     57:          }     else {
                     58:         b[0] = c;
                     59:            [data appendBytes: b length: 1];
                     60:            size--;
                     61:          }
                     62:          break;
                     63:        case 4:
                     64:          if (c != OX_PACKET_END) {
                     65:                  [self reportError: @"myDecoder: unexpected end of ox_packet.\n" to: myDocument];
                     66:                  [self reset];
                     67:                  return nil;
                     68:          }else{
                     69:            b[0] = 0; [data appendBytes: b length: 1];
                     70:            s = [NSString stringWithCString: [data bytes] encoding: NSUTF8StringEncoding];
                     71:                [self reset];
                     72:                return s;
                     73:          }
                     74:          break;
                     75:        default:
                     76:          [self reportError: @"myDecoder: unknown state.\n" to: myDocument]; [self reset];
                     77:          break;
                     78:        }
                     79:   }
                     80:   return nil;
                     81: }
                     82:
                     83: -(int) getChannel {
                     84:   return channel;
                     85: }
                     86:
                     87: -(void) reset {
                     88:   @synchronized(self) {
                     89:        state = 0;
                     90:        // NSLog(@"data retainCount=%d\n",[data retainCount]);   // --> 2.
                     91:        if (data) { [data autorelease]; data=nil;}
                     92:   }
                     93: }
                     94:
                     95: -(void) reportError: (NSString *) msg to: (id) myDocument {
                     96:    [myDocument outputErrorString: msg];
                     97:    NSLog(@"data=%@",data);
                     98: }
                     99:
                    100:
                    101: @end

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>