- Timestamp:
- 05/29/18 16:51:14 (6 years ago)
- Location:
- cpp/frams/util
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/util/callbacks.cpp
r286 r793 9 9 int MemberCallbackNode::equals(CallbackNode*n) 10 10 { 11 if (n==this) return 1;12 MemberCallbackNode *classok=dynamic_cast<MemberCallbackNode*>(n);13 if (!classok) return 0;14 return ((userdata==classok->userdata)&&(object==classok->object)&&(member==classok->member));11 if (n == this) return 1; 12 MemberCallbackNode *classok = dynamic_cast<MemberCallbackNode*>(n); 13 if (!classok) return 0; 14 return ((userdata == classok->userdata) && (object == classok->object) && (member == classok->member)); 15 15 } 16 16 #endif … … 18 18 int FunctionCallbackNode::equals(CallbackNode*n) 19 19 { 20 if (n==this) return 1;21 FunctionCallbackNode *classok=dynamic_cast<FunctionCallbackNode*>(n);22 if (!classok) return 0;23 return ((userdata==classok->userdata)&&(fun==classok->fun));20 if (n == this) return 1; 21 FunctionCallbackNode *classok = dynamic_cast<FunctionCallbackNode*>(n); 22 if (!classok) return 0; 23 return ((userdata == classok->userdata) && (fun == classok->fun)); 24 24 } 25 25 26 26 int StatrickCallbackNode::equals(CallbackNode*n) 27 27 { 28 if (n==this) return 1;29 StatrickCallbackNode *classok=dynamic_cast<StatrickCallbackNode*>(n);30 if (!classok) return 0;31 return ((object==classok->object)&&(userdata==classok->userdata)&&(fun==classok->fun));28 if (n == this) return 1; 29 StatrickCallbackNode *classok = dynamic_cast<StatrickCallbackNode*>(n); 30 if (!classok) return 0; 31 return ((object == classok->object) && (userdata == classok->userdata) && (fun == classok->fun)); 32 32 } 33 33 … … 36 36 CallbackNode* Callback::add(CallbackNode*n) 37 37 { 38 SList::operator+=(n);39 return n;38 SList::operator+=(n); 39 return n; 40 40 } 41 41 42 42 void Callback::removeNode(CallbackNode*n) 43 43 { 44 SList::operator-=(n);45 delete n;44 SList::operator-=(n); 45 delete n; 46 46 } 47 47 48 48 void Callback::remove(CallbackNode*node) 49 49 { 50 CallbackNode *n;51 //printf("Hint: removing callbacks (former 'DuoList') is more efficient using removeNode(). (refer to 'callbacks.h')\n");52 for (int i=0;n=(CallbackNode *)operator()(i);i++)53 if (node->equals(n))50 CallbackNode *n; 51 //printf("Hint: removing callbacks (former 'DuoList') is more efficient using removeNode(). (refer to 'callbacks.h')\n"); 52 for (int i = 0; n = (CallbackNode *)operator()(i); i++) 53 if (node->equals(n)) 54 54 { 55 55 SList::operator-=(i); 56 56 delete node; 57 if (n !=node) delete n;57 if (n != node) delete n; 58 58 return; 59 59 } 60 delete node; // tu nie wiem czy na pewno...60 delete node; // tu nie wiem czy na pewno... 61 61 } 62 62 63 63 void Callback::action(intptr_t data) 64 64 { 65 if (size()==0) return;66 SList copy=*this;67 FOREACH(CallbackNode*,n,copy)68 n->action(data);65 if (size() == 0) return; 66 SList copy = *this; 67 FOREACH(CallbackNode*, n, copy) 68 n->action(data); 69 69 } 70 70 71 71 Callback::~Callback() 72 72 { 73 CallbackNode *n;74 for (int i=size()-1;i>=0;i--)73 CallbackNode *n; 74 for (int i = size() - 1; i >= 0; i--) 75 75 { 76 n=(CallbackNode *)operator()(i);77 delete n;78 // todo: zrobic zeby kolejnosc delete callbacknode <-> delete callback nie wplywala na poprawne dzialania79 // blad odkryty 24.01 pokazal, ze deletowanie callbacknodow w ~callback80 // moze powodowac problemy, jezeli obiekty sa usuwane w "zlej" kolejnosci81 // ale na razie tak zostanie76 n = (CallbackNode *)operator()(i); 77 delete n; 78 // todo: zrobic zeby kolejnosc delete callbacknode <-> delete callback nie wplywala na poprawne dzialania 79 // blad odkryty 24.01 pokazal, ze deletowanie callbacknodow w ~callback 80 // moze powodowac problemy, jezeli obiekty sa usuwane w "zlej" kolejnosci 81 // ale na razie tak zostanie 82 82 } 83 83 } -
cpp/frams/util/callbacks.h
r286 r793 15 15 { 16 16 public: 17 virtual ~CallbackNode() {}18 virtual void action(intptr_t calldata)=0;19 virtual int equals(CallbackNode*n) {return (this==n);}17 virtual ~CallbackNode() {} 18 virtual void action(intptr_t calldata) = 0; 19 virtual int equals(CallbackNode*n) { return (this == n); } 20 20 }; 21 21 … … 24 24 class MemberCallbackNode :public CallbackNode 25 25 { 26 void *userdata;27 CallBase *object;28 void (CallBase::*member)(void*,intptr_t);29 30 MemberCallbackNode(CallBase *o,void (CallBase::*m)(void*,intptr_t),void *d):object(o),member(m),userdata(d) {}31 void action(intptr_t calldata) {(object->*member)(userdata,calldata);}32 int equals(CallbackNode*);26 void *userdata; 27 CallBase *object; 28 void (CallBase::*member)(void*, intptr_t); 29 public: 30 MemberCallbackNode(CallBase *o, void (CallBase::*m)(void*, intptr_t), void *d) :object(o), member(m), userdata(d) {} 31 void action(intptr_t calldata) { (object->*member)(userdata, calldata); } 32 int equals(CallbackNode*); 33 33 }; 34 34 #define MEMBERCALLBACK(obj,mem,dat) new MemberCallbackNode((CallBase*)(obj),(void (CallBase::*)(void*,intptr_t))(mem),(void*)(dat)) … … 37 37 class FunctionCallbackNode :public CallbackNode 38 38 { 39 void *userdata;40 void (*fun)(void*,intptr_t);41 42 FunctionCallbackNode(void (*f)(void*,intptr_t),void *d):userdata(d),fun(f) {}43 void action(intptr_t calldata) {(*fun)(userdata,calldata);}44 int equals(CallbackNode*);39 void *userdata; 40 void(*fun)(void*, intptr_t); 41 public: 42 FunctionCallbackNode(void(*f)(void*, intptr_t), void *d) :userdata(d), fun(f) {} 43 void action(intptr_t calldata) { (*fun)(userdata, calldata); } 44 int equals(CallbackNode*); 45 45 }; 46 46 #define FUNCTIONCALLBACK(fun,dat) new FunctionCallbackNode((void (*)(void*,intptr_t))(fun),(void*)(dat)) … … 48 48 class StatrickCallbackNode :public CallbackNode 49 49 { 50 void *object;51 void *userdata;52 void (*fun)(void*,void*,intptr_t);53 54 StatrickCallbackNode(void *o,void (*f)(void*,void*,intptr_t),void *d):object(o),userdata(d),fun(f) {}55 void action(intptr_t calldata) {(*fun)(object,userdata,calldata);}56 int equals(CallbackNode*);50 void *object; 51 void *userdata; 52 void(*fun)(void*, void*, intptr_t); 53 public: 54 StatrickCallbackNode(void *o, void(*f)(void*, void*, intptr_t), void *d) :object(o), userdata(d), fun(f) {} 55 void action(intptr_t calldata) { (*fun)(object, userdata, calldata); } 56 int equals(CallbackNode*); 57 57 }; 58 58 #define STATRICKCALLBACK(obj,name,dat) new StatrickCallbackNode((void*)(obj),(void (*)(void*,void*,intptr_t))STATRICKNAME(name),(void*)(dat)) … … 60 60 /** 61 61 Like in old 'DuoList' you can register for an event giving function pointer 62 62 add(Function* fun, void* anydata) 63 63 'fun' will be called with your pointer as the first argument (void*) 64 64 and event specific value as the second argument (intptr_t) 65 65 fun(void* anydata,intptr_t eventdata) 66 66 67 67 'StatrickCallbackNode' uses static functions to emulate object member calls. 68 68 @see statrick.h 69 69 70 70 Unregistering callbacks: 71 71 The old remove(...) still works, but removeNode() is more efficient. 72 72 To use it you have to store what you get from add(...); 73 74 75 73 CallbackNode* node=thelist.l_add.add(&fun,data); 74 // thelist.l_add.remove(&fun,data); 75 thelist.l_add.removeNode(node); // this is better! 76 76 77 */77 */ 78 78 79 class Callback : protected SList79 class Callback : protected SList 80 80 { 81 81 public: 82 ~Callback(); 83 CallbackNode* add(CallbackNode*n); 84 CallbackNode* add(void (*f)(void*,intptr_t),void *d) 85 {return add(new FunctionCallbackNode(f,d));} 86 void remove(void (*f)(void*,intptr_t),void *d) 87 {remove(new FunctionCallbackNode(f,d));} 88 void remove(CallbackNode*n); 89 void removeNode(CallbackNode*n); 90 void operator+=(void* fun) {add((void (*)(void*,intptr_t))fun,0);} 91 void operator-=(void* fun) {remove((void (*)(void*,intptr_t))fun,0);} 92 void action(intptr_t data); 93 void action() {action(0);} 94 int size() {return SList::size();} 95 void clear() {SList::clear();} 82 ~Callback(); 83 CallbackNode* add(CallbackNode*n); 84 CallbackNode* add(void(*f)(void*, intptr_t), void *d) 85 { 86 return add(new FunctionCallbackNode(f, d)); 87 } 88 void remove(void(*f)(void*, intptr_t), void *d) 89 { 90 remove(new FunctionCallbackNode(f, d)); 91 } 92 void remove(CallbackNode*n); 93 void removeNode(CallbackNode*n); 94 void operator+=(void* fun) { add((void(*)(void*, intptr_t))fun, 0); } 95 void operator-=(void* fun) { remove((void(*)(void*, intptr_t))fun, 0); } 96 void action(intptr_t data); 97 void action() { action(0); } 98 int size() { return SList::size(); } 99 void clear() { SList::clear(); } 96 100 }; 97 101 … … 108 112 /* STCALLBACKDEFC(Class,name) 109 113 110 111 114 | 115 v 112 116 113 114 115 116 */117 #define STATRICKCLASS Class 118 STCALLBACKDEF(name) 119 #undef STATRICKCLASS 120 */ 117 121 118 122 #define CALLBACKARGS void* arg1,intptr_t arg2 119 123 120 124 #endif 121 -
cpp/frams/util/extvalue.h
r639 r793 126 126 ExtObject *o; 127 127 }; 128 paInt& idata() const { return (paInt&)i;};129 double& ddata() const { return (double&)d;};130 ExtObject& odata() const { return *o;};131 SString& sdata() const { return *s;};128 paInt& idata() const { return (paInt&)i; }; 129 double& ddata() const { return (double&)d; }; 130 ExtObject& odata() const { return *o; }; 131 SString& sdata() const { return *s; }; 132 132 #endif 133 133 … … 237 237 void seto(const ExtObject &src) { type = TObj; new(data)ExtObject(src); } 238 238 #else 239 void setrs(const SString &v) { setEmpty();sets(v);}240 void setro(const ExtObject &src) { setEmpty();seto(src);}241 void sets(const SString &v) { type=TString;s=new SString(v);}242 void seto(const ExtObject &src) { type=TObj;o=new ExtObject(src);}239 void setrs(const SString &v) { setEmpty(); sets(v); } 240 void setro(const ExtObject &src) { setEmpty(); seto(src); } 241 void sets(const SString &v) { type = TString; s = new SString(v); } 242 void seto(const ExtObject &src) { type = TObj; o = new ExtObject(src); } 243 243 #endif 244 244 -
cpp/frams/util/hashtable.cpp
r348 r793 7 7 int HashTable::hash(const SString &s) 8 8 { 9 return s.hash()&0x7fffffff;9 return s.hash() & 0x7fffffff; 10 10 } 11 11 12 void HashTable::init(int initsize, float lo)12 void HashTable::init(int initsize, float lo) 13 13 { 14 size=initsize;15 load=lo;16 threshold=(int)(load*(float)size);17 tab=(HashEntry**)calloc(size,sizeof(HashEntry*));18 count=0;19 sync=0;14 size = initsize; 15 load = lo; 16 threshold = (int)(load*(float)size); 17 tab = (HashEntry**)calloc(size, sizeof(HashEntry*)); 18 count = 0; 19 sync = 0; 20 20 } 21 21 22 22 void HashTable::rehash(int newsize) 23 23 { 24 if (newsize==size) return;25 HashEntry **newtab=(HashEntry**)calloc(newsize,sizeof(HashEntry*));26 HashEntry **te=tab,*e,*ne;27 int i;28 for (int s=size;s>0;s--,te++)29 for (e=*te;e;)30 {31 ne=e; e=e->next;32 i=ne->hash%newsize;33 ne->next=newtab[i];34 newtab[i]=ne;35 }36 free(tab);37 tab=newtab;38 size=newsize;39 threshold=int(load*(float)size);40 sync++;24 if (newsize == size) return; 25 HashEntry **newtab = (HashEntry**)calloc(newsize, sizeof(HashEntry*)); 26 HashEntry **te = tab, *e, *ne; 27 int i; 28 for (int s = size; s > 0; s--, te++) 29 for (e = *te; e;) 30 { 31 ne = e; e = e->next; 32 i = ne->hash%newsize; 33 ne->next = newtab[i]; 34 newtab[i] = ne; 35 } 36 free(tab); 37 tab = newtab; 38 size = newsize; 39 threshold = int(load*(float)size); 40 sync++; 41 41 } 42 42 43 43 void HashTable::clear() 44 44 { 45 HashEntry *e,**te,*next;46 int n;47 for (n=size,te=tab;n>0;n--,te++)48 for (e=*te;e;e=next)45 HashEntry *e, **te, *next; 46 int n; 47 for (n = size, te = tab; n > 0; n--, te++) 48 for (e = *te; e; e = next) 49 49 { 50 next =e->next;50 next = e->next; 51 51 delete e; 52 52 } 53 if (tab) free(tab);54 tab=0; size=0;55 sync++;53 if (tab) free(tab); 54 tab = 0; size = 0; 55 sync++; 56 56 } 57 57 58 58 HashTable::~HashTable() 59 59 { 60 clear();60 clear(); 61 61 } 62 62 63 void* HashTable::put(const SString& key, void *value)63 void* HashTable::put(const SString& key, void *value) 64 64 { 65 int h=hash(key); 66 int i=h%size; 67 for (HashEntry *e=tab[i];e;e=e->next) 68 { 69 if (e->key==key) 65 int h = hash(key); 66 int i = h%size; 67 for (HashEntry *e = tab[i]; e; e = e->next) 70 68 { 71 void *v=e->value; 72 e->value=value; 73 return v; 69 if (e->key == key) 70 { 71 void *v = e->value; 72 e->value = value; 73 return v; 74 } 74 75 } 75 } 76 if (count>=threshold) { rehash(2*size+1); i=h%size; } 77 HashEntry *e=new HashEntry(h,key,value); 78 e->next=tab[i]; 79 tab[i]=e; 80 count++; 81 sync++; 82 return 0; 76 if (count >= threshold) { rehash(2 * size + 1); i = h%size; } 77 HashEntry *e = new HashEntry(h, key, value); 78 e->next = tab[i]; 79 tab[i] = e; 80 count++; 81 sync++; 82 return 0; 83 83 } 84 84 85 85 void* HashTable::remove(const SString& key) 86 86 { 87 int i=hash(key)%size; 88 HashEntry **ptr=tab+i,*e; 89 for (;e=*ptr;ptr=&e->next) 90 { 91 if (e->key==key) 87 int i = hash(key) % size; 88 HashEntry **ptr = tab + i, *e; 89 for (; e = *ptr; ptr = &e->next) 92 90 { 93 *ptr=e->next; 94 void *v=e->value; 95 delete e; 96 count--; 97 sync++; 98 return v; 91 if (e->key == key) 92 { 93 *ptr = e->next; 94 void *v = e->value; 95 delete e; 96 count--; 97 sync++; 98 return v; 99 } 99 100 } 100 } 101 return 0; 101 return 0; 102 102 } 103 103 104 104 void* HashTable::remove(HashEntryIterator& it) 105 105 { 106 if (!it.entry) return 0; 107 HashEntry **ptr=tab+it.hashindex,*e; 108 for (;e=*ptr;ptr=&e->next) 109 { 110 if (e == it.entry) 106 if (!it.entry) return 0; 107 HashEntry **ptr = tab + it.hashindex, *e; 108 for (; e = *ptr; ptr = &e->next) 111 109 { 112 it++; 113 *ptr=e->next; 114 void *v=e->value; 115 delete e; 116 count--; 117 sync++; 118 it.sync++; 119 return v; 110 if (e == it.entry) 111 { 112 it++; 113 *ptr = e->next; 114 void *v = e->value; 115 delete e; 116 count--; 117 sync++; 118 it.sync++; 119 return v; 120 } 120 121 } 121 } 122 return NULL; 122 return NULL; 123 123 } 124 124 125 void* HashTable::get(const SString& key, int *reallygot)125 void* HashTable::get(const SString& key, int *reallygot) 126 126 { 127 int i=hash(key)%size;128 for (HashEntry *e=tab[i];e;e=e->next)129 if (e->key==key) {if (reallygot) *reallygot=1; return e->value;}130 return 0;127 int i = hash(key) % size; 128 for (HashEntry *e = tab[i]; e; e = e->next) 129 if (e->key == key) { if (reallygot) *reallygot = 1; return e->value; } 130 return 0; 131 131 } 132 132 … … 135 135 void HashEntryIterator::findNext() 136 136 { 137 while (hashindex < (ht->size-1))137 while (hashindex < (ht->size - 1)) 138 138 { 139 hashindex++;140 if (entry=ht->tab[hashindex]) return;139 hashindex++; 140 if (entry = ht->tab[hashindex]) return; 141 141 } 142 142 } … … 145 145 void HashEntryIterator::operator++() 146 146 { 147 if (entry) entry=entry->next;148 if (!entry) findNext();147 if (entry) entry = entry->next; 148 if (!entry) findNext(); 149 149 } 150 150 … … 153 153 void HashTable::debugprint() 154 154 { 155 printf("HashTable: %d/%d (max %d)\n",count,size,threshold);156 HashEntry *e,**te;157 int n;158 for (n=0,te=tab;n<size;n++,te++)159 if (e=*te)160 {161 printf(" %d:",n);162 for (;e;e=e->next)163 printf(" (%x)%s=%p",e->hash,e->key.c_str(),e->value);164 printf("\n");165 }155 printf("HashTable: %d/%d (max %d)\n", count, size, threshold); 156 HashEntry *e, **te; 157 int n; 158 for (n = 0, te = tab; n < size; n++, te++) 159 if (e = *te) 160 { 161 printf(" %d:", n); 162 for (; e; e = e->next) 163 printf(" (%x)%s=%p", e->hash, e->key.c_str(), e->value); 164 printf("\n"); 165 } 166 166 } 167 167 … … 171 171 stats[2]=avg keys in bucket 172 172 stats[3]=max keys in bucket 173 173 */ 174 174 void HashTable::getstats(float *stats) 175 175 { 176 HashEntry *e,**te;177 int used=0, ma=0, mi=count;178 int c,n;179 for (n=size,te=tab;n>0;n--,te++)176 HashEntry *e, **te; 177 int used = 0, ma = 0, mi = count; 178 int c, n; 179 for (n = size, te = tab; n > 0; n--, te++) 180 180 { 181 c=0;182 for (e=*te;e;e=e->next) c++;183 if (c>ma) ma=c;184 if ((c<mi)&&(c>0)) mi=c;185 if (c>0) used++;181 c = 0; 182 for (e = *te; e; e = e->next) c++; 183 if (c > ma) ma = c; 184 if ((c < mi) && (c>0)) mi = c; 185 if (c > 0) used++; 186 186 } 187 stats[0]=(float)used;188 stats[1]=(float)((mi==count)?0:mi);189 stats[2]=(float)count/(float)used;190 stats[3]=(float)ma;187 stats[0] = (float)used; 188 stats[1] = (float)((mi == count) ? 0 : mi); 189 stats[2] = (float)count / (float)used; 190 stats[3] = (float)ma; 191 191 } -
cpp/frams/util/hashtable.h
r286 r793 11 11 { 12 12 public: 13 int hash;14 SString key;15 void *value;16 HashEntry *next;13 int hash; 14 SString key; 15 void *value; 16 HashEntry *next; 17 17 18 HashEntry(int h,const SString& k,void *v):hash(h),key(k),value(v),next(0){}18 HashEntry(int h, const SString& k, void *v) :hash(h), key(k), value(v), next(0){} 19 19 }; 20 20 … … 23 23 class HashTable 24 24 { 25 friend class HashEntryIterator;26 HashEntry **tab;27 int size;28 int count;29 int threshold;30 float load;31 int sync;25 friend class HashEntryIterator; 26 HashEntry **tab; 27 int size; 28 int count; 29 int threshold; 30 float load; 31 int sync; 32 32 33 int hash(const SString &s);34 void rehash(int newsize);33 int hash(const SString &s); 34 void rehash(int newsize); 35 35 public: 36 36 37 HashTable(int initsize,float lo) {init(initsize,lo);}38 HashTable(int initsize) {init(initsize,0.75);}39 HashTable() {init();}40 ~HashTable();37 HashTable(int initsize, float lo) { init(initsize, lo); } 38 HashTable(int initsize) { init(initsize, 0.75); } 39 HashTable() { init(); } 40 ~HashTable(); 41 41 42 /** always use init() after clear() ! */43 void clear();44 void init(int initsize=11,float lo=0.75);42 /** always use init() after clear() ! */ 43 void clear(); 44 void init(int initsize = 11, float lo = 0.75); 45 45 46 int getSize() {return count;}47 void* put(const SString& key,void *value);48 void* get(const SString& key, int *reallygot=0);49 void* remove(const SString& key);50 /** can be used inside iteration loop:51 52 53 54 void* remove(HashEntryIterator& it);46 int getSize() { return count; } 47 void* put(const SString& key, void *value); 48 void* get(const SString& key, int *reallygot = 0); 49 void* remove(const SString& key); 50 /** can be used inside iteration loop: 51 for(HashEntryIterator it(hashtable);it;) hashtable.remove(it); 52 \note iterator is "iterated" to the next entry when the current one is removed (no "it++"!) 53 */ 54 void* remove(HashEntryIterator& it); 55 55 56 void debugprint();57 void getstats(float *);56 void debugprint(); 57 void getstats(float *); 58 58 }; 59 59 60 60 /** for(HashEntryIterator it(hashtable);it;it++) 61 62 63 64 65 */61 { 62 ... it->value 63 ... it->key 64 } 65 */ 66 66 class HashEntryIterator 67 67 { 68 void findNext(); 69 public: 70 const HashTable *ht; 71 int hashindex; 72 HashEntry *entry; 73 int sync; 74 HashEntryIterator(const HashTable&t):ht(&t),hashindex(0),entry(t.tab[0]),sync(ht->sync) 75 {if (!entry) findNext();} 76 HashEntryIterator() {} 77 void operator++(); 78 void operator++(int) {operator++();} 79 HashEntry* operator->() {return entry;} 80 bool isValid() {return (entry&&(sync==ht->sync))?1:0;} 68 void findNext(); 69 public: 70 const HashTable *ht; 71 int hashindex; 72 HashEntry *entry; 73 int sync; 74 HashEntryIterator(const HashTable&t) :ht(&t), hashindex(0), entry(t.tab[0]), sync(ht->sync) 75 { 76 if (!entry) findNext(); 77 } 78 HashEntryIterator() {} 79 void operator++(); 80 void operator++(int) { operator++(); } 81 HashEntry* operator->() { return entry; } 82 bool isValid() { return (entry && (sync == ht->sync)) ? 1 : 0; } 81 83 }; 82 84 -
cpp/frams/util/list.h
r781 r793 18 18 public: 19 19 int objects, allocations, copied, totalmem, usedmem; 20 SListStats() :objects(0),allocations(0),copied(0),totalmem(0),usedmem() {}20 SListStats() :objects(0), allocations(0), copied(0), totalmem(0), usedmem() {} 21 21 ~SListStats() 22 22 { … … 29 29 " usage = %ld %%\n" 30 30 "------------------------\n", 31 objects, allocations,copied,totalmem,(usedmem*100)/totalmem);31 objects, allocations, copied, totalmem, (usedmem * 100) / totalmem); 32 32 } 33 33 }; … … 47 47 #ifdef SLISTSTATS 48 48 SListStats::stats.allocations++; 49 SListStats::stats.copied +=sizeof(T)*min(x,have);49 SListStats::stats.copied += sizeof(T)*min(x, have); 50 50 #endif 51 51 } … … 65 65 #ifdef SLISTSTATS 66 66 SListStats::stats.objects++; 67 SListStats::stats.totalmem +=sizeof(T)*have;68 SListStats::stats.usedmem +=sizeof(T)*used;67 SListStats::stats.totalmem += sizeof(T)*have; 68 SListStats::stats.usedmem += sizeof(T)*used; 69 69 #endif 70 70 hardclear(); … … 100 100 { 101 101 if (size() != l.size()) return 0; 102 for (int i = 0; i <size(); i++) if (l.mem[i] != mem[i]) return 0;102 for (int i = 0; i < size(); i++) if (l.mem[i] != mem[i]) return 0; 103 103 return 1; 104 104 } -
cpp/frams/util/rndutil.h
r286 r793 12 12 13 13 /** @param x change seed if x<=0 14 14 @return random value [0..x-1] if x>0 */ 15 15 unsigned short pseudornd(short x); 16 16 … … 25 25 class RandomGener 26 26 { 27 28 RandomGener() {isNextGauss=0;}29 30 31 double Gauss(double m,double s); ///< usually will not return further than 5*stdd32 33 34 27 public: 28 RandomGener() { isNextGauss = 0; } 29 static double Uni(double begin, double end); ///< uniform excluding 'end' boundary 30 double GaussStd(); 31 double Gauss(double m, double s); ///< usually will not return further than 5*stdd 32 private: 33 int isNextGauss; 34 double nextGauss; 35 35 }; 36 36 -
cpp/frams/util/sstring-simple.cpp
r395 r793 6 6 void SString::initEmpty() 7 7 { 8 txt=NULL; used=0; size=0;8 txt = NULL; used = 0; size = 0; 9 9 } 10 10 11 11 SString::SString() 12 12 { 13 initEmpty();13 initEmpty(); 14 14 } 15 15 16 16 SString::~SString() 17 17 { 18 resize(0);18 resize(0); 19 19 } 20 20 21 21 SString::SString(int x) 22 22 { 23 initEmpty();24 if (x)25 ensureSize(x+1);26 } 27 28 SString::SString(const char *t, int t_len)29 { 30 initEmpty();31 if (!t) return;32 copyFrom(t,t_len);23 initEmpty(); 24 if (x) 25 ensureSize(x + 1); 26 } 27 28 SString::SString(const char *t, int t_len) 29 { 30 initEmpty(); 31 if (!t) return; 32 copyFrom(t, t_len); 33 33 } 34 34 35 35 SString::SString(const SString &from) 36 36 { 37 initEmpty();38 operator=(from);37 initEmpty(); 38 operator=(from); 39 39 } 40 40 41 41 SString::SString(SString&& from) 42 42 { 43 txt=from.txt; size=from.size; used=from.used;44 from.txt=NULL; from.size=0; from.used=0;43 txt = from.txt; size = from.size; used = from.used; 44 from.txt = NULL; from.size = 0; from.used = 0; 45 45 } 46 46 47 47 void SString::resize(int newsize) 48 48 { 49 if (newsize==size) return;50 txt=(char*)realloc(txt,newsize);51 size=newsize;49 if (newsize == size) return; 50 txt = (char*)realloc(txt, newsize); 51 size = newsize; 52 52 } 53 53 54 54 void SString::ensureSize(int needed) 55 55 { 56 if (size>needed) return;57 resize( (size>0) ? (needed+needed/2+1) : (needed+1));56 if (size > needed) return; 57 resize((size > 0) ? (needed + needed / 2 + 1) : (needed + 1)); 58 58 } 59 59 60 60 char *SString::directWrite(int ensuresize) 61 61 { 62 ensureSize(ensuresize);63 appending=used;64 return txt;62 ensureSize(ensuresize); 63 appending = used; 64 return txt; 65 65 } 66 66 67 67 char *SString::directAppend(int maxappend) 68 68 { 69 ensureSize(used+maxappend);70 appending=used;71 return txt+appending;69 ensureSize(used + maxappend); 70 appending = used; 71 return txt + appending; 72 72 } 73 73 74 74 void SString::endWrite(int newlength) 75 75 { 76 if (newlength<0) newlength=strlen(txt);77 else txt[newlength]=0;78 used=newlength;79 assert(used<size);76 if (newlength < 0) newlength = strlen(txt); 77 else txt[newlength] = 0; 78 used = newlength; 79 assert(used < size); 80 80 } 81 81 82 82 void SString::endAppend(int newappend) 83 83 { 84 if (newappend<0) newappend=strlen(txt+appending);85 else txt[appending+newappend]=0;86 used=appending+newappend;87 assert(used<size);84 if (newappend < 0) newappend = strlen(txt + appending); 85 else txt[appending + newappend] = 0; 86 used = appending + newappend; 87 assert(used < size); 88 88 } 89 89 … … 92 92 void SString::operator+=(const char *s) 93 93 { 94 if (!s) return;95 int x=strlen(s);96 if (!x) return;97 append(s,x);98 } 99 100 void SString::append(const char *t, int n)101 { 102 if (!n) return;103 ensureSize(used+n);104 memmove(txt+used,t,n);105 used+=n;106 txt[used]=0;94 if (!s) return; 95 int x = strlen(s); 96 if (!x) return; 97 append(s, x); 98 } 99 100 void SString::append(const char *t, int n) 101 { 102 if (!n) return; 103 ensureSize(used + n); 104 memmove(txt + used, t, n); 105 used += n; 106 txt[used] = 0; 107 107 } 108 108 109 109 void SString::operator+=(const SString&s) 110 110 { 111 append(s.c_str(),s.len());111 append(s.c_str(), s.len()); 112 112 } 113 113 114 114 SString SString::operator+(const SString& s) const 115 115 { 116 SString ret(len()+s.len());117 ret=*this;118 ret+=s;119 return ret;116 SString ret(len() + s.len()); 117 ret = *this; 118 ret += s; 119 return ret; 120 120 } 121 121 122 122 ///////////////////////////// 123 123 124 void SString::copyFrom(const char *ch, int chlen)125 { 126 if (!ch) chlen=0;127 else if (chlen<0) chlen=strlen(ch);128 if (chlen)124 void SString::copyFrom(const char *ch, int chlen) 125 { 126 if (!ch) chlen = 0; 127 else if (chlen < 0) chlen = strlen(ch); 128 if (chlen) 129 129 { 130 ensureSize(chlen);131 memmove(txt,ch,chlen);132 txt[chlen]=0;133 used=chlen;130 ensureSize(chlen); 131 memmove(txt, ch, chlen); 132 txt[chlen] = 0; 133 used = chlen; 134 134 } 135 else135 else 136 136 { 137 if (txt)137 if (txt) 138 138 { 139 txt[0]=0;140 used=0;139 txt[0] = 0; 140 used = 0; 141 141 } 142 142 } … … 145 145 void SString::operator=(const char *ch) 146 146 { 147 copyFrom(ch);147 copyFrom(ch); 148 148 } 149 149 150 150 void SString::operator=(const SString&s) 151 151 { 152 if (&s==this) return;153 copyFrom(s.c_str(),s.len());152 if (&s == this) return; 153 copyFrom(s.c_str(), s.len()); 154 154 } 155 155 … … 158 158 SString SString::substr(int begin, int length) const 159 159 { 160 if (begin<0) { length+=begin; begin=0; }161 if (length>=(len()-begin)) length=len()-begin;162 if (length<=0) return SString();163 if (length==len()) return *this;164 return SString((*this)(begin),length);160 if (begin < 0) { length += begin; begin = 0; } 161 if (length >= (len() - begin)) length = len() - begin; 162 if (length <= 0) return SString(); 163 if (length == len()) return *this; 164 return SString((*this)(begin), length); 165 165 } 166 166 … … 169 169 bool SString::equals(const SString& s) const 170 170 { 171 if (this==&s) return true;172 if (len()!=s.len()) return false;173 return strcmp(getPtr(),s.getPtr())==0;171 if (this == &s) return true; 172 if (len() != s.len()) return false; 173 return strcmp(getPtr(), s.getPtr()) == 0; 174 174 } 175 175 176 176 /////////////////////////////////////// 177 177 178 int SString::indexOf(int character, int start) const179 { 180 const char *found=strchr(getPtr()+start,character);181 return found?found-getPtr():-1;182 } 183 184 int SString::indexOf(const char *substring, int start) const185 { 186 const char *found=strstr(getPtr()+start,substring);187 return found?found-getPtr():-1;188 } 189 190 int SString::indexOf(const SString & substring, int start) const191 { 192 const char *found=strstr(getPtr()+start,substring.c_str());193 return found?found-getPtr():-1;194 } 195 196 bool SString::getNextToken (int& pos,SString &token,char separator) const197 { 198 if (pos>=len()) {token=0;return false;}199 int p1=pos,p2;200 const char *t1=getPtr()+pos;201 const char *t2=strchr(t1,separator);202 if (t2) pos=(p2=(t2-getPtr()))+1; else p2=pos=len();203 strncpy(token.directWrite(p2-p1),t1,p2-p1);204 token.endWrite(p2-p1);205 return true;178 int SString::indexOf(int character, int start) const 179 { 180 const char *found = strchr(getPtr() + start, character); 181 return found ? found - getPtr() : -1; 182 } 183 184 int SString::indexOf(const char *substring, int start) const 185 { 186 const char *found = strstr(getPtr() + start, substring); 187 return found ? found - getPtr() : -1; 188 } 189 190 int SString::indexOf(const SString & substring, int start) const 191 { 192 const char *found = strstr(getPtr() + start, substring.c_str()); 193 return found ? found - getPtr() : -1; 194 } 195 196 bool SString::getNextToken(int& pos, SString &token, char separator) const 197 { 198 if (pos >= len()) { token = 0; return false; } 199 int p1 = pos, p2; 200 const char *t1 = getPtr() + pos; 201 const char *t2 = strchr(t1, separator); 202 if (t2) pos = (p2 = (t2 - getPtr())) + 1; else p2 = pos = len(); 203 strncpy(token.directWrite(p2 - p1), t1, p2 - p1); 204 token.endWrite(p2 - p1); 205 return true; 206 206 } 207 207 208 208 bool SString::startsWith(const char *pattern) const 209 209 { 210 const char *t=this->c_str();211 for (;*pattern;pattern++,t++)212 if (*t != *pattern) return false;213 return true;210 const char *t = this->c_str(); 211 for (; *pattern; pattern++, t++) 212 if (*t != *pattern) return false; 213 return true; 214 214 } 215 215 216 216 SString SString::valueOf(int i) 217 217 { 218 return SString::sprintf("%d",i);218 return SString::sprintf("%d", i); 219 219 } 220 220 SString SString::valueOf(long i) 221 221 { 222 return SString::sprintf("%d",i);222 return SString::sprintf("%d", i); 223 223 } 224 224 SString SString::valueOf(double d) 225 225 { 226 SString tmp=SString::sprintf("%.15g",d);227 if ((!strchr(tmp.c_str(),'.'))&&(!strchr(tmp.c_str(),'e'))) tmp+=".0";228 return tmp;226 SString tmp = SString::sprintf("%.15g", d); 227 if ((!strchr(tmp.c_str(), '.')) && (!strchr(tmp.c_str(), 'e'))) tmp += ".0"; 228 return tmp; 229 229 } 230 230 SString SString::valueOf(const SString& s) 231 231 { 232 return s;232 return s; 233 233 } 234 234 235 235 SString SString::sprintf(const char* format, ...) 236 236 { 237 int n, size = 30;238 va_list ap;239 240 SString ret;237 int n, size = 30; 238 va_list ap; 239 240 SString ret; 241 241 242 242 #ifdef USE_VSCPRINTF 243 va_start(ap, format);244 size=_vscprintf(format, ap);245 va_end(ap);243 va_start(ap, format); 244 size = _vscprintf(format, ap); 245 va_end(ap); 246 246 #endif 247 247 248 while (1)248 while (1) 249 249 { 250 char* p=ret.directWrite(size);251 assert(p!=NULL);252 size=ret.directMaxLen()+1;253 /* Try to print in the allocated space. */254 va_start(ap, format);255 n = vsnprintf(p, size, format, ap);256 va_end(ap);257 /* If that worked, return the string. */258 if (n > -1 && n < size)250 char* p = ret.directWrite(size); 251 assert(p != NULL); 252 size = ret.directMaxLen() + 1; 253 /* Try to print in the allocated space. */ 254 va_start(ap, format); 255 n = vsnprintf(p, size, format, ap); 256 va_end(ap); 257 /* If that worked, return the string. */ 258 if (n > -1 && n < size) 259 259 { 260 ret.endWrite(n);261 return ret;260 ret.endWrite(n); 261 return ret; 262 262 } 263 /* Else try again with more space. */263 /* Else try again with more space. */ 264 264 #ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE 265 if (n > -1) /* glibc 2.1 */266 size = n; /* precisely what is needed */267 else /* glibc 2.0 */265 if (n > -1) /* glibc 2.1 */ 266 size = n; /* precisely what is needed */ 267 else /* glibc 2.0 */ 268 268 #endif 269 size *= 2; /* twice the old size */269 size *= 2; /* twice the old size */ 270 270 } 271 271 } … … 273 273 SString &SString::empty() 274 274 { 275 static SString empty;276 return empty;277 } 275 static SString empty; 276 return empty; 277 } -
cpp/frams/util/sstring-simple.h
r395 r793 13 13 { 14 14 private: 15 char *txt; ///< string buffer or NULL for empty string16 int size; ///< allocated memory (including \0)17 int used; ///< string length18 int appending; ///< append mode, changes can occur after character # 'appending'15 char *txt; ///< string buffer or NULL for empty string 16 int size; ///< allocated memory (including \0) 17 int used; ///< string length 18 int appending; ///< append mode, changes can occur after character # 'appending' 19 19 20 void initEmpty();21 void copyFrom(SString &from); ///< copy from SString22 void resize(int newsize);23 void ensureSize(int needed);24 const char* getPtr() const {return txt?txt:"";}20 void initEmpty(); 21 void copyFrom(SString &from); ///< copy from SString 22 void resize(int newsize); 23 void ensureSize(int needed); 24 const char* getPtr() const { return txt ? txt : ""; } 25 25 26 26 public: 27 SString(); ///< make an empty string28 SString(const char*t,int t_len=-1); ///< make a string from char*29 SString(int x); ///< string with initial buffer allocated for x characters30 SString(const SString& from); ///< duplicate string31 SString(SString&& from);///< move32 ~SString();27 SString(); ///< make an empty string 28 SString(const char*t, int t_len = -1); ///< make a string from char* 29 SString(int x); ///< string with initial buffer allocated for x characters 30 SString(const SString& from); ///< duplicate string 31 SString(SString&& from);///< move 32 ~SString(); 33 33 34 void copyFrom(const char* ch, int chlen=-1); ///< copy string, length of -1 == unknown34 void copyFrom(const char* ch, int chlen = -1); ///< copy string, length of -1 == unknown 35 35 36 void* operator new(size_t s, void* mem) {return mem;}36 void* operator new(size_t s, void* mem){ return mem; } 37 37 #ifdef _MSC_VER 38 void operator delete(void* mem, void* t) {}38 void operator delete(void* mem, void* t) {} 39 39 #endif 40 void* operator new(size_t s) {return malloc(sizeof(SString));}41 void operator delete(void* mem) {free(mem);}40 void* operator new(size_t s){ return malloc(sizeof(SString)); } 41 void operator delete(void* mem) { free(mem); } 42 42 43 int len() const {return used;} ///< get string length44 void shrink(); ///< free unnecessary buffer43 int len() const { return used; } ///< get string length 44 void shrink(); ///< free unnecessary buffer 45 45 46 /// after this call, you can modify sstring directly.47 /// returned value is the pointer to the internal buffer.48 /// <B>ensuresize</B> is minimal value of bytes you need,49 /// the buffer will be resized as needed.50 /// all "direct" operations have to leave the buffer with trailing '\0'51 /// at the end. endWrite() will search for this value in order to determine52 /// new string length.53 /// <P>Sample:<CODE>54 /// SString t;55 /// sprintf(t.directWrite(50),"a=%d,b=%f",a,b);56 /// t.endWrite();</CODE>57 char *directWrite(int ensuresize=-1);58 //char *directWrite();59 /// like directWrite, but it returns the pointer to the first char after current string60 /// for easy appending. <B>maxappend</B> is minimum of character in buffer61 /// that can be appended after this call.62 /// <P>Sample:<CODE>63 /// SString t;64 /// sprintf(t.directAppend(10),"c=%d",c);65 /// t.endAppend();</CODE>66 char *directAppend(int maxappend=0);67 /// update string length, after directWrite.68 /// you don't have to to call endWrite after directWrite if the string's length doesn't change.69 /// optional <B>newlength</B> parameter gives a chance to further optimize70 /// this operation if you know exact length of resulting string.71 /// <P>Sample:<CODE>72 /// SString t("samplestring");73 /// strncpy(t.directWrite(50),src,bytecount);74 /// t.endWrite(bytecount);</CODE>75 void endWrite(int newlength=-1);76 /// update string length, after directAppend.77 /// you will usually need to call endAppend (or endWrite) after directAppend,78 /// because the purpose of directAppend is to change string's length.79 /// optional <B>appendlength</B> parameter gives a chance to further optimize80 /// this operation if you know exact length of the appended string.81 /// <P>Sample:<CODE>82 /// SString t("samplestring");83 /// strncpy(t.directAppend(50),src,bytecount);84 /// t.endAppend(bytecount);</CODE>85 void endAppend(int appendlength=-1);46 /// after this call, you can modify sstring directly. 47 /// returned value is the pointer to the internal buffer. 48 /// <B>ensuresize</B> is minimal value of bytes you need, 49 /// the buffer will be resized as needed. 50 /// all "direct" operations have to leave the buffer with trailing '\0' 51 /// at the end. endWrite() will search for this value in order to determine 52 /// new string length. 53 /// <P>Sample:<CODE> 54 /// SString t; 55 /// sprintf(t.directWrite(50),"a=%d,b=%f",a,b); 56 /// t.endWrite();</CODE> 57 char *directWrite(int ensuresize = -1); 58 //char *directWrite(); 59 /// like directWrite, but it returns the pointer to the first char after current string 60 /// for easy appending. <B>maxappend</B> is minimum of character in buffer 61 /// that can be appended after this call. 62 /// <P>Sample:<CODE> 63 /// SString t; 64 /// sprintf(t.directAppend(10),"c=%d",c); 65 /// t.endAppend();</CODE> 66 char *directAppend(int maxappend = 0); 67 /// update string length, after directWrite. 68 /// you don't have to to call endWrite after directWrite if the string's length doesn't change. 69 /// optional <B>newlength</B> parameter gives a chance to further optimize 70 /// this operation if you know exact length of resulting string. 71 /// <P>Sample:<CODE> 72 /// SString t("samplestring"); 73 /// strncpy(t.directWrite(50),src,bytecount); 74 /// t.endWrite(bytecount);</CODE> 75 void endWrite(int newlength = -1); 76 /// update string length, after directAppend. 77 /// you will usually need to call endAppend (or endWrite) after directAppend, 78 /// because the purpose of directAppend is to change string's length. 79 /// optional <B>appendlength</B> parameter gives a chance to further optimize 80 /// this operation if you know exact length of the appended string. 81 /// <P>Sample:<CODE> 82 /// SString t("samplestring"); 83 /// strncpy(t.directAppend(50),src,bytecount); 84 /// t.endAppend(bytecount);</CODE> 85 void endAppend(int appendlength = -1); 86 86 87 void memoryHint(int howbig) {ensureSize(howbig);}88 int directMaxLen() {return size-1;} ///< when called after directWrite: max number of characters allowed (can be more than requested)87 void memoryHint(int howbig) { ensureSize(howbig); } 88 int directMaxLen() { return size - 1; } ///< when called after directWrite: max number of characters allowed (can be more than requested) 89 89 90 /// find a character in SString.91 /// return index if the character was found or -1 otherwise.92 int indexOf(int character,int start=0) const;90 /// find a character in SString. 91 /// return index if the character was found or -1 otherwise. 92 int indexOf(int character, int start = 0) const; 93 93 94 /// find a substring.95 /// return index if the substring was found or -1 otherwise.96 int indexOf(const char *substring,int start=0) const;94 /// find a substring. 95 /// return index if the substring was found or -1 otherwise. 96 int indexOf(const char *substring, int start = 0) const; 97 97 98 /// find a substring.99 /// return index if the substring was found or -1 otherwise.100 int indexOf(const SString & substring,int start=0) const;98 /// find a substring. 99 /// return index if the substring was found or -1 otherwise. 100 int indexOf(const SString & substring, int start = 0) const; 101 101 102 const char* c_str() const {return getPtr();} ///< get SString's readonly buffer103 void operator=(const char*t); ///< assign from const char*104 void operator=(const SString &s);102 const char* c_str() const { return getPtr(); } ///< get SString's readonly buffer 103 void operator=(const char*t); ///< assign from const char* 104 void operator=(const SString &s); 105 105 106 void append(const char *t,int n);107 SString operator+(const SString &s) const;108 void operator+=(int x); ///< append x spaces after current string109 void operator+=(const char*); ///< append char* contents110 void operator+=(const SString&); ///< append other SString106 void append(const char *t, int n); 107 SString operator+(const SString &s) const; 108 void operator+=(int x); ///< append x spaces after current string 109 void operator+=(const char*); ///< append char* contents 110 void operator+=(const SString&); ///< append other SString 111 111 112 bool equals(const SString &s) const; ///< TRUE if equal113 bool operator==(const SString &s) const {return equals(s);} ///< TRUE if equal114 bool operator!=(const SString &s) const {return !equals(s);} ///< TRUE if not equal115 bool operator<(const SString &s) const {return strcmp(getPtr(),s.getPtr())<1;}116 const char* operator()(int p) const {return getPtr()+p;} ///< pointer to p'th character in SString117 char operator[](int i) const {return getPtr()[i];} ///< get char like in array112 bool equals(const SString &s) const; ///< TRUE if equal 113 bool operator==(const SString &s) const { return equals(s); } ///< TRUE if equal 114 bool operator!=(const SString &s) const { return !equals(s); } ///< TRUE if not equal 115 bool operator<(const SString &s) const { return strcmp(getPtr(), s.getPtr()) < 1; } 116 const char* operator()(int p) const { return getPtr() + p; } ///< pointer to p'th character in SString 117 char operator[](int i) const { return getPtr()[i]; } ///< get char like in array 118 118 119 /// return a substring of the current string120 SString substr(int begin, int length=1<<30) const;119 /// return a substring of the current string 120 SString substr(int begin, int length = 1 << 30) const; 121 121 122 /// simple tokenization:123 /// starting at <B>pos</B>, get next substring delimited by <B>separator</B> character124 /// and put it in output parameter <B>token</B>.125 /// <B>pos</B> is moved accordingly.126 /// returns <B>false</B> if no more tokens are available or <B>true</B> otherwise.127 bool getNextToken(int& pos,SString &token,char separator) const;122 /// simple tokenization: 123 /// starting at <B>pos</B>, get next substring delimited by <B>separator</B> character 124 /// and put it in output parameter <B>token</B>. 125 /// <B>pos</B> is moved accordingly. 126 /// returns <B>false</B> if no more tokens are available or <B>true</B> otherwise. 127 bool getNextToken(int& pos, SString &token, char separator) const; 128 128 129 void operator+=(char ch) {directAppend(1)[0]=ch;endAppend(1);} ///< append single character129 void operator+=(char ch) { directAppend(1)[0] = ch; endAppend(1); } ///< append single character 130 130 131 bool startsWith(const char *pattern) const;132 char charAt(int pos) const {return operator[](pos);}133 uint32_t hash() const;131 bool startsWith(const char *pattern) const; 132 char charAt(int pos) const { return operator[](pos); } 133 uint32_t hash() const; 134 134 135 static SString valueOf(int);136 static SString valueOf(long);137 static SString valueOf(double);138 static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu139 static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu140 static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu141 static SString sprintf(const char* format, ...);135 static SString valueOf(int); 136 static SString valueOf(long); 137 static SString valueOf(double); 138 static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu 139 static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu 140 static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu 141 static SString sprintf(const char* format, ...); 142 142 143 static SString &empty();143 static SString &empty(); 144 144 }; 145 145 -
cpp/frams/util/sstring.cpp
r395 r793 26 26 #ifdef MULTITHREADED 27 27 #include <pthread.h> 28 static pthread_mutex_t sstring_ref_lock =PTHREAD_MUTEX_INITIALIZER;28 static pthread_mutex_t sstring_ref_lock = PTHREAD_MUTEX_INITIALIZER; 29 29 #define REF_LOCK pthread_mutex_lock(&sstring_ref_lock); 30 30 #define REF_UNLOCK pthread_mutex_unlock(&sstring_ref_lock) … … 36 36 static int guessMemSize(int request) 37 37 { 38 return request+min(request/2,10000)+8;38 return request + min(request / 2, 10000) + 8; 39 39 } 40 40 41 41 SBuf::SBuf() 42 42 { 43 txt=(char*)"";44 size=used=0;45 refcount=1;43 txt = (char*)""; 44 size = used = 0; 45 refcount = 1; 46 46 } 47 47 48 48 SBuf::SBuf(int initsize) 49 49 { 50 size=guessMemSize(initsize);51 if (size>0) { txt=(char*)malloc(size+1); txt[0]=0; }52 else txt =(char*)"";53 used=0;54 refcount=1;50 size = guessMemSize(initsize); 51 if (size > 0) { txt = (char*)malloc(size + 1); txt[0] = 0; } 52 else txt = (char*)""; 53 used = 0; 54 refcount = 1; 55 55 } 56 56 57 57 SBuf::~SBuf() 58 58 { 59 freeBuf();59 freeBuf(); 60 60 } 61 61 62 62 void SBuf::initEmpty() 63 63 { 64 txt=(char*)"";65 used=size=0;66 refcount=1;64 txt = (char*)""; 65 used = size = 0; 66 refcount = 1; 67 67 } 68 68 69 69 void SBuf::freeBuf() 70 70 { 71 if (!size) return; 72 free(txt); used=0; 73 } 74 75 void SBuf::copyFrom(const char *ch,int chlen) 76 { 77 if (chlen==-1) chlen=strlen(ch); 78 if (chlen>0) 79 { 80 if (chlen<size) 71 if (!size) return; 72 free(txt); used = 0; 73 } 74 75 void SBuf::copyFrom(const char *ch, int chlen) 76 { 77 if (chlen == -1) chlen = strlen(ch); 78 if (chlen > 0) 81 79 { 82 memmove(txt,ch,chlen); 83 } 84 else 80 if (chlen < size) 81 { 82 memmove(txt, ch, chlen); 83 } 84 else 85 { 86 size = guessMemSize(chlen); 87 char *newtxt = (char*)malloc(size + 1); 88 memcpy(newtxt, ch, chlen); 89 free(txt); 90 txt = newtxt; 91 } 92 } 93 txt[chlen] = 0; 94 used = chlen; 95 } 96 97 void SBuf::append(const char *ch, int chlen) 98 { // doesn't check anything! 99 memmove(txt + used, ch, chlen); 100 used += chlen; 101 txt[used] = 0; 102 } 103 104 void SBuf::ensureSize(int needed) 105 { 106 if (size >= needed) return; 107 needed = guessMemSize(needed); 108 txt = (char*)realloc(txt, needed + 1); 109 size = needed; 110 } 111 112 ///////////////////////////////////////////// 113 114 SString::SString() 115 { 116 initEmpty(); 117 } 118 119 SString::~SString() 120 { 121 REF_LOCK; 122 detach(); 123 REF_UNLOCK; 124 } 125 126 SString::SString(int x) 127 { 128 buf = new SBuf(x); 129 } 130 131 SString::SString(const char *t, int t_len) 132 { 133 initEmpty(); 134 if (!t) return; 135 copyFrom(t, t_len); 136 } 137 138 SString::SString(SString&& from) 139 { 140 buf = from.buf; 141 from.buf = &SBuf::empty(); 142 } 143 144 SString::SString(const SString &from) 145 { 146 if (from.buf == &SBuf::empty()) 147 buf = &SBuf::empty(); 148 else 85 149 { 86 size=guessMemSize(chlen); 87 char *newtxt=(char*)malloc(size+1); 88 memcpy(newtxt,ch,chlen); 89 free(txt); 90 txt=newtxt; 91 } 92 } 93 txt[chlen]=0; 94 used=chlen; 95 } 96 97 void SBuf::append(const char *ch,int chlen) 98 { // doesn't check anything! 99 memmove(txt+used,ch,chlen); 100 used+=chlen; 101 txt[used]=0; 102 } 103 104 void SBuf::ensureSize(int needed) 105 { 106 if (size>=needed) return; 107 needed=guessMemSize(needed); 108 txt=(char*)realloc(txt,needed+1); 109 size=needed; 110 } 111 112 ///////////////////////////////////////////// 113 114 SString::SString() 115 { 116 initEmpty(); 117 } 118 119 SString::~SString() 120 { 121 REF_LOCK; 122 detach(); 123 REF_UNLOCK; 124 } 125 126 SString::SString(int x) 127 { 128 buf=new SBuf(x); 129 } 130 131 SString::SString(const char *t,int t_len) 132 { 133 initEmpty(); 134 if (!t) return; 135 copyFrom(t,t_len); 136 } 137 138 SString::SString(SString&& from) 139 { 140 buf=from.buf; 141 from.buf=&SBuf::empty(); 142 } 143 144 SString::SString(const SString &from) 145 { 146 if (from.buf==&SBuf::empty()) 147 buf=&SBuf::empty(); 148 else 150 REF_LOCK; 151 buf = from.buf; 152 if (buf->size) 153 buf->refcount++; 154 REF_UNLOCK; 155 } 156 } 157 158 void SString::initEmpty() 159 { 160 buf = &SBuf::empty(); 161 } 162 163 void SString::memoryHint(int howbig) 164 { 165 detachCopy(howbig); 166 } 167 168 void SString::detachEmpty(int ensuresize) 169 { 170 if (buf == &SBuf::empty()) { buf = new SBuf(ensuresize); return; } 171 if (buf->refcount < 2) buf->ensureSize(ensuresize); 172 else 149 173 { 150 REF_LOCK; 151 buf=from.buf; 152 if (buf->size) 153 buf->refcount++; 154 REF_UNLOCK; 155 } 156 } 157 158 void SString::initEmpty() 159 { 160 buf=&SBuf::empty(); 161 } 162 163 void SString::memoryHint(int howbig) 164 { 165 detachCopy(howbig); 166 } 167 168 void SString::detachEmpty(int ensuresize) 169 { 170 if (buf==&SBuf::empty()) { buf=new SBuf(ensuresize); return; } 171 if (buf->refcount<2) buf->ensureSize(ensuresize); 172 else 174 buf->refcount--; 175 buf = new SBuf(ensuresize); 176 } 177 } 178 179 void SString::detach() 180 { 181 if (buf == &SBuf::empty()) return; 182 if (!--buf->refcount) delete buf; 183 } 184 185 void SString::detachCopy(int ensuresize) 186 { 187 if (buf == &SBuf::empty()) { buf = new SBuf(ensuresize); return; } 188 if (buf->refcount < 2) 173 189 { 190 buf->ensureSize(ensuresize); 191 return; 192 } 174 193 buf->refcount--; 175 buf=new SBuf(ensuresize); 176 } 177 } 178 179 void SString::detach() 180 { 181 if (buf==&SBuf::empty()) return; 182 if (!--buf->refcount) delete buf; 183 } 184 185 void SString::detachCopy(int ensuresize) 186 { 187 if (buf==&SBuf::empty()) { buf=new SBuf(ensuresize); return; } 188 if (buf->refcount<2) 189 { 190 buf->ensureSize(ensuresize); 191 return; 192 } 193 buf->refcount--; 194 SBuf *newbuf=new SBuf(ensuresize); 195 newbuf->copyFrom(buf->txt,min(ensuresize,buf->used)); 196 buf=newbuf; 194 SBuf *newbuf = new SBuf(ensuresize); 195 newbuf->copyFrom(buf->txt, min(ensuresize, buf->used)); 196 buf = newbuf; 197 197 } 198 198 199 199 char *SString::directWrite(int ensuresize) 200 200 { 201 if (ensuresize<0) ensuresize=len();202 REF_LOCK;203 detachCopy(ensuresize);204 REF_UNLOCK;205 appending=buf->used;206 return buf->txt;201 if (ensuresize < 0) ensuresize = len(); 202 REF_LOCK; 203 detachCopy(ensuresize); 204 REF_UNLOCK; 205 appending = buf->used; 206 return buf->txt; 207 207 } 208 208 … … 215 215 char *SString::directAppend(int maxappend) 216 216 { 217 REF_LOCK;218 detachCopy(buf->used+maxappend);219 REF_UNLOCK;220 appending=buf->used;221 return buf->txt+appending;217 REF_LOCK; 218 detachCopy(buf->used + maxappend); 219 REF_UNLOCK; 220 appending = buf->used; 221 return buf->txt + appending; 222 222 } 223 223 224 224 void SString::endWrite(int newlength) 225 225 { 226 if (newlength<0) newlength=strlen(buf->txt);227 else buf->txt[newlength]=0;228 buf->used=newlength;226 if (newlength < 0) newlength = strlen(buf->txt); 227 else buf->txt[newlength] = 0; 228 buf->used = newlength; 229 229 } 230 230 231 231 void SString::endAppend(int newappend) 232 232 { 233 if (newappend<0) newappend=strlen(buf->txt+appending);234 else buf->txt[appending+newappend]=0;235 buf->used=appending+newappend;233 if (newappend < 0) newappend = strlen(buf->txt + appending); 234 else buf->txt[appending + newappend] = 0; 235 buf->used = appending + newappend; 236 236 } 237 237 … … 240 240 void SString::operator+=(const char *s) 241 241 { 242 if (!s) return;243 int x=strlen(s);244 if (!x) return;245 append(s,x);246 } 247 248 void SString::append(const char *txt, int count)249 { 250 if (!count) return;251 REF_LOCK;252 detachCopy(buf->used+count);253 REF_UNLOCK;254 buf->append(txt,count);242 if (!s) return; 243 int x = strlen(s); 244 if (!x) return; 245 append(s, x); 246 } 247 248 void SString::append(const char *txt, int count) 249 { 250 if (!count) return; 251 REF_LOCK; 252 detachCopy(buf->used + count); 253 REF_UNLOCK; 254 buf->append(txt, count); 255 255 } 256 256 257 257 void SString::operator+=(const SString&s) 258 258 { 259 append(s.c_str(),s.len());259 append(s.c_str(), s.len()); 260 260 } 261 261 262 262 SString SString::operator+(const SString& s) const 263 263 { 264 SString ret(*this);265 ret+=s;266 return ret;264 SString ret(*this); 265 ret += s; 266 return ret; 267 267 } 268 268 269 269 ///////////////////////////// 270 270 271 void SString::copyFrom(const char *ch, int chlen)272 { 273 if (!ch) chlen=0;274 else if (chlen<0) chlen=strlen(ch);275 REF_LOCK;276 detachEmpty(chlen);277 REF_UNLOCK;278 memmove(buf->txt,ch,chlen);279 buf->txt[chlen]=0; buf->used=chlen;271 void SString::copyFrom(const char *ch, int chlen) 272 { 273 if (!ch) chlen = 0; 274 else if (chlen < 0) chlen = strlen(ch); 275 REF_LOCK; 276 detachEmpty(chlen); 277 REF_UNLOCK; 278 memmove(buf->txt, ch, chlen); 279 buf->txt[chlen] = 0; buf->used = chlen; 280 280 } 281 281 282 282 void SString::operator=(const char *ch) 283 283 { 284 copyFrom(ch);284 copyFrom(ch); 285 285 } 286 286 287 287 void SString::operator=(const SString&s) 288 288 { 289 if (s.buf==buf) return;290 REF_LOCK;291 detach();292 buf=s.buf;293 if (buf->size) buf->refcount++;294 REF_UNLOCK;289 if (s.buf == buf) return; 290 REF_LOCK; 291 detach(); 292 buf = s.buf; 293 if (buf->size) buf->refcount++; 294 REF_UNLOCK; 295 295 } 296 296 /////////////////////////////////////// … … 298 298 SString SString::substr(int begin, int length) const 299 299 { 300 if (begin<0) { length+=begin; begin=0; }301 if (length>=(len()-begin)) length=len()-begin;302 if (length<=0) return SString();303 if (length==len()) return *this;304 return SString((*this)(begin),length);300 if (begin < 0) { length += begin; begin = 0; } 301 if (length >= (len() - begin)) length = len() - begin; 302 if (length <= 0) return SString(); 303 if (length == len()) return *this; 304 return SString((*this)(begin), length); 305 305 } 306 306 … … 309 309 bool SString::equals(const SString& s) const 310 310 { 311 if (s.buf==buf) return true;312 return strcmp(buf->txt,s.buf->txt)==0;311 if (s.buf == buf) return true; 312 return strcmp(buf->txt, s.buf->txt) == 0; 313 313 } 314 314 315 315 /////////////////////////////////////// 316 316 317 int SString::indexOf(int character, int start) const318 { 319 const char *found=strchr(buf->txt+start,character);320 return found?found-buf->txt:-1;321 } 322 323 int SString::indexOf(const char *substring, int start) const324 { 325 char *found=strstr(buf->txt+start,substring);326 return found?found-buf->txt:-1;327 } 328 329 int SString::indexOf(const SString & substring, int start) const330 { 331 char *found=strstr(buf->txt+start,substring.c_str());332 return found?found-buf->txt:-1;333 } 334 335 bool SString::getNextToken (int& pos,SString &token,char separator) const336 { 337 if (pos>=len()) {token=0;return false;}338 int p1=pos,p2;339 const char *t1=buf->txt+pos;340 const char *t2=strchr(t1,separator);341 if (t2) pos=(p2=(t2-buf->txt))+1; else p2=pos=len();342 strncpy(token.directWrite(p2-p1),t1,p2-p1);343 token.endWrite(p2-p1);344 return true;317 int SString::indexOf(int character, int start) const 318 { 319 const char *found = strchr(buf->txt + start, character); 320 return found ? found - buf->txt : -1; 321 } 322 323 int SString::indexOf(const char *substring, int start) const 324 { 325 char *found = strstr(buf->txt + start, substring); 326 return found ? found - buf->txt : -1; 327 } 328 329 int SString::indexOf(const SString & substring, int start) const 330 { 331 char *found = strstr(buf->txt + start, substring.c_str()); 332 return found ? found - buf->txt : -1; 333 } 334 335 bool SString::getNextToken(int& pos, SString &token, char separator) const 336 { 337 if (pos >= len()) { token = 0; return false; } 338 int p1 = pos, p2; 339 const char *t1 = buf->txt + pos; 340 const char *t2 = strchr(t1, separator); 341 if (t2) pos = (p2 = (t2 - buf->txt)) + 1; else p2 = pos = len(); 342 strncpy(token.directWrite(p2 - p1), t1, p2 - p1); 343 token.endWrite(p2 - p1); 344 return true; 345 345 } 346 346 347 347 bool SString::startsWith(const char *pattern) const 348 348 { 349 const char *t=this->c_str();350 for (;*pattern;pattern++,t++)351 if (*t != *pattern) return false;352 return true;349 const char *t = this->c_str(); 350 for (; *pattern; pattern++, t++) 351 if (*t != *pattern) return false; 352 return true; 353 353 } 354 354 355 355 SString SString::valueOf(int i) 356 356 { 357 return SString::sprintf("%d",i);357 return SString::sprintf("%d", i); 358 358 } 359 359 SString SString::valueOf(long i) 360 360 { 361 return SString::sprintf("%d",i);361 return SString::sprintf("%d", i); 362 362 } 363 363 SString SString::valueOf(double d) 364 364 { 365 SString tmp=SString::sprintf("%.15g",d);366 if ((!strchr(tmp.c_str(),'.'))&&(!strchr(tmp.c_str(),'e'))) tmp+=".0";367 return tmp;365 SString tmp = SString::sprintf("%.15g", d); 366 if ((!strchr(tmp.c_str(), '.')) && (!strchr(tmp.c_str(), 'e'))) tmp += ".0"; 367 return tmp; 368 368 } 369 369 SString SString::valueOf(const SString& s) 370 370 { 371 return s;371 return s; 372 372 } 373 373 374 374 #if 0 //testing _vscprintf 375 375 #define USE_VSCPRINTF 376 int _vscprintf(const char *format, va_list argptr)377 { 378 return vsnprintf("",0,format,argptr);376 int _vscprintf(const char *format, va_list argptr) 377 { 378 return vsnprintf("", 0, format, argptr); 379 379 } 380 380 #endif … … 382 382 SString SString::sprintf(const char* format, ...) 383 383 { 384 int n, size = 30;385 va_list ap;386 387 SString ret;384 int n, size = 30; 385 va_list ap; 386 387 SString ret; 388 388 389 389 #ifdef USE_VSCPRINTF 390 va_start(ap, format);391 size=_vscprintf(format, ap);392 va_end(ap);393 #endif 394 395 while (1)390 va_start(ap, format); 391 size = _vscprintf(format, ap); 392 va_end(ap); 393 #endif 394 395 while (1) 396 396 { 397 char* p=ret.directWrite(size);398 assert(p!=NULL);399 size=ret.directMaxLen()+1;400 /* Try to print in the allocated space. */401 va_start(ap, format);402 n = vsnprintf(p, size, format, ap);403 va_end(ap);404 /* If that worked, return the string. */405 if (n > -1 && n < size)397 char* p = ret.directWrite(size); 398 assert(p != NULL); 399 size = ret.directMaxLen() + 1; 400 /* Try to print in the allocated space. */ 401 va_start(ap, format); 402 n = vsnprintf(p, size, format, ap); 403 va_end(ap); 404 /* If that worked, return the string. */ 405 if (n > -1 && n < size) 406 406 { 407 ret.endWrite(n);408 return ret;407 ret.endWrite(n); 408 return ret; 409 409 } 410 /* Else try again with more space. */410 /* Else try again with more space. */ 411 411 #ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE 412 if (n > -1) /* glibc 2.1 */413 size = n; /* precisely what is needed */414 else /* glibc 2.0 */415 #endif 416 size *= 2; /* twice the old size */412 if (n > -1) /* glibc 2.1 */ 413 size = n; /* precisely what is needed */ 414 else /* glibc 2.0 */ 415 #endif 416 size *= 2; /* twice the old size */ 417 417 } 418 418 } … … 420 420 SString &SString::empty() 421 421 { 422 static SString empty;423 return empty;422 static SString empty; 423 return empty; 424 424 } 425 425 426 426 SBuf &SBuf::empty() 427 427 { 428 static SBuf empty;429 return empty;428 static SBuf empty; 429 return empty; 430 430 } 431 431 … … 443 443 Fnv32_t fnv_32a_buf(void *buf, size_t len, Fnv32_t hval) 444 444 { 445 446 447 448 449 450 /* xor the bottom with the current octet */451 hval ^= (Fnv32_t)*bp++;452 453 /* multiply by the 32 bit FNV magic prime mod 2^32 */445 unsigned char *bp = (unsigned char *)buf; /* start of buffer */ 446 unsigned char *be = bp + len; /* beyond end of buffer */ 447 448 while (bp < be) { 449 450 /* xor the bottom with the current octet */ 451 hval ^= (Fnv32_t)*bp++; 452 453 /* multiply by the 32 bit FNV magic prime mod 2^32 */ 454 454 #if defined(NO_FNV_GCC_OPTIMIZATION) 455 hval *= FNV_32_PRIME;455 hval *= FNV_32_PRIME; 456 456 #else 457 hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);458 #endif 459 460 461 462 463 457 hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24); 458 #endif 459 460 } 461 462 /* return our new hash value */ 463 return hval; 464 464 } 465 465 ////////////////////////////////////////////////// … … 468 468 uint32_t SString::hash() const 469 469 { 470 return fnv_32a_buf(txt,used,FNV1_32A_INIT);470 return fnv_32a_buf(txt, used, FNV1_32A_INIT); 471 471 } 472 472 #else 473 473 uint32_t SBuf::hash() const 474 474 { 475 return fnv_32a_buf(txt,used,FNV1_32A_INIT);476 } 477 #endif 475 return fnv_32a_buf(txt, used, FNV1_32A_INIT); 476 } 477 #endif -
cpp/frams/util/sstring.h
r395 r793 32 32 class SBuf 33 33 { 34 char *txt;35 int used; ///< data size36 int size; ///< buffer size, not including \0, special case: 0==buffer not allocated37 int refcount; ///< buffer is used by 'refcount' objects.38 void initEmpty();39 void ensureSize(int wantsize);40 void copyFrom(const char* ch, int chlen=-1);41 void freeBuf();42 void append(const char* ch, int chlen=-1);43 static SBuf &empty();44 SBuf(int initsize);45 friend class SString;46 SBuf(const SBuf& b) {}34 char *txt; 35 int used; ///< data size 36 int size; ///< buffer size, not including \0, special case: 0==buffer not allocated 37 int refcount; ///< buffer is used by 'refcount' objects. 38 void initEmpty(); 39 void ensureSize(int wantsize); 40 void copyFrom(const char* ch, int chlen = -1); 41 void freeBuf(); 42 void append(const char* ch, int chlen = -1); 43 static SBuf &empty(); 44 SBuf(int initsize); 45 friend class SString; 46 SBuf(const SBuf& b) {} 47 47 public: 48 SBuf();49 ~SBuf();50 uint32_t hash() const; // 32-bit FNV-1 hash -> http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash48 SBuf(); 49 ~SBuf(); 50 uint32_t hash() const; // 32-bit FNV-1 hash -> http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash 51 51 }; 52 52 … … 56 56 { 57 57 private: 58 SBuf *buf; ///< buffer59 int appending; ///< append mode, changes can occur after character # 'appending'60 //int memhint;58 SBuf *buf; ///< buffer 59 int appending; ///< append mode, changes can occur after character # 'appending' 60 //int memhint; 61 61 62 void initEmpty();63 int guessMemSize(int request);64 void copyFrom(SString &from); ///< copy from SString, reference if possible65 void detach(); ///< detach from shared buffer, if any66 void detachEmpty(int ensuresize=0); ///< detach and make empty67 void detachCopy(int ensuresize=0); ///< detach and make private copy62 void initEmpty(); 63 int guessMemSize(int request); 64 void copyFrom(SString &from); ///< copy from SString, reference if possible 65 void detach(); ///< detach from shared buffer, if any 66 void detachEmpty(int ensuresize = 0); ///< detach and make empty 67 void detachCopy(int ensuresize = 0); ///< detach and make private copy 68 68 69 69 public: 70 SString(); ///< make an empty string71 SString(const char*t,int t_len=-1); ///< make a string from char*72 SString(int x); ///< string with initial buffer size73 SString(const SString& from); ///< duplicate string74 SString(SString&& from); ///< move75 ~SString();70 SString(); ///< make an empty string 71 SString(const char*t, int t_len = -1); ///< make a string from char* 72 SString(int x); ///< string with initial buffer size 73 SString(const SString& from); ///< duplicate string 74 SString(SString&& from); ///< move 75 ~SString(); 76 76 77 void copyFrom(const char* ch, int chlen=-1); ///< copy string, length of -1 == unknown77 void copyFrom(const char* ch, int chlen = -1); ///< copy string, length of -1 == unknown 78 78 79 void* operator new(size_t s, void* mem) {return mem;}79 void* operator new(size_t s, void* mem){ return mem; } 80 80 #ifdef _MSC_VER 81 void operator delete(void* mem, void* t) {}81 void operator delete(void* mem, void* t) {} 82 82 #endif 83 void* operator new(size_t s) {return malloc(sizeof(SString));}84 void operator delete(void* mem) {free(mem);}83 void* operator new(size_t s){ return malloc(sizeof(SString)); } 84 void operator delete(void* mem) { free(mem); } 85 85 86 int len() const {return buf->used;} ///< get string length87 void shrink(); ///< free unnecessary buffer86 int len() const { return buf->used; } ///< get string length 87 void shrink(); ///< free unnecessary buffer 88 88 89 /// after this call, you can modify sstring directly.90 /// returned value is the pointer to the internal buffer.91 /// <B>ensuresize</B> is minimal value of bytes you need,92 /// the buffer will be resized as needed.93 /// all "direct" operations have to leave the buffer with trailing '\0'94 /// at the end. endWrite() will search for this value in order to determine95 /// new string length.96 /// <P>Sample:<CODE>97 /// SString t;98 /// sprintf(t.directWrite(50),"a=%d,b=%f",a,b);99 /// t.endWrite();</CODE>100 char *directWrite(int ensuresize=-1);101 //char *directWrite();102 /// like directWrite, but it returns the pointer to the first char after current string103 /// for easy appending. <B>maxappend</B> is minimum of character in buffer104 /// that can be appended after this call.105 /// <P>Sample:<CODE>106 /// SString t;107 /// sprintf(t.directAppend(10),"c=%d",c);108 /// t.endAppend();</CODE>109 char *directAppend(int maxappend=0);110 /// update string length, after directWrite.111 /// you don't have to to call endWrite after directWrite if the string's length doesn't change.112 /// optional <B>newlength</B> parameter gives a chance to further optimize113 /// this operation if you know exact length of resulting string.114 /// <P>Sample:<CODE>115 /// SString t("samplestring");116 /// strncpy(t.directWrite(50),src,bytecount);117 /// t.endWrite(bytecount);</CODE>118 void endWrite(int newlength=-1);119 /// update string length, after directAppend.120 /// you will usually need to call endAppend (or endWrite) after directAppend,121 /// because the purpose of directAppend is to change string's length.122 /// optional <B>appendlength</B> parameter gives a chance to further optimize123 /// this operation if you know exact length of the appended string.124 /// <P>Sample:<CODE>125 /// SString t("samplestring");126 /// strncpy(t.directAppend(50),src,bytecount);127 /// t.endAppend(bytecount);</CODE>128 void endAppend(int appendlength=-1);129 /// argument is the amount of memory, that will be probably used130 /// by this string instance. string can use this value131 /// to optimize memory allocation (bigger chunks will be allocated).132 void memoryHint(int howbig);133 int directMaxLen() {return buf->size;} ///< when called after directWrite: max number of characters allowed (can be more than requested)89 /// after this call, you can modify sstring directly. 90 /// returned value is the pointer to the internal buffer. 91 /// <B>ensuresize</B> is minimal value of bytes you need, 92 /// the buffer will be resized as needed. 93 /// all "direct" operations have to leave the buffer with trailing '\0' 94 /// at the end. endWrite() will search for this value in order to determine 95 /// new string length. 96 /// <P>Sample:<CODE> 97 /// SString t; 98 /// sprintf(t.directWrite(50),"a=%d,b=%f",a,b); 99 /// t.endWrite();</CODE> 100 char *directWrite(int ensuresize = -1); 101 //char *directWrite(); 102 /// like directWrite, but it returns the pointer to the first char after current string 103 /// for easy appending. <B>maxappend</B> is minimum of character in buffer 104 /// that can be appended after this call. 105 /// <P>Sample:<CODE> 106 /// SString t; 107 /// sprintf(t.directAppend(10),"c=%d",c); 108 /// t.endAppend();</CODE> 109 char *directAppend(int maxappend = 0); 110 /// update string length, after directWrite. 111 /// you don't have to to call endWrite after directWrite if the string's length doesn't change. 112 /// optional <B>newlength</B> parameter gives a chance to further optimize 113 /// this operation if you know exact length of resulting string. 114 /// <P>Sample:<CODE> 115 /// SString t("samplestring"); 116 /// strncpy(t.directWrite(50),src,bytecount); 117 /// t.endWrite(bytecount);</CODE> 118 void endWrite(int newlength = -1); 119 /// update string length, after directAppend. 120 /// you will usually need to call endAppend (or endWrite) after directAppend, 121 /// because the purpose of directAppend is to change string's length. 122 /// optional <B>appendlength</B> parameter gives a chance to further optimize 123 /// this operation if you know exact length of the appended string. 124 /// <P>Sample:<CODE> 125 /// SString t("samplestring"); 126 /// strncpy(t.directAppend(50),src,bytecount); 127 /// t.endAppend(bytecount);</CODE> 128 void endAppend(int appendlength = -1); 129 /// argument is the amount of memory, that will be probably used 130 /// by this string instance. string can use this value 131 /// to optimize memory allocation (bigger chunks will be allocated). 132 void memoryHint(int howbig); 133 int directMaxLen() { return buf->size; } ///< when called after directWrite: max number of characters allowed (can be more than requested) 134 134 135 /// find a character in SString.136 /// return index if the character was found or -1 otherwise.137 int indexOf(int character,int start=0) const;135 /// find a character in SString. 136 /// return index if the character was found or -1 otherwise. 137 int indexOf(int character, int start = 0) const; 138 138 139 /// find a substring.140 /// return index if the substring was found or -1 otherwise.141 int indexOf(const char *substring,int start=0) const;139 /// find a substring. 140 /// return index if the substring was found or -1 otherwise. 141 int indexOf(const char *substring, int start = 0) const; 142 142 143 /// find a substring.144 /// return index if the substring was found or -1 otherwise.145 int indexOf(const SString & substring,int start=0) const;143 /// find a substring. 144 /// return index if the substring was found or -1 otherwise. 145 int indexOf(const SString & substring, int start = 0) const; 146 146 147 const char* c_str() const {return buf->txt;} ///< get SString's readonly buffer148 //operator char*() {detachCopy(len()); return buf->txt;} ///< get SString's writable buffer149 void operator=(const char*t); ///< assign from const char*150 //void operator=(int x) {free(txt);nowy(x);} ///< clear string and make new empty one151 void operator=(const SString &s);147 const char* c_str() const { return buf->txt; } ///< get SString's readonly buffer 148 //operator char*() {detachCopy(len()); return buf->txt;} ///< get SString's writable buffer 149 void operator=(const char*t); ///< assign from const char* 150 //void operator=(int x) {free(txt);nowy(x);} ///< clear string and make new empty one 151 void operator=(const SString &s); 152 152 153 void append(const char *txt,int count);154 SString operator+(const SString &s) const;155 void operator+=(int x); ///< append x spaces after current string156 void operator+=(const char*); ///< append char* contents157 void operator+=(const SString&); ///< append other SString153 void append(const char *txt, int count); 154 SString operator+(const SString &s) const; 155 void operator+=(int x); ///< append x spaces after current string 156 void operator+=(const char*); ///< append char* contents 157 void operator+=(const SString&); ///< append other SString 158 158 159 bool equals(const SString &s) const; ///< TRUE if equal160 bool operator==(const SString &s) const {return equals(s);} ///< TRUE if equal161 bool operator!=(const SString &s) const {return !equals(s);}162 const char* operator()(int p) const {return buf->txt+p;} ///< pointer to p'th character in SString163 char operator[](int i) const {return buf->txt[i];} ///< get char like in array159 bool equals(const SString &s) const; ///< TRUE if equal 160 bool operator==(const SString &s) const { return equals(s); } ///< TRUE if equal 161 bool operator!=(const SString &s) const { return !equals(s); } 162 const char* operator()(int p) const { return buf->txt + p; } ///< pointer to p'th character in SString 163 char operator[](int i) const { return buf->txt[i]; } ///< get char like in array 164 164 165 /// return a substring of the current string166 SString substr(int begin, int length=1<<30) const;165 /// return a substring of the current string 166 SString substr(int begin, int length = 1 << 30) const; 167 167 168 /// simple tokenization:169 /// starting at <B>pos</B>, get next substring delimited by <B>separator</B> character170 /// and put it in output parameter <B>token</B>.171 /// <B>pos</B> is moved accordingly.172 /// returns <B>false</B> if no more tokens are available or <B>true</B> otherwise.173 bool getNextToken(int& pos,SString &token,char separator) const;168 /// simple tokenization: 169 /// starting at <B>pos</B>, get next substring delimited by <B>separator</B> character 170 /// and put it in output parameter <B>token</B>. 171 /// <B>pos</B> is moved accordingly. 172 /// returns <B>false</B> if no more tokens are available or <B>true</B> otherwise. 173 bool getNextToken(int& pos, SString &token, char separator) const; 174 174 175 void operator+=(char ch) {directAppend(1)[0]=ch;endAppend(1);} ///< append single character175 void operator+=(char ch) { directAppend(1)[0] = ch; endAppend(1); } ///< append single character 176 176 177 bool startsWith(const char *pattern) const;178 char charAt(int pos) const {return buf->txt[pos];}179 uint32_t hash() const {return buf->hash();}177 bool startsWith(const char *pattern) const; 178 char charAt(int pos) const { return buf->txt[pos]; } 179 uint32_t hash() const { return buf->hash(); } 180 180 181 static SString valueOf(int);182 static SString valueOf(long);183 static SString valueOf(double);184 static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu185 static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu186 static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu187 static SString sprintf(const char* format, ...);181 static SString valueOf(int); 182 static SString valueOf(long); 183 static SString valueOf(double); 184 static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu 185 static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu 186 static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu 187 static SString sprintf(const char* format, ...); 188 188 189 static SString &empty();189 static SString &empty(); 190 190 }; 191 191 -
cpp/frams/util/usertags.h
r286 r793 41 41 */ 42 42 43 template<class ID, class T,int N> class UserTags43 template<class ID, class T, int N> class UserTags 44 44 { 45 static char reg[N];46 T data[N];47 48 UserTags() {memset(data,0,sizeof(data));}45 static char reg[N]; 46 T data[N]; 47 public: 48 UserTags() { memset(data, 0, sizeof(data)); } 49 49 50 /** allocate new id */ 51 static int newID() 52 { for(int i=1;i<N;i++) if (!reg[i]) {reg[i]=1; return i;} 53 DB(printf("Warning: UserTags run out of ids!\n")); 54 return 0; } 55 static void freeID(int id) 56 { reg[id]=0; } 57 T& operator[](int id) 50 /** allocate new id */ 51 static int newID() 58 52 { 59 DB(if (!id) printf("Warning: UserTags @ %p is using id=0\n",this);) 60 return data[id]; 53 for (int i = 1; i < N; i++) if (!reg[i]) { reg[i] = 1; return i; } 54 DB(printf("Warning: UserTags run out of ids!\n")); 55 return 0; 61 56 } 62 operator T() { return data[0]; } 63 void operator=(T x) { data[0]=x; } 57 static void freeID(int id) 58 { 59 reg[id] = 0; 60 } 61 T& operator[](int id) 62 { 63 DB(if (!id) printf("Warning: UserTags @ %p is using id=0\n", this);) 64 return data[id]; 65 } 66 operator T() { return data[0]; } 67 void operator=(T x) { data[0] = x; } 64 68 }; 65 69 66 70 #endif 67 68 69 70 71 -
cpp/frams/util/validitychecks.cpp
r651 r793 7 7 #include <common/log.h> 8 8 9 bool listIndexCheck(SList* list, int index,const char* msgobj,const char* msgfun)9 bool listIndexCheck(SList* list, int index, const char* msgobj, const char* msgfun) 10 10 { 11 int size =list->size();12 if ((index<0)||(index>=size))11 int size = list->size(); 12 if ((index < 0) || (index >= size)) 13 13 { 14 if (size>0)15 logPrintf(msgobj,msgfun,LOG_ERROR,"Invalid index %d (allowed range is 0..%d)",index,size-1);16 else17 logPrintf(msgobj,msgfun,LOG_ERROR,"Invalid index %d (this list is empty)",index);18 return false;14 if (size>0) 15 logPrintf(msgobj, msgfun, LOG_ERROR, "Invalid index %d (allowed range is 0..%d)", index, size - 1); 16 else 17 logPrintf(msgobj, msgfun, LOG_ERROR, "Invalid index %d (this list is empty)", index); 18 return false; 19 19 } 20 return true;20 return true; 21 21 } 22 22 23 SString stringCheck(SString& in, const char* msgobj,const char* msgfun,const char* msg,SString(*checker)(const SString& in))23 SString stringCheck(SString& in, const char* msgobj, const char* msgfun, const char* msg, SString(*checker)(const SString& in)) 24 24 { 25 if (!checker)26 checker=trim;27 SString corrected=checker(in);28 if (corrected!=in)25 if (!checker) 26 checker = trim; 27 SString corrected = checker(in); 28 if (corrected != in) 29 29 { 30 SString msg2=SString(msg)+": \"%s\" (adjusted to \"%s\")";31 logPrintf(msgobj,msgfun,LOG_WARN,msg2.c_str(),in.c_str(),corrected.c_str());30 SString msg2 = SString(msg) + ": \"%s\" (adjusted to \"%s\")"; 31 logPrintf(msgobj, msgfun, LOG_WARN, msg2.c_str(), in.c_str(), corrected.c_str()); 32 32 } 33 return corrected;33 return corrected; 34 34 } -
cpp/frams/util/validitychecks.h
r512 r793 9 9 #include <frams/util/sstring.h> 10 10 11 bool listIndexCheck(SList* list, int index,const char* msgobj,const char* msgfun);12 SString stringCheck(SString& in, const char* msgobj,const char* msgfun,const char* msg,SString (*checker)(const SString& in)=NULL);11 bool listIndexCheck(SList* list, int index, const char* msgobj, const char* msgfun); 12 SString stringCheck(SString& in, const char* msgobj, const char* msgfun, const char* msg, SString(*checker)(const SString& in) = NULL); 13 13 14 14 #endif
Note: See TracChangeset
for help on using the changeset viewer.