Subversion Repositories Kolibri OS

Rev

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