Git init
[pkgs/e/elektra.git] / src / backends / ini / ini.h
1 /***************************************************************************
2             ini.h  -  Backend for ini-style like files
3                              -------------------
4     begin                : 01.03.2005
5     updated              : 06.10.2005
6     copyright            : (C) 2005 by Markus Raab
7     email                : debian@markus-raab.org
8  ***************************************************************************/
9
10 /***************************************************************************
11  *                                                                         *
12  *   This program is free software; you can redistribute it and/or modify  *
13  *   it under the terms of the BSD License (revised).                      *
14  *                                                                         *
15  ***************************************************************************/
16
17
18
19 /***************************************************************************
20  *
21  *   This is a ini-style backend.
22  *   Key/Value Pairs are stored in files in following scheme:
23  *   
24  *   key1=value1;comment
25  *
26  * TODO:
27  *   allow subkeys setting/getting
28  *   setting existing keys again
29  *   setting errno properly (update doc, how it should be)
30  *   KDB_ERR_NOMEM bei malloc errors
31  *
32  ***************************************************************************/
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include <kdbbackend.h>
39
40 #define BACKENDNAME "ini"
41 #define BACKENDVERSION "0.1.2"
42
43 struct _backendData
44 {
45         int fd;
46         FILE *fc;
47 };
48
49 typedef struct _backendData backendData;
50
51 #define FILEDES ((((backendData*)kdbhGetBackendData (handle))->fd))
52 #define FILEPTR ((((backendData*)kdbhGetBackendData (handle))->fc))
53
54 int IniReadDir(KDB *handle, Key * key, KeySet * returned, unsigned long options);
55 int IniChooseFile(KDB *handle, Key * key, KeySet * returned, unsigned long options);
56 int IniReadFile (KDB *handle, Key * key, KeySet * returned, unsigned long options);
57 int IniSetKeys (KDB *handle, KeySet * origKeys);
58
59 /**Helper functions*/
60 int open_file (KDB *handle, char * filename, char mode);
61 int close_file (KDB *handle);
62 int enlarge_file (KDB *handle, long where, long space);
63 int shrink_file (KDB *handle, long where, long space);
64 int read_key (KDB *handle, Key * key, char * root);
65 int write_key (KDB *handle, Key * key, long oldpos);
66 int remove_key (KDB *handle, Key * key, long oldpos);
67
68 int kdbiRealloc (void ** buffer, size_t size);
69
70 int stat_file (Key * forKey, char * filename);
71 size_t base_name (const Key * forKey, char * basename);
72 size_t file_name (const Key * forKey, char * basename);
73
74 void * open_dir (char * pathname);
75 int create_dir (char * filename);
76 int read_dir (void * dir, char * filename);
77 int close_dir (void * dir);
78
79 /**Parsing functions*/
80 int parse_buffer (char * c);
81 int convert_engine (char * c);
82 int convert_strlen (char * p, int size);
83 int convert_stream (char * buffer, int size, FILE * stream);
84 int make_key (Key * key, char * root, char * buffer_key, char * buffer_value, char * buffer_comment);
85
86 /**Some systems have even longer pathnames */
87 #ifdef PATH_MAX
88 #define MAX_PATH_LENGTH PATH_MAX
89 /**This value is garanteed on any Posix system */
90 #elif _POSIX_PATH_MAX
91 #define MAX_PATH_LENGTH _POSIX_PATH_MAX
92 #else 
93 /**Fallback: This value should be useful*/
94 #define MAX_PATH_LENGTH 4096
95 #endif
96
97
98 /**This buffer size is fastest for reading and writing
99  * in files*/
100 #define BUFFER_RDWR_SIZE 8024
101
102 /**Buffer for holding strings*/
103 #ifndef BUFFER_SIZE
104 #define BUFFER_SIZE 4048
105 #endif
106
107 /**Some more key types needed for ini
108  * FILE ... is a real file on the system
109  * DIR ... is a real directory
110  * SUBDIR ... is a subdirectoy within a file*/
111 #define KEY_TYPE_FILE 4
112 #define KEY_TYPE_DIR 8
113 #define KEY_TYPE_SUBDIR 16
114
115 #define O_RDONLY 'r'
116 #define O_RDWR 'w'
117