// oneBigArrayDemo2.cpp

#include <iostream>
#include "textUtility2.h"

const int MAX_INT_LENGTH = 11;   // maximum number of characters
                                 // in string representing int,
                                 // including minus sign.

bool parseIntegers(char integerTexts[][MAX_INT_LENGTH + 1],
                   int numbers[],
                   int length);


/*
 * int main(int argc, char* argv[])
 *
 * Echoes any sequence of integers entered as
 * command-line arguments.
 *
 * Command-line arguments:
 *    argv[0] - filename of this program's executable file.
 *    All subsequent argv[i] - any integers.
 *
 * Global variable:
 *    cout - an ostream object for output to the terminal.
 *           cout is an external variable, declared
 *           in library header file <iostream>.
 */
int main(int argc, char* argv[])
{
   if ( argc < 2 )  {
      cout << "Enter any sequence of integers as "
                << "command-line arguments." << endl;
      cout << "This program will echo them." << endl;
      return 0;
   }  // if

   // Create one big array to hold copy of
   // command-line argument array (an array of pointers),
   // INCLUDING argv[0] (filename of executable file).
   char argvCopy[argc][MAX_INT_LENGTH + 1];

   // Copy command-line argument array to one-big array:
   for ( int i = 0; i < argc; i++ )  {
      int j = 0;
      while ( argv[i][j] != '\0' && j < MAX_INT_LENGTH )  {
         argvCopy[i][j] = argv[i][j];
         j++;
      }  // while

      if ( argv[i][j] == '\0' )
         argvCopy[i][j] = '\0';
      else  {   // array length exceeded
         cout << argv[i] << " too long." << endl;
         return 1;
      }  // else
   }  // for i

   // Number of represented integers:
   const int NUMBER_OF_NUMBERS = argc - 1;

   // Array for represented integers:
   int inputNumbers[NUMBER_OF_NUMBERS];

   // Generate the represented integers:
   if ( ! parseIntegers(argvCopy + 1,
                        inputNumbers,
                        NUMBER_OF_NUMBERS) )
   {
      cout << "You must enter integers only." << endl;
      return 1;
   }  // if

   // Echo the represented integers:
   cout << "You entered: ";
   for ( int i = 0; i < NUMBER_OF_NUMBERS; i++ )
      cout << " " << inputNumbers[i];
   cout << endl;

   return 0;
}  // function main


/*
 * bool parseIntegers(char integerTexts[][MAX_INT_LENGTH + 1],
 *                    int numbers[],
 *                    int length)
 *
 * Generates the int values represented by
 * a sequence of C-strings which represent
 * integers.
 *
 * Parameters:
 *    integerTexts - an array of C-strings
 *           representing integers
 *       Precondition:  Each element should
 *           represent an integer.
 *       Postcondition:  unchanged.
 *    numbers - an array of the integers
 *          represented by integerTexts.
 *       Precondition:  none
 *       Postconditions:  Each element of
 *          numbers contains the integer
 *          represented by the corresponding
 *          element of integerTexts, if that
 *          element of integerTexts indeed
 *          represents an integer.  Garbage
 *          otherwise.
 *    length - the length of both arrays
 *       integerTexts and numbers.
 *
 * Returns:  true if all the elements of
 *       integerTexts represent integers,
 *       false otherwise.
 */
bool parseIntegers(char integerTexts[][MAX_INT_LENGTH + 1],
                   int numbers[],
                   int length)
{
   for ( int i = 0; i < length; i++ )  {
      if ( ! parseForbinInt(integerTexts[i], numbers[i]) )
         return false;
   }  // for i

   return true;
}  // function parseIntegers