首页 > 其他分享 >一份采用单例模式编写,可读取配置文件的代码

一份采用单例模式编写,可读取配置文件的代码

时间:2023-05-26 15:35:26浏览次数:38  
标签:const string 配置文件 uPos strLine 单例 CConfAccess return 读取


Confaccess.h

#ifndef __CONFACCESS_H__ 
#define __CONFACCESS_H__ 

#include <pthread.h>
#include <stdlib.h>
#include <string>
#include <map>

class CConfAccess
{
    public:
        static CConfAccess* getInstance()
        {
            pthread_once(&once_, &initInstance);
            return pInstance_;
        }

        static void initInstance()
        {
            ::atexit(&destoryInstance);
            pInstance_ = new CConfAccess;
        }

        static void destoryInstance()
        {
            delete pInstance_;
        }

        bool Load(const std::string& filename);

        std::string GetValue(const std::string& strKey);

    private:
        CConfAccess() {};
        CConfAccess(const CConfAccess&);
        void operator=(const CConfAccess&);

        static CConfAccess* pInstance_;
        static pthread_once_t once_;

        std::map<std::string, std::string> mapKeyValue_; 
};


#endif //__CONFACCESS_H__





Confaccess.cpp

#include "Confaccess.h"
#include <fstream>

#define MAX_LINE 1024
using namespace std;

pthread_once_t CConfAccess::once_ = PTHREAD_ONCE_INIT;
CConfAccess* CConfAccess::pInstance_ = NULL;

//Trim space in the beginning and ending of the string.
string StrTrim(const string& strOriginal)
{
     static const char* whiteSpace = " \t\r\n";

     if (strOriginal.empty())
     {
        return strOriginal;
     }

     string::size_type uFrontPos = strOriginal.find_first_not_of(whiteSpace);

     if (string::npos == uFrontPos)
     {
        return "";
     }

     string::size_type uRearPos = strOriginal.find_last_not_of(whiteSpace);

     return string(strOriginal, uFrontPos, uRearPos - uFrontPos + 1);
}

bool CConfAccess::Load(const string& filename)
{
    ifstream initFile(filename.c_str());
    if (!initFile)
    {
        return false;
    }

    string strLine;
    while (getline(initFile, strLine))
    {   
        if ("" == StrTrim(strLine))
        {
            continue;
        }

        string::size_type uPos = strLine.find("#");
        if (uPos > 0 || (string::npos == uPos))
        {
            strLine = strLine.substr(0, uPos);
        }
        else
        {
            continue;
        }

        uPos = strLine.find("=");
        if (string::npos == uPos)
        {
            continue;
        }

        string strKey = StrTrim(strLine.substr(0, uPos));
        string strValue = StrTrim(strLine.substr(uPos + 1));
        
        mapKeyValue_[strKey] = strValue;
    }

    return true;
}

string CConfAccess::GetValue(const string& strKey)
{
    if (strKey.empty() || (mapKeyValue_.end() == mapKeyValue_.find(strKey)))
    {
        return "";
    }

    return mapKeyValue_[strKey];
}





标签:const,string,配置文件,uPos,strLine,单例,CConfAccess,return,读取
From: https://blog.51cto.com/u_16131207/6356802

相关文章

  • .env.development(开发环境)、.env.prodction(正式环境)、自定义环境 例如:读取vue项目根
    .env.development(开发环境)、.env.prodction(正式环境)、自定义环境原文链接:https://blog.csdn.net/qq_42855675/article/details/114261585文章目录1.配置文件:2.命名规则3.关于文件的加载使用自定义环境1.配置文件:      .env.development:开发环境下的配置文件 ......
  • 使用resource读取properties文件,出现Cause: java.sql.SQLException: No suitable driv
    ###Errorqueryingdatabase.Cause:java.sql.SQLException:Nosuitabledriverfoundforhttp://maven.apache.org###Theerrormayexistincom/louis/dao/UserMapper.xml###Theerrormayinvolvecom.louis.dao.UserMapper.getUserList###Theerroroccurred......
  • Singleton 单例模式简介与 C# 示例【创建型】【设计模式来了】
     〇、简介1、什么是单例模式?一句话解释:  单一的类,只能自己来创建唯一的一个对象。单例模式(SingletonPattern)是日常开发中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。这种模式涉及到一个单一的类,该类负责创建自己的对象,同时......
  • 如何将日志配置文件放入到Apollo配置中心并支持热更新
    增加监听日志文件(LoggerConfiguration.java)@ServicepublicclassLoggerConfiguration{privatestaticfinalLoggerlogger=LoggerFactory.getLogger(LoggerConfiguration.class);privatestaticfinalStringLOGGER_TAG="logging.level.";@Autowired......
  • SpringBoot中使用@Scheduled实现定时任务通过读取配置文件动态开关
    场景SpringBoot中定时任务与异步定时任务的实现:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/117083609上面讲的通过@Scheduled注解实现简单定时任务的方式。如果定时任务有多个,不同业务场景下需要动态配置某个定时任务的开关。可以通过@ConditionalOnPropert......
  • 如何证明Servlet是单例的?
    Servlet是web体系里面最重要的部分,下面罗列几道常见的面试题,小伙伴们一定要好好记住哈。1.Servlet是单例的吗,如何证明?Servlet一般都是单例的,并且是多线程的。如何证明Servlet是单例模式呢?很简单,重写Servlet的init方法,或者添加一个构造方法。然后,在web.xml中配置。如:<?xml ve......
  • Qt读取qss文件失败或qss不生效解决方案
            最近在写qt加载样式表的博文,发现qss文件要么打开失败,要么加载成功,但是不生效,经过一番搜索也是算解决了这个问题。读取qss失败:读取文件的方式有两种,一种是绝对路径,一种是相对路径://绝对路径C:\\Users\\fdog\\Desktop\\sheet.qss//相对路径./lib/sheet.qss出现错......
  • Oracle中读取JSON格式数据实战指南(oracle中读json)
    Oracle中读取JSON格式数据实战指南 随着大数据、云计算等技术的快速发展,JSON(JavaScriptObjectNotation)格式的数据越来越广泛应用于数据交互和存储中。Oracle数据库支持JSON格式数据的存储和查询,本篇文章将介绍如何在Oracle中读取JSON格式数据,并提供相关代码示例。 1.创建......
  • 如何通过商品id来获取商品数据并读取
    在电商平台上,商品id是一个十分重要的属性,能够唯一确定一个商品的信息。而作为API接口爬虫程序员,在进行商品数据爬取时,也是常常需要使用商品id来获取商品相关的数据。本文将介绍如何通过商品id来获取商品数据并读取出来。一、确定API接口在进行商品数据爬取时,首先需要确定要使用哪个......
  • Java笔记(八):单例模式
    懒汉式懒汉式单例模式在第一次调用的时候进行实例化。1.适用于单线程环境(不推荐)此方式在单线程的时候工作正常,但在多线程的情况下就有问题了。如果两个线程同时运行到判断instance是否为null的if语句,并且instance的确没有被创建时,那么两个线程都会创建一个实例,此时类型Singlet......