内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

c++對properties配置文件操作工具類

源代碼GitHub路徑:源代碼地址下載

最近要使用c++對windows api相關(guān)接口的封裝,有2個(gè)接口要求讀寫properties文件。
原以為網(wǎng)上應(yīng)該有一大堆資料的,結(jié)果拜BAI度的大恩大德,一點(diǎn)相關(guān)的資料都沒有。那就只能自己動手豐衣足食。再次感謝十分強(qiáng)大只是找不到相關(guān)資料的BAI度。

創(chuàng)新互聯(lián)建站專注于伊川網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供伊川營銷型網(wǎng)站建設(shè),伊川網(wǎng)站制作、伊川網(wǎng)頁設(shè)計(jì)、伊川網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造伊川網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供伊川網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

頭文件

#pragma once

#ifndef CProperties_H
#define CProperties_H


#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <windows.h>
using namespace std;

#define  IS_OK 1
#define  IS_ERROR 0
typedef  bool PSTATUS;
class CProperties{
private:
    string path;
protected:
public:
    CProperties();
    virtual ~CProperties();
    //打開文件
    PSTATUS open(const char* path);
    //實(shí)現(xiàn)文件加載到map中
    PSTATUS load();
    void print();
    void close();
    vector<string> read(const char *k);
    PSTATUS write(const char *k,const char* v);
    void trim_first(string &s);
    void trim_end(string &s);
    void trim(string &s);
};

#endif

# 源文件

#include "stdafx.h"
#include "properties.h"
/************************************************************************************************/
///*
//*作者:王邵華
//*email:loveofprogram@sina.com
//*時(shí)間:20180404
//*版本歷程:
//*版本號    參與人      事項(xiàng)    
//*V1.0.0.1  王邵華      創(chuàng)建  
//***************************************************************************************************
//***************************************************************************************************
//*本工具類實(shí)現(xiàn)c++對properties文件的讀寫操作,
//*1.支持單行注釋如:行首除去空格后以 //、#、<!--內(nèi)容--> 開始的
//* //這是單行注釋
//* #這是單行注釋
//* <!--這是單行注釋-->
//*2.支持節(jié)點(diǎn) 但節(jié)點(diǎn)并沒有意義 [section]
//*[section0]
//*key=value
//*[section1]
//*key=value
//*用到multimap所以存在key鍵有2個(gè)值而已,節(jié)點(diǎn)并沒有起到區(qū)分的作用,這一點(diǎn)也符合properties配置文件要求規(guī)范的
//*3.支持多行注釋 /*第一行,第二行.....*/
//*案例(1)同行有/*..*/ 如:key=123/*oooooo*/b=890  結(jié)果保留key=123
//*案例(2)多行有/*..*/ 如:key=123/*ooo
//*0000000000000000000
//*ooo*/b=890  結(jié)果保留key=123
//*案例(3)行頭行尾有/*..*/如:/*ooooo*/key=123 結(jié)果不保留
//*
//*/
/************************************************************************************************/
static vector<string> vLine;
static multimap<string,string> msKV;
static bool mulremark = false;//多行注釋開關(guān)

CProperties::CProperties(){};
CProperties::~CProperties(){};

PSTATUS CProperties::open(const char* path){
    /**************************************************************/
    //判斷是否是文本文檔 而不是文件夾 此代碼存在問題一直hFind = 0xFFFFFFFF
    //暫留 以便日后優(yōu)化
    //author:王邵華
    //time:  20180404
    /*WIN32_FIND_DATA fd; 
    bool ret = true;
    HANDLE hFind = FindFirstFile(LPCWSTR(path), &fd); 
    if ((hFind != INVALID_HANDLE_VALUE) && !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 
    { 
        ret = false;
    }
    FindClose(hFind); 
    if (ret)
    {
        //文件屬性不對
        return IS_ERROR;
    }*/
    /**************************************************************/
    if (nullptr == path)
    {
        return IS_ERROR;
    }
    this->path = path;
    ifstream ifs;
    ifs.open(path,ios::in);
    if (!ifs)
    {
        return IS_ERROR;//打開文件失敗
    }
    string sLine;
    while(!ifs.eof()){
        sLine = "";
        getline(ifs,sLine);
        if (mulremark)//已打開多行注釋開關(guān) 需判斷該行有沒有關(guān)閉開關(guān)
        {
            if(sLine.find("*/") != string::npos){
                mulremark = false;
            }
            continue;//無論開關(guān)是否關(guān)閉 繼續(xù)讀下一行數(shù)據(jù)
        }else
        {
            string::size_type pos =sLine.find("/*");string sSubLine;
            if (pos != string::npos)//改行有多行注釋開關(guān)  需打開
            {
                string::size_type epos = sLine.rfind("*/");
                mulremark = epos==string::npos || epos<pos?true:false;
                sSubLine = sLine.substr(0,pos);
            }else{
                sSubLine = sLine;
            }
            trim(sSubLine);
            if (sSubLine.length()<= 0) continue;
            if (sSubLine[0] == '#')continue;
            if (sSubLine[0] == '[')continue;
            if (sSubLine.length()>2 && sSubLine[0] == '/' && sSubLine[1] == '/')continue;
            if (sSubLine.length()>4 && sSubLine[0] == '<' && sSubLine[1] == '!')continue;
            vLine.push_back(sSubLine);
        }
    }
    if (ifs.is_open())
    {
        ifs.close();
    }
    return IS_OK;
}
//實(shí)現(xiàn)文件加載到map中
PSTATUS CProperties::load(){
    string key,value;string sSubStr;
    for (int i = 0;i<vLine.size();i++)
    {
        string::size_type pos = vLine.at(i).find_first_of("=");
        if (pos == string::npos)
        {
            continue;
        }
        key = vLine.at(i).substr(0,pos);
        value = vLine.at(i).substr(pos+1,vLine.at(i).size()-pos);
        msKV.insert(make_pair(key,value));
    }
    return IS_OK;
}

