1 
2 /*
3  *  keyfile - an easy way to read and write keyfiles
4  *  Copyright (c) 2006 by Mattias Hultgren <mattias_hultgren@tele2.se>
5  *
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; version 2 of the License.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public
17  *   License along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  */
20 
21 #ifndef KEYFILE_H_
22 #define KEYFILE_H_
23 
24 #define KEYFILE_H_VERSION "v1"
25 #define KEYFILE_H_DATE    "2006-07"
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include "vartypes.h"
30 #include "utf8_string.h"
31 
32 
33 // Note that the maximum fileline-width is 5000 bytes.
34 
35 // comment lines must start with a '#', no blank steps before are allowed
36 
37 // signs allowed in keyname's are a-z A-Z 0-9 _
38 
39 // eg.  KEYNAME = "key value inside"
40 class keyfile
41 {
42 private:
43 	class keysDB
44 	{
45 	public:
46 		utf8_string name, value;
47 
keysDB()48 		keysDB() {}
49 		keysDB(keysDB &src);
50 
51 		void operator=(keysDB &src);
52 	}*keys;
53 	uint32 nrofkeys;
54 
55 	void read(FILE *open_file, const char *filename) throw(error_obj);
56 
57 public:
58 	// false is default
59 	bool replace_escape_sequences; // known escape sequences are  \\ \n \t \r \"
60 
61 	keyfile();
62 	keyfile(const char *filename) throw(error_obj);
63 	keyfile(const keyfile &src) throw(error_obj);
64 	~keyfile();
65 
66 	// empties the object
67 	void reset(void);
68 
69 	// sets a key to a special value, if the key is found it will be replaced
70 	void set_key(const utf8_string &keyname,const utf8_string &value) throw(error_obj);
71 
72 	// if keyname wasn't found keyname is returned
73 	const char* get_text( const char *keyname );
74 
75 	// if keyname wasn't found the fail_pointer is returned
76 	const utf8_string* get_key(const utf8_string &keyname, const utf8_string *fail_pointer=0);
77 	const char* get_key_c_str(const utf8_string &keyname, const char *fail_pointer=0);
78 
79 	// this will remove the keyname, if it's not found nothing happens
80 	void remove_key(const utf8_string &keyname) throw(error_obj);
81 
82 	// this doesn't call reset(), the keys in the file will be added to the ones already present
83 	void read(const char *filename) throw(error_obj);
84 
85 	// note that intern_file isn't a filename but a file contant (NULL-terminated)
86 	void read_intern(const char *intern_file) throw(error_obj);
87 
88 	void write(const char *filename) throw(error_obj);
89 
90 	void operator=(const keyfile &src);
91 };
92 
93 
94 
95 // load your translation into this object
96 extern keyfile translation;
97 
98 // a gettext look-a-like macro
99 #define _(String) translation.get_text(String)
100 
101 
102 #define THROW_ERROR( error_type, string ) { \
103                                      error_obj error; \
104                                      error.type = error_type; \
105                                      strncpy( error.msg, string, ERROR_OBJ_MSG_LEN ); \
106                                      throw error; \
107                                    }
108 
109 
110 #endif // KEYFILE_H_
111