1 /** \file
2  * \brief Attributes Table.
3  *
4  * See Copyright Notice in im_lib.h
5  */
6 module im.im_attrib;
7 
8 version(IM) :
9 
10 import im.im_attrib_flat;
11 
12 version(DigitalMars) version(Windows) { pragma(lib, "im.lib"); }
13 
14 /** \brief Attributes Table Class
15  *
16  * \par
17  * All the attributes have a name, a type, a count and the data.\n
18  * Names are usually strings with less that 30 chars.
19  * \par
20  * Attributes are stored in a hash table for fast access. \n
21  * We use the hash function described in "The Practice of Programming" of Kernighan & Pike.
22  * \ingroup util */
23 
24 class imAttribTable
25 {
26 private:
27   imAttribTablePrivate* ptable;
28 public:
29 
30   /** Creates an empty table.
31    * If size is zero the default size of 101 is used. Size must be a prime number.
32    * Other common values are 67, 599 and 1499.*/
33   this(int hash_size)
34     { ptable = imAttribTableCreate(hash_size); }
35 
36   /** Destroys the table and all the attributes. */
37   ~this()
38     { imAttribTableDestroy(ptable); ptable = null; }
39 
40   /** Returns the number of elements in the table. */
41   int Count()
42     { return imAttribTableCount(ptable); }
43 
44   /** Removes all the attributes in the table */
45   void RemoveAll()
46     { imAttribTableRemoveAll(ptable); }
47 
48   /** Copies the contents of the given table into this table. */
49   void CopyFrom(imAttribTable table)
50     { imAttribTableCopyFrom(ptable, table.ptable); }
51 
52   /** Merges the contents of the given table into this table. */
53   void MergeFrom(imAttribTable table)
54     { imAttribTableMergeFrom(ptable, table.ptable); }
55 
56   /** Inserts an attribute into the table. \n
57    * If data_type is BYTE then count can be -1 to indicate a NULL terminated string.
58    * Data is duplicated if not NULL, else data is initialized with zeros.
59    * See also \ref imDataType. */
60   void Set(const(char)* name, int data_type, int count, const(void)* data)
61     { imAttribTableSet(ptable, name, data_type, count, data); }
62 
63   /** Inserts a single integer attribute into the table. */
64   void SetInteger(const(char)* name, int data_type, int value)
65     { imAttribTableSetInteger(ptable, name, data_type, value); }
66 
67   /** Inserts a single real attribute into the table. */
68   void SetReal(const(char)* name, int data_type, double value)
69     { imAttribTableSetReal(ptable, name, data_type, value); }
70 
71   /** Inserts a string attribute into the table.
72    * data_type=IM_BYTE and is zero terminated. */
73   void SetString(const(char)* name, const(char)* value)
74     { imAttribTableSetString(ptable, name, value); }
75 
76   /** Removes an attribute from the table given its name. */
77   void UnSet(const(char)* name)
78     { imAttribTableUnSet(ptable, name); }
79 
80   /** Returns an attribute from the table.
81    * Returns the attribute if found, NULL otherwise.
82    * See also \ref imDataType. */
83   const(void)* Get(const(char)* name, int* data_type = null, int* count = null) const
84     { return imAttribTableGet(ptable, name, data_type, count); }
85 
86   /** Returns the attribute value at given index as an integer.
87    * If not found or complex returns 0. */
88   int GetInteger(const(char)* name, int index = 0)
89     { return imAttribTableGetInteger(ptable, name, index); }
90 
91   /** Returns the attribute value at given index as a real.
92    * If not found or complex returns 0. */
93   double GetReal(const(char)* name, int index = 0)
94     { return imAttribTableGetReal(ptable, name, index); }
95 
96   /** Returns the attribute value as a string.
97    * If not found or not a zero terminated string returns 0. */
98   const(char)* GetString(const(char)* name)
99     { return imAttribTableGetString(ptable, name); }
100 
101   /** For each attribute calls the user callback. If the callback returns 0 the function returns. */
102   void ForEach(void* user_data, imAttribTableCallback attrib_func) const
103     { imAttribTableForEach(ptable, user_data, attrib_func); }
104 }
105 
106 
107 /** \brief Attributes Array Class
108  *
109  * \par
110  * Same as \ref imAttribTable, but uses an array of fixed size.
111  * \ingroup util */
112 class imAttribArray
113 {
114 private:
115   imAttribTablePrivate* ptable;
116 public:
117 
118   /** Creates an empty array. */
119   this(int count)
120     { ptable = imAttribArrayCreate(count); }
121 
122   /** Destroys the array and all the attributes. */
123   ~this()
124     { imAttribTableDestroy(ptable); ptable = null; }
125 
126   /** Returns the number of elements in the array. */
127   int Count() /*const*/
128     { return imAttribTableCount(ptable); }
129 
130   /** Removes all the attributes in the array */
131   void RemoveAll()
132     { imAttribTableRemoveAll(ptable); }
133 
134   /** Copies the contents of the given table into this table. */
135   void CopyFrom(imAttribArray table)
136     { imAttribArrayCopyFrom(ptable, table.ptable); }
137 
138   /** Inserts one attribute into the array.
139    * The attribute data is a simple array of data_type elements of count length. \n
140    * Data is duplicated if not NULL, else data is initialized with zeros.
141    * When NULL is specified use the Get method to retrieve a pointer to the data
142    * so you can initialize it with other values.
143    * See also \ref imDataType. */
144   void Set(int index, const(char)* name, int data_type, int count, const void* data)
145     { imAttribArraySet(ptable, index, name, data_type, count, data); }
146 
147   /** Finds one attribute in the array.
148    * Returns the attribute if found, NULL otherwise.
149    * See also \ref imDataType. */
150   const(void)* Get(int index, char* name = null, int* data_type = null, int* count = null) const
151     { return imAttribArrayGet(ptable, index, name, data_type, count); }
152 
153   /** For each attribute calls the user callback. If the callback returns 0 the function returns. */
154   void ForEach(void* user_data, imAttribTableCallback attrib_func) const
155     { imAttribTableForEach(ptable, user_data, attrib_func); }
156 }