comparison 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
comparison
equal deleted inserted replaced
143:436b87d4b992 144:b32947b002da
1 #ifndef UTILS_HPP 1 #ifndef UTILS_HPP
2 #define UTILS_HPP 2 #define UTILS_HPP
3 3
4 #include <iosfwd> 4 #include <iosfwd>
5 #include <string>
6 #include <vector>
5 7
6 static const double pi = 3.14159265358979323846; 8 static const double pi = 3.14159265358979323846;
9
10 static const std::string separator = " ";
11 const char commentChar = '#';
12
13 /** Loads lines of numbers from a text file, separated by some character */
14 std::vector<std::vector<float> > loadNumbers(const std::string& filename, const std::string& separator = ::separator);
15
16 /** Gets line in a file, skipping comments (starting with '#') (wrapper around getline).
17 * Warning: returns an empty string if all the lines to the end of the file are comments. */
18 std::string getlineComment(std::ifstream& f);
19
20 /** Converts a string to an integer. */
21 int toInt(const std::string& s);
22
23 /** Converts a string to a float. */
24 float toFloat(const std::string& s);
25
26 /** Converts a string to a double. */
27 double toDouble(const std::string& s);
7 28
8 /** Opens file for writing with fixed scientific precision. */ 29 /** Opens file for writing with fixed scientific precision. */
9 void openWriteScientificPrecision(std::ofstream& out, const std::string& filename, const int& precision); 30 void openWriteScientificPrecision(std::ofstream& out, const std::string& filename, const int& precision);
10 31
11 void openWritePrecision(std::ofstream& out, const std::string& filename, const int& precision); 32 void openWritePrecision(std::ofstream& out, const std::string& filename, const int& precision);
28 49
29 inline bool interruptionKey(const int& pressedKey) { return (((char)pressedKey) == 'q' || ((char)pressedKey) == 'Q');} 50 inline bool interruptionKey(const int& pressedKey) { return (((char)pressedKey) == 'q' || ((char)pressedKey) == 'Q');}
30 51
31 inline bool skipKey(const int& pressedKey) { return (((char)pressedKey) == 'n' || ((char)pressedKey) == 'N');} 52 inline bool skipKey(const int& pressedKey) { return (((char)pressedKey) == 'n' || ((char)pressedKey) == 'N');}
32 53
54
55 // templates
56
57 /** Splits a string on the separator. The resulting string items are added to the result (the list is not cleared). */
58 template<class Seq>
59 inline void split(Seq& result, const std::string& text, const std::string& separator = ::separator) {
60 int n = text.length();
61
62 int start = text.find_first_not_of(separator);
63 while ((start >= 0) && (start < n)) {
64 int stop = text.find_first_of(separator, start);
65 if ((stop < 0) || (stop > n))
66 stop = n;
67 result.push_back(text.substr(start, stop - start));
68 start = text.find_first_not_of(separator, stop+1);
69 }
70 }
71
72 /** Converts strings to numbers. */
73 template<typename T>
74 bool fromString(T & result, const std::string & s) {
75 std::istringstream iss(s);
76 return iss >> result != 0;
77 }
78
33 #endif 79 #endif