1 #ifndef INI_CONFIGURATOR_CLASS
2 #define INI_CONFIGURATOR_CLASS
4 /*****************************************************************************\
6 * Name : ini_configurator *
7 * Author : Chris Koeritz *
9 *******************************************************************************
10 * Copyright (c) 2000-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
18 #include "configurator.h"
19 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
20 #include "ini_parser.h"
21 #include <basis/utf_conversion.h>
24 #include <basis/contracts.h>
25 #include <filesystem/byte_filer.h>
26 #include <filesystem/filename.h>
28 namespace configuration {
30 //! Supports a configurator-based interface on text initialization files.
32 class ini_configurator : public configurator
35 //! chooses where the ini file is located if no path to it is provided.
36 /*! the ini file being manipulated will be stored in either the same
37 directory as the program being executed (APPLICATION_DIRECTORY) or in the
38 directory where the operating system resides (OS_DIRECTORY). however, the
39 OS_DIRECTORY choice only really makes sense on windows. if the flag is
40 instead ALL_USERS_DIRECTORY, then the directory pointed at by the
41 $ALLUSERSPROFILE variable will be used on windows; otherwise, the default
42 is the same as for APPLICATION_DIRECTORY. */
43 enum file_location_default {
44 APPLICATION_DIRECTORY, //!< config files live with application.
45 OS_DIRECTORY, //!< config files live in operating system directory.
46 ALL_USERS_DIRECTORY //!< config files live in the "all users" account.
49 ini_configurator(const basis::astring &ini_filename,
50 treatment_of_defaults behavior = RETURN_ONLY,
51 file_location_default where = ALL_USERS_DIRECTORY);
52 //!< creates an ini_configurator that stores entries into "ini_filename".
53 /*!< the ini config will have the "behavior" specified for how to handle
54 missing items. "where" dictates the file's location if no path is
55 specified as part of the "ini_filename". */
57 virtual ~ini_configurator();
59 DEFINE_CLASS_NAME("ini_configurator");
62 //!< useful mainly on unix/linux, where the file is parsed and held in memory.
69 basis::astring name() const;
70 //!< observes the name of the file used for ini entries.
71 void name(const basis::astring &name);
72 //!< modifies the name of the file used for ini entries.
74 virtual bool get(const basis::astring §ion, const basis::astring &entry,
75 basis::astring &found);
76 //!< implements the configurator retrieval function.
77 /*!< this returns true if the entry was present and stores it in
80 virtual void sections(structures::string_array &list);
81 //!< retrieves the section names into "list".
83 virtual bool section_exists(const basis::astring §ion);
84 //!< returns true if the "section" was found in the file.
85 /*!< NOTE: for an INI file, this is quite a heavy-weight call. */
87 virtual bool put(const basis::astring §ion, const basis::astring &entry,
88 const basis::astring &to_store);
89 //!< implements the configurator storage function.
91 virtual bool delete_section(const basis::astring §ion);
92 //!< removes the entire "section" specified.
94 virtual bool delete_entry(const basis::astring §ion, const basis::astring &entry);
95 //!< removes the entry specified by the "section" and "entry" name.
97 virtual bool get_section(const basis::astring §ion, structures::string_table &info);
98 //!< reads the entire "section" into a table called "info".
99 /*!< on win95, this will fail if the section's data exceeds 32K. */
101 virtual bool put_section(const basis::astring §ion, const structures::string_table &info);
102 //!< writes a table called "info" into the "section" in the INI file.
103 /*!< any existing data for that section is wiped out. on win95, this will
104 fail if the section's data exceeds 32K. */
106 // dictates whether the stored entries will have spaces between '='
107 // and the key name and value. only applicable on linux.
108 bool add_spaces() const { return _add_spaces; }
109 void add_spaces(bool add_them) { _add_spaces = add_them; }
112 filesystem::filename *_ini_name; //!< the file we're manipulating.
113 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
114 ini_parser *_parser; //!< used for real storage and parsing.
116 file_location_default _where; //!< where to find and store the file.
117 bool _add_spaces; //!< tracks whether we're adding spaces around equals.
120 bool put_profile_string(const basis::astring §ion, const basis::astring &entry,
121 const basis::astring &to_store);
122 //!< encapsulates windows' ini storage method.
123 void get_profile_string(const basis::astring §ion, const basis::astring &entry,
124 const basis::astring &default_value, basis::flexichar *return_buffer,
126 //!< encapsulates windows' ini retrieval method.
128 void read_ini_file();
129 //!< reads the INI file's contents into memory.
130 void write_ini_file();
131 //!< store the current contents into the INI file.
135 ini_configurator(const ini_configurator &);
136 ini_configurator &operator =(const ini_configurator &);
138 static const basis::astring &ini_str_fake_default();