Vidalia  0.3.1
stringutil.h
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file stringutil.h
13 ** \brief Common string manipulation functions
14 */
15 
16 #ifndef _STRINGUTIL_H
17 #define _STRINGUTIL_H
18 
19 #include <QStringList>
20 #include <QHash>
21 #include <QDateTime>
22 
23 
24 /** Creates a QStringList from the array of C strings. */
25 QStringList char_array_to_stringlist(char **arr, int len);
26 
27 /** Ensures all characters in str are in validChars. If a character appears
28  * in str but not in validChars, it will be removed and the resulting
29  * string returned. */
30 QString ensure_valid_chars(const QString &str, const QString &validChars);
31 
32 /** Scrubs an email address by replacing "@" with " at " and "." with " dot ". */
33 QString scrub_email_addr(const QString &email);
34 
35 /** Conditionally assigns errmsg to string if str is not null and returns
36  * false. */
37 bool err(QString *str, const QString &errmsg);
38 
39 /** Wraps <b>str</b> at <b>width</b> characters wide, using <b>sep</b> as the
40  * word separator (" ", for example), and placing the line ending <b>le</b> at
41  * the end of each line, except the last.*/
42 QString string_wrap(const QString &str, int width,
43  const QString &sep = QString(" "),
44  const QString &le = QString("\n"));
45 
46 /** Encodes the bytes in <b>buf</b> as an uppercase hexadecimal string and
47  * returns the result. This function is derived from base16_encode() in Tor's
48  * util.c. See LICENSE for details on Tor's license. */
49 QString base16_encode(const QByteArray &buf);
50 
51 /** Given a string <b>str</b>, this function returns a quoted string with all
52  * '"' and '\' characters escaped with a single '\'. */
53 QString string_escape(const QString &str);
54 
55 /** Given a quoted string <b>str</b>, this function returns an unquoted,
56  * unescaped string. <b>str</b> must start and end with an unescaped quote. */
57 QString string_unescape(const QString &str, bool *ok = 0);
58 
59 /** Parses a series of space-separated key[=value|="value"] tokens from
60  * <b>str</b> and returns the mappings in a QHash. If <b>str</b> was unable
61  * to be parsed, <b>ok</b> is set to false. */
62 QHash<QString,QString> string_parse_keyvals(const QString &str, bool *ok = 0);
63 
64 /** Parses a series of command line arguments from <b>str</b>. If <b>str</b>
65  * was unable to be parsed, <b>ok</b> is set to false. */
66 QStringList string_parse_arguments(const QString &str, bool *ok = 0);
67 
68 /** Formats the list of command line arguments in <b>args</b> as a string.
69  * Arguments that contain ' ', '\', or '"' tokens will be escaped and wrapped
70  * in double quotes. */
71 QString string_format_arguments(const QStringList &args);
72 
73 /** Returns true if <b>str</b> is a valid hexademical string. Returns false
74  * otherwise. */
75 bool string_is_hex(const QString &str);
76 
77 /** Returns a human-readable description of the time elapsed given by
78  * <b>seconds</b>, broken down into days, hours, minutes and seconds. */
79 QString string_format_uptime(quint64 seconds);
80 
81 /** Returns a string representation of <b>date</b> formatted according to
82  * "yyyy-MM-dd HH:mm:ss". */
83 QString string_format_datetime(const QDateTime &date);
84 
85 /** Returns a string representation of <b>bytes</b> with the appropriate
86  * (localized) suffix of either "B/s", "KB/s", "MB/s" or "GB/s". */
87 QString string_format_bandwidth(quint64 bytes);
88 
89 #endif
90