1 /** \file
2 * \brief Attributes Table.
3 *
4 * See Copyright Notice in im_lib.h
5 */6 moduleim.im_attrib;
7 8 version(IM) :
9 10 importim.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 classimAttribTable25 {
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(inthash_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 intCount()
42 { returnimAttribTableCount(ptable); }
43 44 /** Removes all the attributes in the table */45 voidRemoveAll()
46 { imAttribTableRemoveAll(ptable); }
47 48 /** Copies the contents of the given table into this table. */49 voidCopyFrom(imAttribTabletable)
50 { imAttribTableCopyFrom(ptable, table.ptable); }
51 52 /** Merges the contents of the given table into this table. */53 voidMergeFrom(imAttribTabletable)
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 voidSet(const(char)* name, intdata_type, intcount, const(void)* data)
61 { imAttribTableSet(ptable, name, data_type, count, data); }
62 63 /** Inserts a single integer attribute into the table. */64 voidSetInteger(const(char)* name, intdata_type, intvalue)
65 { imAttribTableSetInteger(ptable, name, data_type, value); }
66 67 /** Inserts a single real attribute into the table. */68 voidSetReal(const(char)* name, intdata_type, doublevalue)
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 voidSetString(const(char)* name, const(char)* value)
74 { imAttribTableSetString(ptable, name, value); }
75 76 /** Removes an attribute from the table given its name. */77 voidUnSet(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) const84 { returnimAttribTableGet(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 intGetInteger(const(char)* name, intindex = 0)
89 { returnimAttribTableGetInteger(ptable, name, index); }
90 91 /** Returns the attribute value at given index as a real.
92 * If not found or complex returns 0. */93 doubleGetReal(const(char)* name, intindex = 0)
94 { returnimAttribTableGetReal(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 { returnimAttribTableGetString(ptable, name); }
100 101 /** For each attribute calls the user callback. If the callback returns 0 the function returns. */102 voidForEach(void* user_data, imAttribTableCallbackattrib_func) const103 { 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 classimAttribArray113 {
114 private:
115 imAttribTablePrivate* ptable;
116 public:
117 118 /** Creates an empty array. */119 this(intcount)
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 intCount() /*const*/128 { returnimAttribTableCount(ptable); }
129 130 /** Removes all the attributes in the array */131 voidRemoveAll()
132 { imAttribTableRemoveAll(ptable); }
133 134 /** Copies the contents of the given table into this table. */135 voidCopyFrom(imAttribArraytable)
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 voidSet(intindex, const(char)* name, intdata_type, intcount, constvoid* 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(intindex, char* name = null, int* data_type = null, int* count = null) const151 { returnimAttribArrayGet(ptable, index, name, data_type, count); }
152 153 /** For each attribute calls the user callback. If the callback returns 0 the function returns. */154 voidForEach(void* user_data, imAttribTableCallbackattrib_func) const155 { imAttribTableForEach(ptable, user_data, attrib_func); }
156 }