bsp
|
00001 #pragma once 00002 /* shitty code yuck */ 00003 #include "global.h" 00004 00005 //mini-list 00006 template <typename T1> 00007 struct listitem 00008 { 00009 T1 data; 00010 listitem *next; 00011 }; 00012 template <typename T2> 00013 class list 00014 { 00015 public: 00016 inline list(bool delete_data = true) : items(0), tail(0), count(0), delete_data(delete_data) {} 00017 inline ~list() 00018 { 00019 reset<T2>(); 00020 } 00021 template <typename T3> 00022 inline void reset() 00023 { 00024 listitem<T3> *item = items, *tmp; 00025 for(; item; item = tmp) 00026 { 00027 if(delete_data) 00028 delete item->data; 00029 tmp = item->next; 00030 delete item; 00031 } 00032 this->count = 0; 00033 this->tail = 0; 00034 this->items = 0; 00035 } 00036 template <typename T4> 00037 inline void add(T4 data) 00038 { 00039 listitem<T4> *item = new listitem<T4>; 00040 item->data = data; 00041 item->next = 0; 00042 if(!items) 00043 { 00044 items = tail = item; 00045 } 00046 else 00047 { 00048 tail->next = item; 00049 tail = item; 00050 } 00051 ++count; 00052 } 00053 // addsorted specialized for char* 00054 // template <typename T> 00055 // inline void addsorted(T) { syserror("ERROR: "__FUNCTION__": T is not char*"); } 00056 typedef listitem< char* > licharptr; 00057 inline void addsorted(char* data) 00058 { 00059 licharptr *item = new licharptr; 00060 item->data = data; 00061 item->next = 0; 00062 if(!items) 00063 { 00064 items = tail = item; 00065 } 00066 else 00067 { 00068 licharptr *ATLS_iter = items; 00069 00070 /* add item to list with one or more items */ 00071 licharptr **ATLS_previns = &items; /* insert point, start at list head */ 00072 00073 for(; ATLS_iter; ATLS_iter = ATLS_iter->next) 00074 { 00075 if(compare_strings_int_parts(item->data, ATLS_iter->data) <= 0) 00076 { 00077 item->next = ATLS_iter; /* next item is current iter item */ 00078 (*ATLS_previns) = item; /* previous item points to new item */ 00079 break; 00080 } 00081 if(!ATLS_iter->next) /* before next... */ 00082 { 00083 ATLS_iter->next = item; 00084 break; 00085 } 00086 ATLS_previns = &ATLS_iter->next; /* remember insert point */ 00087 } 00088 00089 00090 if(!item->next) 00091 tail = item; 00092 } 00093 ++count; 00094 } 00095 00096 //var 00097 listitem<T2> *items, *tail; 00098 int count; 00099 bool delete_data; 00100 }; 00101 00102 #ifndef min 00103 #define min(a, b) ((a) < (b) ? (a) : (b)) 00104 #endif 00105 #ifndef max 00106 #define max(a, b) ((a) > (b) ? (a) : (b)) 00107 #endif 00108 00109 class Str 00110 { 00111 public: 00112 char *str; 00113 size_t length; 00114 //create a new string, length 00115 Str(char *fmt,...) 00116 { 00117 va_list vargs; 00118 va_start(vargs, fmt); 00119 length = _vscprintf(fmt, vargs); 00120 str = new char[length + 1]; 00121 vsprintf(str,fmt,vargs); 00122 va_end(vargs); 00123 } 00124 Str(size_t minsize, char *fmt,...) 00125 { 00126 va_list vargs; 00127 va_start(vargs, fmt); 00128 length = max(minsize, (size_t) _vscprintf(fmt, vargs)); 00129 str = new char[length + 1]; 00130 vsprintf(str,fmt,vargs); 00131 va_end(vargs); 00132 } 00133 char *detach() 00134 { 00135 char *retval=str; 00136 str=0; 00137 return retval; 00138 } 00139 ~Str() 00140 { 00141 delete[] str; 00142 } 00143 operator char *() 00144 { 00145 return str; 00146 } 00147 };