CoreStorageFolder.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //============================================================================================
  2. // Spirenkov Maxim, 2004, 2008
  3. //============================================================================================
  4. // CoreStorageFolder
  5. //============================================================================================
  6. #ifndef _CoreStorageFolder_h_
  7. #define _CoreStorageFolder_h_
  8. #include "..\..\common_h\core.h"
  9. class CoreStorageFolder
  10. {
  11. struct Item
  12. {
  13. long nameId; //Идентификатор имени в общем массиве имён
  14. ICoreStorageItem::Type type; //Тип итема
  15. union //Возможные вариации итема в зависимости от type
  16. {
  17. dword vString; //Индекс строки в массиве строк
  18. long vLong; //long
  19. float vFloat; //float
  20. CoreStorageFolder * vFolder; //Указатель на дочернюю папку
  21. };
  22. };
  23. //------------------------------------------------------------------------------------
  24. public:
  25. class WriteStream
  26. {
  27. public:
  28. WriteStream(array<byte> & _data);
  29. void WriteByte(const byte & b);
  30. void WriteDWord(const dword & dw);
  31. void WriteFloat(const float & fl);
  32. void WriteString(const char * str);
  33. void WriteTable(const void * buffer, dword size);
  34. void PinSizePosition();
  35. void SaveSize();
  36. private:
  37. array<byte> & data;
  38. long offset;
  39. };
  40. class ReadStream
  41. {
  42. public:
  43. ReadStream(dword & _pointer, const void * _data, dword _size);
  44. bool ReadByte(byte & b);
  45. bool ReadDWord(dword & dw);
  46. bool ReadFloat(float & fl);
  47. bool ReadString(string & str);
  48. bool ReadTable(void * buffer, dword size);
  49. bool PinSizePosition();
  50. bool CheckSize();
  51. private:
  52. dword & pointer;
  53. const byte * data;
  54. dword size;
  55. long offset;
  56. dword offsetSize;
  57. };
  58. //------------------------------------------------------------------------------------
  59. public:
  60. CoreStorageFolder(CoreStorageFolder * p, long nId);
  61. ~CoreStorageFolder();
  62. public:
  63. //Получить по пути итем, если объекты не созданы, попытаться создать
  64. //Если встречается на месте нужного итем другого типа то возвращает индекс этого итем и папку null
  65. //Если в режиме isCreatePath == false передать тип t_error, то последний итем не проверяеться на тип
  66. CoreStorageFolder * FindItem(const array<long> & p, ICoreStorageItem::Type type, bool isCreatePath, long & index);
  67. //Заполнить полный путь
  68. void BuildFullPath(array<long> & p, long size);
  69. public:
  70. //Установить элементу массива строковое поле
  71. void SetString(long index, const char * value);
  72. //Получить строку
  73. const char * GetString(long index);
  74. //Установить целочисленное поле
  75. void SetLong(long index, long value);
  76. //Получить целочисленное поле
  77. long GetLong(long index);
  78. //Установить численное поле
  79. void SetFloat(long index, float value);
  80. //Получить численное поле
  81. float GetFloat(long index);
  82. //Получить папку по индексу
  83. CoreStorageFolder * GetFolder(long index);
  84. public:
  85. //Получить количество полей данной групы
  86. dword GetCount();
  87. //Получить идентификатор имени поля по индексу
  88. long GetNameByIndex(long index);
  89. //Получить тип поля по индексу
  90. ICoreStorageItem::Type GetTypeByIndex(long index);
  91. public:
  92. //Вывести содержимое в строку, (перевод строки \n)
  93. void Print(long tab, string & buffer, long index);
  94. //Сохранить группу
  95. void Save(long index, WriteStream & stream);
  96. //Прочитать группу
  97. bool Load(ReadStream & stream);
  98. //Удалить все дочернии поля
  99. void Delete(long index);
  100. //------------------------------------------------------------------------------------
  101. protected:
  102. //Сохранить в поток поле
  103. void SaveItem(Item & item, WriteStream & stream);
  104. //Вывести элемент в буфер
  105. void PrintItemToBuffer(long tab, string & buffer, Item & item);
  106. protected:
  107. array<Item> items; //Элементы папки
  108. array<string> strings; //Используемые строки
  109. CoreStorageFolder * parent; //Родительская папка
  110. long nameId; //Идентификатор имени
  111. };
  112. //------------------------------------------------------------------------------------
  113. //Установить элементу массива строковое поле
  114. inline void CoreStorageFolder::SetString(long index, const char * value)
  115. {
  116. Item & item = items[index];
  117. Assert(item.type == ICoreStorageItem::t_string);
  118. strings[item.vString] = value;
  119. }
  120. //Получить строку
  121. inline const char * CoreStorageFolder::GetString(long index)
  122. {
  123. Item & item = items[index];
  124. Assert(item.type == ICoreStorageItem::t_string);
  125. return strings[item.vString].c_str();
  126. }
  127. //Установить целочисленное поле
  128. inline void CoreStorageFolder::SetLong(long index, long value)
  129. {
  130. Item & item = items[index];
  131. Assert(item.type == ICoreStorageItem::t_long);
  132. item.vLong = value;
  133. }
  134. //Получить целочисленное поле
  135. inline long CoreStorageFolder::GetLong(long index)
  136. {
  137. Item & item = items[index];
  138. Assert(item.type == ICoreStorageItem::t_long);
  139. return item.vLong;
  140. }
  141. //Установить численное поле
  142. inline void CoreStorageFolder::SetFloat(long index, float value)
  143. {
  144. Item & item = items[index];
  145. Assert(item.type == ICoreStorageItem::t_float);
  146. item.vFloat = value;
  147. }
  148. //Получить численное поле
  149. inline float CoreStorageFolder::GetFloat(long index)
  150. {
  151. Item & item = items[index];
  152. Assert(item.type == ICoreStorageItem::t_float);
  153. return item.vFloat;
  154. }
  155. //Получить папку по индексу
  156. inline CoreStorageFolder * CoreStorageFolder::GetFolder(long index)
  157. {
  158. Item & item = items[index];
  159. Assert(item.type == ICoreStorageItem::t_folder);
  160. return item.vFolder;
  161. }
  162. //Получить количество полей данной групы
  163. inline dword CoreStorageFolder::GetCount()
  164. {
  165. return items.Size();
  166. }
  167. //Получить идентификатор имени поля по индексу
  168. inline long CoreStorageFolder::GetNameByIndex(long index)
  169. {
  170. if(index < 0 || index >= items)
  171. {
  172. return -1;
  173. }
  174. return items[index].nameId;
  175. }
  176. //Получить тип поля по индексу
  177. inline ICoreStorageItem::Type CoreStorageFolder::GetTypeByIndex(long index)
  178. {
  179. if(index < 0 || index >= items)
  180. {
  181. return ICoreStorageItem::t_error;
  182. }
  183. return items[index].type;
  184. }
  185. #endif