Vidalia 0.2.10
|
00001 /* 00002 ** This file is part of Vidalia, and is subject to the license terms in the 00003 ** LICENSE file, found in the top level directory of this distribution. If you 00004 ** did not receive the LICENSE file with this file, you may obtain it from the 00005 ** Vidalia source package distributed by the Vidalia Project at 00006 ** http://www.vidalia-project.net/. No part of Vidalia, including this file, 00007 ** may be copied, modified, propagated, or distributed except according to the 00008 ** terms described in the LICENSE file. 00009 */ 00010 00011 /* 00012 ** \file stringutil.h 00013 ** \version $Id: stringutil.h 4085 2009-08-29 18:39:35Z edmanm $ 00014 ** \brief Common string manipulation functions 00015 */ 00016 00017 #ifndef _STRINGUTIL_H 00018 #define _STRINGUTIL_H 00019 00020 #include <QStringList> 00021 #include <QHash> 00022 #include <QDateTime> 00023 00024 00025 /** Creates a QStringList from the array of C strings. */ 00026 QStringList char_array_to_stringlist(char **arr, int len); 00027 00028 /** Ensures all characters in str are in validChars. If a character appears 00029 * in str but not in validChars, it will be removed and the resulting 00030 * string returned. */ 00031 QString ensure_valid_chars(const QString &str, const QString &validChars); 00032 00033 /** Scrubs an email address by replacing "@" with " at " and "." with " dot ". */ 00034 QString scrub_email_addr(const QString &email); 00035 00036 /** Conditionally assigns errmsg to string if str is not null and returns 00037 * false. */ 00038 bool err(QString *str, const QString &errmsg); 00039 00040 /** Wraps <b>str</b> at <b>width</b> characters wide, using <b>sep</b> as the 00041 * word separator (" ", for example), and placing the line ending <b>le</b> at 00042 * the end of each line, except the last.*/ 00043 QString string_wrap(const QString &str, int width, 00044 const QString &sep = QString(" "), 00045 const QString &le = QString("\n")); 00046 00047 /** Encodes the bytes in <b>buf</b> as an uppercase hexadecimal string and 00048 * returns the result. This function is derived from base16_encode() in Tor's 00049 * util.c. See LICENSE for details on Tor's license. */ 00050 QString base16_encode(const QByteArray &buf); 00051 00052 /** Given a string <b>str</b>, this function returns a quoted string with all 00053 * '"' and '\' characters escaped with a single '\'. */ 00054 QString string_escape(const QString &str); 00055 00056 /** Given a quoted string <b>str</b>, this function returns an unquoted, 00057 * unescaped string. <b>str</b> must start and end with an unescaped quote. */ 00058 QString string_unescape(const QString &str, bool *ok = 0); 00059 00060 /** Parses a series of space-separated key[=value|="value"] tokens from 00061 * <b>str</b> and returns the mappings in a QHash. If <b>str</b> was unable 00062 * to be parsed, <b>ok</b> is set to false. */ 00063 QHash<QString,QString> string_parse_keyvals(const QString &str, bool *ok = 0); 00064 00065 /** Parses a series of command line arguments from <b>str</b>. If <b>str</b> 00066 * was unable to be parsed, <b>ok</b> is set to false. */ 00067 QStringList string_parse_arguments(const QString &str, bool *ok = 0); 00068 00069 /** Formats the list of command line arguments in <b>args</b> as a string. 00070 * Arguments that contain ' ', '\', or '"' tokens will be escaped and wrapped 00071 * in double quotes. */ 00072 QString string_format_arguments(const QStringList &args); 00073 00074 /** Returns true if <b>str</b> is a valid hexademical string. Returns false 00075 * otherwise. */ 00076 bool string_is_hex(const QString &str); 00077 00078 /** Returns a human-readable description of the time elapsed given by 00079 * <b>seconds</b>, broken down into days, hours, minutes and seconds. */ 00080 QString string_format_uptime(quint64 seconds); 00081 00082 /** Returns a string representation of <b>date</b> formatted according to 00083 * "yyyy-MM-dd HH:mm:ss". */ 00084 QString string_format_datetime(const QDateTime &date); 00085 00086 /** Returns a string representation of <b>bytes</b> with the appropriate 00087 * (localized) suffix of either "B/s", "KB/s", "MB/s" or "GB/s". */ 00088 QString string_format_bandwidth(quint64 bytes); 00089 00090 #endif 00091