Re: C and objective C together
Re: C and objective C together
- Subject: Re: C and objective C together
- From: Ondra Cada <email@hidden>
- Date: Tue, 2 Apr 2002 22:23:04 +0200
On Tuesday, April 2, 2002, at 09:15 , Jason Reece wrote:
I have a C program that I'm porting to Cocoa. As, the bulk of the
program is just number crunching I'm leaving it as C. However, the
program produces text messages while it is running via printf's. How do
I 'catch' these and display them on a window? I need to do them while
the thing is running ideally. I can easily replace the printf's with
some other function, but how do I get the data into the objective-C
windowing bits? Thanks
You want to have an application with a window which contains a text view
and an instantiated controller, which has a (say) textView outlet bound to
the text view. So far you know how to? (If not, see the examples and
tutorials: it's really darn easy).
Now, the controller would publish "its own printf-like method". There are
a few different ways; I would prefer this one myself:
// Controller.h
#import ....
@interface Controller:NSObject {
IBOutlet NSTextView *textView;
}
@end
void myPrintf(const char *fmt,...);
// EOF
You would import this header wherever the printf is needed. The
implementation would look like this:
// Controller.m
#import "Controller.h"
static Controller *_theCurrentController;
@implementation Controller
-(void)awakeFromNib {
_theCurrentController=self;
}
@end
void myPrintf(const char *fmt,...) {
NSString *s;
va_list al;
va_start(al,fmt);
// you can use the standard vsprintf too, but I'd prefer this in Cocoa,
unless speed is crucial
s=[[[NSString alloc] initWithFormat:[NSString stringWithCString:fmt]
arguments:al] autorelease];
va_end(al);
// now the displaying stuff
[_theCurrentController
replaceCharactersInRange:NSMakeRange([[_theCurrentController string]
length],0) withString:s];
// you might not want this, but I guess you do
[_theCurrentController
scrollRangeToVisible:NSMakeRange([[_theCurrentController string] length],0)
];
}
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
2K Development: email@hidden
http://www.2kdevelopment.cz
private email@hidden
http://www.ocs.cz/oc
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.