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