Subversion Repositories Kolibri OS

Rev

Rev 5974 | Rev 6738 | 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. struct collection
  6. {
  7.         int realloc_size, count;
  8.         dword data_start;
  9.         dword data_size;
  10.         dword element_offset[4090];
  11.         int add();
  12.         dword get();
  13.         void drop();
  14.         void increase_data_size();
  15. };
  16.  
  17. void collection::increase_data_size() {
  18.         int filled_size;
  19.         if (realloc_size<4096) realloc_size = 4096;
  20.         if (!data_size) {
  21.                 data_size = realloc_size;
  22.                 data_start = malloc(realloc_size);             
  23.         }
  24.         else {
  25.                 data_size = data_size + realloc_size;
  26.                 data_start = realloc(data_start, data_size);
  27.         }
  28. }
  29.  
  30. int collection::add(dword in) {
  31.         if (count >= 4090) return 0;
  32.         if (element_offset[count]+strlen(in)+2 > data_size) {
  33.                 increase_data_size();
  34.                 add(in);
  35.                 return;
  36.         }
  37.         strcpy(data_start+element_offset[count], in);
  38.         count++;
  39.         element_offset[count] = element_offset[count-1] + strlen(in) + 1;
  40.         return 1;
  41. }
  42.  
  43. dword collection::get(dword pos) {
  44.         if (pos<0) || (pos>=count) return 0;
  45.         return data_start + element_offset[pos];
  46. }
  47.  
  48. void collection::drop() {
  49.         if (data_start) free(data_start);
  50.         data_size = data_start = element_offset[count] = count = 0;
  51. }
  52.  
  53. #endif