strings.hpp
1 //------------------------------------------------------------------------------
2 // strings.hpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; 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_HPP_
24 #define __STRINGS_HPP_
25 // Why hpp in this case? Glibc 2.26 changed string.h to include its strings.h
26 // so this file would shadow that header and cause errors.
27 
28 #include "global.h"
29 
30 class QString;
31 class QStringList;
32 
33 // \c = '\034'
34 #define ESCAPE_COLOR_CHAR '\034'
35 
39 class MAIN_EXPORT Strings
40 {
41  public:
46  static QString colorizeString(const QString &str, int def=4);
47 
54  static QStringList combineManyPaths(const QStringList &fronts, const QString &pathEnd);
55 
73  static QString combinePaths(QString pathFront, QString pathEnd);
74 
80  static QString createRandomAlphaNumericString(unsigned numChars);
81  static QString createRandomAlphaNumericStringWithNewLines(unsigned numCharsPerLine, unsigned numLines);
82 
86  static const QString &escape(QString &str);
87 
94  static QString formatDataAmount(qint64 bytes);
95 
105  static QString formatDataSpeed(float speedInBytesPerSecond);
106 
118  static QString formatTime(float seconds);
119 
120  static bool isCharOnCharList(char c, const QString& charList);
121 
129  static QString normalizePath(QString path);
130 
134  static QByteArray readUntilByte(QDataStream& stream, unsigned char stopByte);
135 
145  static void translateServerAddress(const QString& addressString, QString& hostname, unsigned short& port, const QString& defaultAddress);
146 
147  static const QString &unescape(QString &str);
148 
156  static bool isUrlSafe(const QString& url);
157 
158  static QString timestamp(const QString& format);
159  static QString& trim(QString& str, const QString& charList) { return trimr(triml(str, charList), charList); }
160  static QString& trimr(QString& str, const QString& charList);
161  static QString& triml(QString& str, const QString& charList);
162 
174  static QString wrapUrlsWithHtmlATags(const QString& str);
175 
176  protected:
177  enum DataUnit
178  {
179  Byte = 0,
180  Kilobyte = 1,
181  Megabyte = 2,
182  Gigabyte = 3
183  };
184 
185  static const unsigned RANDOM_CHAR_POOL_SIZE = 36;
186  static const char RANDOM_CHAR_POOL[RANDOM_CHAR_POOL_SIZE];
187 
188 
189  static float scaleDataUnit(float bytes, DataUnit& outUnit);
190 };
191 
192 #endif