Subversion Repositories Kolibri OS

Rev

Rev 7738 | 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. };
  24.  
  25. :void collection::increase_data_size() {
  26.         int filled_size;
  27.         if (realloc_size<4096) realloc_size = 4096;
  28.         if (!data_size) {
  29.                 data_size = realloc_size;
  30.                 data_start = malloc(realloc_size);             
  31.         }
  32.         else {
  33.                 data_size = data_size + realloc_size;
  34.                 data_start = realloc(data_start, data_size);
  35.         }
  36. }
  37.  
  38. :int collection::add(dword in) {
  39.         return addn(in, strlen(in));
  40. }
  41.  
  42. :int collection::addn(dword in, len) {
  43.         if (count >= 4000) return 0;
  44.         if (element_offset[count]+len+2 > data_size) {
  45.                 increase_data_size();
  46.                 addn(in, len);
  47.                 return 1;
  48.         }
  49.         strncpy(data_start+element_offset[count], in, len);
  50.         count++;
  51.         element_offset[count] = element_offset[count-1] + len + 1;
  52.         return 1;
  53. }
  54.  
  55. :dword collection::get(dword pos) {
  56.         if (pos<0) || (pos>=count) return 0;
  57.         return data_start + element_offset[pos];
  58. }
  59.  
  60. :dword collection::get_pos_by_name(dword name) {
  61.         dword i;
  62.         for (i=0; i<count; i++) {
  63.                 if (strcmp(data_start + element_offset[i], name)==0) return i;
  64.         }
  65.         return -1;
  66. }
  67.  
  68. :void collection::drop() {
  69.         if (data_start) free(data_start);
  70.         data_size = 0;
  71.         data_start = 0;
  72.         element_offset[count] = 0;
  73.         count = 0;
  74. }
  75.  
  76.  
  77. /*========================================================
  78. =                                                        =
  79. =                       Integer                          =
  80. =                                                        =
  81. ========================================================*/
  82.  
  83. struct collection_int
  84. {
  85.         int count;
  86.         dword element[4096*3];
  87.         int add();
  88.         dword get();
  89.         dword get_last();
  90.         void pop();
  91.         void drop();
  92. };
  93.  
  94. :int collection_int::add(dword in) {
  95.         if (count >= 4096*3) return 0;
  96.         element[count] = in;
  97.         count++;
  98.         return 1;
  99. }
  100.  
  101. :dword collection_int::get(dword pos) {
  102.         if (pos<0) || (pos>=count) return 0;
  103.         return element[pos];
  104. }
  105.  
  106. :dword collection_int::get_last() {
  107.         return element[count];
  108. }
  109.  
  110. :void collection_int::pop() {
  111.         if (count>0) count--;
  112. }
  113.  
  114. :void collection_int::drop() {
  115.         element[0] =
  116.         count = 0;
  117. }
  118.  
  119. #endif