Base/Tools.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
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     // s1 and s2 must be numbers represented as string
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     // find highest suffix
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) { // same prefix
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     // check for first character whether it's a digit
00111     std::string CleanName = name;
00112     if (!CleanName.empty() && CleanName[0] >= 48 && CleanName[0] <= 57)
00113         CleanName[0] = '_';
00114     // strip illegal chars
00115     for (std::string::iterator it = CleanName.begin(); it != CleanName.end(); ++it) {
00116         if (!((*it>=48 && *it<=57) ||  // number
00117              (*it>=65 && *it<=90)  ||  // uppercase letter
00118              (*it>=97 && *it<=122)))   // lowercase letter
00119              *it = '_'; // it's neither number nor letter
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 }

Generated on Wed Nov 23 19:00:45 2011 for FreeCAD by  doxygen 1.6.1