1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/***************************************************************************
LineObject.m
-------------------
begin : Thu May 30 02:19:30 UTC 2002
copyright : (C) 2003 by Andy Ruder
email : aeruder@yahoo.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/**
* <title>LineObject class reference</title>
* <author name="Andrew Ruder">
* <email address="aeruder@ksu.edu" />
* <url url="http://aeruder.gnustep.us/index.html" />
* </author>
* <version>Revision 1</version>
* <date>November 8, 2003</date>
* <copy>Andrew Ruder</copy>
*/
#import "LineObject.h"
#import <Foundation/NSData.h>
#import <Foundation/NSString.h>
#include <string.h>
static inline NSData *chomp_line(NSMutableData *data)
{
char *memory = [data mutableBytes];
char *memoryEnd = memory + [data length];
char *lineEndWithControls;
char *lineEnd;
int tempLength;
id lineData;
lineEndWithControls = lineEnd =
memchr(memory, '\n', memoryEnd - memory);
if (!lineEnd)
{
return nil;
}
while (((*lineEnd == '\n') || (*lineEnd == '\r'))
&& (lineEnd >= memory))
{
lineEnd--;
}
lineData = [NSData dataWithBytes: memory length: lineEnd - memory + 1];
tempLength = memoryEnd - lineEndWithControls - 1;
memmove(memory, lineEndWithControls + 1,
tempLength);
[data setLength: tempLength];
return lineData;
}
/**
* LineObject is used for line-buffered connections (end in \r\n or just \n).
* To use, simply override lineReceived: in a subclass of LineObject. By
* default, LineObject does absolutely nothing with lineReceived except throw
* the line away. Use line object if you simply want line-buffered input.
* This can be used on IRC, telnet, etc.
*/
@implementation LineObject
/**
* Cleans up the instance variables and closes/releases the transport.
*/
- (void)connectionLost
{
[transport close];
DESTROY(transport);
RELEASE(_readData);
}
/**
* Initializes data and retains <var>aTransport</var>
* <var>aTransport</var> should conform to the [(NetTransport)]
* protocol.
*/
- connectionEstablished: (id <NetTransport>)aTransport
{
transport = RETAIN(aTransport);
[[NetApplication sharedInstance] connectObject: self];
_readData = [NSMutableData new];
return self;
}
/**
* Adds the data to a buffer. Then calls -lineReceived: for all
* full lines currently in the buffer. Don't override this, override
* -lineReceived:.
*/
- dataReceived: (NSData *)newData
{
id newLine;
[_readData appendData: newData];
while ((newLine = chomp_line(_readData))) [self lineReceived: newLine];
return self;
}
/**
* Returns the transport
*/
- (id <NetTransport>)transport
{
return transport;
}
/**
* <override-subclass />
* <var>aLine</var> contains a full line of text (without the ending newline)
*/
- lineReceived: (NSData *)aLine
{
return self;
}
@end
|