Skip to content

Commit 48d0046

Browse files
committed
xrCore: Rewrite of CInifile
1 parent 4a8b6fb commit 48d0046

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1443
-846
lines changed

src/Layers/xrRender/ModelPool.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ void CModelPool::Prefetch()
425425
for (auto I = sect.Data.cbegin(); I != sect.Data.cend(); ++I)
426426
{
427427
const CInifile::Item& item = *I;
428-
dxRender_Visual* V = Create(item.first.c_str());
428+
dxRender_Visual* V = Create(item.name.c_str());
429429
Delete(V, FALSE);
430430
}
431431
Logging(TRUE);

src/Layers/xrRender/Texture.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ bool is_enough_address_space_available() { return true; }
2727

2828
int get_texture_load_lod(LPCSTR fn)
2929
{
30-
CInifile::Sect& sect = pSettings->r_section("reduce_lod_texture_list");
30+
const CInifile::Sect& sect = pSettings->r_section("reduce_lod_texture_list");
3131
auto it_ = sect.Data.cbegin();
3232
auto it_e_ = sect.Data.cend();
3333

@@ -38,7 +38,7 @@ int get_texture_load_lod(LPCSTR fn)
3838

3939
for (; it != it_e; ++it)
4040
{
41-
if (strstr(fn, it->first.c_str()))
41+
if (strstr(fn, it->name.c_str()))
4242
{
4343
if (psTextureLOD < 1)
4444
{

src/Layers/xrRender/TextureDescrManager.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ void CTextureDescrMngr::LoadLTX(pcstr initial, bool listTHM)
5353
const auto processAssociation = [&](const CInifile::Item& item)
5454
{
5555
if (listTHM)
56-
Msg("\t\t%s = %s", item.first.c_str(), item.second.c_str());
56+
Msg("\t\t%s = %s", item.name.c_str(), item.value.c_str());
5757

5858
lock.Enter();
59-
texture_desc& desc = m_texture_details[item.first];
60-
cl_dt_scaler*& dts = m_detail_scalers[item.first];
59+
texture_desc& desc = m_texture_details[item.name];
60+
cl_dt_scaler*& dts = m_detail_scalers[item.name];
6161
lock.Leave();
6262

6363
if (desc.m_assoc)
@@ -68,19 +68,19 @@ void CTextureDescrMngr::LoadLTX(pcstr initial, bool listTHM)
6868
string_path T;
6969
float s;
7070

71-
const int res = sscanf(*item.second, "%[^,],%f", T, &s);
72-
R_ASSERT4(res == 2, "Bad texture association", item.first.c_str(), fname);
71+
const int res = sscanf(*item.value, "%[^,],%f", T, &s);
72+
R_ASSERT4(res == 2, "Bad texture association", item.name.c_str(), fname);
7373
desc.m_assoc->detail_name = T;
7474
if (dts)
7575
dts->scale = s;
7676
else
7777
dts = xr_new<cl_dt_scaler>(s);
7878

79-
if (strstr(item.second.c_str(), "usage[diffuse_or_bump]"))
79+
if (strstr(item.value.c_str(), "usage[diffuse_or_bump]"))
8080
desc.m_assoc->usage.set(texture_assoc::flDiffuseDetail | texture_assoc::flBumpDetail);
81-
else if (strstr(item.second.c_str(), "usage[bump]"))
81+
else if (strstr(item.value.c_str(), "usage[bump]"))
8282
desc.m_assoc->usage.set(texture_assoc::flBumpDetail);
83-
else if (strstr(item.second.c_str(), "usage[diffuse]"))
83+
else if (strstr(item.value.c_str(), "usage[diffuse]"))
8484
desc.m_assoc->usage.set(texture_assoc::flDiffuseDetail);
8585
};
8686
xr_parallel_for_each(data.Data, processAssociation);
@@ -97,10 +97,10 @@ void CTextureDescrMngr::LoadLTX(pcstr initial, bool listTHM)
9797
const auto processSpecification = [&](const CInifile::Item& item)
9898
{
9999
if (listTHM)
100-
Msg("\t\t%s = %s", item.first.c_str(), item.second.c_str());
100+
Msg("\t\t%s = %s", item.name.c_str(), item.value.c_str());
101101

102102
lock.Enter();
103-
texture_desc& desc = m_texture_details[item.first];
103+
texture_desc& desc = m_texture_details[item.name];
104104
lock.Leave();
105105

106106
if (desc.m_spec)
@@ -110,8 +110,8 @@ void CTextureDescrMngr::LoadLTX(pcstr initial, bool listTHM)
110110

111111
string_path bmode;
112112
const int res =
113-
sscanf(item.second.c_str(), "bump_mode[%[^]]], material[%f]", bmode, &desc.m_spec->m_material);
114-
R_ASSERT4(res == 2, "Bad texture specification", item.first.c_str(), fname);
113+
sscanf(item.value.c_str(), "bump_mode[%[^]]], material[%f]", bmode, &desc.m_spec->m_material);
114+
R_ASSERT4(res == 2, "Bad texture specification", item.name.c_str(), fname);
115115
if ((bmode[0] == 'u') && (bmode[1] == 's') && (bmode[2] == 'e') && (bmode[3] == ':'))
116116
{
117117
// bump-map specified

src/Layers/xrRenderDX11/dx11Texture.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void fix_texture_name(pstr fn)
1717

1818
int get_texture_load_lod(LPCSTR fn)
1919
{
20-
CInifile::Sect& sect = pSettings->r_section("reduce_lod_texture_list");
20+
const CInifile::Sect& sect = pSettings->r_section("reduce_lod_texture_list");
2121
auto it_ = sect.Data.cbegin();
2222
auto it_e_ = sect.Data.cend();
2323

@@ -29,7 +29,7 @@ int get_texture_load_lod(LPCSTR fn)
2929

3030
for (; it != it_e; ++it)
3131
{
32-
if (strstr(fn, it->first.c_str()))
32+
if (strstr(fn, it->name.c_str()))
3333
{
3434
if (psTextureLOD < 1)
3535
{

src/Layers/xrRenderGL/glTexture.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ void fix_texture_name(pstr fn)
2121

2222
int get_texture_load_lod(LPCSTR fn)
2323
{
24-
CInifile::Sect& sect = pSettings->r_section("reduce_lod_texture_list");
24+
const CInifile::Sect& sect = pSettings->r_section("reduce_lod_texture_list");
2525

2626
for (const auto& item : sect.Data)
2727
{
28-
if (strstr(fn, item.first.c_str()))
28+
if (strstr(fn, item.name.c_str()))
2929
{
3030
if (psTextureLOD < 1)
3131
return 0;

src/utils/mp_configs_verifyer/configs_dump_verifyer.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ LPCSTR configs_verifyer::get_section_diff(CInifile::Sect* sect_ptr, CInifile& ac
9393

9494
for (auto cit = sect_ptr->Data.cbegin(), ciet = sect_ptr->Data.cend(); cit != ciet; ++cit)
9595
{
96-
shared_str const& tmp_value = cit->second;
96+
shared_str const& tmp_value = cit->value;
9797
shared_str real_value;
9898
if (tmp_active_param)
9999
{
100-
if (active_params.line_exist(sect_ptr->Name.c_str(), cit->first))
100+
if (active_params.line_exist(sect_ptr->Name.c_str(), cit->name))
101101
{
102-
real_value = active_params.r_string(sect_ptr->Name.c_str(), cit->first.c_str());
102+
real_value = active_params.r_string(sect_ptr->Name.c_str(), cit->name.c_str());
103103
if (tmp_value != real_value)
104104
{
105105
pcstr tmp_key_str = nullptr;
106-
STRCONCAT(tmp_key_str, sect_ptr->Name.c_str(), "::", cit->first.c_str());
106+
STRCONCAT(tmp_key_str, sect_ptr->Name.c_str(), "::", cit->name.c_str());
107107
STRCONCAT(diff_str, tmp_key_str, " = ", tmp_value.c_str(), ",right = ", real_value.c_str());
108108
strncpy_s(dst_diff, diff_str, sizeof(dst_diff) - 1);
109109
dst_diff[sizeof(dst_diff) - 1] = 0;
@@ -112,18 +112,18 @@ LPCSTR configs_verifyer::get_section_diff(CInifile::Sect* sect_ptr, CInifile& ac
112112
continue;
113113
}
114114
}
115-
if (!pSettings->line_exist(sect_ptr->Name, cit->first))
115+
if (!pSettings->line_exist(sect_ptr->Name, cit->name))
116116
{
117-
STRCONCAT(diff_str, "line ", sect_ptr->Name.c_str(), "::", cit->first.c_str(), " not found");
117+
STRCONCAT(diff_str, "line ", sect_ptr->Name.c_str(), "::", cit->name.c_str(), " not found");
118118
strncpy_s(dst_diff, diff_str, sizeof(dst_diff) - 1);
119119
dst_diff[sizeof(dst_diff) - 1] = 0;
120120
return dst_diff;
121121
}
122-
real_value = pSettings->r_string(sect_ptr->Name.c_str(), cit->first.c_str());
122+
real_value = pSettings->r_string(sect_ptr->Name.c_str(), cit->name.c_str());
123123
if (tmp_value != real_value)
124124
{
125125
pcstr tmp_key_str = nullptr;
126-
STRCONCAT(tmp_key_str, sect_ptr->Name.c_str(), "::", cit->first.c_str());
126+
STRCONCAT(tmp_key_str, sect_ptr->Name.c_str(), "::", cit->name.c_str());
127127
STRCONCAT(diff_str, tmp_key_str, " = ", tmp_value.c_str(), ",right = ", real_value.c_str());
128128
strncpy_s(dst_diff, diff_str, sizeof(dst_diff) - 1);
129129
dst_diff[sizeof(dst_diff) - 1] = 0;

src/utils/mp_configs_verifyer/mp_config_sections.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ bool mp_config_sections::dump_one(CMemoryWriter& dest)
3939
return false;
4040

4141
R_ASSERT(pSettings->section_exist(m_current_dump_sect->c_str()));
42-
CInifile::Sect& tmp_sect = pSettings->r_section(m_current_dump_sect->c_str());
42+
const CInifile::Sect& tmp_sect = pSettings->r_section(m_current_dump_sect->c_str());
4343

44-
m_tmp_dumper.sections().push_back(&tmp_sect);
44+
m_tmp_dumper.sections().push_back(const_cast<CInifile::Sect*>(&tmp_sect));
4545
m_tmp_dumper.save_as(dest);
4646
m_tmp_dumper.sections().pop_back();
4747
++m_current_dump_sect;

src/utils/xrCompress/xrCompress.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ void xrCompressor::OpenPack(LPCSTR tgt_folder, int num)
320320
W.w_string(buff);
321321
for (const auto& it : S.Data)
322322
{
323-
xr_sprintf(buff, "%s = %s", it.first.c_str(), it.second.c_str());
323+
xr_sprintf(buff, "%s = %s", it.name.c_str(), it.value.c_str());
324324
W.w_string(buff);
325325
}
326326
W.seek(0);
@@ -470,15 +470,15 @@ bool xrCompressor::IsFolderAccepted(const CInifile& ltx, LPCSTR path, bool& recu
470470
const auto& ef_sect = ltx.r_section("exclude_folders");
471471
for (const auto& it : ef_sect.Data)
472472
{
473-
recurse = CInifile::isBool(it.second.c_str());
473+
recurse = CInifile::isBool(it.value.c_str());
474474
if (recurse)
475475
{
476-
if (path == strstr(path, it.first.c_str()))
476+
if (path == strstr(path, it.name.c_str()))
477477
return false;
478478
}
479479
else
480480
{
481-
if (0 == xr_strcmp(path, it.first.c_str()))
481+
if (0 == xr_strcmp(path, it.name.c_str()))
482482
return false;
483483
}
484484
}
@@ -501,11 +501,11 @@ void xrCompressor::ProcessLTX(CInifile& ltx)
501501
const CInifile::Sect& if_sect = ltx.r_section("include_folders");
502502
for (const auto& it : if_sect.Data)
503503
{
504-
const BOOL ifRecurse = CInifile::isBool(it.second.c_str());
504+
const BOOL ifRecurse = CInifile::isBool(it.value.c_str());
505505
const u32 folder_mask = FS_ListFolders | (ifRecurse ? 0 : FS_RootOnly);
506506

507507
string_path path;
508-
const LPCSTR _path = 0 == xr_strcmp(it.first.c_str(), ".\\") ? "" : it.first.c_str();
508+
const LPCSTR _path = 0 == xr_strcmp(it.name.c_str(), ".\\") ? "" : it.name.c_str();
509509
xr_strcpy(path, _path);
510510
const size_t path_len = xr_strlen(path);
511511
if ((0 != path_len) && (path[path_len - 1] != '\\'))
@@ -556,7 +556,7 @@ void xrCompressor::ProcessLTX(CInifile& ltx)
556556
{
557557
const CInifile::Sect& if_sect = ltx.r_section("include_files");
558558
for (const auto& it : if_sect.Data)
559-
files_list->push_back(xr_strdup(it.first.c_str()));
559+
files_list->push_back(xr_strdup(it.name.c_str()));
560560
}
561561

562562
PerformWork();

src/xrCore/Animation/SkeletonMotions.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ void CPartition::load(IKinematics* V, LPCSTR model_name)
5252
for (; it != it_e; ++it)
5353
{
5454
const CInifile::Item& I = *it;
55-
if (I.first == part_name)
55+
if (I.name == part_name)
5656
{
57-
P[i].Name = I.second;
57+
P[i].Name = I.value;
5858
}
5959
else
6060
{
61-
u32 bid = V->LL_BoneID(I.first.c_str());
61+
u32 bid = V->LL_BoneID(I.name.c_str());
6262
P[i].bones.push_back(bid);
6363
}
6464
}

src/xrCore/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,14 @@ target_sources_grouped(
344344
os_clipboard.h
345345
)
346346

347+
target_sources_grouped(
348+
TARGET xrCore
349+
NAME "Parsing"
350+
FILES
351+
ParsingUtils.cpp
352+
ParsingUtils.hpp
353+
)
354+
347355
target_sources_grouped(
348356
TARGET xrCore
349357
NAME "PCH"

src/xrCore/ParsingUtils.cpp

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "stdafx.h"
2+
3+
#include "ParsingUtils.hpp"
4+
5+
ParseIncludeResult ParseInclude(pstr string, pcstr& out_include_name)
6+
{
7+
VERIFY(string);
8+
9+
// Skip any whitespace characters
10+
string = ParseAllSpaces(string);
11+
12+
// Check for #include
13+
static constexpr pcstr IncludeTag = "#include";
14+
if (std::strncmp(string, IncludeTag, 8) != 0)
15+
return ParseIncludeResult::NoInclude;
16+
17+
string += 8;
18+
19+
// Skip any whitespace characters
20+
string = ParseAllSpaces(string);
21+
22+
// Check that after the tag there is a quote
23+
if (*string != '\"')
24+
return ParseIncludeResult::Error;
25+
26+
// Mark the start of the include name
27+
++string;
28+
out_include_name = string;
29+
30+
string = ParseUntil(string, '\"');
31+
32+
// Check for unterminated or empty include name
33+
if (*string == '\0' || out_include_name == string)
34+
return ParseIncludeResult::Error;
35+
36+
// Check for unreasonably long include names
37+
const size_t size = string - out_include_name;
38+
if (size > 1024)
39+
return ParseIncludeResult::Error;
40+
41+
// NOTE(Andre): Yes this might look scary but it's perfectly fine. Since the include name is already in the string
42+
// we are parsing and its not used afterwards we simply replace the closing quote with a null byte and we have a
43+
// valid c-string pointed to by 'out_include_name' and safe ourselves the need to copy the string.
44+
*string = '\0';
45+
46+
return ParseIncludeResult::Success;
47+
}
48+
49+
pcstr ParseAllSpaces(pcstr string)
50+
{
51+
VERIFY(string);
52+
53+
while (*string != '\0' && std::isspace(*string))
54+
++string;
55+
56+
return string;
57+
}
58+
59+
pstr ParseAllSpaces(pstr string) { return const_cast<pstr>(ParseAllSpaces(reinterpret_cast<pcstr>(string))); }
60+
61+
pcstr ParseUntil(pcstr string, const char character)
62+
{
63+
VERIFY(string);
64+
65+
while (*string != '\0' && *string != character)
66+
++string;
67+
68+
return string;
69+
}
70+
71+
pstr ParseUntil(pstr string, const char character)
72+
{
73+
return const_cast<pstr>(ParseUntil(reinterpret_cast<pcstr>(string), character));
74+
}
75+
76+
void StringCopyLowercase(pstr destination, pcstr src, std::size_t size)
77+
{
78+
VERIFY(destination);
79+
VERIFY(src);
80+
81+
for (std::size_t i = 0; *src != '\0' && i < size; ++i)
82+
{
83+
*destination = std::tolower(*src);
84+
++src;
85+
++destination;
86+
}
87+
88+
// Ensure the string is null-terminated
89+
*destination = '\0';
90+
}
91+
92+
void StringCopyLowercase(pstr destination, shared_str src, std::size_t size)
93+
{
94+
StringCopyLowercase(destination, *src, size);
95+
}

src/xrCore/ParsingUtils.hpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include "xr_types.h"
4+
#include "xrstring.h"
5+
6+
enum class ParseIncludeResult
7+
{
8+
Success, /// There is a valid #include and 'out_include_name' contains the filename
9+
Error, /// There is a #include but there is some problem
10+
NoInclude, /// There is no #include on this line
11+
};
12+
13+
// Given a string of the form: '#include "filename"' we try to parse filename into 'out_include_name'
14+
// Note that the file name is parsed inplace to avoid copying the string
15+
ParseIncludeResult ParseInclude(pstr string, pcstr& out_include_name);
16+
17+
// Starting from the beginning of the string skips all characters for which 'std::isspace' is 'true'.
18+
// Returns the first position where 'std::isspace' is 'false'.
19+
pcstr ParseAllSpaces(pcstr string);
20+
21+
pstr ParseAllSpaces(pstr string);
22+
23+
// Starting from the begging of the string skips all characters until 'character' is found
24+
// or until the end of the string is reached.
25+
// Returns the first position where 'character' is found or the end of string if 'character' is not found
26+
pcstr ParseUntil(pcstr string, const char character);
27+
28+
pstr ParseUntil(pstr string, const char character);
29+
30+
// Copies 'size' characters from 'src' to 'destination' and converts it to lowercase
31+
void StringCopyLowercase(pstr destination, pcstr src, std::size_t size);
32+
33+
void StringCopyLowercase(pstr destination, shared_str src, std::size_t size);

0 commit comments

Comments
 (0)