从配置文件中读取语法
Read Syntax from configuration file
我正在尝试在 C/C++ 中编写配置 reader(使用低级 I/O)。
配置包含如下说明:
App example.com {
use: python vauejs sass;
root: www/html;
ssl: Enabled;
}
如何将内容读入 std::map 或结构? Google 还没有给我想要的结果。我希望有一些想法...
到目前为止我得到了什么:
// File Descriptor
int fd;
// Open File
const errno_t ferr = _sopen_s(&fd, _file, _O_RDONLY, _SH_DENYWR, _S_IREAD);
// Handle returned value
switch (ferr) {
// The given path is a directory, or the file is read-only, but an open-for-writing operation was attempted.
case EACCES:
perror("[Error] Following error occurred while reading configuration");
return false;
break;
// Invalid oflag, shflag, or pmode argument, or pfh or filename was a null pointer.
case EINVAL:
perror("[Error] Following error occurred while reading configuration");
return false;
break;
// No more file descriptors available.
case EMFILE:
perror("[Error] Following error occurred while reading configuration");
return false;
break;
// File or path not found.
case ENOENT:
perror("[Error] Following error occured while reading configuration");
return false;
break;
}
// Notify Screen
if (pDevMode)
std::printf("[Configuration]: '_sopen_s' were able to open file \"%s\".\n", _file);
// Allocate memory for buffer
buffer = new (std::nothrow) char[4098 * 4];
// Did the allocation succeed?
if (buffer == nullptr) {
_close(fd);
std::perror("[Error] Following error occurred while reading configuration");
return false;
}
// Notify Screen
if (pDevMode)
std::printf("[Configuration]: Buffer Allocation succeed.\n");
// Reading content from file
const std::size_t size = _read(fd, buffer, (4098 * 4));
如果您将缓冲区放入 std::string
中,您可以从有关在 SO 上拆分字符串的各种答案中拼凑出一个解决方案。
基本结构似乎是"stuff { key:value \n key:value \n }"
有不同数量的空白。许多关于 trimming 字符串的问题被问到。拆分字符串可以通过多种方式进行,例如
std::string config = "App example.com {\n"
" use: python vauejs sass;\n"
" root: www / html; \n"
" ssl: Enabled;"
"}";
std::istringstream ss(config);
std::string token;
std::getline(ss, token, '{');
std::cout << token << "... ";
std::getline(ss, token, ':');
//use handy trim function - loads of e.g.s on SO
std::cout << token << " = ";
std::getline(ss, token, '\n');
// trim function required...
std::cout << token << "...\n\n";
//as many times or in a loop..
//then check for closing }
如果您有更复杂的解析,请考虑使用完整的解析器。
我正在尝试在 C/C++ 中编写配置 reader(使用低级 I/O)。 配置包含如下说明:
App example.com {
use: python vauejs sass;
root: www/html;
ssl: Enabled;
}
如何将内容读入 std::map 或结构? Google 还没有给我想要的结果。我希望有一些想法...
到目前为止我得到了什么:
// File Descriptor
int fd;
// Open File
const errno_t ferr = _sopen_s(&fd, _file, _O_RDONLY, _SH_DENYWR, _S_IREAD);
// Handle returned value
switch (ferr) {
// The given path is a directory, or the file is read-only, but an open-for-writing operation was attempted.
case EACCES:
perror("[Error] Following error occurred while reading configuration");
return false;
break;
// Invalid oflag, shflag, or pmode argument, or pfh or filename was a null pointer.
case EINVAL:
perror("[Error] Following error occurred while reading configuration");
return false;
break;
// No more file descriptors available.
case EMFILE:
perror("[Error] Following error occurred while reading configuration");
return false;
break;
// File or path not found.
case ENOENT:
perror("[Error] Following error occured while reading configuration");
return false;
break;
}
// Notify Screen
if (pDevMode)
std::printf("[Configuration]: '_sopen_s' were able to open file \"%s\".\n", _file);
// Allocate memory for buffer
buffer = new (std::nothrow) char[4098 * 4];
// Did the allocation succeed?
if (buffer == nullptr) {
_close(fd);
std::perror("[Error] Following error occurred while reading configuration");
return false;
}
// Notify Screen
if (pDevMode)
std::printf("[Configuration]: Buffer Allocation succeed.\n");
// Reading content from file
const std::size_t size = _read(fd, buffer, (4098 * 4));
如果您将缓冲区放入 std::string
中,您可以从有关在 SO 上拆分字符串的各种答案中拼凑出一个解决方案。
基本结构似乎是"stuff { key:value \n key:value \n }"
有不同数量的空白。许多关于 trimming 字符串的问题被问到。拆分字符串可以通过多种方式进行,例如
std::string config = "App example.com {\n"
" use: python vauejs sass;\n"
" root: www / html; \n"
" ssl: Enabled;"
"}";
std::istringstream ss(config);
std::string token;
std::getline(ss, token, '{');
std::cout << token << "... ";
std::getline(ss, token, ':');
//use handy trim function - loads of e.g.s on SO
std::cout << token << " = ";
std::getline(ss, token, '\n');
// trim function required...
std::cout << token << "...\n\n";
//as many times or in a loop..
//then check for closing }
如果您有更复杂的解析,请考虑使用完整的解析器。