Next: 3 Extending the program Up: GNUstep Distributed Objects Previous: 1 What are Distributed

2 A basic program to show files

We start our tutorial with a very basic non-DO program - a command line tool which accepts a filename as an argument, reads this file from disk, and prints it out to stdout. For example, typing

Example Client.m
should display the contents of the file Client.m. Once we have this basic example in place, in the next sections we'll use DO to extend its capabilities.

Our basic tool is the following one:

#include <Foundation/Foundation.h>

/* This object does the job of fetching a file from 
   the hard disk */

@interface FileReader : NSObject
- (NSString *)getFile: (NSString *)fileName;
@end

@implementation FileReader
- (NSString *)getFile: (NSString *)fileName
{
  return [NSString stringWithContentsOfFile: fileName];
}
@end


int 
main (void)
{
  NSAutoreleasePool *pool;
  NSArray *args;
  int count;
  FileReader *reader;
  NSString *filename;
  NSString *file;

  pool = [NSAutoreleasePool new];

  /* Create our FileReader object */
  reader = [FileReader new];

  /* Get program arguments */
  args = [[NSProcessInfo processInfo] arguments];

  /* the first string in args is the program name; 
     get the second one if any */
  if ([args count] == 1)
    {
      NSLog (@"Error: you should specify a filename");
      exit (1);
    }
  
  filename = [args objectAtIndex: 1];

  /* Ask the reader object to get the file */
  file = [reader getFile: filename];

  /* If the reader object could get the file, show it */
  if (file != nil)
    {
      printf ("%s\n", [file lossyCString]);
    }
  else
    {
      NSLog (@"Error: could not read file `%@'", filename);
      exit (1);
    }

  return 0;
}
To compile the program, you need a GNUmakefile, such as the following one:
include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = Example
Example_OBJC_FILES = example.m

include $(GNUSTEP_MAKEFILES)/tool.make


Next: 3 Extending the program Up: GNUstep Distributed Objects Previous: 1 What are Distributed
Nicola 2002-02-24