Files
gitx/NSFileHandleExt.m
T
André Berg 6c04aea44b Bug fix: Change improper usage of dot notation.
Calling methods which are not properties through use of dot notation is a
no-no in Apple's Objective-C 2.0 documentation. According to Apple it might
work but the compiler will not warn about any dangerous use cases.

The prominent example from the docs of how not to do it is "someObject.retain".

Here retain is a method and not a property so proper use is "[someObject retain]".
Unfortunately, often it is not clear if something in the API is merely an accessor
or a method which acts like an accessor but does more than the name might imply.

In this case, we can see this in PBEasyPipe where we have method calls like
"NSTask.standardOutput = ...". Even though they may look correct this can be
dangerous for obvious reasons. I assume hat this could also play a role in the appearance
of the "bad file descriptor" messages.
2009-10-20 04:03:05 +02:00

74 lines
2.1 KiB
Objective-C

/*
* Extension for NSFileHandle to make it capable of easy network programming
*
* Version 1.0, get the newest from http://michael.stapelberg.de/NSFileHandleExt.php
*
* Copyright 2007 Michael Stapelberg
*
* Distributed under BSD-License, see http://michael.stapelberg.de/BSD.php
*
*/
#define CONN_TIMEOUT 5
#define BUFFER_SIZE 256
#import <objc/objc-auto.h> /* for objc_collect */
@implementation NSFileHandle(NSFileHandleExt)
-(NSString*)readLine {
// If the socket is closed, return an empty string
if ([self fileDescriptor] <= 0)
return @"";
int fd = [self fileDescriptor];
// Allocate BUFFER_SIZE bytes to store the line
int bufferSize = BUFFER_SIZE;
char *buffer = (char*)malloc(bufferSize + 1);
if (buffer == NULL)
[[NSException exceptionWithName:@"No memory left" reason:@"No more memory for allocating buffer" userInfo:nil] raise];
int bytesReceived = 0, n = 1;
while (n > 0) {
n = read(fd, buffer + bytesReceived++, 1);
if (n < 0)
[[NSException exceptionWithName:@"Socket error" reason:@"Remote host closed connection" userInfo:nil] raise];
if (bytesReceived >= bufferSize) {
// Make buffer bigger
bufferSize += BUFFER_SIZE;
buffer = (char*)realloc(buffer, bufferSize + 1);
if (buffer == NULL)
[[NSException exceptionWithName:@"No memory left" reason:@"No more memory for allocating buffer" userInfo:nil] raise];
}
switch (*(buffer + bytesReceived - 1)) {
case '\n':
buffer[bytesReceived-1] = '\0';
NSString* s = [NSString stringWithCString: buffer encoding: NSUTF8StringEncoding];
if ([s length] == 0)
s = [NSString stringWithCString: buffer encoding: NSISOLatin1StringEncoding];
return s;
case '\r':
bytesReceived--;
}
}
buffer[bytesReceived-1] = '\0';
NSString *retVal = [NSString stringWithCString: buffer encoding: NSUTF8StringEncoding];
if ([retVal length] == 0)
retVal = [NSString stringWithCString: buffer encoding: NSISOLatin1StringEncoding];
free(buffer);
[[NSGarbageCollector defaultCollector] collectExhaustively];
return retVal;
}
@end