// file: $isip/doc/examples/class/system/system_example_00/example.cc
// version: $Id: example.cc 5936 2000-12-17 16:43:26Z hamaker $
//

// isip include files
//
#include <File.h>
#include <Console.h>
#include <String.h>

// main program starts here:
//   this program emulates the tool 'cat -n' by printing a file line by
//   line and prepending the line number to the output.
//
int main() {
  
  // declare local variables for the File object and the filename
  //
  File input;
  String temp_file(L"./input.text");

  // return an error if the file cannot be opened. note that if the file
  // is not found, the File class will poll for the file. the number of
  // times to poll and the time between attempts can be adjusted through
  // the File::setOpenRetry method
  //
  if (!input.open(temp_file, File::READ_ONLY)) {
    Error::handle(input.name(), L"open", Error::IO, __FILE__, __LINE__);
  }
  
  // declare strings to be used for reading and writing
  //
  String line_num;
  String buf;

  // initialize the line count
  //
  long line_count = 0;

  // read in a line of text with the File::get method and increment the
  // line count
  //
  while (input.get(buf)) {
    line_count = line_count + 1;
    
    // build the output string first assigning a string from an integral
    // type and then inserting the line number information in front of
    // the buffer read from the file.
    //
    line_num.assign((long)line_count);
    line_num.concat(L"   ");
    buf.insert(line_num, 0);
    
    // print the string to the console
    //
    Console::put(buf);
  }
  
  // close the file
  //
  input.close();
  
  // exit gracefully
  //
  Integral::exit();
}
