#include <iostream>
#include <string>
#include <stdint.h>
#include <map>
#include <fstream>
#include <stdlib.h>
#include <stdio.h> enum
{
enmMaxConfigWordLen = ,
}; typedef std::map<std::string,std::string> ATTRMAP; void GetTextFileContent(const char *fileName,char buf[],const uint32_t maxBufLen,uint32_t &readBufLen)
{
std::ifstream file(fileName,std::ios::in);
if(!file)
{
return ;
}
char c;
readBufLen = ;
while(file.get(c) && readBufLen < maxBufLen)
{
if(c != '\n' && c != ' ' && c != )
{
buf[readBufLen++] = c;
}
}
buf[readBufLen]='\0';
} void GetAllAttrFromStr(const char *buf,const uint32_t bufLen,ATTRMAP &attrMap)
{
char key[enmMaxConfigWordLen],value[enmMaxConfigWordLen];
int32_t flag = ,len = ; //0->key,1->value
for(uint32_t i = ;i < bufLen; ++i)
{
const char &c = buf[i];
switch(c)
{
case '{':flag=;break;
case '}':
value[len] = '\0';
attrMap[key] = value;
break;
case ':':
key[len] = '\0';
len = ;
flag = ;
break;
case ',':
value[len] = '\0';
len = ;
flag = ;
attrMap[key] = value;
break;
default:
if( flag == )
{
key[len++] = c;
}
else if( flag == )
{
value[len++] = c;
}
break;
}
}
} int32_t main()
{
const uint32_t maxFileBufLen = ;
char buf[maxFileBufLen];
uint32_t bufLen;
GetTextFileContent("dbconfig.cfg",buf,maxFileBufLen,bufLen);
ATTRMAP attrMap;
GetAllAttrFromStr(buf,bufLen,attrMap);
std::string szHost = attrMap["host"];
std::string szUsr = attrMap["user"];
std::string szPasswd = attrMap["passwd"];
std::string szDbName = attrMap["dbname"];
std::string szTableName = attrMap["tablename"];
uint32_t unPort = atol(attrMap["port"].c_str());
printf("szHost = '%s'\nszUsr = '%s'\nszPasswd = '%s'\nszDbName = '%s'\nszTableName = '%s'\nunPort = %d\n",\
szHost.c_str(),szUsr.c_str(),szPasswd.c_str(),szDbName.c_str(),szTableName.c_str(),unPort);
return ;
}

测试文件:

{
host:192.168.75.134,
port:,
user:sdo,
passwd:,
dbname:sdo_config,
tablename:sdo_item
}

测试结果:

szHost = '192.168.75.134'
szUsr = 'sdo'
szPasswd = ''
szDbName = 'sdo_config'
szTableName = 'sdo_item'
unPort =

