Subversion Repositories Kolibri OS

Rev

Rev 7286 | Rev 7878 | 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 = data_start = element_offset[count] = count = 0;
  71. }
  72.  
  73.  
  74. /*========================================================
  75. =                                                        =
  76. =                       Integer                          =
  77. =                                                        =
  78. ========================================================*/
  79.  
  80. struct collection_int
  81. {
  82.         int count;
  83.         dword element[4096*3];
  84.         int add();
  85.         dword get();
  86.         dword get_last();
  87.         void pop();
  88.         void drop();
  89. };
  90.  
  91. :int collection_int::add(dword in) {
  92.         if (count >= 4096*3) return 0;
  93.         element[count] = in;
  94.         count++;
  95.         return 1;
  96. }
  97.  
  98. :dword collection_int::get(dword pos) {
  99.         if (pos<0) || (pos>=count) return 0;
  100.         return element[pos];
  101. }
  102.  
  103. :dword collection_int::get_last() {
  104.         return element[count];
  105. }
  106.  
  107. :void collection_int::pop() {
  108.         if (count>0) count--;
  109. }
  110.  
  111. :void collection_int::drop() {
  112.         element[0] =
  113.         count = 0;
  114. }
  115.  
  116. #endif