summaryrefslogtreecommitdiffstats
path: root/labb6/lib/StanfordCPPLib
diff options
context:
space:
mode:
Diffstat (limited to 'labb6/lib/StanfordCPPLib')
-rwxr-xr-xlabb6/lib/StanfordCPPLib/error.cpp42
-rwxr-xr-xlabb6/lib/StanfordCPPLib/error.h54
-rwxr-xr-xlabb6/lib/StanfordCPPLib/simpio.cpp67
-rwxr-xr-xlabb6/lib/StanfordCPPLib/simpio.h53
-rwxr-xr-xlabb6/lib/StanfordCPPLib/strlib.cpp252
-rwxr-xr-xlabb6/lib/StanfordCPPLib/strlib.h204
6 files changed, 672 insertions, 0 deletions
diff --git a/labb6/lib/StanfordCPPLib/error.cpp b/labb6/lib/StanfordCPPLib/error.cpp
new file mode 100755
index 0000000..c0e0a36
--- /dev/null
+++ b/labb6/lib/StanfordCPPLib/error.cpp
@@ -0,0 +1,42 @@
+/*
+ * File: error.cpp
+ * ---------------
+ * Implementation of the error function.
+ */
+
+#include <exception>
+#include <string>
+#include <iostream>
+#include "error.h"
+using namespace std;
+
+/* Definitions for the ErrorException class */
+
+ErrorException::ErrorException(string msg) {
+ this->msg = msg;
+}
+
+ErrorException::~ErrorException() throw () {
+ /* Empty */
+}
+
+string ErrorException::getMessage() const {
+ return msg;
+}
+
+const char *ErrorException::what() const throw () {
+ return ("Error: " + msg).c_str();
+}
+
+/*
+ * Implementation notes: error
+ * ---------------------------
+ * Earlier implementations of error made it possible, at least on the
+ * Macintosh, to help the debugger generate a backtrace at the point
+ * of the error. Unfortunately, doing so is no longer possible if
+ * the errors are catchable.
+ */
+
+void error(string msg) {
+ throw ErrorException(msg);
+}
diff --git a/labb6/lib/StanfordCPPLib/error.h b/labb6/lib/StanfordCPPLib/error.h
new file mode 100755
index 0000000..ebc02e9
--- /dev/null
+++ b/labb6/lib/StanfordCPPLib/error.h
@@ -0,0 +1,54 @@
+/*
+ * File: error.h
+ * -------------
+ * This file defines the <code>ErrorException</code> class and the
+ * <code>error</code> function.
+ */
+
+#ifndef _error_h
+#define _error_h
+
+#include <string>
+#include <exception>
+
+/*
+ * Class: ErrorException
+ * ---------------------
+ * This exception is thrown by calls to the <code>error</code>
+ * function. Typical code for catching errors looks like this:
+ *
+ *<pre>
+ * try {
+ * ... code in which an error might occur ...
+ * } catch (ErrorException & ex) {
+ * ... code to handle the error condition ...
+ * }
+ *</pre>
+ *
+ * If an <code>ErrorException</code> is thrown at any point in the
+ * range of the <code>try</code> (including in functions called from
+ * that code), control will jump immediately to the error handler.
+ */
+
+class ErrorException : public std::exception {
+public:
+ ErrorException(std::string msg);
+ virtual ~ErrorException() throw ();
+ virtual std::string getMessage() const;
+ virtual const char *what() const throw ();
+
+private:
+ std::string msg;
+};
+
+/*
+ * Function: error
+ * Usage: error(msg);
+ * ------------------
+ * Signals an error condition in a program by throwing an
+ * <code>ErrorException</code> with the specified message.
+ */
+
+void error(std::string msg);
+
+#endif
diff --git a/labb6/lib/StanfordCPPLib/simpio.cpp b/labb6/lib/StanfordCPPLib/simpio.cpp
new file mode 100755
index 0000000..4f91551
--- /dev/null
+++ b/labb6/lib/StanfordCPPLib/simpio.cpp
@@ -0,0 +1,67 @@
+/*
+ * File: simpio.cpp
+ * ----------------
+ * This file implements the simpio.h interface.
+ */
+
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+#include "simpio.h"
+using namespace std;
+
+/*
+ * Implementation notes: getInteger, getReal
+ * -----------------------------------------
+ * Each of these functions reads a complete input line and then uses the
+ * <sstream> library to parse that line into a value of the desired type.
+ * If that fails, the implementation asks the user for a new value.
+ */
+
+int getInteger(string prompt) {
+ int value;
+ string line;
+ while (true) {
+ cout << prompt;
+ getline(cin, line);
+ istringstream stream(line);
+ stream >> value >> ws;
+ if (!stream.fail() && stream.eof()) break;
+ cout << "Illegal integer format. Try again." << endl;
+ if (prompt == "") prompt = "Enter an integer: ";
+ }
+ return value;
+}
+
+double getReal(string prompt) {
+ double value;
+ string line;
+ while (true) {
+ cout << prompt;
+ getline(cin, line);
+ istringstream stream(line);
+ stream >> value >> ws;
+ if (!stream.fail() && stream.eof()) break;
+ cout << "Illegal numeric format. Try again." << endl;
+ if (prompt == "") prompt = "Enter a number: ";
+ }
+ return value;
+}
+
+/*
+ * Implementation notes: getLine
+ * -----------------------------
+ * The getLine function simply combines the process of displaying a
+ * prompt and reading an input line into a single call. The primary
+ * reason for including this function in the library is to ensure
+ * that the process of reading integers, floating-point numbers, and
+ * strings remains as consistent as possible.
+ */
+
+string getLine(string prompt) {
+ string line;
+ cout << prompt;
+ getline(cin, line);
+ return line;
+}
diff --git a/labb6/lib/StanfordCPPLib/simpio.h b/labb6/lib/StanfordCPPLib/simpio.h
new file mode 100755
index 0000000..25c32c1
--- /dev/null
+++ b/labb6/lib/StanfordCPPLib/simpio.h
@@ -0,0 +1,53 @@
+/*
+ * File: simpio.h
+ * --------------
+ * This file exports a set of functions that simplify input/output
+ * operations in C++ and provide some error-checking on console input.
+ */
+
+#ifndef _simpio_h
+#define _simpio_h
+
+#include <string>
+
+/*
+ * Function: getInteger
+ * Usage: int n = getInteger(prompt);
+ * ----------------------------------
+ * Reads a complete line from <code>cin</code> and scans it as an
+ * integer. If the scan succeeds, the integer value is returned. If
+ * the argument is not a legal integer or if extraneous characters
+ * (other than whitespace) appear in the string, the user is given
+ * a chance to reenter the value. If supplied, the optional
+ * <code>prompt</code> string is printed before reading the value.
+ */
+
+int getInteger(std::string prompt = "");
+
+/*
+ * Function: getReal
+ * Usage: double x = getReal(prompt);
+ * ----------------------------------
+ * Reads a complete line from <code>cin</code> and scans it as a
+ * floating-point number. If the scan succeeds, the floating-point
+ * value is returned. If the input is not a legal number or if
+ * extraneous characters (other than whitespace) appear in the string,
+ * the user is given a chance to reenter the value. If supplied, the
+ * optional <code>prompt</code> string is printed before reading the value.
+ */
+
+double getReal(std::string prompt = "");
+
+/*
+ * Function: getLine
+ * Usage: string line = getLine(prompt);
+ * -------------------------------------
+ * Reads a line of text from <code>cin</code> and returns that line
+ * as a string. The newline character that terminates the input is
+ * not stored as part of the return value. If supplied, the optional
+ * <code>prompt</code> string is printed before reading the value.
+ */
+
+std::string getLine(std::string prompt = "");
+
+#endif
diff --git a/labb6/lib/StanfordCPPLib/strlib.cpp b/labb6/lib/StanfordCPPLib/strlib.cpp
new file mode 100755
index 0000000..7e53235
--- /dev/null
+++ b/labb6/lib/StanfordCPPLib/strlib.cpp
@@ -0,0 +1,252 @@
+/*
+ * File: strlib.cpp
+ * ----------------
+ * This file implements the strlib.h interface.
+ */
+
+#include <cctype>
+#include <iomanip>
+#include <iostream>
+#include <sstream>
+#include "error.h"
+#include "strlib.h"
+using namespace std;
+
+/* Function prototypes */
+
+/*
+ * Implementation notes: numeric conversion
+ * ----------------------------------------
+ * These functions use the <sstream> library to perform the conversion.
+ */
+
+string integerToString(int n) {
+ ostringstream stream;
+ stream << n;
+ return stream.str();
+}
+
+int stringToInteger(string str) {
+ istringstream stream(str);
+ int value;
+ stream >> value >> ws;
+ if (stream.fail() || !stream.eof()) {
+ error("stringToInteger: Illegal integer format (" + str + ")");
+ }
+ return value;
+}
+
+string realToString(double d) {
+ ostringstream stream;
+ stream << uppercase << d;
+ return stream.str();
+}
+
+double stringToReal(string str) {
+ istringstream stream(str);
+ double value;
+ stream >> value >> ws;
+ if (stream.fail() || !stream.eof()) {
+ error("stringToReal: Illegal floating-point format (" + str + ")");
+ }
+ return value;
+}
+
+/*
+ * Implementation notes: case conversion
+ * -------------------------------------
+ * The functions toUpperCase and toLowerCase return a new string whose
+ * characters appear in the desired case. These implementations rely on
+ * the fact that the characters in the string are copied when the
+ * argument is passed to the function, which makes it possible to change
+ * the case of the copy without affecting the original.
+ */
+
+string toUpperCase(string str) {
+ int nChars = str.length();
+ for (int i = 0; i < nChars; i++) {
+ str[i] = toupper(str[i]);
+ }
+ return str;
+}
+
+string toLowerCase(string str) {
+ int nChars = str.length();
+ for (int i = 0; i < nChars; i++) {
+ str[i] = tolower(str[i]);
+ }
+ return str;
+}
+
+/*
+ * Implementation notes: equalsIgnoreCase
+ * --------------------------------------
+ * This implementation uses a for loop to cycle through the characters in
+ * each string. Converting each string to uppercase and then comparing
+ * the results makes for a shorter but less efficient implementation.
+ */
+
+bool equalsIgnoreCase(string s1, string s2) {
+ if (s1.length() != s2.length()) return false;
+ int nChars = s1.length();
+ for (int i = 0; i < nChars; i++) {
+ if (tolower(s1[i]) != tolower(s2[i])) return false;
+ }
+ return true;
+}
+
+/*
+ * Implementation notes: startsWith, endsWith
+ * ------------------------------------------
+ * These implementations are overloaded to allow the second argument to
+ * be either a string or a character.
+ */
+
+bool startsWith(string str, string prefix) {
+ if (str.length() < prefix.length()) return false;
+ int nChars = prefix.length();
+ for (int i = 0; i < nChars; i++) {
+ if (str[i] != prefix[i]) return false;
+ }
+ return true;
+}
+
+bool startsWith(string str, char prefix) {
+ return str.length() > 0 && str[0] == prefix;
+}
+
+bool endsWith(string str, string suffix) {
+ int nChars = suffix.length();
+ int start = str.length() - nChars;
+ if (start < 0) return false;
+ for (int i = 0; i < nChars; i++) {
+ if (str[start + i] != suffix[i]) return false;
+ }
+ return true;
+}
+
+bool endsWith(string str, char suffix) {
+ return str.length() > 0 && str[str.length() - 1] == suffix;
+}
+
+string trim(string str) {
+ int finish = str.length() - 1;
+ while (finish >= 0 && isspace(str[finish])) {
+ finish--;
+ }
+ int start = 0;
+ while (start <= finish && isspace(str[start])) {
+ start++;
+ }
+ return str.substr(start, finish - start + 1);
+}
+
+/*
+ * Implementation notes: readQuotedString and writeQuotedString
+ * ------------------------------------------------------------
+ * Most of the work in these functions has to do with escape sequences.
+ */
+
+static const string STRING_DELIMITERS = ",:)}]\n";
+
+bool stringNeedsQuoting(const string & str) {
+ int n = str.length();
+ for (int i = 0; i < n; i++) {
+ char ch = str[i];
+ if (isspace(ch)) return false;
+ if (STRING_DELIMITERS.find(ch) != string::npos) return true;
+ }
+ return false;
+}
+
+void readQuotedString(istream & is, string & str) {
+ str = "";
+ char ch;
+ while (is.get(ch) && isspace(ch)) {
+ /* Empty */
+ }
+ if (is.fail()) return;
+ if (ch == '\'' || ch == '"') {
+ char delim = ch;
+ while (is.get(ch) && ch != delim) {
+ if (is.fail()) error("Unterminated string");
+ if (ch == '\\') {
+ if (!is.get(ch)) error("Unterminated string");
+ if (isdigit(ch) || ch == 'x') {
+ int maxDigits = 3;
+ int base = 8;
+ if (ch == 'x') {
+ base = 16;
+ maxDigits = 2;
+ }
+ int result = 0;
+ int digit = 0;
+ for (int i = 0; i < maxDigits && ch != delim; i++) {
+ if (isdigit(ch)) {
+ digit = ch - '0';
+ } else if (base == 16 && isxdigit(ch)) {
+ digit = toupper(ch) - 'A' + 10;
+ } else {
+ break;
+ }
+ result = base * result + digit;
+ if (!is.get(ch)) error("Unterminated string");
+ }
+ ch = char(result);
+ is.unget();
+ } else {
+ switch (ch) {
+ case 'a': ch = '\a'; break;
+ case 'b': ch = '\b'; break;
+ case 'f': ch = '\f'; break;
+ case 'n': ch = '\n'; break;
+ case 'r': ch = '\r'; break;
+ case 't': ch = '\t'; break;
+ case 'v': ch = '\v'; break;
+ case '"': ch = '"'; break;
+ case '\'': ch = '\''; break;
+ case '\\': ch = '\\'; break;
+ }
+ }
+ }
+ str += ch;
+ }
+ } else {
+ str += ch;
+ int endTrim = 0;
+ while (is.get(ch) && STRING_DELIMITERS.find(ch) == string::npos) {
+ str += ch;
+ if (!isspace(ch)) endTrim = str.length();
+ }
+ if (is) is.unget();
+ str = str.substr(0, endTrim);
+ }
+}
+
+void writeQuotedString(ostream & os, const string & str, bool forceQuotes) {
+ if (!forceQuotes && stringNeedsQuoting(str)) forceQuotes = true;
+ if (forceQuotes) os << '"';
+ int len = str.length();
+ for (int i = 0; i < len; i++) {
+ char ch = str.at(i);
+ switch (ch) {
+ case '\a': os << "\\a"; break;
+ case '\b': os << "\\b"; break;
+ case '\f': os << "\\f"; break;
+ case '\n': os << "\\n"; break;
+ case '\r': os << "\\r"; break;
+ case '\t': os << "\\t"; break;
+ case '\v': os << "\\v"; break;
+ case '\\': os << "\\\\"; break;
+ default:
+ if (isprint(ch) && ch != '"') {
+ os << ch;
+ } else {
+ ostringstream oss;
+ oss << oct << setw(3) << setfill('0') << (int(ch) & 0xFF);
+ os << "\\" << oss.str();
+ }
+ }
+ }
+ if (forceQuotes) os << '"';
+}
diff --git a/labb6/lib/StanfordCPPLib/strlib.h b/labb6/lib/StanfordCPPLib/strlib.h
new file mode 100755
index 0000000..d3be3d2
--- /dev/null
+++ b/labb6/lib/StanfordCPPLib/strlib.h
@@ -0,0 +1,204 @@
+/*
+ * File: strlib.h
+ * --------------
+ * This file exports several useful string functions that are not
+ * included in the C++ string library.
+ */
+
+#ifndef _strlib_h
+#define _strlib_h
+
+#include <iostream>
+#include <string>
+
+/*
+ * Function: integerToString
+ * Usage: string s = integerToString(n);
+ * -------------------------------------
+ * Converts an integer into the corresponding string of digits.
+ * For example, calling <code>integerToString(123)</code> returns
+ * the string <code>"123"</code>.
+ */
+
+std::string integerToString(int n);
+
+/*
+ * Function: stringToInteger
+ * Usage: int n = stringToInteger(str);
+ * ------------------------------------
+ * Converts a string of digits into an integer. If the string is not a
+ * legal integer or contains extraneous characters other than whitespace,
+ * <code>stringToInteger</code> calls <code>error</code> with an
+ * appropriate message.
+ */
+
+int stringToInteger(std::string str);
+
+/*
+ * Function: realToString
+ * Usage: string s = realToString(d);
+ * ----------------------------------
+ * Converts a floating-point number into the corresponding string form.
+ * For example, calling <code>realToString(23.45)</code> returns
+ * the string <code>"23.45"</code>.
+ */
+
+std::string realToString(double d);
+
+/*
+ * Function: stringToReal
+ * Usage: double d = stringToReal(str);
+ * ------------------------------------
+ * Converts a string representing a real number into its corresponding
+ * value. If the string is not a legal floating-point number or contains
+ * extraneous characters other than whitespace, <code>stringToReal</code>
+ * calls <code>error</code> with an appropriate message.
+ */
+
+double stringToReal(std::string str);
+
+/*
+ * Function: toUpperCase
+ * Usage: string s = toUpperCase(str);
+ * -----------------------------------
+ * Returns a new string in which all lowercase characters have been converted
+ * into their uppercase equivalents.
+ */
+
+std::string toUpperCase(std::string str);
+
+/*
+ * Function: toLowerCase
+ * Usage: string s = toLowerCase(str);
+ * -----------------------------------
+ * Returns a new string in which all uppercase characters have been converted
+ * into their lowercase equivalents.
+ */
+
+std::string toLowerCase(std::string str);
+
+/*
+ * Function: equalsIgnoreCase
+ * Usage: if (equalsIgnoreCase(s1, s2)) ...
+ * ----------------------------------------
+ * Returns <code>true</code> if <code>s1</code> and <code>s2</code> are
+ * equal discounting differences in case.
+ */
+
+bool equalsIgnoreCase(std::string s1, std::string s2);
+
+/*
+ * Function: startsWith
+ * Usage: if (startsWith(str, prefix)) ...
+ * ---------------------------------------
+ * Returns <code>true</code> if the string <code>str</code> starts with
+ * the specified prefix, which may be either a string or a character.
+ */
+
+bool startsWith(std::string str, std::string prefix);
+bool startsWith(std::string str, char prefix);
+
+/*
+ * Function: endsWith
+ * Usage: if (endsWith(str, suffix)) ...
+ * -------------------------------------
+ * Returns <code>true</code> if the string <code>str</code> ends with
+ * the specified suffix, which may be either a string or a character.
+ */
+
+bool endsWith(std::string str, std::string suffix);
+bool endsWith(std::string str, char suffix);
+
+/*
+ * Function: trim
+ * Usage: string trimmed = trim(str);
+ * ----------------------------------
+ * Returns a new string after removing any whitespace characters
+ * from the beginning and end of the argument.
+ */
+
+std::string trim(std::string str);
+
+/* Private section */
+
+/**********************************************************************/
+/* Note: Everything below this point in the file is logically part */
+/* of the implementation and should not be of interest to clients. */
+/**********************************************************************/
+
+/*
+ * Friend function: writeQuotedString
+ * Usage: writeQuotedString(outfile, str, forceQuotes);
+ * ----------------------------------------------------
+ * Writes the string str to outfile surrounded by double quotes, converting
+ * special characters to escape sequences, as necessary. If the optional
+ * parameter forceQuotes is explicitly set to false, quotes are included
+ * in the output only if necessary.
+ */
+
+void writeQuotedString(std::ostream & os, const std::string & str,
+ bool forceQuotes = true);
+
+/*
+ * Friend function: readQuotedString
+ * Usage: readQuotedString(infile, str);
+ * -------------------------------------
+ * Reads the next string from infile into the reference parameter str.
+ * If the first character (other than whitespace) is either a single
+ * or a double quote, this function reads characters up to the
+ * matching quote, processing standard escape sequences as it goes.
+ * If not, readString reads characters up to any of the characters
+ * in the string STRING_DELIMITERS in the implementation file.
+ */
+
+void readQuotedString(std::istream & is, std::string & str);
+
+/*
+ * Friend function: stringNeedsQuoting
+ * Usage: if (stringNeedsQuoting(str)) ...
+ * ---------------------------------------
+ * Checks whether the string needs quoting in order to be read correctly.
+ */
+
+bool stringNeedsQuoting(const std::string & str);
+
+/*
+ * Friend function: writeGenericValue
+ * Usage: writeGenericValue(os, value, forceQuotes);
+ * -------------------------------------------------
+ * Writes a generic value to the output stream. If that value is a string,
+ * this function uses writeQuotedString to write the value.
+ */
+
+template <typename ValueType>
+void writeGenericValue(std::ostream & os, const ValueType & value,
+ bool) {
+ os << value;
+}
+
+template <>
+inline void writeGenericValue(std::ostream & os, const std::string & value,
+ bool forceQuotes) {
+ writeQuotedString(os, value, forceQuotes);
+}
+
+/*
+ * Friend function: readGenericValue
+ * Usage: readGenericValue(is, value);
+ * -----------------------------------
+ * Reads a generic value from the input stream. If that value is a string,
+ * this function uses readQuotedString to read the value.
+ */
+
+template <typename ValueType>
+void readGenericValue(std::istream & is, ValueType & value) {
+ is >> value;
+}
+
+template <>
+inline void readGenericValue(std::istream & is, std::string & value) {
+ readQuotedString(is, value);
+}
+
+
+#endif