Changeset 793


Ignore:
Timestamp:
05/29/18 16:51:14 (2 years ago)
Author:
Maciej Komosinski
Message:

Code formatting

Location:
cpp/frams/util
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/util/callbacks.cpp

    r286 r793  
    99int MemberCallbackNode::equals(CallbackNode*n)
    1010{
    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));
    1515}
    1616#endif
     
    1818int FunctionCallbackNode::equals(CallbackNode*n)
    1919{
    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));
    2424}
    2525
    2626int StatrickCallbackNode::equals(CallbackNode*n)
    2727{
    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));
    3232}
    3333
     
    3636CallbackNode* Callback::add(CallbackNode*n)
    3737{
    38 SList::operator+=(n);
    39 return n;
     38        SList::operator+=(n);
     39        return n;
    4040}
    4141
    4242void Callback::removeNode(CallbackNode*n)
    4343{
    44 SList::operator-=(n);
    45 delete n;
     44        SList::operator-=(n);
     45        delete n;
    4646}
    4747
    4848void Callback::remove(CallbackNode*node)
    4949{
    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))
    5454                {
    5555                SList::operator-=(i);
    5656                delete node;
    57                 if (n!=node) delete n;
     57                if (n != node) delete n;
    5858                return;
    5959                }
    60 delete node; // tu nie wiem czy na pewno...
     60        delete node; // tu nie wiem czy na pewno...
    6161}
    6262
    6363void Callback::action(intptr_t data)
    6464{
    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);
    6969}
    7070
    7171Callback::~Callback()
    7272{
    73 CallbackNode *n;
    74 for (int i=size()-1;i>=0;i--)
     73        CallbackNode *n;
     74        for (int i = size() - 1; i >= 0; i--)
    7575        {
    76         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
     76                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
    8282        }
    8383}
  • cpp/frams/util/callbacks.h

    r286 r793  
    1515{
    1616public:
    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); }
    2020};
    2121
     
    2424class MemberCallbackNode :public CallbackNode
    2525{
    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*);
     26        void *userdata;
     27        CallBase *object;
     28        void (CallBase::*member)(void*, intptr_t);
     29public:
     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*);
    3333};
    3434#define MEMBERCALLBACK(obj,mem,dat) new MemberCallbackNode((CallBase*)(obj),(void (CallBase::*)(void*,intptr_t))(mem),(void*)(dat))
     
    3737class FunctionCallbackNode :public CallbackNode
    3838{
    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*);
     39        void *userdata;
     40        void(*fun)(void*, intptr_t);
     41public:
     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*);
    4545};
    4646#define FUNCTIONCALLBACK(fun,dat) new FunctionCallbackNode((void (*)(void*,intptr_t))(fun),(void*)(dat))
     
    4848class StatrickCallbackNode :public CallbackNode
    4949{
    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*);
     50        void *object;
     51        void *userdata;
     52        void(*fun)(void*, void*, intptr_t);
     53public:
     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*);
    5757};
    5858#define STATRICKCALLBACK(obj,name,dat) new StatrickCallbackNode((void*)(obj),(void (*)(void*,void*,intptr_t))STATRICKNAME(name),(void*)(dat))
     
    6060/**
    6161   Like in old 'DuoList' you can register for an event giving function pointer
    62       add(Function* fun, void* anydata)
     62   add(Function* fun, void* anydata)
    6363   'fun' will be called with your pointer as the first argument (void*)
    6464   and event specific value as the second argument (intptr_t)
    65       fun(void* anydata,intptr_t eventdata)
     65   fun(void* anydata,intptr_t eventdata)
    6666
    6767   'StatrickCallbackNode' uses static functions to emulate object member calls.
    6868   @see statrick.h
    69    
     69
    7070   Unregistering callbacks:
    7171   The old remove(...) still works, but removeNode() is more efficient.
    7272   To use it you have to store what you get from add(...);
    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!
     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!
    7676
    77  */
     77   */
    7878
    79 class Callback: protected SList
     79class Callback : protected SList
    8080{
    8181public:
    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(); }
    96100};
    97101
     
    108112/* STCALLBACKDEFC(Class,name)
    109113
    110     |
    111     v
     114        |
     115        v
    112116
    113   #define STATRICKCLASS Class
    114   STCALLBACKDEF(name)
    115   #undef STATRICKCLASS
    116  */
     117        #define STATRICKCLASS Class
     118        STCALLBACKDEF(name)
     119        #undef STATRICKCLASS
     120        */
    117121
    118122#define CALLBACKARGS void* arg1,intptr_t arg2
    119123
    120124#endif
    121 
  • cpp/frams/util/extvalue.h

    r639 r793  
    126126                ExtObject *o;
    127127        };
    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; };
    132132#endif
    133133
     
    237237        void seto(const ExtObject &src) { type = TObj; new(data)ExtObject(src); }
    238238#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); }
    243243#endif
    244244
  • cpp/frams/util/hashtable.cpp

    r348 r793  
    77int HashTable::hash(const SString &s)
    88{
    9 return s.hash()&0x7fffffff;
     9        return s.hash() & 0x7fffffff;
    1010}
    1111
    12 void HashTable::init(int initsize,float lo)
     12void HashTable::init(int initsize, float lo)
    1313{
    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;
    2020}
    2121
    2222void HashTable::rehash(int newsize)
    2323{
    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++;
    4141}
    4242
    4343void HashTable::clear()
    4444{
    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)
    4949                {
    50                 next=e->next;
     50                next = e->next;
    5151                delete e;
    5252                }
    53 if (tab) free(tab);
    54 tab=0; size=0;
    55 sync++;
     53        if (tab) free(tab);
     54        tab = 0; size = 0;
     55        sync++;
    5656}
    5757
    5858HashTable::~HashTable()
    5959{
    60 clear();
     60        clear();
    6161}
    6262
    63 void* HashTable::put(const SString& key,void *value)
     63void* HashTable::put(const SString& key, void *value)
    6464{
    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)
    7068        {
    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                }
    7475        }
    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;
    8383}
    8484
    8585void* HashTable::remove(const SString& key)
    8686{
    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)
    9290        {
    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                }
    99100        }
    100     }
    101 return 0;
     101        return 0;
    102102}
    103103
    104104void* HashTable::remove(HashEntryIterator& it)
    105105{
    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)
    111109        {
    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                }
    120121        }
    121     }
    122    return NULL;
     122        return NULL;
    123123}
    124124
    125 void* HashTable::get(const SString& key,int *reallygot)
     125void* HashTable::get(const SString& key, int *reallygot)
    126126{
    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;
    131131}
    132132
     
    135135void HashEntryIterator::findNext()
    136136{
    137 while (hashindex < (ht->size-1))
     137        while (hashindex < (ht->size - 1))
    138138        {
    139         hashindex++;
    140         if (entry=ht->tab[hashindex]) return;
     139                hashindex++;
     140                if (entry = ht->tab[hashindex]) return;
    141141        }
    142142}
     
    145145void HashEntryIterator::operator++()
    146146{
    147 if (entry) entry=entry->next;
    148 if (!entry) findNext();
     147        if (entry) entry = entry->next;
     148        if (!entry) findNext();
    149149}
    150150
     
    153153void HashTable::debugprint()
    154154{
    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                }
    166166}
    167167
     
    171171stats[2]=avg keys in bucket
    172172stats[3]=max keys in bucket
    173  */
     173*/
    174174void HashTable::getstats(float *stats)
    175175{
    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++)
    180180        {
    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++;
    186186        }
    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;
    191191}
  • cpp/frams/util/hashtable.h

    r286 r793  
    1111{
    1212public:
    13 int hash;
    14 SString key;
    15 void *value;
    16 HashEntry *next;
     13        int hash;
     14        SString key;
     15        void *value;
     16        HashEntry *next;
    1717
    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){}
    1919};
    2020
     
    2323class HashTable
    2424{
    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;
    3232
    33 int hash(const SString &s);
    34 void rehash(int newsize);
     33        int hash(const SString &s);
     34        void rehash(int newsize);
    3535public:
    3636
    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();
    4141
    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);
    4545
    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);
     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);
    5555
    56 void debugprint();
    57 void getstats(float *);
     56        void debugprint();
     57        void getstats(float *);
    5858};
    5959
    6060/** for(HashEntryIterator it(hashtable);it;it++)
    61       {
    62       ... it->value
    63       ... it->key
    64       }
    65  */
     61          {
     62          ... it->value
     63          ... it->key
     64          }
     65          */
    6666class HashEntryIterator
    6767{
    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();
     69public:
     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; }
    8183};
    8284
  • cpp/frams/util/list.h

    r781 r793  
    1818public:
    1919        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() {}
    2121        ~SListStats()
    2222        {
     
    2929                        " usage       = %ld %%\n"
    3030                        "------------------------\n",
    31                         objects,allocations,copied,totalmem,(usedmem*100)/totalmem);
     31                        objects, allocations, copied, totalmem, (usedmem * 100) / totalmem);
    3232        }
    3333};
     
    4747#ifdef SLISTSTATS
    4848                        SListStats::stats.allocations++;
    49                         SListStats::stats.copied+=sizeof(T)*min(x,have);
     49                        SListStats::stats.copied += sizeof(T)*min(x, have);
    5050#endif 
    5151                }
     
    6565#ifdef SLISTSTATS
    6666                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;
    6969#endif
    7070                hardclear();
     
    100100        {
    101101                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;
    103103                return 1;
    104104        }
  • cpp/frams/util/rndutil.h

    r286 r793  
    1212
    1313/** @param x change seed if x<=0
    14     @return random value [0..x-1] if x>0 */
     14        @return random value [0..x-1] if x>0 */
    1515unsigned short pseudornd(short x);
    1616
     
    2525class RandomGener
    2626{
    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;
     27public:
     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
     32private:
     33        int isNextGauss;
     34        double nextGauss;
    3535};
    3636
  • cpp/frams/util/sstring-simple.cpp

    r395 r793  
    66void SString::initEmpty()
    77{
    8 txt=NULL; used=0; size=0;
     8        txt = NULL; used = 0; size = 0;
    99}
    1010
    1111SString::SString()
    1212{
    13 initEmpty();
     13        initEmpty();
    1414}
    1515
    1616SString::~SString()
    1717{
    18 resize(0);
     18        resize(0);
    1919}
    2020
    2121SString::SString(int x)
    2222{
    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
     28SString::SString(const char *t, int t_len)
     29{
     30        initEmpty();
     31        if (!t) return;
     32        copyFrom(t, t_len);
    3333}
    3434
    3535SString::SString(const SString &from)
    3636{
    37 initEmpty();
    38 operator=(from);
     37        initEmpty();
     38        operator=(from);
    3939}
    4040
    4141SString::SString(SString&& from)
    4242{
    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;
    4545}
    4646
    4747void SString::resize(int newsize)
    4848{
    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;
    5252}
    5353
    5454void SString::ensureSize(int needed)
    5555{
    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));
    5858}
    5959
    6060char *SString::directWrite(int ensuresize)
    6161{
    62 ensureSize(ensuresize);
    63 appending=used;
    64 return txt;
     62        ensureSize(ensuresize);
     63        appending = used;
     64        return txt;
    6565}
    6666
    6767char *SString::directAppend(int maxappend)
    6868{
    69 ensureSize(used+maxappend);
    70 appending=used;
    71 return txt+appending;
     69        ensureSize(used + maxappend);
     70        appending = used;
     71        return txt + appending;
    7272}
    7373
    7474void SString::endWrite(int newlength)
    7575{
    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);
    8080}
    8181
    8282void SString::endAppend(int newappend)
    8383{
    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);
    8888}
    8989
     
    9292void SString::operator+=(const char *s)
    9393{
    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
     100void 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;
    107107}
    108108
    109109void SString::operator+=(const SString&s)
    110110{
    111 append(s.c_str(),s.len());
     111        append(s.c_str(), s.len());
    112112}
    113113
    114114SString SString::operator+(const SString& s) const
    115115{
    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;
    120120}
    121121
    122122/////////////////////////////
    123123
    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)
     124void SString::copyFrom(const char *ch, int chlen)
     125{
     126        if (!ch) chlen = 0;
     127        else if (chlen < 0) chlen = strlen(ch);
     128        if (chlen)
    129129        {
    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;
    134134        }
    135 else
     135        else
    136136        {
    137         if (txt)
     137                if (txt)
    138138                {
    139                 txt[0]=0;
    140                 used=0;
     139                        txt[0] = 0;
     140                        used = 0;
    141141                }
    142142        }
     
    145145void SString::operator=(const char *ch)
    146146{
    147 copyFrom(ch);
     147        copyFrom(ch);
    148148}
    149149
    150150void SString::operator=(const SString&s)
    151151{
    152 if (&s==this) return;
    153 copyFrom(s.c_str(),s.len());
     152        if (&s == this) return;
     153        copyFrom(s.c_str(), s.len());
    154154}
    155155
     
    158158SString SString::substr(int begin, int length) const
    159159{
    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);
    165165}
    166166
     
    169169bool SString::equals(const SString& s) const
    170170{
    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;
    174174}
    175175
    176176///////////////////////////////////////
    177177
    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;
     178int SString::indexOf(int character, int start) const
     179{
     180        const char *found = strchr(getPtr() + start, character);
     181        return found ? found - getPtr() : -1;
     182}
     183
     184int 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
     190int 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
     196bool 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;
    206206}
    207207
    208208bool SString::startsWith(const char *pattern) const
    209209{
    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;
    214214}
    215215
    216216SString SString::valueOf(int i)
    217217{
    218 return SString::sprintf("%d",i);
     218        return SString::sprintf("%d", i);
    219219}
    220220SString SString::valueOf(long i)
    221221{
    222 return SString::sprintf("%d",i);
     222        return SString::sprintf("%d", i);
    223223}
    224224SString SString::valueOf(double d)
    225225{
    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;
    229229}
    230230SString SString::valueOf(const SString& s)
    231231{
    232 return s;
     232        return s;
    233233}
    234234
    235235SString SString::sprintf(const char* format, ...)
    236236{
    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;
    241241
    242242#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);
    246246#endif
    247247
    248 while (1)
     248        while (1)
    249249        {
    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)
    259259                {
    260                 ret.endWrite(n);
    261                 return ret;
     260                        ret.endWrite(n);
     261                        return ret;
    262262                }
    263         /* Else try again with more space. */
     263                /* Else try again with more space. */
    264264#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 */
    268268#endif
    269                 size *= 2;  /* twice the old size */
     269                        size *= 2;  /* twice the old size */
    270270        }
    271271}
     
    273273SString &SString::empty()
    274274{
    275 static SString empty;
    276 return empty;
    277 }
     275        static SString empty;
     276        return empty;
     277}
  • cpp/frams/util/sstring-simple.h

    r395 r793  
    1313{
    1414private:
    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'
     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'
    1919
    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:"";}
     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 : ""; }
    2525
    2626public:
    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();
     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();
    3333
    34 void copyFrom(const char* ch, int chlen=-1); ///< copy string, length of -1 == unknown
     34        void copyFrom(const char* ch, int chlen = -1); ///< copy string, length of -1 == unknown
    3535
    36 void* operator new(size_t s, void* mem) {return mem;}
     36        void* operator new(size_t s, void* mem){ return mem; }
    3737#ifdef _MSC_VER
    38 void operator delete(void* mem, void* t) {}
     38                void operator delete(void* mem, void* t) {}
    3939#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); }
    4242
    43 int len() const {return used;} ///< get string length
    44 void shrink(); ///< free unnecessary buffer
     43        int len() const { return used; } ///< get string length
     44        void shrink(); ///< free unnecessary buffer
    4545
    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);
     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);
    8686
    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)
    8989
    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;
    9393
    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;
    9797
    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;
    101101
    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);
     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);
    105105
    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 string
    109 void operator+=(const char*); ///< append char* contents
    110 void operator+=(const SString&); ///< append other SString
     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 string
     109        void operator+=(const char*); ///< append char* contents
     110        void operator+=(const SString&); ///< append other SString
    111111
    112 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
     112        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
    118118
    119 /// return a substring of the current string
    120 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;
    121121
    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;
     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;
    128128
    129 void operator+=(char ch) {directAppend(1)[0]=ch;endAppend(1);} ///< append single character
     129        void operator+=(char ch) { directAppend(1)[0] = ch; endAppend(1); } ///< append single character
    130130
    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;
    134134
    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, ...);
     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, ...);
    142142
    143 static SString &empty();
     143        static SString &empty();
    144144};
    145145
  • cpp/frams/util/sstring.cpp

    r395 r793  
    2626#ifdef MULTITHREADED
    2727#include <pthread.h>
    28 static pthread_mutex_t sstring_ref_lock=PTHREAD_MUTEX_INITIALIZER;
     28static pthread_mutex_t sstring_ref_lock = PTHREAD_MUTEX_INITIALIZER;
    2929#define REF_LOCK pthread_mutex_lock(&sstring_ref_lock);
    3030#define REF_UNLOCK pthread_mutex_unlock(&sstring_ref_lock)
     
    3636static int guessMemSize(int request)
    3737{
    38 return request+min(request/2,10000)+8;
     38        return request + min(request / 2, 10000) + 8;
    3939}
    4040
    4141SBuf::SBuf()
    4242{
    43 txt=(char*)"";
    44 size=used=0;
    45 refcount=1;
     43        txt = (char*)"";
     44        size = used = 0;
     45        refcount = 1;
    4646}
    4747
    4848SBuf::SBuf(int initsize)
    4949{
    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;
    5555}
    5656
    5757SBuf::~SBuf()
    5858{
    59 freeBuf();
     59        freeBuf();
    6060}
    6161
    6262void SBuf::initEmpty()
    6363{
    64 txt=(char*)"";
    65 used=size=0;
    66 refcount=1;
     64        txt = (char*)"";
     65        used = size = 0;
     66        refcount = 1;
    6767}
    6868
    6969void SBuf::freeBuf()
    7070{
    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
     75void SBuf::copyFrom(const char *ch, int chlen)
     76{
     77        if (chlen == -1) chlen = strlen(ch);
     78        if (chlen > 0)
    8179        {
    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
     97void 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
     104void 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
     114SString::SString()
     115{
     116        initEmpty();
     117}
     118
     119SString::~SString()
     120{
     121        REF_LOCK;
     122        detach();
     123        REF_UNLOCK;
     124}
     125
     126SString::SString(int x)
     127{
     128        buf = new SBuf(x);
     129}
     130
     131SString::SString(const char *t, int t_len)
     132{
     133        initEmpty();
     134        if (!t) return;
     135        copyFrom(t, t_len);
     136}
     137
     138SString::SString(SString&& from)
     139{
     140        buf = from.buf;
     141        from.buf = &SBuf::empty();
     142}
     143
     144SString::SString(const SString &from)
     145{
     146        if (from.buf == &SBuf::empty())
     147                buf = &SBuf::empty();
     148        else
    85149        {
    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
     158void SString::initEmpty()
     159{
     160        buf = &SBuf::empty();
     161}
     162
     163void SString::memoryHint(int howbig)
     164{
     165        detachCopy(howbig);
     166}
     167
     168void 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
    149173        {
    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
     179void SString::detach()
     180{
     181        if (buf == &SBuf::empty()) return;
     182        if (!--buf->refcount) delete buf;
     183}
     184
     185void SString::detachCopy(int ensuresize)
     186{
     187        if (buf == &SBuf::empty()) { buf = new SBuf(ensuresize); return; }
     188        if (buf->refcount < 2)
    173189        {
     190                buf->ensureSize(ensuresize);
     191                return;
     192        }
    174193        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;
    197197}
    198198
    199199char *SString::directWrite(int ensuresize)
    200200{
    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;
    207207}
    208208
     
    215215char *SString::directAppend(int maxappend)
    216216{
    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;
    222222}
    223223
    224224void SString::endWrite(int newlength)
    225225{
    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;
    229229}
    230230
    231231void SString::endAppend(int newappend)
    232232{
    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;
    236236}
    237237
     
    240240void SString::operator+=(const char *s)
    241241{
    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
     248void 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);
    255255}
    256256
    257257void SString::operator+=(const SString&s)
    258258{
    259 append(s.c_str(),s.len());
     259        append(s.c_str(), s.len());
    260260}
    261261
    262262SString SString::operator+(const SString& s) const
    263263{
    264 SString ret(*this);
    265 ret+=s;
    266 return ret;
     264        SString ret(*this);
     265        ret += s;
     266        return ret;
    267267}
    268268
    269269/////////////////////////////
    270270
    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;
     271void 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;
    280280}
    281281
    282282void SString::operator=(const char *ch)
    283283{
    284 copyFrom(ch);
     284        copyFrom(ch);
    285285}
    286286
    287287void SString::operator=(const SString&s)
    288288{
    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;
    295295}
    296296///////////////////////////////////////
     
    298298SString SString::substr(int begin, int length) const
    299299{
    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);
    305305}
    306306
     
    309309bool SString::equals(const SString& s) const
    310310{
    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;
    313313}
    314314
    315315///////////////////////////////////////
    316316
    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;
     317int 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
     323int 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
     329int 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
     335bool 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;
    345345}
    346346
    347347bool SString::startsWith(const char *pattern) const
    348348{
    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;
    353353}
    354354
    355355SString SString::valueOf(int i)
    356356{
    357 return SString::sprintf("%d",i);
     357        return SString::sprintf("%d", i);
    358358}
    359359SString SString::valueOf(long i)
    360360{
    361 return SString::sprintf("%d",i);
     361        return SString::sprintf("%d", i);
    362362}
    363363SString SString::valueOf(double d)
    364364{
    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;
    368368}
    369369SString SString::valueOf(const SString& s)
    370370{
    371 return s;
     371        return s;
    372372}
    373373
    374374#if 0 //testing _vscprintf
    375375#define USE_VSCPRINTF
    376 int _vscprintf(const char *format,va_list argptr)
    377 {
    378 return vsnprintf("",0,format,argptr);
     376int _vscprintf(const char *format, va_list argptr)
     377{
     378        return vsnprintf("", 0, format, argptr);
    379379}
    380380#endif
     
    382382SString SString::sprintf(const char* format, ...)
    383383{
    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;
    388388
    389389#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)
    396396        {
    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)
    406406                {
    407                 ret.endWrite(n);
    408                 return ret;
     407                        ret.endWrite(n);
     408                        return ret;
    409409                }
    410         /* Else try again with more space. */
     410                /* Else try again with more space. */
    411411#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 */
    417417        }
    418418}
     
    420420SString &SString::empty()
    421421{
    422 static SString empty;
    423 return empty;
     422        static SString empty;
     423        return empty;
    424424}
    425425
    426426SBuf &SBuf::empty()
    427427{
    428 static SBuf empty;
    429 return empty;
     428        static SBuf empty;
     429        return empty;
    430430}
    431431
     
    443443Fnv32_t fnv_32a_buf(void *buf, size_t len, Fnv32_t hval)
    444444{
    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 */
     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 */
    454454#if defined(NO_FNV_GCC_OPTIMIZATION)
    455         hval *= FNV_32_PRIME;
     455                hval *= FNV_32_PRIME;
    456456#else
    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;
     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;
    464464}
    465465//////////////////////////////////////////////////
     
    468468uint32_t SString::hash() const
    469469{
    470 return fnv_32a_buf(txt,used,FNV1_32A_INIT);
     470        return fnv_32a_buf(txt, used, FNV1_32A_INIT);
    471471}
    472472#else
    473473uint32_t SBuf::hash() const
    474474{
    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  
    3232class SBuf
    3333{
    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) {}
     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) {}
    4747public:
    48 SBuf();
    49 ~SBuf();
    50 uint32_t hash() const; // 32-bit FNV-1 hash -> http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash
     48        SBuf();
     49        ~SBuf();
     50        uint32_t hash() const; // 32-bit FNV-1 hash -> http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash
    5151};
    5252
     
    5656{
    5757private:
    58 SBuf *buf;      ///< buffer
    59 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;
    6161
    62 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
     62        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
    6868
    6969public:
    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();
     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();
    7676
    77 void copyFrom(const char* ch, int chlen=-1); ///< copy string, length of -1 == unknown
     77        void copyFrom(const char* ch, int chlen = -1); ///< copy string, length of -1 == unknown
    7878
    79 void* operator new(size_t s, void* mem) {return mem;}
     79        void* operator new(size_t s, void* mem){ return mem; }
    8080#ifdef _MSC_VER
    81 void operator delete(void* mem, void* t) {}
     81                void operator delete(void* mem, void* t) {}
    8282#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); }
    8585
    86 int len() const {return buf->used;} ///< get string length
    87 void shrink(); ///< free unnecessary buffer
     86        int len() const { return buf->used; } ///< get string length
     87        void shrink(); ///< free unnecessary buffer
    8888
    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)
     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)
    134134
    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;
    138138
    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;
    142142
    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;
    146146
    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);
     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);
    152152
    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 string
    156 void operator+=(const char*); ///< append char* contents
    157 void operator+=(const SString&); ///< append other SString
     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 string
     156        void operator+=(const char*); ///< append char* contents
     157        void operator+=(const SString&); ///< append other SString
    158158
    159 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
     159        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
    164164
    165 /// return a substring of the current string
    166 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;
    167167
    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;
     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;
    174174
    175 void operator+=(char ch) {directAppend(1)[0]=ch;endAppend(1);} ///< append single character
     175        void operator+=(char ch) { directAppend(1)[0] = ch; endAppend(1); } ///< append single character
    176176
    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(); }
    180180
    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, ...);
     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, ...);
    188188
    189 static SString &empty();
     189        static SString &empty();
    190190};
    191191
  • cpp/frams/util/usertags.h

    r286 r793  
    4141 */
    4242
    43 template<class ID,class T,int N> class UserTags
     43template<class ID, class T, int N> class UserTags
    4444{
    45 static char reg[N];
    46 T data[N];
    47   public:
    48 UserTags() {memset(data,0,sizeof(data));}
     45        static char reg[N];
     46        T data[N];
     47public:
     48        UserTags() { memset(data, 0, sizeof(data)); }
    4949
    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()
    5852        {
    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;
    6156        }
    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; }
    6468};
    6569
    6670#endif
    67 
    68 
    69 
    70 
    71 
  • cpp/frams/util/validitychecks.cpp

    r651 r793  
    77#include <common/log.h>
    88
    9 bool listIndexCheck(SList* list,int index,const char* msgobj,const char* msgfun)
     9bool listIndexCheck(SList* list, int index, const char* msgobj, const char* msgfun)
    1010{
    11         int size=list->size();
    12 if ((index<0)||(index>=size))
     11        int size = list->size();
     12        if ((index < 0) || (index >= size))
    1313        {
    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;
     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;
    1919        }
    20 return true;
     20        return true;
    2121}
    2222
    23 SString stringCheck(SString& in,const char* msgobj,const char* msgfun,const char* msg,SString (*checker)(const SString& in))
     23SString stringCheck(SString& in, const char* msgobj, const char* msgfun, const char* msg, SString(*checker)(const SString& in))
    2424{
    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)
    2929        {
    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());
    3232        }
    33 return corrected;
     33        return corrected;
    3434}
  • cpp/frams/util/validitychecks.h

    r512 r793  
    99#include <frams/util/sstring.h>
    1010
    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);
     11bool listIndexCheck(SList* list, int index, const char* msgobj, const char* msgfun);
     12SString stringCheck(SString& in, const char* msgobj, const char* msgfun, const char* msg, SString(*checker)(const SString& in) = NULL);
    1313
    1414#endif
Note: See TracChangeset for help on using the changeset viewer.