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 
132  static QByteArray readUntilByte(QDataStream& stream, unsigned char stopByte);
133 
143  static void translateServerAddress(const QString& addressString, QString& hostname, unsigned short& port, const QString& defaultAddress);
144 
145  static const QString &unescape(QString &str);
146 
154  static bool isUrlSafe(const QString& url);
155 
156  static QString timestamp(const QString& format);
157  static QString& trim(QString& str, const QString& charList) { return trimr(triml(str, charList), charList); }
158  static QString& trimr(QString& str, const QString& charList);
159  static QString& triml(QString& str, const QString& charList);
160 
172  static QString wrapUrlsWithHtmlATags(const QString& str);
173 
174  protected:
175  enum DataUnit
176  {
177  Byte = 0,
178  Kilobyte = 1,
179  Megabyte = 2,
180  Gigabyte = 3
181  };
182 
183  static const unsigned RANDOM_CHAR_POOL_SIZE = 36;
184  static const char RANDOM_CHAR_POOL[RANDOM_CHAR_POOL_SIZE];
185 
186 
187  static float scaleDataUnit(float bytes, DataUnit& outUnit);
188 };
189 
190 #endif