1 /** \file lstridmap.h 2 \brief Name <-> Id map 3 4 CoolReader Engine DOM Tree 5 6 Implements mapping between Name and Id 7 8 (c) Vadim Lopatin, 2000-2006 9 10 This source code is distributed under the terms of 11 GNU General Public License. 12 13 See LICENSE file for details. 14 15 */ 16 17 #ifndef __LSTR_ID_MAP_H__INCLUDED__ 18 #define __LSTR_ID_MAP_H__INCLUDED__ 19 20 #include "lvstring.h" 21 #include <stdio.h> 22 23 struct css_elem_def_props_t; 24 class SerialBuf; 25 26 //=========================================== 27 class LDOMNameIdMapItem 28 { 29 /// custom data pointer 30 css_elem_def_props_t * data; 31 public: 32 /// id 33 lUInt16 id; 34 /// value 35 lString32 value; 36 /// constructor 37 LDOMNameIdMapItem(lUInt16 _id, const lString32 & _value, const css_elem_def_props_t * _data); 38 /// copy constructor 39 LDOMNameIdMapItem(LDOMNameIdMapItem & item); 40 /// destructor 41 ~LDOMNameIdMapItem(); 42 getData()43 const css_elem_def_props_t * getData() const { return data; } 44 45 /// serialize to byte array (pointer will be incremented by number of bytes written) 46 void serialize( SerialBuf & buf ); 47 /// deserialize from byte array (pointer will be incremented by number of bytes read) 48 static LDOMNameIdMapItem * deserialize( SerialBuf & buf ); 49 }; 50 //=========================================== 51 52 //=========================================== 53 class LDOMNameIdMap 54 { 55 private: 56 LDOMNameIdMapItem * * m_by_id; 57 LDOMNameIdMapItem * * m_by_name; 58 lUInt16 m_count; // non-empty count 59 lUInt16 m_size; // max number of ids 60 bool m_sorted; 61 bool m_changed; 62 63 void Sort(); 64 public: 65 /// Main constructor 66 LDOMNameIdMap( lUInt16 maxId ); 67 /// Copy constructor 68 LDOMNameIdMap( LDOMNameIdMap & map ); 69 ~LDOMNameIdMap(); 70 71 /// serialize to byte array (pointer will be incremented by number of bytes written) 72 void serialize( SerialBuf & buf ); 73 /// deserialize from byte array (pointer will be incremented by number of bytes read) 74 bool deserialize( SerialBuf & buf ); 75 76 void Clear(); 77 78 void AddItem( lUInt16 id, const lString32 & value, const css_elem_def_props_t * data ); 79 80 void AddItem( LDOMNameIdMapItem * item ); 81 findItem(lUInt16 id)82 const LDOMNameIdMapItem * findItem( lUInt16 id ) const 83 { 84 return m_by_id[id]; 85 } 86 87 const LDOMNameIdMapItem * findItem( const lChar32 * name ); 88 const LDOMNameIdMapItem * findItem( const lChar8 * name ); findItem(const lString32 & name)89 const LDOMNameIdMapItem * findItem( const lString32 & name ) { return findItem(name.c_str()); } 90 idByName(const lChar32 * name)91 inline lUInt16 idByName( const lChar32 * name ) 92 { 93 const LDOMNameIdMapItem * item = findItem(name); 94 return item?item->id:0; 95 } 96 idByName(const lChar8 * name)97 inline lUInt16 idByName( const lChar8 * name ) 98 { 99 const LDOMNameIdMapItem * item = findItem(name); 100 return item?item->id:0; 101 } 102 nameById(lUInt16 id)103 inline const lString32 & nameById( lUInt16 id ) 104 { 105 if (id>=m_size) 106 return lString32::empty_str; 107 const LDOMNameIdMapItem * item = findItem(id); 108 return item?item->value:lString32::empty_str; 109 } 110 dataById(lUInt16 id)111 inline const css_elem_def_props_t * dataById( lUInt16 id ) 112 { 113 if (id>=m_size) 114 return NULL; 115 const LDOMNameIdMapItem * item = findItem(id); 116 return item ? item->getData() : NULL; 117 } 118 119 // debug dump of all unknown entities 120 void dumpUnknownItems( FILE * f, int start_id ); 121 lString32 getUnknownItems( int start_id ); 122 }; 123 124 #endif 125