diff include/utils.hpp @ 144:b32947b002da

added the code to read matrices from text files
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 26 Aug 2011 19:38:11 -0400
parents a52653dca25d
children bee0e7407af7
line wrap: on
line diff
--- a/include/utils.hpp	Wed Aug 24 19:43:44 2011 -0400
+++ b/include/utils.hpp	Fri Aug 26 19:38:11 2011 -0400
@@ -2,9 +2,30 @@
 #define UTILS_HPP
 
 #include <iosfwd>
+#include <string>
+#include <vector>
 
 static const double pi = 3.14159265358979323846;
 
+static const std::string separator = " ";
+const char commentChar = '#';
+
+/** Loads lines of numbers from a text file, separated by some character */
+std::vector<std::vector<float> > loadNumbers(const std::string& filename, const std::string& separator = ::separator);
+
+/** Gets line in a file, skipping comments (starting with '#') (wrapper around getline).
+ * Warning: returns an empty string if all the lines to the end of the file are comments. */
+std::string getlineComment(std::ifstream& f);
+
+/** Converts a string to an integer. */
+int toInt(const std::string& s);
+
+/** Converts a string to a float. */
+float toFloat(const std::string& s);
+
+/** Converts a string to a double. */
+double toDouble(const std::string& s);
+
 /** Opens file for writing with fixed scientific precision. */
 void openWriteScientificPrecision(std::ofstream& out, const std::string& filename, const int& precision);
 
@@ -30,4 +51,29 @@
 
 inline bool skipKey(const int& pressedKey) { return (((char)pressedKey) == 'n' || ((char)pressedKey) == 'N');}
 
+
+// templates
+
+/** Splits a string on the separator. The resulting string items are added to the result (the list is not cleared). */
+template<class Seq>
+  inline void split(Seq& result, const std::string& text, const std::string& separator = ::separator) {
+  int n = text.length();
+
+  int start = text.find_first_not_of(separator);
+  while ((start >= 0) && (start < n)) {
+    int stop = text.find_first_of(separator, start);
+    if ((stop < 0) || (stop > n))
+      stop = n;
+    result.push_back(text.substr(start, stop - start));
+    start = text.find_first_not_of(separator, stop+1);
+  }
+}
+
+/** Converts strings to numbers. */
+template<typename T>
+bool fromString(T & result, const std::string & s) {
+  std::istringstream iss(s);
+  return iss >> result != 0;
+}
+
 #endif