D:/work/code_by/ListTools/ListTools.cpp

查看本檔案說明文件.
00001 /*===========================================================================
00002 實作BCB中的TList、TStringList和TIniFile元件的部份功能。
00003 ===========================================================================*/
00004 #include <stdio.h>
00005 #include <string.h>
00006 #include "ListTools.h"
00007 
00008 /*===========================================================================
00009 功能:建構子
00010 ===========================================================================*/
00011 TMyList::TMyList()
00012 {
00013   freeItems = false;  //清除時只清掉指標,不清掉指標所指的記憶體
00014   Head = Tail = NULL;
00015   count = ptrIndex = 0;
00016 }
00017 
00018 /*===========================================================================
00019 功能:解構子
00020 ===========================================================================*/
00021 TMyList::~TMyList()
00022 {
00023   Clear();  //清除所有項目
00024 }
00031 /*===========================================================================
00032 功能:增加一個項目,只有將項目的指標記錄下來
00033 傳入:item為欲增加的項目
00034 傳出:新增項目的索引
00035 ===========================================================================*/
00036 int TMyList::Add(void *item)
00037 {
00038   Ptr = new TMyListItem();  //建立一個項目
00039   Ptr->item = item;
00040   Ptr->Next = NULL;
00041   if (Tail)
00042   {
00043     Tail->Next = Ptr;  //附加在最後
00044     Tail = Ptr;
00045   }
00046   else
00047     Head = Tail = Ptr;
00048   return ptrIndex = count++;  //項目個數加1
00049 }
00055 /*===========================================================================
00056 功能:清除所有項目
00057 ===========================================================================*/
00058 void TMyList::Clear(void)
00059 {
00060   TMyListItem *ListItem;
00061 
00062   Ptr = Head;
00063   while (Ptr)
00064   {
00065     ListItem = Ptr->Next;
00066     if (freeItems)  //TMyStringList才是true
00067       delete [] (char *)Ptr->item;  //清掉項目所佔的記憶體
00068     delete Ptr;  //清掉指標
00069     Ptr = ListItem;
00070   }
00071   Head = Tail = NULL;
00072   count = ptrIndex = 0;
00073 }
00080 /*===========================================================================
00081 功能:刪除某一個項目
00082 傳入:index為被刪除項目的索引
00083 ===========================================================================*/
00084 void TMyList::Delete(int index)
00085 {
00086   TMyListItem *DelItem;
00087 
00088   if (index >= count || index < 0)  //索引超出範圍
00089     return;
00090   if (index == 0)  //刪除第一個項目
00091   {
00092     DelItem = Head;
00093     Head = Head->Next;
00094     ptrIndex = 0;
00095     if (count == 1)  //刪除的是唯一的項目
00096       Tail = NULL;
00097   }
00098   else
00099   {
00100     if (ptrIndex >= index)
00101     {
00102       Ptr = Head;
00103       ptrIndex = 0;
00104     }
00105     for (;ptrIndex < index - 1; ptrIndex++)  //找到要刪除的項目
00106       Ptr = Ptr->Next;
00107     DelItem = Ptr->Next;
00108     Ptr->Next = DelItem->Next;
00109     if (index == count - 1)  //刪除的是最後一個項目
00110       Tail = Ptr;
00111   }
00112   if (freeItems)  //TMyStringList才是true
00113     delete [] (char *)DelItem->item;  //清掉項目所佔的記憶體
00114   delete DelItem;  //清除指標
00115   count--;
00116 }
00123 /*===========================================================================
00124 功能:將一個項目插入某一個位置,只有將指標記錄下來
00125 傳入:index為插入的位置
00126       item為欲插入的項目
00127 ===========================================================================*/
00128 void TMyList::Insert(int index, void *item)
00129 {
00130   TMyListItem *ListItem = new TMyListItem();
00131 
00132   if (index > count)  //檢查索引範圍
00133     index = count;
00134   else if (index < 0)  //檢查索引範圍
00135     index = 0;
00136   ListItem->item = item;
00137   if (index == 0)  //插入第一個
00138   {
00139     ListItem->Next = Head;
00140     Head = ListItem;
00141     ptrIndex++;
00142     if (count == 0)  //原本沒有項目
00143       Ptr = Tail = ListItem;
00144   }
00145   else
00146   {
00147     if (ptrIndex >= index)
00148     {
00149       Ptr = Head;
00150       ptrIndex = 0;
00151     }
00152     for (;ptrIndex < index - 1; ptrIndex++)  //找出要插入的位置
00153       Ptr = Ptr->Next;
00154     ListItem->Next = Ptr->Next;
00155     Ptr->Next = ListItem;
00156     if (index == count)  //插入最後一個位置
00157       Tail = ListItem;
00158   }
00159   count++;
00160 }
00166 /*===========================================================================
00167 功能:傳回某一個項目
00168 傳入:index為欲傳回項目的索引
00169 ===========================================================================*/
00170 void *TMyList::Items(int index)
00171 {
00172   if (index >= count || index < 0)  //索引超出範圍
00173     return NULL;
00174   if (ptrIndex > index || index == 0)
00175   {
00176     Ptr = Head;
00177     ptrIndex = 0;
00178   }
00179   for (;ptrIndex < index; ptrIndex++)  //找出該項目
00180     Ptr = Ptr->Next;
00181   return Ptr->item;
00182 }
00183 
00184 /*===========================================================================
00185 功能:建構子
00186 ===========================================================================*/
00187 TMyStringList::TMyStringList() : TMyList()
00188 {
00189   freeItems = true;  //清除指標前先清除指標所指向的記憶體
00190   itemName[0] = '\0';
00191 }
00198 /*===========================================================================
00199 功能:增加一個項目的複製
00200 傳入:item為欲增加的項目
00201 傳出:新增項目的索引
00202 ===========================================================================*/
00203 int TMyStringList::Add(char *item)
00204 {
00205   char *newItem = new char[strlen(item) + 1];
00206   strcpy(newItem, item);
00207   return TMyList::Add(newItem);
00208 }
00215 /*===========================================================================
00216 功能:將一個項目的複製插入某一個位置
00217 傳入:index為插入的位置
00218       item為欲插入的項目
00219 ===========================================================================*/
00220 void TMyStringList::Insert(int index, char *item)
00221 {
00222   char *newItem = new char[strlen(item) + 1];
00223   strcpy(newItem, item);
00224   TMyList::Insert(index, newItem);
00225 }
00232 /*===========================================================================
00233 功能:傳回某一個名字
00234 傳入:index為欲傳回名字的索引
00235 傳出:字串
00236 ===========================================================================*/
00237 char *TMyStringList::Names(int index)
00238 {
00239   char *str, *p;
00240 
00241   str = Strings(index);
00242   if (str == NULL)
00243     return NULL;
00244   if ((p = strchr(str, '=')) == NULL)  //找出'='的位置
00245     return strcpy(itemName, str);
00246   itemName[p - str] = '\0';
00247   return strncpy(itemName, str, p - str);
00248 }
00256 /*===========================================================================
00257 功能:設定某一個名稱的值,若此名稱不存在,則新增此項目
00258 傳入:name為名稱
00259       item為值
00260 傳出:此項目的索引
00261 ===========================================================================*/
00262 int TMyStringList::SetValue(char *name, char *item)
00263 {
00264   int i;
00265   char str[201];
00266 
00267   sprintf(str, "%s=%s", name, item);
00268   for (i = 0; i < count; i++)
00269     if (strcmp(Names(i), name) == 0)  //找到該項目
00270       break;
00271   if (i < count)  //找到該項目
00272   {
00273     Delete(i);  //刪除該項目
00274     Insert(i, str);  //在原位置插入新項目
00275     return i;
00276   }
00277   else
00278     return Add(str);  //新增一個項目
00279 }
00286 /*===========================================================================
00287 功能:傳回某一個字串
00288 傳入:index為欲傳回字串的索引
00289 傳出:字串
00290 ===========================================================================*/
00291 char *TMyStringList::Strings(int index)
00292 {
00293   return (char *)Items(index);
00294 }
00301 /*===========================================================================
00302 功能:傳回某一個名字所代表的值
00303 傳入:name為名字索引
00304 傳出:該名字所代表的值,如果找不到名字,則傳回空字串"\0"
00305 ===========================================================================*/
00306 char *TMyStringList::Values(char *name)
00307 {
00308   static char *emptyStr = "\0";
00309   char *tempc = NULL;
00310   int i;
00311 
00312   for (i = 0; i < count; i++)
00313     if (strcmp(Names(i), name) == 0)  //找到名字相符的項目
00314     {
00315       tempc = strchr(Strings(i), '=');  //找出'='的位置
00316       break;
00317     }
00318   return tempc != NULL? (tempc + 1) : emptyStr;
00319 }
00320 

產生日期:Tue Jul 11 11:52:19 2006, 專案:cbmr, 產生器:  doxygen 1.4.7