kstringlist.h
Go to the documentation of this file.00001
00004 #ifndef _FBXSDK_KSTRING_LIST_H_
00005 #define _FBXSDK_KSTRING_LIST_H_
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <kbaselib_h.h>
00042
00043 #include <stdlib.h>
00044
00045 #if defined(KARCH_ENV_MACOSX_CFM)
00046 #include <extras.h>
00047 #endif
00048
00049
00050 #include <klib/karrayul.h>
00051
00052 #include <isocpp_conformant.h>
00053
00054
00055
00056 #include <klib/t-str.h>
00057 #include <klib/kstring.h>
00058 #include <kbaselib_nsbegin.h>
00059
00060 class KBASELIB_DLL KStringListItem {
00061 public:
00062 KString mString;
00063 kReference mReference;
00064 KStringListItem( ) { mReference = 0;}
00065 KStringListItem( char* pString, kReference pRef=0 ) { mString = pString; mReference = pRef;}
00066 };
00067
00068 inline int CompareKStringList(const void *E1,const void *E2)
00069 {
00070 return t_stricmp((*(KStringListItem *)E1).mString.Buffer(),(*(KStringListItem *)E2).mString.Buffer());
00071 }
00072
00073 inline int CompareCaseSensitiveKStringList(const void *E1,const void *E2)
00074 {
00075 return strcmp((*(KStringListItem *)E1).mString.Buffer() ,(*(KStringListItem *)E2).mString.Buffer());
00076 }
00077
00078 typedef class KArrayTemplate<KStringListItem *> KArrayStringListItem;
00079
00080 template <class Type> class KStringListTemplate {
00081 protected:
00082 KArrayTemplate<Type *> mList;
00083
00084 public:
00085 int AddItem( Type *pItem ) { return mList.Add( pItem ); }
00086 int InsertItemAt( int pIndex, Type *pItem ) { return mList.InsertAt( pIndex, pItem ); }
00087 Type *GetItemAt( int pIndex ) { return mList[pIndex]; }
00088 int FindItem( Type *pItem ) { return mList.Find( pItem ); }
00089
00090 public :
00091 KStringListTemplate()
00092 {
00093 }
00094
00095 virtual ~KStringListTemplate() { Clear(); }
00096
00097 void RemoveLast() { RemoveAt( mList.GetCount()-1 ); }
00098 inline int GetCount() { return mList.GetCount(); }
00099 KString &operator[](int pIndex) { return mList[pIndex]->mString; }
00100 kReference GetReferenceAt(int pIndex) { return mList[pIndex]->mReference; }
00101 void SetReferenceAt(int pIndex, kReference pRef) { mList[pIndex]->mReference = pRef; }
00102 char* GetStringAt(int pIndex) { if (pIndex<mList.GetCount()) return mList[pIndex]->mString.Buffer(); else return NULL; }
00103 virtual bool SetStringAt(int pIndex,char* pString)
00104 {
00105 if (pIndex<mList.GetCount())
00106 {
00107 mList[pIndex]->mString = pString;
00108 return true;
00109 } else return false;
00110 }
00111
00112 int Find( Type &pItem )
00113 {
00114 for (int Count=0; Count<mList.GetCount(); Count++) {
00115 if (mList[Count]==&pItem) {
00116 return Count;
00117 }
00118 }
00119 return -1;
00120 }
00121
00122 int FindIndex( kReference pReference )
00123 {
00124 for (int Count=0; Count<mList.GetCount(); Count++) {
00125 if (mList[Count]->mReference==pReference) {
00126 return Count;
00127 }
00128 }
00129 return -1;
00130 }
00131
00132 int FindIndex( char* pString )
00133 {
00134 for (int Count=0; Count<mList.GetCount(); Count++) {
00135 if (mList[Count]->mString==pString) {
00136 return Count;
00137 }
00138 }
00139 return -1;
00140 }
00141
00142 kReference FindReference( char* pString )
00143 {
00144 int Index = FindIndex( pString );
00145 if (Index!=-1) {
00146 return mList[Index]->mReference;
00147 }
00148 return NULL;
00149 }
00150
00151 bool Remove ( Type &pItem )
00152 {
00153 int Index = Find( pItem );
00154 if (Index>=0) {
00155 RemoveAt( Index );
00156 return true;
00157 }
00158 return false;
00159 }
00160
00161 bool Remove ( char* pString )
00162 {
00163 int Index = FindIndex( pString );
00164 if (Index>=0) {
00165 RemoveAt( Index );
00166 return true;
00167 }
00168 return false;
00169 }
00170
00171 bool RemoveIt ( Type pItem )
00172 {
00173 int Index = Find( pItem );
00174 if (Index>=0) {
00175 RemoveAt( Index );
00176 return true;
00177 }
00178 return false;
00179 }
00180
00181 void Sort( )
00182 {
00183 qsort( &(mList.GetArray()[0]),mList.GetCount(),sizeof(KStringListItem *),CompareKStringList );
00184 }
00185
00186 void* FindEqual( char* pString)
00187 {
00188 KStringListItem Key(pString);
00189
00190 if (mList.GetCount() != 0)
00191 {
00192 return bsearch ( &Key, &(mList.GetArray()[0]),mList.GetCount(),sizeof(KStringListItem *),CompareKStringList );
00193 }
00194 else
00195 {
00196 return NULL ;
00197 }
00198 }
00199
00200 void* FindCaseSensitive(char * pString)
00201 {
00202 KStringListItem Key(pString);
00203
00204 if (mList.GetCount() != 0)
00205 {
00206 return bsearch ( &Key, &(mList.GetArray()[0]),mList.GetCount(),sizeof(KStringListItem *), CompareCaseSensitiveKStringList);
00207 }
00208 else
00209 {
00210 return NULL ;
00211 }
00212
00213 }
00214
00215
00216 int Add( char* pString, kReference pItem=0 )
00217 {
00218 return InsertAt( mList.GetCount(),pString,pItem );
00219 }
00220
00221 virtual int InsertAt( int pIndex, char* pString, kReference pItem=0 )
00222 {
00223 return mList.InsertAt( pIndex,new Type( pString,(kReference)pItem ));
00224 }
00225
00226 virtual void RemoveAt( int pIndex )
00227 {
00228 delete mList[pIndex]; mList.RemoveAt( pIndex );
00229 }
00230
00231 virtual void Clear()
00232 {
00233 for (int Count=0; Count<mList.GetCount(); Count++ ) {
00234 delete mList[Count];
00235 }
00236 mList.Clear();
00237 }
00238
00239 virtual void GetText(KString& pText)
00240 {
00241 int Count;
00242 for (Count=0; Count<mList.GetCount(); Count++)
00243 {
00244 pText += mList[Count]->mString;
00245 if (Count<mList.GetCount()-1)
00246 {
00247 pText += _T("~");
00248 }
00249 }
00250 }
00251
00252
00253 virtual int SetText(char* pList)
00254 {
00255 int Pos=0, OldPos = 0;
00256 int LastIndex=0;
00257 KString Name=pList;
00258
00259 Clear();
00260 for (Pos=0; Name.Buffer()[Pos]!=0; Pos++) {
00261 if (Name.Buffer()[Pos]==_T('~')) {
00262 Name.Buffer()[Pos]=0;
00263 LastIndex = Add(Name.Buffer()+OldPos);
00264 OldPos=Pos+1;
00265 }
00266 }
00267
00268 if(OldPos != Pos)
00269 {
00270 LastIndex = Add(Name.Buffer()+OldPos);
00271 }
00272 return LastIndex;
00273 }
00274
00275
00276 };
00277
00278 typedef class KBASELIB_DLL KStringListTemplate< KStringListItem > KStringListTemplateItem;
00279
00280 class KBASELIB_DLL KStringList : public KStringListTemplate< KStringListItem >
00281 {
00282 public :
00283 KStringList();
00284 KStringList( const KStringList &pOriginal );
00285 void CopyFrom( const KStringList *pOriginal );
00286
00287 KStringList& operator=(const KStringList& pOriginal);
00288 };
00289
00290 typedef KStringList *HKStringList;
00291
00292 #include <kbaselib_nsend.h>
00293
00294 #endif