simpleini
=========
![Test Results Linux](https://github.com/brofield/simpleini/actions/workflows/test.yml/badge.svg)
![Test Results Windows](https://ci.appveyor.com/api/projects/status/github/brofield/simpleini?svg=true)
A cross-platform library that provides a simple API to read and write INI-style configuration files. It supports data files in ASCII, MBCS and Unicode. It is designed explicitly to be portable to any platform and has been tested on Windows, WinCE and Linux. Released as open-source and free using the MIT licence.
# Feature Summary
- MIT Licence allows free use in all software (including GPL and commercial)
- multi-platform (Windows 95 to Windows 10, Windows CE, Linux, Unix)
- loading and saving of INI-style configuration files
- configuration files can have any newline format on all platforms
- liberal acceptance of file format
* key/values with no section, keys with no value
* removal of whitespace around sections, keys and values
- support for multi-line values (values with embedded newline characters)
- optional support for multiple keys with the same name
- optional case-insensitive sections and keys (for ASCII characters only)
- saves files with sections and keys in the same order as they were loaded
- preserves comments on the file, section and keys where possible
- supports both char or wchar_t programming interfaces
- supports both MBCS (system locale) and UTF-8 file encodings
- system locale does not need to be UTF-8 on Linux/Unix to load UTF-8 file
- support for non-ASCII characters in section, keys, values and comments
- support for non-standard character types or file encodings via user-written converter classes
- support for adding/modifying values programmatically
- should compile with no warnings in most compilers
# Documentation
Full documentation of the interface is available in doxygen format.
# Examples
These snippets are included with the distribution in the automatic tests as ts-snippets.cpp.
### SIMPLE USAGE
```c++
// simple demonstration
CSimpleIniA ini;
ini.SetUnicode();
SI_Error rc = ini.LoadFile("example.ini");
if (rc < 0) { /* handle error */ };
ASSERT_EQ(rc, SI_OK);
const char* pv;
pv = ini.GetValue("section", "key", "default");
ASSERT_STREQ(pv, "value");
ini.SetValue("section", "key", "newvalue");
pv = ini.GetValue("section", "key", "default");
ASSERT_STREQ(pv, "newvalue");
```
### LOADING DATA
```c++
// load from a data file
CSimpleIniA ini;
SI_Error rc = ini.LoadFile("example.ini");
if (rc < 0) { /* handle error */ };
ASSERT_EQ(rc, SI_OK);
// load from a string
const std::string example = "[section]\nkey = value\n";
CSimpleIniA ini;
SI_Error rc = ini.LoadData(example);
if (rc < 0) { /* handle error */ };
ASSERT_EQ(rc, SI_OK);
```
### GETTING SECTIONS AND KEYS
```c++
// get all sections
CSimpleIniA::TNamesDepend sections;
ini.GetAllSections(sections);
// get all keys in a section
CSimpleIniA::TNamesDepend keys;
ini.GetAllKeys("section1", keys);
```
### GETTING VALUES
```c++
// get the value of a key that doesn't exist
const char* pv;
pv = ini.GetValue("section1", "key99");
ASSERT_EQ(pv, nullptr);
// get the value of a key that does exist
pv = ini.GetValue("section1", "key1");
ASSERT_STREQ(pv, "value1");
// get the value of a key which may have multiple
// values. If hasMultiple is true, then there are
// multiple values and just one value has been returned
bool hasMulti;
pv = ini.GetValue("section1", "key1", nullptr, &hasMulti);
ASSERT_STREQ(pv, "value1");
ASSERT_EQ(hasMulti, false);
pv = ini.GetValue("section1", "key2", nullptr, &hasMulti);
ASSERT_STREQ(pv, "value2.1");
ASSERT_EQ(hasMulti, true);
// get all values of a key with multiple values
CSimpleIniA::TNamesDepend values;
ini.GetAllValues("section1", "key2", values);
// sort the values into a known order, in this case we want
// the original load order
values.sort(CSimpleIniA::Entry::LoadOrder());
// output all of the items
CSimpleIniA::TNamesDepend::const_iterator it;
for (it = values.begin(); it != values.end(); ++it) {
printf("value = '%s'\n", it->pItem);
}
```
### MODIFYING DATA
```c++
// add a new section
rc = ini.SetValue("section1", nullptr, nullptr);
if (rc < 0) { /* handle error */ };
ASSERT_EQ(rc, SI_INSERTED);
// not an error to add one that already exists
rc = ini.SetValue("section1", nullptr, nullptr);
if (rc < 0) { /* handle error */ };
ASSERT_EQ(rc, SI_UPDATED);
// get the value of a key that doesn't exist
const char* pv;
pv = ini.GetValue("section2", "key1", "default-value");
ASSERT_STREQ(pv, "default-value");
// adding a key (the section will be added if needed)
rc = ini.SetValue("section2", "key1", "value1");
if (rc < 0) { /* handle error */ };
ASSERT_EQ(rc, SI_INSERTED);
// ensure it is set to expected value
pv = ini.GetValue("section2", "key1", nullptr);
ASSERT_STREQ(pv, "value1");
// change the value of a key
rc = ini.SetValue("section2", "key1", "value2");
if (rc < 0) { /* handle error */ };
ASSERT_EQ(rc, SI_UPDATED);
// ensure it is set to expected value
pv = ini.GetValue("section2", "key1", nullptr);
ASSERT_STREQ(pv, "value2");
```
### DELETING DATA
```c++
// deleting a key from a section. Optionally the entire
// section may be deleted if it is now empty.
bool done, deleteSectionIfEmpty = true;
done = ini.Delete("section1", "key1", deleteSectionIfEmpty);
ASSERT_EQ(done, true);
done = ini.Delete("section1", "key1");
ASSERT_EQ(done, false);
// deleting an entire section and all keys in it
done = ini.Delete("section2", nullptr);
ASSERT_EQ(done, true);
done = ini.Delete("section2", nullptr);
ASSERT_EQ(done, false);
```
### SAVING DATA
```c++
// save the data to a string
std::string data;
rc = ini.Save(data);
if (rc < 0) { /* handle error */ };
ASSERT_EQ(rc, SI_OK);
// save the data back to the file
rc = ini.SaveFile("example2.ini");
if (rc < 0) { /* handle error */ };
ASSERT_EQ(rc, SI_OK);
```
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
1. 只需要包含头文件. 2. 支持无SECTION的 Key-value 读写. 3. 跨平台. 4. 可配置 "=" 两边需不需要空格等. 详情用法请见Test内容。 eg: CSimpleIniA ini; ini.SetUnicode(); SI_Error rc = ini.LoadFile("example.ini"); if (rc < 0) { /* handle error */ }; ASSERT_EQ(rc, SI_OK); const char* pv; pv = ini.GetValue("section", "key", "default"); ASSERT_STREQ(pv, "value"); ini.SetValue("section", "key", "newvalue"); pv = ini.GetValue("section", "key", "default"); ASSERT_STREQ(pv, "newvalue");
资源详情
资源评论
资源推荐
收起资源包目录
纯C++封装ini配置文件的读写类(file wraper ). (160个子文件)
ConvertUTF.c 19KB
package.cmd 739B
test.cmd 667B
config 304B
packages.config 188B
tests.vcxproj.CopyComplete 0B
testsi.cpp 9KB
ts-snippets.cpp 7KB
ts-roundtrip.cpp 5KB
ts-quotes.cpp 4KB
test1.cpp 4KB
ts-bugfix.cpp 3KB
ts-noconvert.cpp 2KB
ts-wchar.cpp 2KB
ts-utf8.cpp 2KB
pch.cpp 109B
Browse.VC.db 39.25MB
Browse.VC.db-shm 32KB
Browse.VC.db-wal 0B
description 73B
simpleini.doxy 55KB
exclude 240B
tests.exe 1.64MB
SimpleIni.vcxproj.filters 525B
.gitignore 6KB
.gitignore 30B
gtest-param-util-generated.h 219KB
gtest-type-util.h 185KB
SimpleIni.h 130KB
gtest-port.h 96KB
gtest.h 89KB
gtest-param-test.h 77KB
gtest-internal.h 51KB
gtest-printers.h 40KB
gtest-tuple.h 29KB
gtest-param-util.h 28KB
gtest_pred_impl.h 15KB
gtest-death-test.h 14KB
gtest-typed-test.h 14KB
gtest-death-test-internal.h 11KB
gtest-spi.h 10KB
gtest-filepath.h 10KB
gtest-message.h 9KB
gtest-linked_ptr.h 8KB
gtest-string.h 7KB
gtest-test-part.h 7KB
ConvertUTF.h 6KB
gtest-port-arch.h 4KB
gtest_prod.h 3KB
gtest-printers.h 2KB
gtest-port.h 2KB
gtest.h 2KB
pch.h 108B
HEAD 187B
HEAD 187B
HEAD 32B
HEAD 23B
pack-f8c54818a99ffa0a0a53b416d5303c8422a09e91.idx 13KB
tests.ilk 7.11MB
index 3KB
test1-expected.ini 2KB
test1-input.ini 2KB
testsi-UTF8.ini 844B
testsi-EUCJP.ini 818B
testsi-SJIS.ini 816B
tests.ini 185B
example.ini 24B
example2.ini 3B
CONVERTUTF.ipch 384KB
tests.lastbuildstate 307B
gtestd.lib 8.56MB
gtestd.lib 5.85MB
gtest.lib 3.8MB
gtest.lib 2.79MB
gtest_maind.lib 72KB
gtest_maind.lib 67KB
gtest_main.lib 62KB
gtest_main.lib 61KB
tests.log 229B
Makefile 405B
Makefile 395B
master 187B
master 41B
README.md 6KB
README.md 2KB
Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.5.nupkg 5.39MB
ts-snippets.obj 2.04MB
ts-roundtrip.obj 1.91MB
ts-quotes.obj 1.81MB
ts-bugfix.obj 1.75MB
ts-noconvert.obj 1.63MB
ts-wchar.obj 1.62MB
ts-utf8.obj 1.59MB
pch.obj 97KB
.signature.p7s 18KB
pack-f8c54818a99ffa0a0a53b416d5303c8422a09e91.pack 223KB
packed-refs 1KB
tests.pch 35.31MB
tests.pdb 8.99MB
vc142.pdb 1.64MB
共 160 条
- 1
- 2
边城浪子书生
- 粉丝: 6
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0