/*
 * Josh Hursey
 * CS127
 *
 * Collection of commonly used functions for CS127.
 *
 *    Update History
 * ----------------------------------
 * Josh Hursey    Oct. 28, 2003
 *    -- Created
 * Josh Hursey    Nov. 11, 2003
 *    -- Added some common libraries
 * Josh Hursey    Nov. 14, 2003
 *    -- Added itoa and ftoa functionality
 *
 */
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <string>
#include <stdio.h>  // sprintf
#include <fstream>
using namespace std;
/*****************************************************
 *                Function Declarations
 *****************************************************/
//  File Operations
void rewind(ifstream&);

//   String Operations
bool strContain(string, string);
void itoa(int, string&);
string itoa(int);
void itoa(int, char* );
void ftoa(double, string&);
string ftoa(double);
void ftoa(double, char* );

// General Array Size
const int ARRAYSIZE = 256;
/*****************************************************
 *                Function Definitions
 *****************************************************/
/*
 * Move the input stream passed to the start of the file
 *
 * Input: Input Stream
 * Output: Input Stream Repositioned to the start of the file.
 */
void rewind(ifstream& in){
  in.clear();
  in.seekg(0,ios::beg);
}

/*
 * Check to see if the first string contains the latter string.
 * 
 * Input: full    = Complete string
 *        section = the string that we hope to find in full
 * Output: true if full contains section, 
 *         false otherwise
 */
bool strContain(string full, string section){
  unsigned int i = 0, j = 0;
  bool matching = false;

  for(i = 0; i < full.size(); ++i){
    if(full[i] == section[j]){
      if(matching == false) 
        matching = true;
      if(section.size()-1 == j)
        return true;
      ++j;
    }
    else{
      if(matching == true){
        matching = false;
        j = 0;
      }
    }
  }
  return false;
}

/*
 * Convert an integer to a string
 *
 * Input: Integer, and a pointer to a string
 * Output: Set the string value to the string equivalent
 *         of the Integer.
 */
void itoa(int num, string& eq){
  char temp[ARRAYSIZE];
  sprintf(temp, "%d" , num);
  eq = temp;
}

/*
 * Overloaded itoa function for use with C Strings
 */
void itoa(int num, char *eq){
  char temp[ARRAYSIZE];
  sprintf(temp, "%d" , num);
  strcpy(eq,temp);
}

/*
 * Overloaded itoa function
 */
string itoa(int num){
  string temp;
  itoa(num, temp);
  return temp;
}
/*
 * Convert a floating point number to a string
 *
 * Input: Floating Point Number, and a pointer to a string
 * Output: Set the string value to the string equivalent
 *         of the Floating Point Number.
 */
void ftoa(double num, string& eq){
  char temp[ARRAYSIZE];
  sprintf(temp, "%.6f" , num);
  eq = temp;
}

/*
 * Overloaded itoa function for use with C Strings
 */
void ftoa(double num, char *eq){
  char temp[ARRAYSIZE];
  sprintf(temp, "%.6f" , num);
  strcpy(eq,temp);
}

/*
 * Overloaded ftoa function
 */
string ftoa(double num){
  string temp;
  ftoa(num, temp);
  return temp;
}
