strings.h
1 //------------------------------------------------------------------------------
2 // strings.h
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (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 Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #ifndef __STRINGS_H_
24 #define __STRINGS_H_
25 
26 #include "global.h"
27 
28 class QString;
29 class QStringList;
30 
31 // \c = '\034'
32 #define ESCAPE_COLOR_CHAR '\034'
33 
37 class MAIN_EXPORT Strings
38 {
39  public:
44  static QString colorizeString(const QString &str, int def=4);
45 
52  static QStringList combineManyPaths(const QStringList &fronts, const QString &pathEnd);
53 
71  static QString combinePaths(QString pathFront, QString pathEnd);
72 
78  static QString createRandomAlphaNumericString(unsigned numChars);
79  static QString createRandomAlphaNumericStringWithNewLines(unsigned numCharsPerLine, unsigned numLines);
80 
84  static const QString &escape(QString &str);
85 
92  static QString formatDataAmount(qint64 bytes);
93 
103  static QString formatDataSpeed(float speedInBytesPerSecond);
104 
116  static QString formatTime(float seconds);
117 
118  static bool isCharOnCharList(char c, const QString& charList);
119 
127  static QString normalizePath(QString path);
128 
152  static QByteArray readUntilByte(QDataStream& stream, unsigned char stopByte);
153 
163  static void translateServerAddress(const QString& addressString, QString& hostname, unsigned short& port, const QString& defaultAddress);
164 
165  static const QString &unescape(QString &str);
166 
174  static bool isUrlSafe(const QString& url);
175 
176  static QString timestamp(const QString& format);
177  static QString& trim(QString& str, const QString& charList) { return trimr(triml(str, charList), charList); }
178  static QString& trimr(QString& str, const QString& charList);
179  static QString& triml(QString& str, const QString& charList);
180 
192  static QString wrapUrlsWithHtmlATags(const QString& str);
193 
194  protected:
195  enum DataUnit
196  {
197  Byte = 0,
198  Kilobyte = 1,
199  Megabyte = 2,
200  Gigabyte = 3
201  };
202 
203  static const unsigned RANDOM_CHAR_POOL_SIZE = 36;
204  static const char RANDOM_CHAR_POOL[RANDOM_CHAR_POOL_SIZE];
205 
206 
207  static float scaleDataUnit(float bytes, DataUnit& outUnit);
208 };
209 
210 #endif