// Hello-world program in Objective-C
// (to be compiled with GCC and GNUstep)
// gcc -I/usr/include/GNUstep -fconstant-string-class=NSConstantString objc-hello.m -lobjc -lgnustep-base

// This is a little more extended than a normal hello-world because I
// wanted to exercise the Objective-C class mechanism, which is the
// delta from C, the alloc/init/autorelease methods from NeXTStEp,
// Objective-C strings, and the sneaky load method.

#import <unistd.h>
#import <stdio.h>
#import <Foundation/Foundation.h>

@interface Greeting : NSObject
- (void)to: (char*)greetee;
- (void)greetOnStream: (FILE*)output;
+ (void)load;
@end

@interface Greeting ()
// @property on this would require us to implement greetee and
// setGreetee methods, which would be transparently invoked for
// self.greetee access:
char *greetee;
@end

@implementation Greeting
- (void)to: (char*)newGreetee
{
  greetee = newGreetee;
}

- (void)greetOnStream: (FILE*)output
{
  NSString *g = [NSString stringWithCString: greetee encoding: NSUTF8StringEncoding];
  NSString *s = [NSString stringWithFormat: @"hello, %@\n", g];
  fprintf(output, "%s", [s cString]);
}

+ (void)load
{
  write(1, "hi\n", 3);
}
@end

int
main(int argc, const char **argv)
{
  printf("ok\n");
  CREATE_AUTORELEASE_POOL(pool);
  Greeting *g = [[[Greeting alloc] init] autorelease];
  [g to: "world"];
  [g greetOnStream: stdout];
  RELEASE(pool);
  return 0;
}
