8 * Superblock definitions
10 #define NC_SB_MAGIC ("\0\11\22") // Superblock identifier
13 * Key type definitions
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
24 * File access definitions
26 #define NC_O_RO 0x01 // Open file in read-only mode
27 #define NC_O_RW 0x02 // Open file in read-write mode
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
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
52 * Truth value definitions
62 * Header of the config file.
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
77 * Free chunk descriptor
85 * In-file directory entry
109 * pass TRUE as parameter to enable
110 * write protection when leaving library
112 NConfig(int protect = FALSE);
116 * Set file name (prior to open)
118 * NC_ERR_PERM file is already open
119 * NC_ERR_NVAL no file name is given
121 int setName(const char *name);
124 * Open the configuration file, re-open it
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
132 int open(const int how = NC_O_RW);
135 * Close the configuration file
140 void flush(); // flush file if not mmap'ed
144 * resize is filesize increment is system pages
145 * dirent is directory increment
146 * mchunks is memory block increment
148 * NC_ERR_PERM file already exists
149 * NC_ERR_NFILE cannot create new file
150 * NC_ERR_NVAL file is already open
152 int createNew(unsigned resize = 4, unsigned dirent = 32, unsigned mchunks = 32);
155 * Get an unsigned integer
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
161 int getKey(const char *name, unsigned &value);
162 int getKey(const char *name, unsigned long long &value);
165 * Get a signed integer
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
171 int getKey(const char *name, int &value);
172 int getKey(const char *name, signed long long &value);
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
181 int getKey(const char *name, char *&value);
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
190 int getKey(const char *name, double &value);
191 int getKey(const char *name, long double &value);
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
200 int getKey(const char *name, char *&value, unsigned &len);
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)
210 int setKey(const char *name, const unsigned value);
211 int setKey(const char *name, const unsigned long long value);
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)
221 int setKey(const char *name, const int value);
222 int setKey(const char *name, const signed long long value);
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)
232 int setKey(const char *name, const char *value);
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)
242 int setKey(const char *name, const char *value, const unsigned len);
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)
252 int setKey(const char *name, const double value);
253 int setKey(const char *name, const long double value);
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
263 int renameKey(const char *oldname, const char *newname);
269 void delKey(const char *name);
273 * entries parameter specifies number of direntries to preallocate
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)
281 int createDir(const char *name, unsigned entries = 0);
284 * Change working directory
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
290 int chDir(const char *name);
293 * Print working directory
295 * Returns NULL on error
300 * List all keys in current/specified directory
301 * Result is a NULL-terminated array of nc_ls_s
304 * Returns NULL on error
306 * You need to free the returned pointer,
307 * as well as all the names in it.
309 struct nc_ls_s *ls(const char *dir = NULL);
313 * This will block until lock becomes available
315 * NC_L_RO for read-only lock
316 * NC_L_RW for read-write lock
319 * NOTE: lock may get promoted
321 void lockFile(int type, int force = FALSE);
330 * Print out (to stderr) information about current file
336 * Return version string
338 static char *version();
341 * Dump current file to XML
343 * NC_ERR_NFILE no file is open
344 * NC_ERR_PERM could not write XML output
346 int toXML(const char *filename);
349 * Load XML to current file
350 * a file has to be open
352 * TRUE - existing keys will be deleted
353 * FALSE - import will ignore a key if existing key type conflicts
355 * NC_ERR_NFILE no file is open
356 * NC_ERR_PERM file is open read-only
358 int fromXML(const char *filename, int force = TRUE);
361 int fd, omode, lock, careful;
362 char *fname, *data, *cname;
363 unsigned lsize, update;
364 unsigned revision, olck;
366 struct nc_de_s *rdir, *cdir;
367 struct nc_chunk_s *chunks;
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);
373 void fast_free(unsigned);
375 unsigned getChunk(unsigned);
376 void freeChunk(unsigned);
377 static inline unsigned alignSize(unsigned);
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 *);
384 void _remap(const size_t, const size_t);
386 inline unsigned crc(const char *, unsigned);
387 void store(nc_de_s *, FILE *);
388 void restore(void *, int);
391 #endif /* NC_NCONFIG_H */