winnerhust / inifile2 Goto Github PK
View Code? Open in Web Editor NEW一个轻量级的inifile文件解析库,支持解析、修改、保存ini文件
一个轻量级的inifile文件解析库,支持解析、修改、保存ini文件
void IniFile::deleteKey(const string §ion, const string &key)
{
IniSection *sect = getSection(section);
if (sect != NULL) {
for (IniSection::iterator it = sect->begin(); it != sect->end(); ++it) {
if (it->key == key) {
sect->items.erase(it);
break;
}
}
}
}
这个deleteKey函数是在section中删除键为key的键值对,当有重复的键值对时,不能删除彻底。
而且,在vector迭代过程中进行erase操作,不是会造成迭代器失效吗?O(∩_∩)O~
IniFile::IniFile()
:commentDelimiter("#")
{
}
/-------------------------------------------------------------------------/
/**
@brief 判断是否是注释
@param str 一个string变量
@return 如果是注释则为真
/
/--------------------------------------------------------------------------*/
bool IniFile::IsCommentLine(const string &str)
{
return StartWith(str, commentDelimiter);
}
此时commentDelimiter在构造函数被写死为"#",当为”;“无法判断
The details of exception is same as the below Links:
https://blog.csdn.net/liangzhao_jay/article/details/78064399
环境:Windows 7 , Visual studio
The error message:
(base) louielu@FVFC128NL410 inifile2 % ./build.sh
CMake Warning (dev) in CMakeLists.txt:
No project() command is present. The top-level CMakeLists.txt file must
contain a literal, direct call to the project() command. Add a line of
code such as
project(ProjectName)
near the top of the file, but after cmake_minimum_required().
CMake is pretending there is a "project(Project)" command on the first
line.
This warning is for project developers. Use -Wno-dev to suppress it.
-- The C compiler identification is AppleClang 12.0.5.12050022
-- The CXX compiler identification is AppleClang 12.0.5.12050022
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at test/googletest/CMakeLists.txt:54 (project):
VERSION keyword not followed by a value or was followed by a value that
expanded to nothing.
CMake Deprecation Warning at test/googletest/CMakeLists.txt:56 (cmake_minimum_required):
Compatibility with CMake < 2.8.12 will be removed from a future version of
CMake.
Update the VERSION argument value or use a ... suffix to tell
CMake that the project does not need compatibility with older versions.
-- Found PythonInterp: /opt/miniconda3/bin/python (found version "3.9.5")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
CMake Warning (dev) in CMakeLists.txt:
No cmake_minimum_required command is present. A line of code such as
cmake_minimum_required(VERSION 3.21)
should be added at the top of the file. The version specified may be lower
if you wish to support older CMake versions for this project. For more
information run "cmake --help-policy CMP0000".
This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done
CMake Warning (dev):
Policy CMP0042 is not set: MACOSX_RPATH is enabled by default. Run "cmake
--help-policy CMP0042" for policy details. Use the cmake_policy command to
set the policy and suppress this warning.
MACOSX_RPATH is not specified for the following targets:
inifile_share
This warning is for project developers. Use -Wno-dev to suppress it.
-- Generating done
-- Build files have been written to: /Users/louielu/work/opensource/inifile2/build
[ 20%] Building CXX object src/CMakeFiles/inifile_share.dir/inifile.o
[ 20%] Building CXX object test/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
In file included from /Users/louielu/work/opensource/inifile2/test/googletest/src/gtest-all.cc:38:
In file included from /Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/gtest.h:62:
In file included from /Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-internal.h:40:
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:825:12: error: no member named 'make_tuple' in namespace 'std'
using std::make_tuple;
~~~~~^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:826:12: error: no member named 'tuple' in namespace 'std'
using std::tuple;
~~~~~^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:905:3: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
GTEST_DISALLOW_ASSIGN_(RE);
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:671:34: note: expanded from macro 'GTEST_DISALLOW_ASSIGN_'
void operator=(type const &) = delete
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:948:3: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog);
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:676:24: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
type(type const &) = delete;
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:948:3: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:677:3: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
GTEST_DISALLOW_ASSIGN_(type)
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:671:34: note: expanded from macro 'GTEST_DISALLOW_ASSIGN_'
void operator=(type const &) = delete
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1192:3: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:676:24: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
type(type const &) = delete;
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1192:3: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:677:3: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
GTEST_DISALLOW_ASSIGN_(type)
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:671:34: note: expanded from macro 'GTEST_DISALLOW_ASSIGN_'
void operator=(type const &) = delete
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1302:22: error: 'override' keyword is a C++11 extension [-Werror,-Wc++11-extensions]
~ThreadWithParam() override { Join(); }
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1311:14: error: 'override' keyword is a C++11 extension [-Werror,-Wc++11-extensions]
void Run() override {
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1326:3: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:676:24: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
type(type const &) = delete;
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1326:3: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:677:3: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
GTEST_DISALLOW_ASSIGN_(type)
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:671:34: note: expanded from macro 'GTEST_DISALLOW_ASSIGN_'
void operator=(type const &) = delete
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1684:3: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:676:24: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
type(type const &) = delete;
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1684:3: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:677:3: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
GTEST_DISALLOW_ASSIGN_(type)
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:671:34: note: expanded from macro 'GTEST_DISALLOW_ASSIGN_'
void operator=(type const &) = delete
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1702:3: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:676:24: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
type(type const &) = delete;
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1702:3: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:677:3: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
GTEST_DISALLOW_ASSIGN_(type)
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:671:34: note: expanded from macro 'GTEST_DISALLOW_ASSIGN_'
void operator=(type const &) = delete
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1759:5: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:676:24: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
type(type const &) = delete;
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1759:5: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:677:3: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
GTEST_DISALLOW_ASSIGN_(type)
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:671:34: note: expanded from macro 'GTEST_DISALLOW_ASSIGN_'
void operator=(type const &) = delete
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1791:5: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory);
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:676:24: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
type(type const &) = delete;
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:1791:5: error: deleted function definitions are a C++11 extension [-Werror,-Wc++11-extensions]
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:677:3: note: expanded from macro 'GTEST_DISALLOW_COPY_AND_ASSIGN_'
GTEST_DISALLOW_ASSIGN_(type)
^
/Users/louielu/work/opensource/inifile2/test/googletest/include/gtest/internal/gtest-port.h:671:34: note: expanded from macro 'GTEST_DISALLOW_ASSIGN_'
void operator=(type const &) = delete
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make[2]: *** [test/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o] Error 1
make[1]: *** [test/googletest/CMakeFiles/gtest.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 30%] Linking CXX shared library ../lib/libinifile_share.dylib
[ 30%] Built target inifile_share
make: *** [all] Error 2
只要加入H文件,就报错
#include "inifile.h"
错误 3 error C1075: 与左侧的 大括号“{”(位于“..\inifile.h(58)”)匹配之前遇到文件结束 ..\DllMain.cpp 102 1
只要加入using,新增一个错误
using namespace inifile;
错误 2 error C2143: 语法错误 : 缺少“;”(在“using”的前面) ..\DllMain.cpp 17 1
我这个DLL是新建的空DLL,只有一个默认模板DLLMAIN 函数
[test]
key=1 '测试
不支持=左右两边有空格的场景 key##=#value(#代表空格)
Strange Problems occurred.
It seems like that the code file has some strange characters, causing encoding problems......
Such as following:
warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
then it's followed by a series of unreasonable errors
1>c:\users\proto\desktop\inifile2-master\test1\test1\inifile.cpp(64): warning C4101: 'newSection': unreferenced local variable
1>c:\users\proto\desktop\inifile2-master\test1\test1\inifile.cpp(80): error C2065: 's': undeclared identifier
1>c:\users\proto\desktop\inifile2-master\test1\test1\inifile.cpp(80): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\proto\desktop\inifile2-master\test1\test1\inifile.cpp(83): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\proto\desktop\inifile2-master\test1\test1\inifile.cpp(83): error C2065: 's': undeclared identifier
This is just a reminder, since I have found an alternative.
visual C++ 2017
编译配置 debug , ‘使用多字节字符集’
在函数 IniFile::trim 中,调用的 c 函数 isspace 中传入中文字符就会崩溃
经查,isspace 参数是 int ,传入 中文字符可能是 负数的 char, 而 isspace 内部某个位置 又强转成无符号 int ,从而导致 结果是个超过 256 的 int ;但 某处 又断言 要小于等于 256
临时解决方法:强转成 unsigned char :
isspace((unsigned char)str[i])
del
// step 2,判断line内容是否为段或键
//段开头查找 [
if (cleanLine[0] == '[') {
err = UpdateSection(cleanLine, comment, rightComment, &currSection);
} else if (cleanLine[0] != '#' && cleanLine[0] != ';') {
// 如果该行是键值,添加到section段的items容器
err = AddKeyValuePair(cleanLine, comment, rightComment, currSection);
}
这里面把[section]前面的注释都作为section->comment保存起来。
那下面这样的结构,
#####################################################################
[Translation]
TRANSLATION_BY = Prevedel
TRANSLATION_NAME = Jadran Rudec , [email protected]
TRANSLATION_LINK = http://www.mikrodata.si/english/employee.htm#jadran
LANGUAGENAME = Slovenski jezik
FILEVER = 3160
#File version = 3.2.7
#####################################################################
[Common]
SYSTEM_FONT_NAME = tahoma
SYSTEM_FONT_SIZE = 8
SYSTEM_TITLE_BOLD = 1
SYSTEM_EDITOR_PANEL_SIZEOVERFLOW = 0
POPUPMARGIN =35
[Common]上面两行本属于[Translation]里面的注释就会附属到 Common->comment 中,
在后面的 save 中,从[Common]中得到的comment 就会包含[Translation]中的#File version = 3.2.7
,还会改变原来的注释结构。比如:
#File version = 3.2.7
#####################################################################
[Common]
SYSTEM_FONT_NAME = tahoma
SYSTEM_FONT_SIZE = 8
SYSTEM_TITLE_BOLD = 1
SYSTEM_EDITOR_PANEL_SIZEOVERFLOW = 0
POPUPMARGIN =35
#####################################################################
[Translation]
TRANSLATION_BY = Prevedel
TRANSLATION_NAME = Jadran Rudec , [email protected]
TRANSLATION_LINK = http://www.mikrodata.si/english/employee.htm#jadran
LANGUAGENAME = Slovenski jezik
FILEVER = 3160
当然,如果不考虑注释的话,就很容易解决了。
我在vs2010中调试发现在读一个utf8无bom格式编码文件时,在load中getline读空行时返回为1,line="",line.size=0,line.capacity=15;然后进入trim函数中,str为空字符串,str[0]访问越界。
我又在qt中和linux下测试了,都没有越界。
vs 中字符串有点不一样,空串没有'\0'结束符
vs2010下我只能修改trim
while (i<len && isspace(str[i]) && str[i] != '\0') {
i++;
}
vs2013 下面我写的测试代码函数 在ini.getValue("", "USER", value);函数出会有0xC0000005: Access violation reading location 0xCCCCCCD0.错误 我看了下代码没看出来
还有源码文件inifile.cpp 里面 我发现new IniSection(); 会有问题 结构体用new 是否通用?
我修改了下用变量定义
//section = new IniSection();
IniSection tmp;
section = &tmp;
void IniFileTest(){
// write config
char filepath[] = "./IniTest.ini";
FILE *fp = fopen(filepath, "w");
char content[] = " USER=root \r\n [COMMON] \n DB=sys \nPASSWD=tt \nPASSWD=dd \n#commit \n ;--------- \n[DEFINE] \nname=cxy\nvalue=1 #test";
fwrite(content, sizeof(char), strlen(content), fp);
fclose(fp);
// read config
IniFile ini;
ini.load(filepath);
string value;
vector valueList;
ini.getValue("", "USER", value);
cout << value;
ini.getValue("COMMON", "DB", value);
cout << value;
ini.getValues("COMMON", "PASSWD", valueList);
for (vector::iterator i = valueList.begin(); i < valueList.end(); i++){
cout << *i;
}
ini.getValue("DEFINE", "name", value);
cout << value;
ini.getValue("DEFINE", "value", value);
cout << value;
// update config
ini.setValue("COMMON", "PASSWD", "root", "数据库密码");
// delete config
ini.deleteKey("COMMON", "DB");
#ifdef _DEBUG
printf("IniFileTest end any key continue .. ");
getchar();
#endif
}
RT
这个函数有问题 void IniFile::trim(string &str)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.