【C/C++】解析简单配置文件|解析从脚本读出的流配置

目录

解析简单配置文件 std::getline

配置文件的格式

实施摘要

实现细节 1 - 去除空格

Implementation detail 2 - Splitting a string at the delimiter

执行脚本并从脚本中读出配置 GNU getline


解析简单配置文件 std::getline

本文解释了如何解析类似于 Windows .ini 文件的 name=value 形式的配置文件。该代码从行中删除所有空格并跳过空行和包含注释的行。

配置文件的格式

The code explained in this article can parse both a formatted and unformatted input

# formatted
generates_output=true
file_format=txt
  # unformatted
generates_output  =  false
  file_format=doc

实施摘要

下面的代码解析配置文件。在我们成功加载文件后,我们一行一行地读取它。我们从该行中删除所有空格,如果该行为空或包含注释(用“#”表示),则跳过该行。之后,我们在分隔符“=”处拆分字符串“name=value”并打印名称和值。

#include <iostream>
#include <fstream>
#include <algorithm>

int main()
{
    // std::ifstream is RAII, i.e. no need to call close
    std::ifstream cFile ("config2.txt");
    if (cFile.is_open())
    {
        std::string line;
        while(getline(cFile, line)){
            line.erase(std::remove_if(line.begin(), line.end(), isspace),
                                 line.end());
            if(line[0] == '#' || line.empty())
                continue;
            auto delimiterPos = line.find("=");
            auto name = line.substr(0, delimiterPos);
            auto value = line.substr(delimiterPos + 1);
            std::cout << name << " " << value << '\n';
        }
        
    }
    else {
        std::cerr << "Couldn't open config file for reading.\n";
    }
}
警告:如果您在源代码的顶部添加了 using namespace std,则必须使用 ::isspace(来自全局命名空间)而不是 isspace(来自命名空间 std)。

我看到的唯一问如果非注释行不包含 '=' 字符,代码将不起作用。第 14 行删除了该行中的所有空格,但这就是两边是空的,应该判断一下。(http://www.cplusplus.com/forum/general/266880/

实现细节 1 - 去除空格

在进行任何进一步处理之前,我们从行中删除所有空格。这是在几个函数的帮助下完成的,即erase()、remove_if() 和 isspace。

line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());          

remove_if()

The function remove_if() takes a sequence (i.e. the line) and transforms it into a sequence without the undesired characters (i.e. the whitespaces). The length of the sequence does not get altered, however, the elements representing the undesired characters are moved to the end of the sequence and remain in an unspecified state. The function returns an iterator to the new end of the sequence. This is illustrated below. remove_if() takes three arguments. The first two arguments are forward iterators to the initial and final positions in the sequence. The last argument is a function pointer or a function object (in our case the address of the function isspace).

line.erase(remove_if(line.begin(), line.end(), isspace), line.end());          

isspace

The function isspace checks whether an individual character is a whitespace character. Behind the scenes, isspace accepts a single element of the sequence as an argument and returns a value convertible to bool, i.e. true or false.

line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());          

Note: In "C" locale whitespace characters include the space (’ ’), form feed (’\f’), line feed (’\n’), carriage return (’\r’), horizontal tab (’\t’), and vertical tab (’\v’). The backspace character (’\b’) is not a whitespace character in "C" locale. Different locales might define other whitespace characters. From C++ In a Nutshell: A Desktop Quick Reference By Ray Lischner.

string::erase()

The method std::string::erase() erases the sequence of characters in the range (first, last). In our case, it removes the part between the new end of the sequence returned by std::remove_if and the original end of the sequence. This can be seen in the figure below.

line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());        

Implementation detail 2 - Splitting a string at the delimiter

The code below splits the line at the delimiter =. Notice, that we are processing the line under the assumption that there are no whitespace characters, as they have all been removed in the previous step. We firstly find the position of the delimiter = with std::string::find(). After that, we use the method std::string::substr(pos, len) to extract the name and the value. The method substr(pos,len) creates a substring starting at the position pos and spans len characters (or until the end of the string, whichever comes first). Notice, that in the second case we pass only the first parameter, i.e. the position. The default value, i.e. all characters until the end of the string, is used as the second parameter.

auto delimiterPos = line.find("=");
auto name = line.substr(0, delimiterPos);
auto value = line.substr(delimiterPos + 1);

原文:https://www.walletfox.com/course/parseconfigfile.php 

执行脚本并从脚本中读出配置 GNU getline

https://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html

执行脚本,脚本输入如下的配置格式:

device_name=mlx5_1
gid_index=1
ib_port=1

写代码执行脚本并获取

例子:

device_info.txt

device_name=mlx5_1
gid_index=1
ib_port=1
#include <stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;

int main()
{
   
    std::string get_cfg_cmd = string("cat ./device_info.txt") ;
 
    FILE *file     = popen(get_cfg_cmd.c_str(), "r");
    char *outline  = NULL;
    size_t len     = 0;
    ssize_t read   = 0;

    if (file)
    {
        std::string line;
        while((read = getline(&outline, &len, file)) != -1)
        {
            
            line=outline;
            line.erase(std::remove_if(line.begin(), line.end(),::isspace),
                       line.end());
           if(line[0] == '#' || line.empty())
                continue;

            auto delimiterPos = line.find("=");
            auto name  = line.substr(0, delimiterPos);
            auto value = line.substr(delimiterPos + 1);
            std::cout << name << " " << value << '\n';
        }

    }
    else
    {
        std::cerr << "Couldn't open config file for reading.\n";
    }
    
    if(outline != NULL)
        free(outline);
    pclose(file);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值