Re: Objective-C BufferedReader
Re: Objective-C BufferedReader
- Subject: Re: Objective-C BufferedReader
- From: Jason Jobe <email@hidden>
- Date: Sat, 12 Feb 2005 18:16:39 -0500
The advantage to NSFileHandle is that the NSData will enlarge as
required.
There is also
char *
fgetln(FILE *stream, size_t *len);
DESCRIPTION
The fgetln() function returns a pointer to the next line from the
stream
referenced by stream. This line is not a C string as it does not
end
with a terminating NUL character. The length of the line,
including the
final newline, is stored in the memory location to which len
points.
(Note, however, that if the line is the last in a file that does
not end
in a newline, the returned text will not contain a newline.)
but I think the real issue is that the fgets should be in the loop.
Something like this (NOT tested)
- (NSString *)readLineFromStdin
{
NSMutableString *ret = [NSMutableString
stringWithCapacity:BUFFER_LENGTH];
char buf[BUFFER_LENGTH + 1];
char *chars;
while (chars = fgets(buf, BUFFER_LENGTH, stdin) {
{
char *endl = strchr(chars, '\n');
if (endl != NULL)
{
[ret appendString: [NSString stringWithCString:buf length:(endl -
buf)]];
return ret;
} else {
[ret appendString: [NSString stringWithCString:buf
length:BUFFER_LENGTH]];
}
}
return ret;
}
On Feb 12, 2005, at 6:01 PM, Hamish Allan wrote:
Could you please elaborate? java.io.BufferedReader provides a
readLine() function which returns string input newline by newline, but
I don't see the equivalent in NSFileHandle.
Thanks,
Hamish
On Feb 12, 2005, at 22:56, Jason Jobe wrote:
What's wrong with NSFileHandle?
file:///Developer/ADC Reference Library/documentation/Cocoa/
Reference/Foundation/ObjC_classic/Classes/NSFileHandle.html
On Feb 12, 2005, at 5:39 PM, Hamish Allan wrote:
Hi,
I have as yet been unable to find anything written in Objective-C
that provides the functionality of Java's java.io.BufferedReader.
1) Does such a library object exist, and if so, where?
Assuming not, I thought I might try to implement it naively as below
(readLine hard-coded to stdin for pedagogical purposes). It works
fine for strings that are less than BUFFER_LENGTH characters long,
but if they are over, fgets() never returns. I know this is not
really a Cocoa question, but since I was asking (1) above, I thought
I'd also ask here:
2) What's wrong with the following code?
- (NSString *)readLineFromStdin
{
NSMutableString *ret = [NSMutableString
stringWithCapacity:BUFFER_LENGTH];
char buf[BUFFER_LENGTH + 1];
char *chars = fgets(buf, BUFFER_LENGTH, stdin);
if (chars != NULL)
{
char *endl = strchr(chars, '\n');
while (endl == NULL)
{
[ret appendString: [NSString stringWithCString:buf
length:BUFFER_LENGTH]];
endl = strchr(chars, '\n');
}
[ret appendString: [NSString stringWithCString:buf length:(endl -
buf)]];
}
return ret;
}
Many thanks,
Hamish
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
- jason
email@hidden
- jason
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden