Skip to content

Commit ede00e1

Browse files
committed
Add ini_putbool() (as a companion to ini_getbool()).
Some reformatting and clean-up of the manual.
1 parent 755f9a8 commit ede00e1

File tree

5 files changed

+82
-67
lines changed

5 files changed

+82
-67
lines changed

README.md

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# minIni
2-
minIni is a portable and configurable library for reading and writing ".INI" files. At roughly 950 lines of commented source
3-
code, minIni truly is a "mini" INI file parser, especially considering its features.
2+
minIni is a portable and configurable library for reading and writing ".INI" files. At less than a thousand lines of
3+
commented source code, minIni truly is a "mini" INI file parser, especially considering its features.
44

5-
The library does not require the file I/O functions from the standard C/C++ library, but instead lets you configure
6-
the file I/O interface to use via macros. minIni uses limited stack space and does not use dynamic memory (malloc and
5+
The library does not require the file I/O functions from the standard C/C++ library, but instead lets you configure
6+
the file I/O interface to use via macros. minIni uses limited stack space and does not use dynamic memory (malloc and
77
friends) at all.
88

99
Some minor variations on standard INI files are supported too, notably minIni supports INI files that lack sections.
@@ -13,15 +13,15 @@ Some minor variations on standard INI files are supported too, notably minIni su
1313

1414
minIni is derived from an earlier INI file parser (which I wrote) for desktop systems.
1515

16-
In turn, that earlier parser was a re-write of the code from the article "Multiplatform .INI Files" by Joseph J. Graf
17-
in the March 1994 issue of Dr. Dobb's Journal. In other words, minIni has its roots in the work of Joseph Graf (even
16+
In turn, that earlier parser was a re-write of the code from the article "Multiplatform .INI Files" by Joseph J. Graf
17+
in the March 1994 issue of Dr. Dobb's Journal. In other words, minIni has its roots in the work of Joseph Graf (even
1818
though the code has been almost completely re-written).
1919

2020

2121
# Features
2222

23-
minIni is a programmer's library to read and write "INI" files in embedded systems. minIni takes little resources,
24-
can be configured for various kinds of file I/O libraries and provides functionality for reading, writing and
23+
minIni is a programmer's library to read and write "INI" files in embedded systems. minIni takes little resources,
24+
can be configured for various kinds of file I/O libraries and provides functionality for reading, writing and
2525
deleting keys from an INI file.
2626

2727
Although the main feature of minIni is that it is small and minimal, it has a few other features:
@@ -34,32 +34,32 @@ Although the main feature of minIni is that it is small and minimal, it has a fe
3434
* Section and key enumeration are supported.
3535
* You can optionally set the line termination (for text files) that minIni will use. (This is a compile-time setting, not a run-time setting.)
3636
* Since writing speed is much lower than reading speed in Flash memory (SD/MMC cards, USB memory sticks), minIni minimizes "file writes" at the expense of double "file reads".
37-
* The memory footprint is deterministic. There is no dynamic memory allocation.
37+
* The memory footprint is deterministic. There is no dynamic memory allocation.
3838

3939
## INI file reading paradigms
4040

41-
There are two approaches to reading settings from an INI file. One way is to call a function, such as
42-
GetProfileString() for every section and key that you need. This is especially convenient if there is a large
43-
INI file, but you only need a few settings from that file at any time —especially if the INI file can also
41+
There are two approaches to reading settings from an INI file. One way is to call a function, such as
42+
GetProfileString() for every section and key that you need. This is especially convenient if there is a large
43+
INI file, but you only need a few settings from that file at any time —especially if the INI file can also
4444
change while your program runs. This is the approach that the Microsoft Windows API uses.
4545

