import SimpleInput;
import SimpleOutput;

/**
 * Reads in an array of strings (one per line) from standard
 * input.  Stops when it runs out of strings.  Created as part
 * of the answer key for homework 7 of Grinnell's CSC152 99S.
 *
 * Although this is intended as a utility class, it also includes
 * a main method so that it can be used to test itself.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of March 1999
 */
public class StringArrayInput {

  // +--------+--------------------------------------------------
  // | Fields |
  // +--------+

  /** The array of strings we've read so far. */
  public String[] strings;

  /** The number of things we've read. */
  int thingsRead;


  // +--------------+--------------------------------------------
  // | Constructors |
  // +--------------+

  /**
   * Build a new thing that can read strings.
   */
  public StringArrayInput() {
    // Start with some reasonable size.
    strings = new String[100];
    // Hey!  We haven't read anything.
    thingsRead = 0;
  } // StringArrayInput()


  // +----------------+------------------------------------------
  // | Public Methods |
  // +----------------+

  /**
   * Read in an array of strings.  The array is terminated
   * by the end of input.
   */
  public String[] readStrings(SimpleInput in) {
    String str;
    // Keep reading until you hit the null string.
    while ((str = in.readString()) != null) {
      store(str);
    } // while
    // Build and return the appropriate subarray.
    return stringsRead();
  } // readStrings(SimpleInput)

  /**
   * Read in an array of strings until you hit a specified
   * string.  The array is terminated by the given string
   * or by end of input.
   */
  public String[] readStrings(SimpleInput in, String terminator) {
    String str;
    // Keep reading until you hit the null string.
    while ( ((str = in.readString()) != null) &&
            (!terminator.equals(str)) ) {
      store(str);
    } // while
    // Build and return the appropriate subarray.
    return stringsRead();
  } // readStrings(SimpleInput,String)


  // +----------------+------------------------------------------
  // | Helper Methods |
  // +----------------+

  /**
   * Store a string in the array, increasing the size if
   * necessary.
   */
  protected void store(String str) {
    // Make sure the array is big enough.
    if (thingsRead >= strings.length) {
      // Remember the old array.
      String[] tmp = strings;
      // Create a new one.
      strings = new String[2*tmp.length];
      // Copy over the old elements.
      for (int i = 0; i < strings.length; ++i) {
        strings[i] = tmp[i];
      }
    }
    // Fill in the new thing
    strings[thingsRead] = str;
    // And update our count
    ++thingsRead;
  } // store(String)

  /**
   * Get a subarray of the strings read.
   */
  protected String[] stringsRead() {
    // Create the new array.
    String[] stuff = new String[thingsRead];
    // Fill it in.
    for (int i = 0; i < thingsRead; ++i) {
      stuff[i] = strings[i];
    }
    // Return it.
    return stuff;
  } // stringsRead()

  // +------+----------------------------------------------------
  // | Main |
  // +------+

  public static void main(String[] args) {
    SimpleOutput out = new SimpleOutput();
    SimpleInput in = new SimpleInput();
    StringArrayInput sai = new StringArrayInput();
    String[] strings;
  
    // Get the list of strings (either using a specified terminator
    // or not.
    if (args.length > 0)
      strings = sai.readStrings(in,args[0]);
    else 
      strings = sai.readStrings(in);
    
    // Print out the result.
    for (int i = 0; i < strings.length; ++i) {
      out.println(i + ": " + strings[i]);
    }
  } // main(String[])

} // StringArrayInput

