Base/Tools.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <sstream>
00027 #endif
00028
00029 # include <QTime>
00030
00031 #include "Tools.h"
00032
00033 namespace Base {
00034 struct string_comp : public std::binary_function<std::string,
00035 std::string, bool>
00036 {
00037
00038 bool operator()(const std::string& s1, const std::string& s2)
00039 {
00040 if (s1.size() < s2.size())
00041 return true;
00042 else if (s1.size() > s2.size())
00043 return false;
00044 else
00045 return s1 < s2;
00046 }
00047 static std::string increment(const std::string& s)
00048 {
00049 std::string n = s;
00050 int addcarry=1;
00051 for (std::string::reverse_iterator it = n.rbegin(); it != n.rend(); ++it) {
00052 if (addcarry == 0)
00053 break;
00054 int d = *it - 48;
00055 d = d + addcarry;
00056 *it = ((d%10) + 48);
00057 addcarry = d / 10;
00058 }
00059 if (addcarry > 0) {
00060 std::string b;
00061 b.resize(1);
00062 b[0] = addcarry + 48;
00063 n = b + n;
00064 }
00065
00066 return n;
00067 }
00068 };
00069 }
00070
00071 std::string Base::Tools::getUniqueName(const std::string& name, const std::vector<std::string>& names, int d)
00072 {
00073
00074 std::string num_suffix;
00075 for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); ++it) {
00076 if (it->substr(0, name.length()) == name) {
00077 std::string suffix(it->substr(name.length()));
00078 if (suffix.size() > 0) {
00079 std::string::size_type pos = suffix.find_first_not_of("0123456789");
00080 if (pos==std::string::npos)
00081 num_suffix = std::max<std::string>(num_suffix, suffix, Base::string_comp());
00082 }
00083 }
00084 }
00085
00086 std::stringstream str;
00087 str << name;
00088 if (d > 0) {
00089 str.fill('0');
00090 str.width(d);
00091 }
00092 str << Base::string_comp::increment(num_suffix);
00093 return str.str();
00094 }
00095
00096 std::string Base::Tools::addNumber(const std::string& name, unsigned int num, int d)
00097 {
00098 std::stringstream str;
00099 str << name;
00100 if (d > 0) {
00101 str.fill('0');
00102 str.width(d);
00103 }
00104 str << num;
00105 return str.str();
00106 }
00107
00108 std::string Base::Tools::getIdentifier(const std::string& name)
00109 {
00110
00111 std::string CleanName = name;
00112 if (!CleanName.empty() && CleanName[0] >= 48 && CleanName[0] <= 57)
00113 CleanName[0] = '_';
00114
00115 for (std::string::iterator it = CleanName.begin(); it != CleanName.end(); ++it) {
00116 if (!((*it>=48 && *it<=57) ||
00117 (*it>=65 && *it<=90) ||
00118 (*it>=97 && *it<=122)))
00119 *it = '_';
00120 }
00121
00122 return CleanName;
00123 }
00124
00125
00126
00127 using namespace Base;
00128
00129 struct StopWatch::Private
00130 {
00131 QTime t;
00132 };
00133
00134 StopWatch::StopWatch() : d(new Private)
00135 {
00136 }
00137
00138 StopWatch::~StopWatch()
00139 {
00140 delete d;
00141 }
00142
00143 void StopWatch::start()
00144 {
00145 d->t.start();
00146 }
00147
00148 int StopWatch::elapsed()
00149 {
00150 return d->t.elapsed();
00151 }
00152
00153 std::string StopWatch::toString(int ms) const
00154 {
00155 int total = ms;
00156 int msec = total % 1000;
00157 total = total / 1000;
00158 int secs = total % 60;
00159 total = total / 60;
00160 int mins = total % 60;
00161 int hour = total / 60;
00162 std::stringstream str;
00163 str << "Needed time: ";
00164 if (hour > 0)
00165 str << hour << "h " << mins << "m " << secs << "s";
00166 else if (mins > 0)
00167 str << mins << "m " << secs << "s";
00168 else if (secs > 0)
00169 str << secs << "s";
00170 else
00171 str << msec << "ms";
00172 return str.str();
00173 }