Subversion Repositories Kolibri OS

Rev

Rev 8330 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef INCLUDE_COLLECTION_H
  2. #define INCLUDE_COLLECTION_H
  3. #print "[include <collection.h>]\n"
  4.  
  5. /*========================================================
  6. =                                                        =
  7. =                       Integer                          =
  8. =                                                        =
  9. ========================================================*/
  10.  
  11. struct collection_int
  12. {
  13.         dword buf;
  14.         dword buf_size;
  15.         unsigned count;
  16.         void alloc();
  17.         void add();
  18.         dword get();
  19.         dword set();
  20.         void swap();
  21.         dword len();
  22.         dword get_last();
  23.         void pop();
  24.         void drop();
  25. };
  26.  
  27. :void collection_int::alloc() {
  28.         if (!buf) {
  29.                 buf_size = 4096;
  30.                 buf = malloc(4096);
  31.         } else {
  32.                 buf_size += 4096;
  33.                 buf = realloc(buf, buf_size);
  34.         }
  35. }
  36.  
  37. :void collection_int::add(dword _in) {
  38.         if (!buf) || (count * sizeof(dword) >= buf_size) alloc();
  39.         EAX = count * sizeof(dword) + buf;
  40.         ESDWORD[EAX] = _in;
  41.         count++;
  42. }
  43.  
  44. :dword collection_int::get(dword pos) {
  45.         if (pos<0) || (pos>=count) return 0;
  46.         return ESDWORD[pos * sizeof(dword) + buf];
  47. }
  48.  
  49.  
  50. :dword collection_int::set(dword pos, _in) {
  51.         while (pos >= count) add(0);
  52.         EAX = pos * sizeof(dword) + buf;
  53.         ESDWORD[EAX] = _in;
  54.         return ESDWORD[EAX];
  55. }
  56.  
  57. :void collection_int::swap(dword pos1, pos2) {
  58.         while (pos1 >= count) add(0);
  59.         while (pos2 >= count) add(0);
  60.         EAX = pos1 * sizeof(dword) + buf;
  61.         EBX = pos2 * sizeof(dword) + buf;
  62.         ESDWORD[EAX] >< ESDWORD[EBX];
  63. }
  64.  
  65. :dword collection_int::len(dword pos) {
  66.         if (pos<0) || (pos+1>=count) return 0;
  67.         return get(pos+1) - get(pos);
  68. }
  69.  
  70. :dword collection_int::get_last() {
  71.         return get(count-1);
  72. }
  73.  
  74. :void collection_int::pop() {
  75.         if (count>0) count--;
  76. }
  77.  
  78. :void collection_int::drop() {
  79.         count = 0;
  80. }
  81.  
  82. /*========================================================
  83. =                                                        =
  84. =                       String                           =
  85. =                                                        =
  86. ========================================================*/
  87.  
  88. struct collection
  89. {
  90.         int realloc_size, count;
  91.         dword data_start;
  92.         dword data_size;
  93.         collection_int offset;
  94.         int add();
  95.         int addn();
  96.         dword get(); //get_name_by_pos
  97.         dword get_pos_by_name();
  98.         void drop();
  99.         void increase_data_size();
  100.         dword get_last();
  101.         bool pop();
  102. };
  103.  
  104. :void collection::increase_data_size() {
  105.         if (realloc_size<4096) realloc_size = 4096;
  106.         if (!data_size) {
  107.                 data_size = realloc_size;
  108.                 data_start = malloc(realloc_size);             
  109.         }
  110.         else {
  111.                 data_size = data_size + realloc_size;
  112.                 data_start = realloc(data_start, data_size);
  113.         }
  114. }
  115.  
  116. :int collection::add(dword in) {
  117.         return addn(in, strlen(in));
  118. }
  119.  
  120. :int collection::addn(dword in, len) {
  121.         if (offset.get(count)+len+2 > data_size) {
  122.                 increase_data_size();
  123.                 addn(in, len);
  124.                 return 1;
  125.         }
  126.         strncpy(data_start+offset.get(count), in, len);
  127.         count++;
  128.         offset.set(count, offset.get(count-1) + len + 1);
  129.         return 1;
  130. }
  131.  
  132. :dword collection::get(dword pos) {
  133.         if (pos<0) || (pos>=count) return 0;
  134.         return data_start + offset.get(pos);
  135. }
  136.  
  137. :dword collection::get_last() {
  138.         return get(count-1);
  139. }
  140.  
  141. :dword collection::get_pos_by_name(dword name) {
  142.         dword i;
  143.         for (i=0; i<count; i++) {
  144.                 if (strcmp(data_start + offset.get(i), name)==0) return i;
  145.         }
  146.         return -1;
  147. }
  148.  
  149. :void collection::drop() {
  150.         if (data_start) free(data_start);
  151.         data_size = 0;
  152.         data_start = 0;
  153.         offset.drop();
  154.         count = 0;
  155. }
  156.  
  157. :bool collection::pop() {
  158.         if (count>0) count--;
  159. }
  160.  
  161. #endif