Subversion Repositories Kolibri OS

Rev

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