Subversion Repositories Kolibri OS

Rev

Rev 7771 | Rev 7885 | Go to most recent revision | 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. =                       String                           =
  8. =                                                        =
  9. ========================================================*/
  10.  
  11. struct collection
  12. {
  13.         int realloc_size, count;
  14.         dword data_start;
  15.         dword data_size;
  16.         dword element_offset[4000];
  17.         int add();
  18.         int addn();
  19.         dword get(); //get_name_by_pos
  20.         dword get_pos_by_name();
  21.         void drop();
  22.         void increase_data_size();
  23.         dword get_last();
  24.         bool delete_last();
  25. };
  26.  
  27. :void collection::increase_data_size() {
  28.         int filled_size;
  29.         if (realloc_size<4096) realloc_size = 4096;
  30.         if (!data_size) {
  31.                 data_size = realloc_size;
  32.                 data_start = malloc(realloc_size);             
  33.         }
  34.         else {
  35.                 data_size = data_size + realloc_size;
  36.                 data_start = realloc(data_start, data_size);
  37.         }
  38. }
  39.  
  40. :int collection::add(dword in) {
  41.         return addn(in, strlen(in));
  42. }
  43.  
  44. :int collection::addn(dword in, len) {
  45.         if (count >= 4000) {
  46.                 debugln("collection: more than 4000 elements!");
  47.                 return 0;
  48.         }
  49.         if (element_offset[count]+len+2 > data_size) {
  50.                 increase_data_size();
  51.                 addn(in, len);
  52.                 return 1;
  53.         }
  54.         strncpy(data_start+element_offset[count], in, len);
  55.         count++;
  56.         element_offset[count] = element_offset[count-1] + len + 1;
  57.         return 1;
  58. }
  59.  
  60. :dword collection::get(dword pos) {
  61.         if (pos<0) || (pos>=count) return 0;
  62.         return data_start + element_offset[pos];
  63. }
  64.  
  65. :dword collection::get_last() {
  66.         return get(count-1);
  67. }
  68.  
  69. :dword collection::get_pos_by_name(dword name) {
  70.         dword i;
  71.         for (i=0; i<count; i++) {
  72.                 if (strcmp(data_start + element_offset[i], name)==0) return i;
  73.         }
  74.         return -1;
  75. }
  76.  
  77. :void collection::drop() {
  78.         if (data_start) free(data_start);
  79.         data_size = 0;
  80.         data_start = 0;
  81.         element_offset[count] = 0;
  82.         count = 0;
  83. }
  84.  
  85. :bool collection::delete_last() {
  86.         count--;
  87. }
  88.  
  89. /*========================================================
  90. =                                                        =
  91. =                       Integer                          =
  92. =                                                        =
  93. ========================================================*/
  94.  
  95. struct collection_int
  96. {
  97.         int count;
  98.         dword element[4096*3];
  99.         int add();
  100.         dword get();
  101.         dword get_last();
  102.         void pop();
  103.         void drop();
  104. };
  105.  
  106. :int collection_int::add(dword in) {
  107.         if (count >= 4096*3) return 0;
  108.         element[count] = in;
  109.         count++;
  110.         return 1;
  111. }
  112.  
  113. :dword collection_int::get(dword pos) {
  114.         if (pos<0) || (pos>=count) return 0;
  115.         return element[pos];
  116. }
  117.  
  118. :dword collection_int::get_last() {
  119.         return element[count];
  120. }
  121.  
  122. :void collection_int::pop() {
  123.         if (count>0) count--;
  124. }
  125.  
  126. :void collection_int::drop() {
  127.         element[0] =
  128.         count = 0;
  129. }
  130.  
  131. #endif