void CProperties::print(){
    multimap<string,string>::iterator  itr = msKV.begin();
    cout<<"################################################################################"<<endl;
    for (;itr!=msKV.end();itr++)
    {
        cout<<"key:"<< itr->first.c_str()<<";value:"<<itr->second.c_str()<<endl;
    }
    cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl;
}

vector<string> CProperties::read(const char *k){
    vector<string> vVauleCol;
    if (msKV.size()>0)
    {
        multimap<string, string>::size_type  cnt = msKV.count(k);
        multimap<string, string>::iterator  iter = msKV.find(k);
        for(;cnt > 0; cnt--, iter++)
        {
            vVauleCol.push_back(iter->second);
        }
    }
    return vVauleCol;
}

/*
*aim:將key=value追加到文本末尾,更新multimap映射插入
*/
PSTATUS CProperties::write(const char *k,const char* v){
    if (nullptr == k || nullptr == v)
    {
        return IS_ERROR; //校驗(yàn)入?yún)?    }
    ofstream ofs;
    ofs.open(this->path.c_str(),ios::app);
    if(!ofs)
    {
        return IS_ERROR;//打開文件失敗
    }
    char sStr[1024] = {};
    sprintf(sStr,"%s=%s",k,v);
    ofs<<endl<<sStr;
    msKV.insert(make_pair(k,v));
    if(ofs.is_open())
    {
        ofs.close();
    }
    return IS_OK;
}

/*
*aim:防止多次造作臟數(shù)據(jù)干擾
*/
void CProperties::close(){
    vLine.erase(vLine.begin(),vLine.end());
    msKV.erase(msKV.begin(),msKV.end());
}

void CProperties::trim_first(string &s){
    if( !s.empty() )
        s.erase(0,s.find_first_not_of(" "));
}
void CProperties::trim_end(string &s){
    if( !s.empty() )
        s.erase(s.find_last_not_of(" ") + 1);
}
void CProperties::trim(string &s)
{
    int index = 0;
    if( !s.empty())
    {
        while( (index = s.find(' ',index)) != string::npos)
        {
            s.erase(index,1);
        }
    }
}

調(diào)用實(shí)例

int _tmain1(int argc, _TCHAR* argv[])
{

//########################################################//
//read
//1.創(chuàng)建對象open()文件
//2.load()加載到內(nèi)存中
//3.read()讀取相關(guān)key值的value
//4.close()釋放資源

CProperties cprop;
PSTATUS ret = cprop.open("D:\\job\\greatwall\\test\\bank.properties");
if (ret != IS_OK)
{
    cout<<"打開配置文件失敗"<<endl;
    getchar();
    return 0;
}
cprop.load();
vector<string> vec = cprop.read("key3");
for (int i=0; i<vec.size();i++)
{
    cout<<vec[i].c_str()<<endl;
}
cprop.close();

//########################################################//

//########################################################//
//write
//1.創(chuàng)建對象
//2.load()加載到內(nèi)存中
//3.write()讀取相關(guān)key值的value
//4.close()釋放資源

CProperties cprop_write;
ret = cprop_write.open("D:\\job\\greatwall\\test\\bank2.properties");
cprop_write.write("aaa","bbb");
cprop_write.write("aaa","bbb");
vec = cprop_write.read("aaa");
for (int i=0; i<vec.size();i++)
{
    cout<<vec[i].c_str()<<endl;
}
cprop_write.close();

//########################################################//

getchar();
return 0;
}

網(wǎng)頁題目:c++對properties配置文件操作工具類
本文來源:http://www.rwnh.cn/article20/pgcgco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)商城網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)微信公眾號、網(wǎng)站維護(hù)虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司
宁安市| 河源市| 金平| 梅州市| 礼泉县| 丘北县| 寻乌县| 巫山县| 贵阳市| 徐汇区| 怀远县| 上饶县| 邛崃市| 独山县| 郑州市| 百色市| 镇巴县| 高要市| 利辛县| 盖州市| 平远县| 临桂县| 苏州市| 镶黄旗| 金乡县| 松溪县| 东阿县| 美姑县| 多伦县| 平远县| 建水县| 东明县| 南召县| 丘北县| 都匀市| 什邡市| 彭州市| 冕宁县| 孝感市| 工布江达县| 阜新市|