key_value 类型配置文件的解析的更多相关文章

  1. WCF 已知类型和泛型解析程序 KnownType

    数据协定继承 已知类型和泛型解析程序 Juval Lowy 下载代码示例 自首次发布以来,Windows Communication Foundation (WCF) 开发人员便必须处理数据协定继承方 ...

  2. Spring系列之手写注解与配置文件的解析

    目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 Spring系列之AOP的原理及手动实现 引入 在前面我们已经完成了IOC,DI,AOP的实现,基本的功能都已经 ...

  3. mybatis源码-解析配置文件(四-1)之配置文件Mapper解析(cache)

    目录 1. 简介 2. 解析 3 StrictMap 3.1 区别HashMap:键必须为String 3.2 区别HashMap:多了成员变量 name 3.3 区别HashMap:key 的处理多 ...

  4. Spring——ClassPathXmlApplicationContext(配置文件路径解析 1)

    ClassPathXmlApplicationContext     在我的 BeanFactory 容器文章中主要提及了 BeanFactory 容器初始化(Spring 配置文件加载(还没解析)) ...

  5. java内存分配和String类型的深度解析

    [尊重原创文章出自:http://my.oschina.net/xiaohui249/blog/170013] 摘要 从整体上介绍java内存的概念.构成以及分配机制,在此基础上深度解析java中的S ...

  6. 解剖SQLSERVER 第四篇 OrcaMDF里对dates类型数据的解析(译)

    解剖SQLSERVER 第四篇  OrcaMDF里对dates类型数据的解析(译) http://improve.dk/parsing-dates-in-orcamdf/ 在SQLSERVER里面有几 ...

  7. C#匿名类型和动态解析减少定义传输类模板

    C#作为强类型语言,在序列化和反序列化(json)场景中对字符串解析常常需要定义强类型模板,造成编码上的繁琐.其实可以使用匿名类型和动态解析减少json序列化时候的数据模板定义: string a = ...

  8. mybatis源码-解析配置文件(四)之配置文件Mapper解析

    在 mybatis源码-解析配置文件(三)之配置文件Configuration解析 中, 讲解了 Configuration 是如何解析的. 其中, mappers作为configuration节点的 ...

  9. 【转】java内存分配和String类型的深度解析

    一.引题 在java语言的所有数据类型中,String类型是比较特殊的一种类型,同时也是面试的时候经常被问到的一个知识点,本文结合java内存分配深度分析关于String的许多令人迷惑的问题.下面是本 ...

随机推荐

  1. Xamarin.iOS编译时无法连接苹果系统

    Xamarin.iOS编译时无法连接苹果系统   错误信息:Unable to connect to Address=’***.***.***.***’ with User=’***’   即使Vis ...

  2. 递推DP URAL 1119 Metro

    题目传送门 /* 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 递推DP:仿照JayYe,处理的很巧妙,学习:) 好 ...

  3. 水题 Codeforces Round #300 A Cutting Banner

    题目传送门 /* 水题:一开始看错题意,以为是任意切割,DFS来做:结果只是在中间切出一段来 判断是否余下的是 "CODEFORCES" :) */ #include <cs ...

  4. 常用元素默认margin和padding值问题探讨

    关于默认元素在不同浏览器中的margin值是多少的问题,今天做了一个探讨 复制代码 代码如下: // body的margin值 firefox 20.0 ----------------------- ...

  5. 洛谷 P1009 阶乘之和 Label:高精度

    题目描述 用高精度计算出S=1!+2!+3!+…+n!(n≤50) 其中“!”表示阶乘,例如:5!=5*4*3*2*1. 输入输出格式 输入格式: 一个正整数N. 输出格式: 一个正整数S,表示计算结 ...

  6. 低调的华丽,Windows Server 2003 ... 写给厌倦了XP,但又纠结于vista/win7花哨的童鞋(转)

    发布于2001年10月25日的windows XP 距今已近8年 时间, 微软从没有一个操作系统能像XP那样  坚挺这么久,婚姻既有7年之痒,何况用了8年XP的广大 同学,但07年发布的vista似乎 ...

  7. Using pg_dump restore dump file on Odoo

    pg_restore -C -d postgres db.dump

  8. 开源top100

    1.SwitchyOmega 项目简介:SwitchyOmega 是 SwitchySharp 的新版本.这是一个 Chrome 浏览器用来切换不同代理的插件.SwitchyOmega 初次安装时会检 ...

  9. Mybatis resultMap空值映射问题解决

    Mybatis在使用resultMap来映射查询结果中的列,如果查询结果中包含空值的列(不是null),则Mybatis在映射的时候,不会映射这个字段,例如 查询 name,sex,age,数据库中的 ...

  10. html 实体转换为字符:转换 UEditor 编辑器 ( 在 ThinkPHP 3.2.2 中 ) 保存的数据

    在 ThinkPHP 3.2.2 中使用 UEditor 编辑器保存文章内容时,数据库中保存的数据都被转义成实体,例如:&lt;p&gt;&lt;strong&gt;& ...