00001 /**************************************************************** 00002 * Vidalia is distributed under the following license: 00003 * 00004 * Copyright (C) 2006, Matt Edman, Justin Hipple 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 ****************************************************************/ 00021 00022 /** 00023 * \file string.cpp 00024 * \version $Id: string.cpp 1256 2006-10-01 23:40:24Z edmanm $ 00025 * \brief Common string manipulation functions 00026 */ 00027 00028 #include "string.h" 00029 00030 00031 /** Create a QStringList from the array of C-style strings. */ 00032 QStringList 00033 char_array_to_stringlist(char **arr, int len) 00034 { 00035 QStringList list; 00036 for (int i = 0; i < len; i++) { 00037 list << QString(arr[i]); 00038 } 00039 return list; 00040 } 00041 00042 /** Conditionally assigns errmsg to str if str is not null and returns false. 00043 * This is a seemingly pointless function, but it saves some messiness in 00044 * methods whose QString *errmsg parameter is optional. */ 00045 bool 00046 err(QString *str, QString errmsg) 00047 { 00048 if (str) { 00049 *str = errmsg; 00050 } 00051 return false; 00052 } 00053 00054 /** Ensures all characters in str are in validChars. If a character appears 00055 * in str but not in validChars, it will be removed and the resulting 00056 * string returned. */ 00057 QString 00058 ensure_valid_chars(QString str, QString validChars) 00059 { 00060 QString out = str; 00061 for (int i = 0; i < str.length(); i++) { 00062 QChar c = str.at(i); 00063 if (validChars.indexOf(c) < 0) { 00064 out.remove(c); 00065 } 00066 } 00067 return out; 00068 } 00069 00070 /** Scrubs an email address by replacing "@" with " at " and "." with " dot ". */ 00071 QString 00072 scrub_email_addr(QString email) 00073 { 00074 QString scrubbed = email; 00075 scrubbed = scrubbed.replace("@", " at "); 00076 scrubbed = scrubbed.replace(".", " dot "); 00077 return scrubbed; 00078 } 00079 00080 /** Wraps <b>str</b> at <b>width</b> characters wide, using <b>sep</b> as the 00081 * word separator (" ", for example), and placing the line ending <b>le</b> at 00082 * the end of each line, except the last. */ 00083 QString 00084 string_wrap(QString str, int width, QString sep, QString le) 00085 { 00086 QString wrapped; 00087 int pos, nextsep, wordlen, n; 00088 int seplen = sep.length(); 00089 00090 if (str.length() < width) { 00091 return str; 00092 } 00093 00094 pos = 0; 00095 n = width; 00096 while (pos < str.length()) { 00097 /* Get the length of a "word" */ 00098 nextsep = str.indexOf(sep, pos); 00099 if (nextsep < 0) { 00100 nextsep = str.length(); 00101 } 00102 wordlen = nextsep-pos; 00103 00104 /* Check if there is room for the word on this line */ 00105 if (wordlen > n) { 00106 /* Create a new line */ 00107 wrapped.append(le); 00108 n = width; 00109 } 00110 00111 /* Add the word to the current line */ 00112 wrapped.append(str.mid(pos, wordlen+seplen)); 00113 n = n - wordlen - seplen; 00114 pos += wordlen + seplen; 00115 } 00116 return wrapped.trimmed(); 00117 } 00118