#include "functions.h"
#include <iostream>
using namespace std;

/*!
 * \brief Make the string safe for use in SQL statements.
 *  This involves escaping single quotes, double quotes, backslashes,
 *  and optionally, percentage symbols.  Percentage symbols are used
 *  as wildcards sometimes and so when using the string as part of the
 *  LIKE phrase of a select statement, should be escaped.
 * \arg const string in The input string to make safe.
 * \arg const bool escapePercentage Should we escape the percentage symbol?
 * \return The string made safe for SQL statements.
 */
const string sqlSafe(const string &in, const bool escapePercentage)
{
    string retval;
    for (string::const_iterator it = in.begin(); it != in.end(); it++) {
        if (*it == '\"') {
            retval += "\\\"";
        } else if (*it == '\'') {
            retval += "\\'";
        } else if (*it == '\\') {
            retval += "\\\\";
        } else if (escapePercentage && (*it == '%') ) {
            retval += "\\%";
        } else {
            retval += *it;
        }
    }
    // POSTCONDITION(retval.length() >= in.length());
    return retval;
}

void debug(std::string& in)
{
  std::cout << in << endl;
}
void error(const std::string& in)
{
  std::cout << "ERROR: " << in << endl;
}
