fixed refcount of eListboxPythonStringContent
[enigma2.git] / lib / base / nconfig.h
1 #ifndef NC_NCONFIG_H
2 #define NC_NCONFIG_H    1
3
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 /*
8  * Superblock definitions
9  */
10 #define NC_SB_MAGIC             ("\0\11\22")                    // Superblock identifier
11
12 /*
13  * Key type definitions
14  */
15 #define NC_DIR                  0x01    // The key is a directory
16 #define NC_STRING               0x02    // The key contains a string
17 #define NC_INT                  0x03    // The key contains a signed integer
18 #define NC_UINT                 0x04    // The key contains an unsigned integer
19 #define NC_RAW                  0x05    // The key contains raw data
20 #define NC_DOUBLE               0x06    // The key contains a double
21 #define NC_LINK                 0x07    // The key points somewhere else
22
23 /*
24  * File access definitions
25  */
26 #define NC_O_RO                 0x01    // Open file in read-only mode
27 #define NC_O_RW                 0x02    // Open file in read-write mode
28
29 /*
30  * Lock types
31  */
32 #define NC_L_NONE               0x00    // No lock
33 #define NC_L_RO                 0x01    // Read-only lock
34 #define NC_L_RW                 0x02    // Read-write lock
35
36 /*
37  * Error codes
38  */
39 #define NC_ERR_OK                       0               // Everything is OK
40 #define NC_ERR_TYPE              -1             // Type mismatch
41 #define NC_ERR_NDIR              -2             // Key is not a directory
42 #define NC_ERR_PERM              -3             // Operation is not allowed
43 #define NC_ERR_NMEM              -4             // Not enough memory to complete operation
44 #define NC_ERR_NEXIST    -5             // Key does not exist
45 #define NC_ERR_NFILE     -6             // No file is assigned/open
46 #define NC_ERR_CORRUPT -7               // File is corrupted
47 #define NC_ERR_NVAL              -8             // Invalid value
48 #define NC_ERR_RDONLY    -9             // File is open in read-only mode
49 #define NC_ERR_NOSUPPORT -10    // Support is not compiled-in
50
51 /*
52  * Truth value definitions
53  */
54 #ifndef TRUE
55 #define TRUE                    1
56 #endif
57 #ifndef FALSE
58 #define FALSE                   0
59 #endif
60
61 /*
62  * Header of the config file.
63  */
64 struct nc_sb_s {
65         char magic[4];          // superblock magic
66         unsigned size;          // Current file size
67         unsigned ent_inc;       // directory increment
68         unsigned chunk_inc;     // Memory chunks increment
69         unsigned size_inc;      // file size increment
70         unsigned chunk_ttl;     // size of chunkmap
71         unsigned chunk;         // pointer to chunkmap
72         unsigned root;          // pointer to root direntry
73         unsigned modtime;       // file version
74 };
75
76 /*
77  * Free chunk descriptor
78  */
79 struct nc_chunk_s {
80         unsigned offset;
81         unsigned size;
82 };
83
84 /*
85  *  In-file directory entry
86  */
87 struct nc_de_s {
88         unsigned name;
89         unsigned type;
90         unsigned parent;
91         unsigned offset;
92         unsigned pages;
93         unsigned crc;
94 };
95
96 /*
97  * Ls reporting
98  */
99 struct nc_ls_s {
100         const char *name;
101         unsigned type;
102 };
103
104 class NConfig
105 {
106 public:
107                 /*
108                  * Class constructor
109                  * pass TRUE as parameter to enable
110                  * write protection when leaving library
111                  */
112                 NConfig(int protect = FALSE);
113                 virtual ~NConfig();
114                 
115                 /*
116                  * Set file name (prior to open)
117                  * Errors:
118                  * NC_ERR_PERM  file is already open
119                  * NC_ERR_NVAL  no file name is given
120                  */
121                 int setName(const char *name);
122
123                 /*
124                  * Open the configuration file, re-open it
125                  * Errors:
126                  * NC_ERR_NFILE         no file name is assigned
127                  * NC_ERR_TYPE          file open mode is invalid
128                  * NC_ERR_PERM          file cannot be opened/created
129                  * NC_ERR_NMEM          unable to mmap the file
130                  * NC_ERR_CORRUPT       superblock magic mismatch
131                  */
132                 int open(const int how = NC_O_RW);
133
134                 /*
135                  * Close the configuration file
136                  * No errors defined
137                  */
138                 void close();
139                 
140                 void flush(); // flush file if not mmap'ed
141                 
142                 /*
143                  * Create a new file
144                  * resize is filesize increment is system pages
145                  * dirent is directory increment
146                  * mchunks is memory block increment
147                  * Errors:
148                  * NC_ERR_PERM          file already exists
149                  * NC_ERR_NFILE         cannot create new file
150                  * NC_ERR_NVAL          file is already open
151                  */
152                 int createNew(unsigned resize = 4, unsigned dirent = 32, unsigned mchunks = 32);
153
154                 /*
155                  * Get an unsigned integer
156                  * Errors:
157                  * NC_ERR_NFILE         no file is open
158                  * NC_ERR_NEXIST        the key does not exist
159                  * NC_ERR_TYPE          the key exists, but is of different type
160                  */
161                 int getKey(const char *name, unsigned &value);
162                 int getKey(const char *name, unsigned long long &value);
163
164                 /*
165                  * Get a signed integer
166                  * Errors:
167                  * NC_ERR_NFILE         no file is open
168                  * NC_ERR_NEXIST        the key does not exist
169                  * NC_ERR_TYPE          the key exists, but is of different type
170                  */
171                 int getKey(const char *name, int &value);
172                 int getKey(const char *name, signed long long &value);
173
174                 /*
175                  * Get a string
176                  * Errors:
177                  * NC_ERR_NFILE         no file is open
178                  * NC_ERR_NEXIST        the key does not exist
179                  * NC_ERR_TYPE          the key exists, but is of different type
180                  */
181                 int getKey(const char *name, char *&value);
182
183                 /*
184                  * Get a long double
185                  * Errors:
186                  * NC_ERR_NFILE         no file is open
187                  * NC_ERR_NEXIST        the key does not exist
188                  * NC_ERR_TYPE          the key exists, but is of different type
189                  */
190                 int getKey(const char *name, double &value);
191                 int getKey(const char *name, long double &value);
192                 
193                 /*
194                  * Get raw data
195                  * Errors:
196                  * NC_ERR_NFILE         no file is open
197                  * NC_ERR_NEXIST        the key does not exist
198                  * NC_ERR_TYPE          the key exists, but is of different type
199                  */
200                 int getKey(const char *name, char *&value, unsigned &len);
201
202                 /*
203                  * Insert an unsigned integer
204                  * NC_ERR_NFILE         no file is open
205                  * NC_ERR_RDONLY        file is open in read-only mode
206                  * NC_ERR_PERM          intermediate key is not a directory
207                  * NC_ERR_TYPE          key already exists, but is not an usigned integer
208                  * NC_ERR_NEXIST        key does not exist (should NEVER happen)
209                  */
210                 int setKey(const char *name, const unsigned value);
211                 int setKey(const char *name, const unsigned long long value);
212                 
213                 /*
214                  * Insert an integer
215                  * NC_ERR_NFILE         no file is open
216                  * NC_ERR_RDONLY        file is open in read-only mode
217                  * NC_ERR_PERM          intermediate key is not a directory
218                  * NC_ERR_TYPE          key already exists, but is not a signed integer
219                  * NC_ERR_NEXIST        key does not exist (should NEVER happen)
220                  */
221                 int setKey(const char *name, const int value);
222                 int setKey(const char *name, const signed long long value);
223                 
224                 /*
225                  * Insert a string
226                  * NC_ERR_NFILE         no file is open
227                  * NC_ERR_RDONLY        file is open in read-only mode
228                  * NC_ERR_PERM          intermediate key is not a directory
229                  * NC_ERR_TYPE          key already exists, but is not a string
230                  * NC_ERR_NEXIST        key does not exist (should NEVER happen)
231                  */
232                 int setKey(const char *name, const char *value);
233
234                 /*
235                  * Insert raw data
236                  * NC_ERR_NFILE         no file is open
237                  * NC_ERR_RDONLY        file is open in read-only mode
238                  * NC_ERR_PERM          intermediate key is not a directory
239                  * NC_ERR_TYPE          key already exists, but is not raw data
240                  * NC_ERR_NEXIST        key does not exist (should NEVER happen)
241                  */
242                 int setKey(const char *name, const char *value, const unsigned len);
243                 
244                 /*
245                  * Insert a double
246                  * NC_ERR_NFILE         no file is open
247                  * NC_ERR_RDONLY        file is open in read-only mode
248                  * NC_ERR_PERM          intermediate key is not a directory
249                  * NC_ERR_TYPE          key already exists, but is not raw data
250                  * NC_ERR_NEXIST        key does not exist (should NEVER happen)
251                  */
252                 int setKey(const char *name, const double value);
253                 int setKey(const char *name, const long double value);
254                 
255                 /*
256                  * Rename a key
257                  * Errors:
258                  * NC_ERR_NFILE         no file is open
259                  * NC_ERR_RDONLY        file is open read-only
260                  * NC_ERR_NEXIST        the key does not exist
261                  * NC_ERR_PERM          key with specified name already exists
262                  */
263                 int renameKey(const char *oldname, const char *newname);
264
265                 /*
266                  * Delete a key
267                  * No errors defined
268                  */
269                 void delKey(const char *name);
270
271                 /*
272                  * Create a directory
273                  * entries parameter specifies number of direntries to preallocate
274                  * Errors:
275                  * NC_ERR_NFILE         no file is open
276                  * NC_ERR_RDONLY        file is open in read-only mode
277                  * NC_ERR_PERM          intermediate key is not a directory
278                  * NC_ERR_TYPE          key already exists, but is not a directory
279                  * NC_ERR_NEXIST        key does not exist (should NEVER happen)
280                  */
281                 int createDir(const char *name, unsigned entries = 0);
282
283                 /*
284                  * Change working directory
285                  * Errors:
286                  * NC_ERR_NFILE         no file is open
287                  * NC_ERR_NDIR          target key is not a directory
288                  * NC_ERR_NEXIST        target key direcotry does not exist
289                  */
290                 int chDir(const char *name);
291
292                 /*
293                  * Print working directory
294                  * Errors:
295                  * Returns NULL on error
296                  */
297                 const char *pwDir();
298
299                 /*
300                  * List all keys in current/specified directory
301                  * Result is a NULL-terminated array of nc_ls_s
302                  * structures.
303                  * Errors:
304                  * Returns NULL on error
305                  * Note:
306                  * You need to free the returned pointer,
307                  * as well as all the names in it.
308                  */
309                 struct nc_ls_s *ls(const char *dir = NULL);
310         
311                 /*
312                  * Lock file
313                  * This will block until lock becomes available
314                  * type is either:
315                  *              NC_L_RO for read-only lock
316                  *              NC_L_RW for read-write lock
317                  * No errors defined
318                  *
319                  * NOTE: lock may get promoted
320                  */
321                 void lockFile(int type, int force = FALSE);
322
323                 /*
324                  * Unlock file
325                  * No errors defined
326                  */
327                 void unLockFile();
328         
329                 /*
330                  * Print out (to stderr) information about current file
331                  * No errors defined
332                  */
333                 void status();
334
335                 /*
336                  * Return version string
337                  */
338                 static char *version();
339
340                 /*
341                  * Dump current file to XML
342                  * Errors:
343                  * NC_ERR_NFILE         no file is open
344                  * NC_ERR_PERM          could not write XML output
345                  */
346                 int toXML(const char *filename);
347
348                 /*
349                  * Load XML to current file
350                  * a file has to be open
351                  * force can be
352                  *      TRUE - existing keys will be deleted
353                  *      FALSE - import will ignore a key if existing key type conflicts
354                  * Errors:
355                  * NC_ERR_NFILE         no file is open
356                  * NC_ERR_PERM          file is open read-only
357                  */
358                 int fromXML(const char *filename, int force = TRUE);
359                 
360 protected:
361                 int fd, omode, lock, careful;
362                 char *fname, *data, *cname;
363                 unsigned lsize, update;
364                 unsigned revision, olck;
365                 struct nc_sb_s *sb;
366                 struct nc_de_s *rdir, *cdir;
367                 struct nc_chunk_s *chunks;
368                 
369                 int _setKey(const char *, const unsigned, const char *, const unsigned);
370                 void _delKey(const char *, struct nc_de_s *, int);
371                 void expand(unsigned);
372                 
373                 void fast_free(unsigned);
374
375                 unsigned getChunk(unsigned);
376                 void freeChunk(unsigned);
377                 static inline unsigned alignSize(unsigned);
378
379                 struct nc_de_s *getDirEnt(const char *, struct nc_de_s * = NULL);
380                 struct nc_de_s *insert(unsigned, struct nc_de_s *);
381                 char *canonize(const char *);
382                 char *getName(const struct nc_de_s *);
383
384                 void _remap(const size_t, const size_t);
385
386                 inline unsigned crc(const char *, unsigned);
387                 void store(nc_de_s *, FILE *);
388                 void restore(void *, int);
389 };
390
391 #endif /* NC_NCONFIG_H */
392