Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
ogs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Chaofan Chen
ogs
Commits
8059c057
Verified
Commit
8059c057
authored
3 years ago
by
Lars Bilke
Browse files
Options
Downloads
Patches
Plain Diff
[prj] Refactor code to be more C++ like.
parent
f78c4067
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
BaseLib/ConfigTreeUtil.cpp
+30
-30
30 additions, 30 deletions
BaseLib/ConfigTreeUtil.cpp
with
30 additions
and
30 deletions
BaseLib/ConfigTreeUtil.cpp
+
30
−
30
View file @
8059c057
...
...
@@ -26,20 +26,9 @@
namespace
BaseLib
{
auto
read_file
(
std
::
string_view
path
)
->
std
::
string
bool
isEqual
(
const
unsigned
char
*
a
,
std
::
string
const
&
b
)
{
constexpr
auto
read_size
=
std
::
size_t
(
4096
);
auto
stream
=
std
::
ifstream
(
path
.
data
());
stream
.
exceptions
(
std
::
ios_base
::
badbit
);
auto
out
=
std
::
string
();
auto
buf
=
std
::
string
(
read_size
,
'\0'
);
while
(
stream
.
read
(
&
buf
[
0
],
read_size
))
{
out
.
append
(
buf
,
0
,
stream
.
gcount
());
}
out
.
append
(
buf
,
0
,
stream
.
gcount
());
return
out
;
return
std
::
string
{
reinterpret_cast
<
const
char
*>
(
a
)}
==
b
;
}
void
traverseIncludes
(
xmlDoc
*
doc
,
xmlNode
*
node
,
...
...
@@ -53,17 +42,21 @@ void traverseIncludes(xmlDoc* doc, xmlNode* node,
{
continue
;
}
if
(
!
strcmp
(
reinterpret_cast
<
const
char
*>
(
cur_node
->
name
),
"include"
))
if
(
isEqual
(
cur_node
->
name
,
"include"
))
// if (!strcmp(reinterpret_cast<const char*>(cur_node->name),
// "include"))
{
auto
include_file
=
xmlGetProp
(
cur_node
,
xmlCharStrdup
(
"file"
));
if
(
include_file
==
NULL
)
auto
include_file_char_pointer
=
xmlGetProp
(
cur_node
,
xmlCharStrdup
(
"file"
));
if
(
include_file_char_pointer
==
nullptr
)
{
OGS_FATAL
(
"Error while processing includes in prj file. Error in "
"element '{:s}' on line {:d}: no file attribute given!"
,
cur_node
->
name
,
cur_node
->
line
);
}
std
::
string
filename
(
reinterpret_cast
<
char
*>
(
include_file
));
std
::
string
filename
(
reinterpret_cast
<
char
*>
(
include_file_char_pointer
));
if
(
auto
const
filepath
=
std
::
filesystem
::
path
(
filename
);
filepath
.
is_relative
())
{
...
...
@@ -77,11 +70,19 @@ void traverseIncludes(xmlDoc* doc, xmlNode* node,
"element '{:s}' on line {:d}: Include file is not "
"existing: "
"{:s}!"
,
cur_node
->
name
,
cur_node
->
line
,
include_file
);
cur_node
->
name
,
cur_node
->
line
,
include_file
_char_pointer
);
}
INFO
(
"Including {:s} into project file."
,
filename
);
std
::
string
xml
=
read_file
(
filename
);
const
std
::
ifstream
input_stream
(
filename
,
std
::
ios_base
::
binary
);
if
(
input_stream
.
fail
())
{
OGS_FATAL
(
"Failed to open file {s}!"
,
filename
);
}
std
::
stringstream
buffer
;
buffer
<<
input_stream
.
rdbuf
();
const
std
::
string
xml
=
buffer
.
str
();
xmlNodePtr
pNewNode
=
nullptr
;
xmlParseInNodeContext
(
cur_node
->
parent
,
xml
.
c_str
(),
(
int
)
xml
.
length
(),
0
,
&
pNewNode
);
...
...
@@ -113,7 +114,7 @@ void replaceIncludes(std::stringstream& prj_stream,
{
auto
doc
=
xmlParseMemory
(
prj_stream
.
str
().
c_str
(),
prj_stream
.
str
().
size
());
if
(
doc
==
NULL
)
if
(
doc
==
nullptr
)
{
OGS_FATAL
(
"Error reading project file from memory."
);
}
...
...
@@ -132,25 +133,25 @@ void replaceIncludes(std::stringstream& prj_stream,
}
// Applies a patch file to the prj content in prj_stream.
void
patchStream
(
std
::
string
patch_file
,
std
::
stringstream
&
prj_stream
,
void
patchStream
(
std
::
string
const
&
patch_file
,
std
::
stringstream
&
prj_stream
,
bool
after_includes
=
false
)
{
auto
patch
=
xmlParseFile
(
patch_file
.
c_str
());
if
(
patch
==
NULL
)
if
(
patch
==
nullptr
)
{
OGS_FATAL
(
"Error reading XML diff file {:s}."
,
patch_file
);
}
auto
doc
=
xmlParseMemory
(
prj_stream
.
str
().
c_str
(),
prj_stream
.
str
().
size
());
if
(
doc
==
NULL
)
if
(
doc
==
nullptr
)
{
OGS_FATAL
(
"Error reading project file from memory."
);
}
auto
node
=
xmlDocGetRootElement
(
patch
);
int
rc
=
0
;
for
(
node
=
node
?
node
->
children
:
NULL
;
node
;
node
=
node
->
next
)
for
(
node
=
node
?
node
->
children
:
nullptr
;
node
;
node
=
node
->
next
)
{
if
(
node
->
type
!=
XML_ELEMENT_NODE
)
{
...
...
@@ -163,9 +164,8 @@ void patchStream(std::string patch_file, std::stringstream& prj_stream,
// Check for after_includes-attribute
xmlChar
*
value
=
xmlNodeListGetString
(
node
->
doc
,
attribute
->
children
,
1
);
if
(
!
strcmp
(
reinterpret_cast
<
const
char
*>
(
attribute
->
name
),
"after_includes"
)
&&
!
strcmp
(
reinterpret_cast
<
const
char
*>
(
value
),
"true"
))
if
(
isEqual
(
attribute
->
name
,
"after_includes"
)
&&
isEqual
(
value
,
"true"
))
{
node_after_includes
=
true
;
}
...
...
@@ -179,15 +179,15 @@ void patchStream(std::string patch_file, std::stringstream& prj_stream,
continue
;
}
if
(
!
strcmp
(
reinterpret_cast
<
const
char
*>
(
node
->
name
)
,
"add"
))
if
(
isEqual
(
node
->
name
,
"add"
))
{
rc
=
xml_patch_add
(
doc
,
node
);
}
else
if
(
!
strcmp
(
reinterpret_cast
<
const
char
*>
(
node
->
name
)
,
"replace"
))
else
if
(
isEqual
(
node
->
name
,
"replace"
))
{
rc
=
xml_patch_replace
(
doc
,
node
);
}
else
if
(
!
strcmp
(
reinterpret_cast
<
const
char
*>
(
node
->
name
)
,
"remove"
))
else
if
(
isEqual
(
node
->
name
,
"remove"
))
{
rc
=
xml_patch_remove
(
doc
,
node
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment