12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283 |
- /* Copyright (c) 2014-2019 WinnerHust
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * The names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
- * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <iostream>
- #include <fstream>
- #include "inifile.hpp"
- #include "inifile_api.h"
- #include "sample_log.h"
- namespace inifile
- {
- // 构造函数,会初始化注释字符集合flags_(容器),目前只使用#和;作为注释前缀
- IniFile::IniFile()
- :commentDelimiter("#")
- {
- }
- //解析一行数据,得到键值
- /* --------------------------------------------------------------------------*/
- /**
- * @brief parse
- *
- * @param content 数据源指针
- * @param key 键名
- * @param value 键值
- * @param c 分隔符
- *
- * @return bool
- */
- /* ----------------------------------------------------------------------------*/
- bool IniFile::parse(const string &content, string *key, string *value)
- {
- return split(content, "=", key, value);
- }
- int IniFile::UpdateSection(const string &cleanLine, const string &comment,
- const string &rightComment, IniSection **section)
- {
- IniSection *newSection;
- // 查找右中括号
- size_t index = cleanLine.find_first_of(']');
- if (index == string::npos) {
- errMsg = string("no matched ] found");
- return ERR_UNMATCHED_BRACKETS;
- }
- int len = index - 1;
- // 若段名为空,继续下一行
- if (len <= 0) {
- errMsg = string("section name is empty");
- return ERR_SECTION_EMPTY;
- }
- // 取段名
- string s(cleanLine, 1, len);
- trim(s);
- //检查段是否已存在
- if (getSection(s) != NULL) {
- errMsg = string("section ") + s + string("already exist");
- return ERR_SECTION_ALREADY_EXISTS;
- }
- // 申请一个新段,由于map容器会自动排序,打乱原有顺序,因此改用vector存储(sections_vt)
- newSection = new IniSection();
- // 填充段名
- newSection->name = s;
- // 填充段开头的注释
- newSection->comment = comment;
- newSection->rightComment = rightComment;
- sections_vt.push_back(newSection);
- *section = newSection;
- return 0;
- }
- int IniFile::AddKeyValuePair(const string &cleanLine, const string &comment,
- const string &rightComment, IniSection *section)
- {
- string key, value;
- if (!parse(cleanLine, &key, &value)) {
- errMsg = string("parse line failed:") + cleanLine;
- return ERR_PARSE_KEY_VALUE_FAILED;
- }
- IniItem item;
- item.key = key;
- item.value = value;
- item.comment = comment;
- item.rightComment = rightComment;
- section->items.push_back(item);
- return 0;
- }
- int IniFile::Load(const string &filePath)
- {
- int err;
- string line; // 带注释的行
- string cleanLine; // 去掉注释后的行
- string comment;
- string rightComment;
- IniSection *currSection = NULL; // 初始化一个字段指针
- release();
- iniFilePath = filePath;
- std::ifstream ifs(iniFilePath);
- if (!ifs.is_open()) {
- errMsg = string("open") +iniFilePath+ string(" file failed");
- return ERR_OPEN_FILE_FAILED;
- }
- //增加默认段,即 无名段""
- currSection = new IniSection();
- currSection->name = "";
- sections_vt.push_back(currSection);
- // 每次读取一行内容到line
- while (std::getline(ifs, line)) {
- trim(line);
- // step 0,空行处理,如果长度为0,说明是空行,添加到comment,当作是注释的一部分
- if (line.length() <= 0) {
- comment += delim;
- continue;
- }
- // step 1
- // 如果行首不是注释,查找行尾是否存在注释
- // 如果该行以注释开头,添加到comment,跳过当前循环,continue
- if (IsCommentLine(line)) {
- comment += line + delim;
- continue;
- }
- // 如果行首不是注释,查找行尾是否存在注释,若存在,切割该行,将注释内容添加到rightComment
- split(line, commentDelimiter, &cleanLine, &rightComment);
- // step 2,判断line内容是否为段或键
- //段开头查找 [
- if (cleanLine[0] == '[') {
- err = UpdateSection(cleanLine, comment, rightComment, &currSection);
- } else {
- // 如果该行是键值,添加到section段的items容器
- err = AddKeyValuePair(cleanLine, comment, rightComment, currSection);
- }
- if (err != 0) {
- ifs.close();
- return err;
- }
- // comment清零
- comment = "";
- rightComment = "";
- }
- ifs.close();
- return 0;
- }
- int IniFile::Save()
- {
- return SaveAs(iniFilePath);
- }
- int IniFile::SaveAs(const string &filePath)
- {
- string data = "";
- /* 载入section数据 */
- for (IniSection_it sect = sections_vt.begin(); sect != sections_vt.end(); ++sect) {
- if ((*sect)->comment != "") {
- data += (*sect)->comment;
- }
- if ((*sect)->name != "") {
- data += string("[") + (*sect)->name + string("]");
- data += delim;
- }
- if ((*sect)->rightComment != "") {
- data += " " + commentDelimiter +(*sect)->rightComment;
- }
- /* 载入item数据 */
- for (IniSection::IniItem_it item = (*sect)->items.begin(); item != (*sect)->items.end(); ++item) {
- if (item->comment != "") {
- data += item->comment;
- if (data[data.length()-1] != '\n') {
- data += delim;
- }
- }
- data += item->key + "=" + item->value;
- if (item->rightComment != "") {
- data += " " + commentDelimiter + item->rightComment;
- }
- if (data[data.length()-1] != '\n') {
- data += delim;
- }
- }
- }
- std::ofstream ofs(filePath);
- ofs << data;
- ofs.close();
- return 0;
- }
- IniSection *IniFile::getSection(const string §ion /*=""*/)
- {
- for (IniSection_it it = sections_vt.begin(); it != sections_vt.end(); ++it) {
- if ((*it)->name == section) {
- return *it;
- }
- }
- return NULL;
- }
- int IniFile::GetSections(vector<string> *sections)
- {
- for (IniSection_it it = sections_vt.begin(); it != sections_vt.end(); ++it) {
- sections->push_back((*it)->name);
- }
- return sections->size();
- }
- int IniFile::GetSectionNum()
- {
- return sections_vt.size();
- }
- int IniFile::GetStringValue(const string §ion, const string &key, string *value)
- {
- return getValue(section, key, value);
- }
- int IniFile::GetIntValue(const string §ion, const string &key, int *intValue)
- {
- int err;
- string strValue;
- err = getValue(section, key, &strValue);
- *intValue = atoi(strValue.c_str());
- return err;
- }
- int IniFile::GetDoubleValue(const string §ion, const string &key, double *value)
- {
- int err;
- string strValue;
- err = getValue(section, key, &strValue);
- *value = atof(strValue.c_str());
- return err;
- }
- int IniFile::GetBoolValue(const string §ion, const string &key, bool *value)
- {
- int err;
- string strValue;
- err = getValue(section, key, &strValue);
- if (StringCmpIgnoreCase(strValue, "true") || StringCmpIgnoreCase(strValue, "1")) {
- *value = true;
- } else if (StringCmpIgnoreCase(strValue, "false") || StringCmpIgnoreCase(strValue, "0")) {
- *value = false;
- }
- return err;
- }
- /* 获取section段第一个键为key的string值,成功返回获取的值,否则返回默认值 */
- void IniFile::GetStringValueOrDefault(const string §ion, const string &key,
- string *value, const string &defaultValue)
- {
- if (GetStringValue(section, key, value) != 0) {
- *value = defaultValue;
- }
- return;
- }
- /* 获取section段第一个键为key的int值,成功返回获取的值,否则返回默认值 */
- void IniFile::GetIntValueOrDefault(const string §ion, const string &key, int *value, int defaultValue)
- {
- if (GetIntValue(section, key, value) != 0) {
- *value = defaultValue;
- }
- return;
- }
- /* 获取section段第一个键为key的double值,成功返回获取的值,否则返回默认值 */
- void IniFile::GetDoubleValueOrDefault(const string §ion, const string &key, double *value, double defaultValue)
- {
- if (GetDoubleValue(section, key, value) != 0) {
- *value = defaultValue;
- }
- return;
- }
- /* 获取section段第一个键为key的bool值,成功返回获取的值,否则返回默认值 */
- void IniFile::GetBoolValueOrDefault(const string §ion, const string &key, bool *value, bool defaultValue)
- {
- if (GetBoolValue(section, key, value) != 0) {
- *value = defaultValue;
- }
- return;
- }
- /* 获取注释,如果key=""则获取段注释 */
- int IniFile::GetComment(const string §ion, const string &key, string *comment)
- {
- IniSection *sect = getSection(section);
- if (sect == NULL) {
- errMsg = string("not find the section ")+section;
- return ERR_NOT_FOUND_SECTION;
- }
- if (key == "") {
- *comment = sect->comment;
- return RET_OK;
- }
- for (IniSection::IniItem_it it = sect->begin(); it != sect->end(); ++it) {
- if (it->key == key) {
- *comment = it->comment;
- return RET_OK;
- }
- }
- errMsg = string("not find the key ")+section;
- return ERR_NOT_FOUND_KEY;
- }
- /* 获取行尾注释,如果key=""则获取段的行尾注释 */
- int IniFile::GetRightComment(const string §ion, const string &key, string *rightComment)
- {
- IniSection *sect = getSection(section);
- if (sect == NULL) {
- errMsg = string("not find the section ")+section;
- return ERR_NOT_FOUND_SECTION;
- }
- if (key == "") {
- *rightComment = sect->rightComment;
- return RET_OK;
- }
- for (IniSection::IniItem_it it = sect->begin(); it != sect->end(); ++it) {
- if (it->key == key) {
- *rightComment = it->rightComment;
- return RET_OK;
- }
- }
- errMsg = string("not find the key ")+key;
- return ERR_NOT_FOUND_KEY;
- }
- int IniFile::getValue(const string §ion, const string &key, string *value)
- {
- string comment;
- return getValue(section, key, value, &comment);
- }
- int IniFile::getValue(const string §ion, const string &key, string *value, string *comment)
- {
- IniSection *sect = getSection(section);
- if (sect == NULL) {
- errMsg = string("not find the section ")+section;
- return ERR_NOT_FOUND_SECTION;
- }
- for (IniSection::IniItem_it it = sect->begin(); it != sect->end(); ++it) {
- if (it->key == key) {
- *value = it->value;
- *comment = it->comment;
- return RET_OK;
- }
- }
- errMsg = string("not find the key ")+key;
- return ERR_NOT_FOUND_KEY;
- }
- int IniFile::GetValues(const string §ion, const string &key, vector<string> *values)
- {
- vector<string> comments;
- return GetValues(section, key, values, &comments);
- }
- int IniFile::GetValues(const string §ion, const string &key, vector<string> *values, vector<string> *comments)
- {
- string value, comment;
- values->clear();
- comments->clear();
- IniSection *sect = getSection(section);
- if (sect == NULL) {
- errMsg = string("not find the section ")+section;
- return ERR_NOT_FOUND_SECTION;
- }
- for (IniSection::IniItem_it it = sect->begin(); it != sect->end(); ++it) {
- if (it->key == key) {
- value = it->value;
- comment = it->comment;
- values->push_back(value);
- comments->push_back(comment);
- }
- }
- if (values->size() == 0) {
- errMsg = string("not find the key ")+key;
- return ERR_NOT_FOUND_KEY;
- }
- return RET_OK;
- }
- bool IniFile::HasSection(const string §ion)
- {
- return (getSection(section) != NULL);
- }
- bool IniFile::HasKey(const string §ion, const string &key)
- {
- IniSection *sect = getSection(section);
- if (sect != NULL) {
- for (IniSection::IniItem_it it = sect->begin(); it != sect->end(); ++it) {
- if (it->key == key) {
- return true;
- }
- }
- }
- return false;
- }
- int IniFile::setValue(const string §ion, const string &key, const string &value, const string &comment /*=""*/)
- {
- IniSection *sect = getSection(section);
- string comt = comment;
- if (comt != "") {
- comt = commentDelimiter + comt;
- }
- if (sect == NULL) {
- //如果段不存在,新建一个
- sect = new IniSection();
- if (sect == NULL) {
- errMsg = string("no enough memory!");
- return ERR_NO_ENOUGH_MEMORY;
- }
- sect->name = section;
- if (sect->name == "") {
- // 确保空section在第一个
- sections_vt.insert(sections_vt.begin(), sect);
- } else {
- sections_vt.push_back(sect);
- }
- }
- for (IniSection::IniItem_it it = sect->begin(); it != sect->end(); ++it) {
- if (it->key == key) {
- it->value = value;
- it->comment = comt;
- return RET_OK;
- }
- }
- // not found key
- IniItem item;
- item.key = key;
- item.value = value;
- item.comment = comt;
- sect->items.push_back(item);
- return RET_OK;
- }
- int IniFile::SetStringValue(const string §ion, const string &key, const string &value)
- {
- return setValue(section, key, value);
- }
- int IniFile::SetIntValue(const string §ion, const string &key, int value)
- {
- char buf[64] = {0};
- snprintf(buf, sizeof(buf), "%d", value);
- return setValue(section, key, buf);
- }
- int IniFile::SetDoubleValue(const string §ion, const string &key, double value)
- {
- char buf[64] = {0};
- snprintf(buf, sizeof(buf), "%f", value);
- return setValue(section, key, buf);
- }
- int IniFile::SetBoolValue(const string §ion, const string &key, bool value)
- {
- if (value) {
- return setValue(section, key, "true");
- } else {
- return setValue(section, key, "false");
- }
- }
- int IniFile::SetComment(const string §ion, const string &key, const string &comment)
- {
- IniSection *sect = getSection(section);
- if (sect == NULL) {
- errMsg = string("Not find the section ")+section;
- return ERR_NOT_FOUND_SECTION;
- }
- if (key == "") {
- sect->comment = comment;
- return RET_OK;
- }
- for (IniSection::IniItem_it it = sect->begin(); it != sect->end(); ++it) {
- if (it->key == key) {
- it->comment = comment;
- return RET_OK;
- }
- }
- errMsg = string("not find the key ")+key;
- return ERR_NOT_FOUND_KEY;
- }
- int IniFile::SetRightComment(const string §ion, const string &key, const string &rightComment)
- {
- IniSection *sect = getSection(section);
- if (sect == NULL) {
- errMsg = string("Not find the section ")+section;
- return ERR_NOT_FOUND_SECTION;
- }
- if (key == "") {
- sect->rightComment = rightComment;
- return RET_OK;
- }
- for (IniSection::IniItem_it it = sect->begin(); it != sect->end(); ++it) {
- if (it->key == key) {
- it->rightComment = rightComment;
- return RET_OK;
- }
- }
- errMsg = string("not find the key ")+key;
- return ERR_NOT_FOUND_KEY;
- }
- void IniFile::SetCommentDelimiter(const string &delimiter)
- {
- commentDelimiter = delimiter;
- }
- void IniFile::DeleteSection(const string §ion)
- {
- for (IniSection_it it = sections_vt.begin(); it != sections_vt.end(); ) {
- if ((*it)->name == section) {
- delete *it;
- it = sections_vt.erase(it);
- break;
- } else {
- it++;
- }
- }
- }
- void IniFile::DeleteKey(const string §ion, const string &key)
- {
- IniSection *sect = getSection(section);
- if (sect != NULL) {
- for (IniSection::IniItem_it it = sect->begin(); it != sect->end();) {
- if (it->key == key) {
- it = sect->items.erase(it);
- break;
- } else {
- it++;
- }
- }
- }
- }
- /*-------------------------------------------------------------------------*/
- /**
- @brief release: 释放全部资源,清空容器
- @param none
- @return none
- */
- /*--------------------------------------------------------------------------*/
- void IniFile::release()
- {
- iniFilePath = "";
- for (IniSection_it it = sections_vt.begin(); it != sections_vt.end(); ++it) {
- delete (*it); // 清除section
- }
- sections_vt.clear();
- }
- /*-------------------------------------------------------------------------*/
- /**
- @brief 判断是否是注释
- @param str 一个string变量
- @return 如果是注释则为真
- */
- /*--------------------------------------------------------------------------*/
- bool IniFile::IsCommentLine(const string &str)
- {
- return StartWith(str, commentDelimiter);
- }
- /*-------------------------------------------------------------------------*/
- /**
- @brief print for debug
- @param none
- @return none
- */
- /*--------------------------------------------------------------------------*/
- void IniFile::print()
- {
- printf("############ print start ############\n");
- printf("filePath:[%s]\n", iniFilePath.c_str());
- printf("commentDelimiter:[%s]\n", commentDelimiter.c_str());
- for (IniSection_it it = sections_vt.begin(); it != sections_vt.end(); ++it) {
- printf("comment :[\n%s]\n", (*it)->comment.c_str());
- printf("section :\n[%s]\n", (*it)->name.c_str());
- if ((*it)->rightComment != "") {
- printf("rightComment:\n%s", (*it)->rightComment.c_str());
- }
- for (IniSection::IniItem_it i = (*it)->items.begin(); i != (*it)->items.end(); ++i) {
- printf(" comment :[\n%s]\n", i->comment.c_str());
- printf(" parm :%s=%s\n", i->key.c_str(), i->value.c_str());
- if (i->rightComment != "") {
- printf(" rcomment:[\n%s]\n", i->rightComment.c_str());
- }
- }
- }
- printf("############ print end ############\n");
- return;
- }
- const string & IniFile::GetErrMsg()
- {
- return errMsg;
- }
- bool IniFile::StartWith(const string &str, const string &prefix)
- {
- if (strncmp(str.c_str(), prefix.c_str(), prefix.size()) == 0) {
- return true;
- }
- return false;
- }
- void IniFile::trimleft(string &str, char c /*=' '*/)
- {
- int len = str.length();
- int i = 0;
- while (str[i] == c && str[i] != '\0') {
- i++;
- }
- if (i != 0) {
- str = string(str, i, len - i);
- }
- }
- void IniFile::trimright(string &str, char c /*=' '*/)
- {
- int i = 0;
- int len = str.length();
- for (i = len - 1; i >= 0; --i) {
- if (str[i] != c) {
- break;
- }
- }
- str = string(str, 0, i + 1);
- }
- /*-------------------------------------------------------------------------*/
- /**
- @brief trim,整理一行字符串,去掉首尾空格
- @param str string变量
- */
- /*--------------------------------------------------------------------------*/
- void IniFile::trim(string &str)
- {
- int len = str.length();
- int i = 0;
- while ((i < len) && isspace(str[i]) && (str[i] != '\0')) {
- i++;
- }
- if (i != 0) {
- str = string(str, i, len - i);
- }
- len = str.length();
- for (i = len - 1; i >= 0; --i) {
- if (!isspace(str[i])) {
- break;
- }
- }
- str = string(str, 0, i + 1);
- }
- /*-------------------------------------------------------------------------*/
- /**
- @brief split,用分隔符切割字符串
- @param str 输入字符串
- @param left_str 分隔后得到的左字符串
- @param right_str 分隔后得到的右字符串(不包含seperator)
- @param seperator 分隔符
- @return pos
- */
- /*--------------------------------------------------------------------------*/
- bool IniFile::split(const string &str, const string &sep, string *pleft, string *pright)
- {
- size_t pos = str.find(sep);
- string left, right;
- if (pos != string::npos) {
- left = string(str, 0, pos);
- right = string(str, pos+1);
- trim(left);
- trim(right);
- *pleft = left;
- *pright = right;
- return true;
- } else {
- left = str;
- right = "";
- trim(left);
- *pleft = left;
- *pright = right;
- return false;
- }
- }
- bool IniFile::StringCmpIgnoreCase(const string &str1, const string &str2)
- {
- string a = str1;
- string b = str2;
- transform(a.begin(), a.end(), a.begin(), towupper);
- transform(b.begin(), b.end(), b.begin(), towupper);
- return (a == b);
- }
- static AX_S32 str2array(const AX_CHAR *pszInput, AX_F64 *pszOut, AX_S32 maxSize)
- {
- AX_CHAR *pszData = strdup(pszInput);
- AX_CHAR *pszToken = NULL;
- AX_S32 szCnt = 0;
- AX_CHAR *p = pszData;
- while (p && *p != '\0') {
- if (*p == '[' || *p == ']') {
- *p = ' ';
- }
- p++;
- }
- const AX_CHAR *pszDelimiters = ",";
- pszToken = strtok(pszData, pszDelimiters);
- while(pszToken) {
- if (szCnt >= maxSize) {
- break;
- }
- pszOut[szCnt++] = (AX_F64)strtod(pszToken, NULL);
- pszToken = strtok(NULL, pszDelimiters);
- }
- free(pszData);
- return szCnt;
- }
- } /* namespace inifile */
- /* 打开并解析一个名为fname的INI文件 */
- AX_S32 AX_INI_Load(const AX_CHAR *fname, AX_INI_HANDLE *handle)
- {
- if (!fname || !handle) {
- return -1;
- }
- IniFile *f = new IniFile;
- if (!f) {
- return -1;
- }
- std::string strIniFile = fname;
- AX_S32 ret = f->Load(strIniFile);
- if (0 != ret) {
- ALOGE("load %s fail", strIniFile.c_str());
- return ret;
- }
- *handle = (AX_INI_HANDLE)f;
- return 0;
- }
- /* 关闭文件 */
- AX_S32 AX_INI_Close(AX_INI_HANDLE handle)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- delete f;
- return 0;
- }
- /* 将内容保存到当前文件 */
- AX_S32 AX_INI_Save(AX_INI_HANDLE handle)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- AX_S32 ret = f->Save();
- return ret;
- }
- /* 将内容另存到一个名为fname的文件 */
- AX_S32 AX_INI_SaveAs(AX_INI_HANDLE handle, const AX_CHAR *fname)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- AX_S32 ret = f->SaveAs(fname);
- return ret;
- }
- /* 获取section段第一个键为key的string值,成功返回0,否则返回错误码 */
- AX_S32 AX_INI_GetStringValue(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, const AX_CHAR **value)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- static string strValue;
- AX_S32 ret = f->GetStringValue(section, key, &strValue);
- *value = (const AX_CHAR *)strValue.c_str();
- return ret;
- }
- /* 获取section段第一个键为key的数组,成功返回0,否则返回错误码 */
- AX_S32 AX_INI_GetArrayValue(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, AX_F64 *value, AX_S32 *valueSize, AX_S32 maxSize)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- string strValue;
- AX_S32 ret = f->GetStringValue(section, key, &strValue);
- *valueSize = str2array(strValue.c_str(), value, maxSize);
- return ret;
- }
- /* 获取section段第一个键为key的AX_S32值,成功返回0,否则返回错误码 */
- AX_S32 AX_INI_GetIntValue(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, AX_S32 *value)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- AX_S32 ret = f->GetIntValue(section, key, value);
- return ret;
- }
- /* 获取section段第一个键为key的double值,成功返回0,否则返回错误码 */
- AX_S32 AX_INI_GetDoubleValue(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, AX_F64 *value)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- AX_S32 ret = f->GetDoubleValue(section, key, value);
- return ret;
- }
- /* 获取section段第一个键为key的AX_BOOL值,成功返回0,否则返回错误码 */
- AX_S32 AX_INI_GetBoolValue(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, AX_BOOL *value)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- bool t = false;
- AX_S32 ret = f->GetBoolValue(section, key, &t);
- *value = (AX_BOOL)t;
- return ret;
- }
- /* 获取注释,如果key=""则获取段注释 */
- AX_S32 AX_INI_GetComment(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, const AX_CHAR **comment)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- static string strValue;
- AX_S32 ret = f->GetComment(section, key, &strValue);
- *comment = (const AX_CHAR *)strValue.c_str();
- return ret;
- }
- /* 获取行尾注释,如果key=""则获取段的行尾注释 */
- AX_S32 AX_INI_GetRightComment(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, const AX_CHAR **rightComment)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- static string strValue;
- AX_S32 ret = f->GetRightComment(section, key, &strValue);
- *rightComment = (const AX_CHAR *)strValue.c_str();
- return ret;
- }
- /* 获取section段第一个键为key的string值,成功返回获取的值,否则返回默认值 */
- AX_S32 AX_INI_GetStringValueOrDefault(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, const AX_CHAR **value, const AX_CHAR *defaultValue)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- static string strValue;
- f->GetStringValueOrDefault(section, key, &strValue, defaultValue);
- *value = (const AX_CHAR *)strValue.c_str();
- return 0;
- }
- /* 获取section段第一个键为key的AX_S32值,成功返回获取的值,否则返回默认值 */
- AX_S32 AX_INI_GetIntValueOrDefault(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, AX_S32 *value, AX_S32 defaultValue)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- f->GetIntValueOrDefault(section, key, value, defaultValue);
- return 0;
- }
- /* 获取section段第一个键为key的double值,成功返回获取的值,否则返回默认值 */
- AX_S32 AX_INI_GetDoubleValueOrDefault(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, AX_F64 *value, AX_F64 defaultValue)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- f->GetDoubleValueOrDefault(section, key, value, defaultValue);
- return 0;
- }
- /* 获取section段第一个键为key的AX_BOOL值,成功返回获取的值,否则返回默认值 */
- AX_S32 AX_INI_GetBoolValueOrDefault(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, AX_BOOL *value, AX_BOOL defaultValue)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- bool t = false;
- f->GetBoolValueOrDefault(section, key, &t, (bool)defaultValue);
- *value = (AX_BOOL)t;
- return 0;
- }
- /* 获取section个数,至少存在一个空section */
- AX_S32 AX_INI_GetSectionNum(AX_INI_HANDLE handle)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- return (AX_BOOL)f->GetSectionNum();
- }
- /* 是否存在某个section */
- AX_BOOL AX_INI_HasSection(AX_INI_HANDLE handle, const AX_CHAR *section)
- {
- if (!handle) {
- return AX_FALSE;
- }
- IniFile *f = (IniFile *)handle;
- return (AX_BOOL)f->HasSection(section);
- }
- /* 是否存在某个key */
- AX_BOOL AX_INI_HasKey(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key)
- {
- if (!handle) {
- return AX_FALSE;
- }
- IniFile *f = (IniFile *)handle;
- return (AX_BOOL)f->HasKey(section, key);
- }
- /* 设置字符串值 */
- AX_S32 AX_INI_SetStringValue(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, const AX_CHAR *value)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- AX_S32 ret = f->SetStringValue(section, key, value);
- return ret;
- }
- /* 设置整形值 */
- AX_S32 AX_INI_SetIntValue(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, AX_S32 value)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- AX_S32 ret = f->SetIntValue(section, key, value);
- return ret;
- }
- /* 设置浮点数值 */
- AX_S32 AX_INI_SetDoubleValue(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, AX_F64 value)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- AX_S32 ret = f->SetDoubleValue(section, key, value);
- return ret;
- }
- /* 设置布尔值 */
- AX_S32 AX_INI_SetBoolValue(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, AX_BOOL value)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- AX_S32 ret = f->SetBoolValue(section, key, value);
- return ret;
- }
- /* 设置注释,如果key=""则设置段注释 */
- AX_S32 AX_INI_SetComment(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, const AX_CHAR *comment)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- AX_S32 ret = f->SetComment(section, key, comment);
- return ret;
- }
- /* 设置行尾注释,如果key=""则设置段的行尾注释 */
- AX_S32 AX_INI_SetRightComment(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key, const AX_CHAR *rightComment)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- AX_S32 ret = f->SetRightComment(section, key, rightComment);
- return ret;
- }
- /* 删除段 */
- AX_S32 AX_INI_DeleteSection(AX_INI_HANDLE handle, const AX_CHAR *section)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- f->DeleteSection(section);
- return 0;
- }
- /* 删除特定段的特定参数 */
- AX_S32 AX_INI_DeleteKey(AX_INI_HANDLE handle, const AX_CHAR *section, const AX_CHAR *key)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- f->DeleteKey(section, key);
- return 0;
- }
- /*设置注释分隔符,默认为"#" */
- AX_S32 AX_INI_SetCommentDelimiter(AX_INI_HANDLE handle, const AX_CHAR *delimiter)
- {
- if (!handle) {
- return -1;
- }
- IniFile *f = (IniFile *)handle;
- f->SetCommentDelimiter(delimiter);
- return 0;
- }
|