00001 // 00002 // StringList.cc 00003 // 00004 // StringList: Specialized List containing String objects. 00005 // 00006 // Part of the ht://Dig package <http://www.htdig.org/> 00007 // Copyright (c) 1999, 2000, 2001 The ht://Dig Group 00008 // For copyright details, see the file COPYING in your distribution 00009 // or the GNU General Public License version 2 or later 00010 // <http://www.gnu.org/copyleft/gpl.html> 00011 // 00012 // $Id: StringList_8cc-source.html,v 1.1 2008年06月08日 10:12:56 sebdiaz Exp $ 00013 // 00014 00015 #ifdef HAVE_CONFIG_H 00016 #include "config.h" 00017 #endif /* HAVE_CONFIG_H */ 00018 00019 #include "StringList.h" 00020 #include "htString.h" 00021 #include "List.h" 00022 00023 #include <stdlib.h> 00024 00025 00026 //***************************************************************************** 00027 // StringList::StringList() 00028 // 00029 StringList::StringList() 00030 { 00031 } 00032 00033 //***************************************************************************** 00034 // int StringList::Create(const char *str, char *sep) 00035 // 00036 int StringList::Create(const char *str, const char *sep) 00037 { 00038 String word; 00039 00040 while (str && *str) 00041 { 00042 if (strchr(sep, *str)) 00043 { 00044 if (word.length()) 00045 { 00046 List::Add(new String(word)); 00047 word = 0; 00048 } 00049 } 00050 else 00051 word << *str; 00052 str++; 00053 } 00054 00055 // 00056 // Add the last word to the list 00057 // 00058 if (word.length()) 00059 List::Add(new String(word)); 00060 return Count(); 00061 } 00062 00063 00064 //***************************************************************************** 00065 // int StringList::Create(const char *str, char sep) 00066 // 00067 int StringList::Create(const char *str, char sep) 00068 { 00069 String word; 00070 00071 while (str && *str) 00072 { 00073 if (*str == sep) 00074 { 00075 if (word.length()) 00076 { 00077 List::Add(new String(word)); 00078 word = 0; 00079 } 00080 } 00081 else 00082 word << *str; 00083 str++; 00084 } 00085 00086 // 00087 // Add the last word to the list 00088 // 00089 if (word.length()) 00090 List::Add(new String(word)); 00091 return Count(); 00092 } 00093 00094 00095 //***************************************************************************** 00096 // char *StringList::operator [] (int n) 00097 // 00098 char *StringList::operator [] (int n) 00099 { 00100 String *str = (String *) Nth(n); 00101 if (str) 00102 return str->get(); 00103 else 00104 return 0; 00105 } 00106 00107 00108 //***************************************************************************** 00109 // void StringList::Add(char *str) 00110 // 00111 void StringList::Add(char *str) 00112 { 00113 List::Add(new String(str)); 00114 } 00115 00116 00117 //***************************************************************************** 00118 // void StringList::Assign(char *str, int pos) 00119 // 00120 void StringList::Assign(char *str, int pos) 00121 { 00122 List::Assign(new String(str), pos); 00123 } 00124 00125 //***************************************************************************** 00126 // void StringList::Insert(char *str, int pos) 00127 // 00128 void StringList::Insert(char *str, int pos) 00129 { 00130 List::Insert(new String(str), pos); 00131 } 00132 00133 static int StringCompare(const void *a, const void *b) 00134 { 00135 String *sa, *sb; 00136 00137 sa = *((String **) a); 00138 sb = *((String **) b); 00139 00140 return strcmp(sa->get(), sb->get()); 00141 } 00142 00143 00144 //***************************************************************************** 00145 // void StringList::Sort(int direction) 00146 // 00147 void StringList::Sort(int) 00148 { 00149 String **array = new String*[Count()]; 00150 int i; 00151 int n = Count(); 00152 00153 ListCursor cursor; 00154 00155 Start_Get(cursor); 00156 Object *obj; 00157 for(i = 0; i < n && (obj = Get_Next(cursor)); i++) { 00158 array[i] = (String*)obj; 00159 } 00160 00161 qsort((char *) array, (size_t) n, (size_t) sizeof(String *), 00162 StringCompare); 00163 00164 Release(); 00165 00166 for (i = 0; i < n; i++) 00167 { 00168 List::Add(array[i]); 00169 } 00170 00171 delete array; 00172 } 00173 00174 String StringList::Join(char sep) const 00175 { 00176 String str; 00177 int i; 00178 00179 for (i=0; i < number; i++) 00180 { 00181 if (str.length()) 00182 str.append(sep); 00183 str.append(*((const String *) Nth(i))); 00184 } 00185 return str; 00186 }