46-
The above procedure is quite inefficient, however, when you need to retrieve quite a few settings in a row from
47-
the INI file —especially if the INI file is not cached in memory (which it isn't, in minIni). A different approach
48-
to getting settings from an INI file is to call a "parsing" function and let that function call the application
49-
back with the section and key names plus the associated data. XML parsing libraries often use this approach; see
46+
The above procedure is quite inefficient, however, when you need to retrieve quite a few settings in a row from
47+
the INI file —especially if the INI file is not cached in memory (which it isn't, in minIni). A different approach
48+
to getting settings from an INI file is to call a "parsing" function and let that function call the application
49+
back with the section and key names plus the associated data. XML parsing libraries often use this approach; see
5050
for example the Expat library.
5151

52-
minIni supports both approaches. For reading a single setting, use functions like ini_gets(). For the callback
52+
minIni supports both approaches. For reading a single setting, use functions like ini_gets(). For the callback
5353
approach, implement a callback and call ini_browse(). See the minIni manual for details.
5454

5555

5656
# INI file syntax
5757

58-
INI files are best known from Microsoft Windows, but they are also used with applications that run on other
58+
INI files are best known from Microsoft Windows, but they are also used with applications that run on other
5959
platforms (although their file extension is sometimes ".cfg" instead of ".ini").
6060

61-
INI files have a simple syntax with name/value pairs in a plain text file. The name must be unique (per section)
62-
and the value must fit on a single line. INI files are commonly separated into sections —in minIni, this is
61+
INI files have a simple syntax with name/value pairs in a plain text file. The name must be unique (per section)
62+
and the value must fit on a single line. INI files are commonly separated into sections —in minIni, this is
6363
optional. A section is a name between square brackets, like "[Network]" in the example below.
6464

6565
```
@@ -69,30 +69,30 @@ address=dhcp
6969
dns = 192.168.1.1
7070
```
7171

72-
In the API and in this documentation, the "name" for a setting is denoted as the key for the setting. The key
73-
and the value are separated by an equal sign ("="). minIni supports the colon (":") as an alternative to the
72+
In the API and in this documentation, the "name" for a setting is denoted as the key for the setting. The key
73+
and the value are separated by an equal sign ("="). minIni supports the colon (":") as an alternative to the
7474
equal sign for the key/value delimiter.
7575

76-
Leading a trailing spaces around values or key names are removed. If you need to include leading and/or trailing
77-
spaces in a value, put the value between double quotes. The ini_gets() function (from the minIni library, see the
78-
minIni manual) strips off the double quotes from the returned value. Function ini_puts() adds double quotes if
76+
Leading a trailing spaces around values or key names are removed. If you need to include leading and/or trailing
77+
spaces in a value, put the value between double quotes. The ini_gets() function (from the minIni library, see the
78+
minIni manual) strips off the double quotes from the returned value. Function ini_puts() adds double quotes if
7979
the value to write contains trailing white space (or special characters).
8080

81-
minIni ignores spaces around the "=" or ":" delimiters, but it does not ignore spaces between the brackets in a
82-
section name. In other words, it is best not to put spaces behind the opening bracket "[" or before the closing
81+
minIni ignores spaces around the "=" or ":" delimiters, but it does not ignore spaces between the brackets in a
82+
section name. In other words, it is best not to put spaces behind the opening bracket "[" or before the closing
8383
bracket "]" of a section name.
8484

85-
Comments in the INI must start with a semicolon (";") or a hash character ("#"), and run to the end of the line.
86-
A comment can be a line of its own, or it may follow a key/value pair (the "#" character and trailing comments
85+
Comments in the INI must start with a semicolon (";") or a hash character ("#"), and run to the end of the line.
86+
A comment can be a line of its own, or it may follow a key/value pair (the "#" character and trailing comments
8787
are extensions of minIni).
8888

89-
For more details on the format, please see http://en.wikipedia.org/wiki/INI_file.
89+
For more details on the format, please see http://en.wikipedia.org/wiki/INI_file.
9090

9191

9292
# Adapting minIni to a file system
9393

94-
The minIni library must be configured for a platform with the help of a so- called "glue file". This glue file
95-
contains macros (and possibly functions) that map file reading and writing functions used by the minIni library
94+
The minIni library must be configured for a platform with the help of a so- called "glue file". This glue file
95+
contains macros (and possibly functions) that map file reading and writing functions used by the minIni library
9696
to those provided by the operating system. The glue file must be called "minGlue.h".
9797

9898
To get you started, the minIni distribution comes with the following example glue files:
@@ -101,36 +101,36 @@ To get you started, the minIni distribution comes with the following example glu
101101
* a glue file for Microchip's "Memory Disk Drive File System Library" (see http://www.microchip.com/),
102102
* a glue file for the FAT library provided with the CCS PIC compiler (see http://www.ccsinfo.com/)
103103
* a glue file for the EFS Library (EFSL, http://www.efsl.be/),
104-
* and a glue file for the FatFs and Petit-FatFs libraries (http://elm-chan.org/fsw/ff/00index_e.html).
104+
* and a glue file for the FatFs and Petit-FatFs libraries (http://elm-chan.org/fsw/ff/00index_e.html).
105105

106-
The minIni library does not rely on the availability of a standard C library, because embedded operating systems
107-
may have limited support for file I/O. Even on full operating systems, separating the file I/O from the INI format
106+
The minIni library does not rely on the availability of a standard C library, because embedded operating systems
107+
may have limited support for file I/O. Even on full operating systems, separating the file I/O from the INI format
108108
parsing carries advantages, because it allows you to cache the INI file and thereby enhance performance.
109109

110-
The glue file must specify the type that identifies a file, whether it is a handle or a pointer. For the standard
110+
The glue file must specify the type that identifies a file, whether it is a handle or a pointer. For the standard
111111
C/C++ file I/O library, this would be:
112112

113113
```C
114114
#define INI_FILETYPE FILE*
115115
```
116116

117-
If you are not using the standard C/C++ file I/O library, chances are that you need a different handle or
118-
"structure" to identify the storage than the ubiquitous "FILE*" type. For example, the glue file for the FatFs
117+
If you are not using the standard C/C++ file I/O library, chances are that you need a different handle or
118+
"structure" to identify the storage than the ubiquitous "FILE*" type. For example, the glue file for the FatFs
119119
library uses the following declaration:
120120

121121
```C
122122
#define INI_FILETYPE FIL
123123
```
124124

125-
The minIni functions declare variables of this INI_FILETYPE type and pass these variables to sub-functions
125+
The minIni functions declare variables of this INI_FILETYPE type and pass these variables to sub-functions
126126
(including the glue interface functions) by reference.
127127

128-
For "write support", another type that must be defined is for variables that hold the "current position" in a
128+
For "write support", another type that must be defined is for variables that hold the "current position" in a
129129
file. For the standard C/C++ I/O library, this is "fpos_t".
130130

131-
Another item that needs to be configured is the buffer size. The functions in the minIni library allocate this
132-
buffer on the stack, so the buffer size is directly related to the stack usage. In addition, the buffer size
133-
determines the maximum line length that is supported in the INI file and the maximum path name length for the
131+
Another item that needs to be configured is the buffer size. The functions in the minIni library allocate this
132+
buffer on the stack, so the buffer size is directly related to the stack usage. In addition, the buffer size
133+
determines the maximum line length that is supported in the INI file and the maximum path name length for the
134134
temporary file. For example, minGlue.h could contain the definition:
135135

136136
```C
@@ -139,9 +139,9 @@ temporary file. For example, minGlue.h could contain the definition:
139139

140140
The above macro limits the line length of the INI files supported by minIni to 512 characters.
141141

142-
The temporary file is only used when writing to INI files. The minIni routines copy/change the INI file to a
143-
temporary file and then rename that temporary file to the original file. This approach uses the least amount of
144-
memory. The path name of the temporary file is the same as the input file, but with the last character set to a
142+
The temporary file is only used when writing to INI files. The minIni routines copy/change the INI file to a
143+
temporary file and then rename that temporary file to the original file. This approach uses the least amount of
144+
memory. The path name of the temporary file is the same as the input file, but with the last character set to a
145145
tilde ("~").
146146

147147
Below is an example of a glue file (this is the one that maps to the C/C++ "stdio" library).
@@ -165,5 +165,5 @@ Below is an example of a glue file (this is the one that maps to the C/C++ "stdi
165165

166166
As you can see, a glue file is mostly a set of macros that wraps one function definition around another.
167167

168-
The glue file may contain more settings, for support of rational numbers, to explicitly set the line termination
169-
character(s), or to disable write support (for example). See the manual that comes with the archive for the details.
168+
The glue file may contain more settings, for support of rational numbers, to explicitly set the line termination
169+
character(s), or to disable write support (for example). See the manual that comes with the archive for the details.

dev/minIni.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* These routines are in part based on the article "Multiplatform .INI Files"
44
* by Joseph J. Graf in the March 1994 issue of Dr. Dobb's Journal.
55
*
6-
* Copyright (c) CompuPhase, 2008-2021
6+
* Copyright (c) CompuPhase, 2008-2024
77
*
88
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
99
* use this file except in compliance with the License. You may obtain a copy
@@ -949,4 +949,18 @@ int ini_putf(const TCHAR *Section, const TCHAR *Key, INI_REAL Value, const TCHAR
949949
return ini_puts(Section, Key, LocalBuffer, Filename);
950950
}
951951
#endif /* INI_REAL */
952+
953+
/** ini_putbool()
954+
* \param Section the name of the section to write the value in
955+
* \param Key the name of the entry to write
956+
* \param Value the value to write; it should be 0 or 1.
957+
* \param Filename the name and full path of the .ini file to write to
958+
*
959+
* \return 1 if successful, otherwise 0
960+
*/
961+
int ini_putbool(const TCHAR *Section, const TCHAR *Key, int Value, const TCHAR *Filename)
962+
{
963+
return ini_puts(Section, Key, Value ? __T("true") : __T("false"), Filename);
964+
}
965+
952966
#endif /* !INI_READONLY */

dev/minIni.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* minIni - Multi-Platform INI file parser, suitable for embedded systems
22
*
3-
* Copyright (c) CompuPhase, 2008-2021
3+
* Copyright (c) CompuPhase, 2008-2024
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
66
* use this file except in compliance with the License. You may obtain a copy
@@ -37,24 +37,25 @@
3737
extern "C" {
3838
#endif
3939

40-
int ini_getbool(const mTCHAR *Section, const mTCHAR *Key, int DefValue, const mTCHAR *Filename);
41-
long ini_getl(const mTCHAR *Section, const mTCHAR *Key, long DefValue, const mTCHAR *Filename);
42-
int ini_gets(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *DefValue, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename);
43-
int ini_getsection(int idx, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename);
44-
int ini_getkey(const mTCHAR *Section, int idx, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename);
40+
int ini_getbool(const mTCHAR *Section, const mTCHAR *Key, int DefValue, const mTCHAR *Filename);
41+
long ini_getl(const mTCHAR *Section, const mTCHAR *Key, long DefValue, const mTCHAR *Filename);
42+
int ini_gets(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *DefValue, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename);
43+
int ini_getsection(int idx, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename);
44+
int ini_getkey(const mTCHAR *Section, int idx, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename);
4545

46-
int ini_hassection(const mTCHAR *Section, const mTCHAR *Filename);
47-
int ini_haskey(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Filename);
46+
int ini_hassection(const mTCHAR *Section, const mTCHAR *Filename);
47+
int ini_haskey(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Filename);
4848

4949
#if defined INI_REAL
5050
INI_REAL ini_getf(const mTCHAR *Section, const mTCHAR *Key, INI_REAL DefValue, const mTCHAR *Filename);
5151
#endif
5252

5353
#if !defined INI_READONLY
54-
int ini_putl(const mTCHAR *Section, const mTCHAR *Key, long Value, const mTCHAR *Filename);
55-
int ini_puts(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Value, const mTCHAR *Filename);
54+
int ini_putbool(const TCHAR *Section, const TCHAR *Key, int Value, const TCHAR *Filename);
55+
int ini_putl(const mTCHAR *Section, const mTCHAR *Key, long Value, const mTCHAR *Filename);
56+
int ini_puts(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Value, const mTCHAR *Filename);
5657
#if defined INI_REAL
57-
int ini_putf(const mTCHAR *Section, const mTCHAR *Key, INI_REAL Value, const mTCHAR *Filename);
58+
int ini_putf(const mTCHAR *Section, const mTCHAR *Key, INI_REAL Value, const mTCHAR *Filename);
5859
#endif
5960
#endif /* INI_READONLY */
6061

@@ -124,15 +125,15 @@ int ini_browse(INI_CALLBACK Callback, void *UserData, const mTCHAR *Filename);
124125
#endif
125126

126127
#if ! defined INI_READONLY
128+
bool put(const std::string& Section, const std::string& Key, bool Value)
129+
{ return ini_putbool(Section.c_str(), Key.c_str(), (int)Value, iniFilename.c_str()) != 0; }
130+
127131
bool put(const std::string& Section, const std::string& Key, long Value)
128132
{ return ini_putl(Section.c_str(), Key.c_str(), Value, iniFilename.c_str()) != 0; }
129133

130134
bool put(const std::string& Section, const std::string& Key, int Value)
131135
{ return ini_putl(Section.c_str(), Key.c_str(), (long)Value, iniFilename.c_str()) != 0; }
132136

133-
bool put(const std::string& Section, const std::string& Key, bool Value)
134-
{ return ini_putl(Section.c_str(), Key.c_str(), (long)Value, iniFilename.c_str()) != 0; }
135-
136137
bool put(const std::string& Section, const std::string& Key, const std::string& Value)
137138
{ return ini_puts(Section.c_str(), Key.c_str(), Value.c_str(), iniFilename.c_str()) != 0; }
138139

dev/wxMinIni.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* minIni - Multi-Platform INI file parser, wxWidgets interface
22
*
3-
* Copyright (c) CompuPhase, 2008-2012
3+
* Copyright (c) CompuPhase, 2008-2024
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
66
* use this file except in compliance with the License. You may obtain a copy
@@ -73,15 +73,15 @@ class minIni
7373
#endif
7474

7575
#if ! defined INI_READONLY
76+
bool put(const wxString& Section, const wxString& Key, bool Value) const
77+
{ return ini_putbool(Section.utf8_str(), Key.utf8_str(), (int)Value, iniFilename.utf8_str()) != 0; }
78+
7679
bool put(const wxString& Section, const wxString& Key, long Value) const
7780
{ return ini_putl(Section.utf8_str(), Key.utf8_str(), Value, iniFilename.utf8_str()) != 0; }
7881

7982
bool put(const wxString& Section, const wxString& Key, int Value) const
8083
{ return ini_putl(Section.utf8_str(), Key.utf8_str(), (long)Value, iniFilename.utf8_str()) != 0; }
8184

82-
bool put(const wxString& Section, const wxString& Key, bool Value) const
83-
{ return ini_putl(Section.utf8_str(), Key.utf8_str(), (long)Value, iniFilename.utf8_str()) != 0; }
84-
8585
bool put(const wxString& Section, const wxString& Key, const wxString& Value) const
8686
{ return ini_puts(Section.utf8_str(), Key.utf8_str(), Value.utf8_str(), iniFilename.utf8_str()) != 0; }
8787

doc/minIni.pdf

-21.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)