| 1 | /*************************************************************************** |
| 2 | * This file is part of Robocut. * |
| 3 | * Copyright (C) 2010 Tim Hutt <[email protected]> * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 3 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | /// Result Class |
| 24 | |
| 25 | #include <string> |
| 26 | #include <sstream> |
| 27 | #include <cstdio> |
| 28 | #include <vector> |
| 29 | #include <cstdlib> |
| 30 | |
| 31 | using std::vector; |
| 32 | using std::string; |
| 33 | |
| 34 | typedef string::size_type stringpos; |
| 35 | |
| 36 | /// Error Handling |
| 37 | |
| 38 | class Error |
| 39 | { |
| 40 | public: |
| 41 | explicit Error(bool S) : s(S) { } |
| 42 | explicit Error(const char* C) : s(false), m(C) { } |
| 43 | Error(const string& msg) : s(false), m(msg) { } |
| 44 | operator bool() { return s; } // TODO: Use bool hack from boost so int i = Error(); doesn't work. |
| 45 | string message() { if (m.empty()) return "Unspecified Error"; return m; } |
| 46 | |
| 47 | private: |
| 48 | bool s; |
| 49 | string m; |
| 50 | }; |
| 51 | |
| 52 | const Error Success(true); |
| 53 | const Error Failure(false); |
| 54 | |
| 55 | // Integer to string. |
| 56 | inline string ItoS(int I) |
| 57 | { |
| 58 | std::stringstream S; |
| 59 | S << I; |
| 60 | return S.str(); |
| 61 | } |
| 62 | |
| 63 | // Unsigned integer to string. |
| 64 | inline string UItoS(unsigned int I) |
| 65 | { |
| 66 | std::stringstream S; |
| 67 | S << I; |
| 68 | return S.str(); |
| 69 | } |
| 70 | |
| 71 | // Unsigned long long to string. |
| 72 | inline string ULLtoS(unsigned long long I) |
| 73 | { |
| 74 | char out[128]; |
| 75 | sprintf(out, "%lld", I); |
| 76 | return out; |
| 77 | } |
| 78 | |
| 79 | // String to integer, returns Fail on fail. |
| 80 | inline int StoI(const string& S, int Fail = 0) |
| 81 | { |
| 82 | char* EP; |
| 83 | const char* P = S.c_str(); |
| 84 | int R = strtol(P, &EP, 0); |
| 85 | if (EP == P) |
| 86 | return Fail; |
| 87 | return R; |
| 88 | } |
| 89 | |
| 90 | // String to unsigned integer, returns Fail on fail. |
| 91 | inline unsigned int StoUI(const string& S, unsigned int Fail = 0) |
| 92 | { |
| 93 | char* EP; |
| 94 | const char* P = S.c_str(); |
| 95 | int R = strtoul(P, &EP, 0); |
| 96 | if (EP == P) |
| 97 | return Fail; |
| 98 | return R; |
| 99 | } |
| 100 | |
| 101 | // String to unsigned integer, returns Fail on fail. |
| 102 | inline unsigned int StoULL(const string& S, unsigned long long Fail = 0) |
| 103 | { |
| 104 | char* EP; |
| 105 | const char* P = S.c_str(); |
| 106 | int R = strtoull(P, &EP, 0); |
| 107 | if (EP == P) |
| 108 | return Fail; |
| 109 | return R; |
| 110 | } |
| 111 | |
| 112 | // Get an environmental variable. |
| 113 | inline string GetEnv(const string& Var) |
| 114 | { |
| 115 | char* V = getenv(Var.c_str()); |
| 116 | if (!V) |
| 117 | return ""; |
| 118 | return V; |
| 119 | } |