| 1 | /* |
|---|
| 2 | * QuteCom, a voice over Internet phone |
|---|
| 3 | * Copyright (C) 2010 Mbdsys |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program 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 |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include <util/StringList.h> |
|---|
| 21 | |
|---|
| 22 | #include <util/String.h> |
|---|
| 23 | #include <util/Logger.h> |
|---|
| 24 | |
|---|
| 25 | using namespace std; |
|---|
| 26 | |
|---|
| 27 | StringList StringList::null; |
|---|
| 28 | |
|---|
| 29 | StringList::StringList(const std::list<std::string> & strList) { |
|---|
| 30 | for (std::list<string>::const_iterator it = strList.begin(); |
|---|
| 31 | it != strList.end(); ++it) { |
|---|
| 32 | |
|---|
| 33 | push_back(*it); |
|---|
| 34 | } |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | StringList::StringList() { |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | StringList::operator std::list<std::string>() { |
|---|
| 41 | std::list<std::string> strList; |
|---|
| 42 | for (unsigned i = 0; i < size(); i++) { |
|---|
| 43 | strList.push_back((*this)[i]); |
|---|
| 44 | } |
|---|
| 45 | return strList; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | std::string StringList::operator[](unsigned i) const { |
|---|
| 49 | if (i >= size()) { |
|---|
| 50 | return String::null; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | return List<std::string>::operator[](i); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | void StringList::operator+=(const std::string & str) { |
|---|
| 57 | List<std::string>::operator+=(str); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | void StringList::operator+=(const StringList & strList) { |
|---|
| 61 | for (unsigned i = 0; i < strList.size(); i++) { |
|---|
| 62 | (*this) += strList[i]; |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | unsigned StringList::contains(const std::string & str, bool caseSensitive) const { |
|---|
| 67 | unsigned result = 0; |
|---|
| 68 | for (unsigned i = 0; i < size(); i++) { |
|---|
| 69 | String tmp1 = str; |
|---|
| 70 | String tmp2 = (*this)[i]; |
|---|
| 71 | if (!caseSensitive) { |
|---|
| 72 | tmp1 = tmp1.toLowerCase(); |
|---|
| 73 | tmp2 = tmp2.toLowerCase(); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | if (tmp1 == tmp2) { |
|---|
| 77 | ++result; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | return result; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | void StringList::sort(SortingOrder order) { |
|---|
| 85 | switch(order) { |
|---|
| 86 | case Ascendant: |
|---|
| 87 | std::sort(begin(), end()); |
|---|
| 88 | break; |
|---|
| 89 | |
|---|
| 90 | case Descendant: |
|---|
| 91 | std::sort(begin(), end(), StringCompareDescendant()); |
|---|
| 92 | break; |
|---|
| 93 | |
|---|
| 94 | default: |
|---|
| 95 | LOG_FATAL("unknown sorting order=" + String::fromNumber(order)); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | std::string StringList::join(const std::string & separator) const { |
|---|
| 100 | std::string joinedString; |
|---|
| 101 | for (unsigned i = 0; i < size(); i++) { |
|---|
| 102 | //Last token |
|---|
| 103 | if (i == (size() - 1)) { |
|---|
| 104 | joinedString += (*this)[i]; |
|---|
| 105 | } |
|---|
| 106 | else { |
|---|
| 107 | joinedString += (*this)[i] + separator; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | return joinedString; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void StringList::removeDuplicatedStrings() { |
|---|
| 114 | for (unsigned i = 0; i < size(); i++) { |
|---|
| 115 | std::string tmp = (*this)[i]; |
|---|
| 116 | for (unsigned j = 0; j < size(); j++) { |
|---|
| 117 | if (tmp == (*this)[j] && i != j) { |
|---|
| 118 | remove(tmp); |
|---|
| 119 | |
|---|
| 120 | //Restarts the entire loop |
|---|
| 121 | i = 0; |
|---|
| 122 | break; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | std::string StringList::toString(const std::string & separator) const { |
|---|
| 129 | std::string result; |
|---|
| 130 | |
|---|
| 131 | for (const_iterator it = begin(); it != end(); ++it) { |
|---|
| 132 | if (it != begin()) { |
|---|
| 133 | result += separator; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | result += *it; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | return result; |
|---|
| 140 | } |
|---|