Skip to content

Commit af08325

Browse files
committed
Add changes from code review
1 parent 33adccb commit af08325

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

include/ur_client_library/control/script_reader.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ class ScriptReader
7171

7272
/*!
7373
* \brief Reads a script file and applies variable replacements, includes, and conditionals.
74-
* \param filename Filename (absolute path) of the script to be loaded.
74+
* \param file_path Path of the script file to be loaded.
7575
* \param data Data dictionary used for variable replacements and expression evaluation.
7676
* \return The Script code with all replacements, includes and conditionals applied.
7777
*/
78-
std::string readScriptFile(const std::string& filename, const DataDict& data = DataDict());
78+
std::string readScriptFile(const std::string& file_path, const DataDict& data = DataDict());
7979

8080
/*!
8181
* \brief Evaluate a boolean expression
@@ -102,7 +102,7 @@ class ScriptReader
102102

103103
std::filesystem::path script_path_;
104104

105-
static std::string readFileContent(const std::string& filename);
105+
static std::string readFileContent(const std::string& file_path);
106106
void replaceIncludes(std::string& script_code, const DataDict& data);
107107
static void replaceVariables(std::string& script_code, const DataDict& data);
108108
static void replaceConditionals(std::string& script_code, const DataDict& data);
@@ -125,4 +125,4 @@ inline bool operator>=(const ScriptReader::DataVariant& lhs, const ScriptReader:
125125
return (lhs > rhs || lhs == rhs);
126126
}
127127
} // namespace control
128-
} // namespace urcl
128+
} // namespace urcl

src/control/script_reader.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace urcl
4141
{
4242
namespace control
4343
{
44+
constexpr double ZERO_EPSILON = 0.000001;
4445

4546
bool operator<(const ScriptReader::DataVariant& lhs, const ScriptReader::DataVariant& rhs)
4647
{
@@ -96,7 +97,7 @@ bool operator==(const ScriptReader::DataVariant& lhs, const ScriptReader::DataVa
9697
{
9798
if (std::holds_alternative<double>(rhs))
9899
{
99-
return std::abs((static_cast<double>(std::get<bool>(lhs)) - std::get<double>(rhs))) < 0.0001;
100+
return std::abs((static_cast<double>(std::get<bool>(lhs)) - std::get<double>(rhs))) < ZERO_EPSILON;
100101
}
101102
if (std::holds_alternative<int>(rhs))
102103
{
@@ -144,12 +145,11 @@ std::string ScriptReader::readScriptFile(const std::string& filename, const Data
144145
return script_code;
145146
}
146147

147-
std::string ScriptReader::readFileContent(const std::string& filename)
148+
std::string ScriptReader::readFileContent(const std::string& file_path)
148149
{
149150
std::ifstream ifs;
150-
ifs.open(filename);
151+
ifs.open(file_path);
151152
std::string content;
152-
std::ifstream file(filename);
153153
if (ifs)
154154
{
155155
content = std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
@@ -158,7 +158,7 @@ std::string ScriptReader::readFileContent(const std::string& filename)
158158
else
159159
{
160160
std::stringstream ss;
161-
ss << "Could not open script file '" << filename << "'. Please check if the file exists and is readable.";
161+
ss << "Could not open script file '" << file_path << "'. Please check if the file exists and is readable.";
162162
throw UrException(ss.str().c_str());
163163
}
164164

@@ -174,8 +174,9 @@ void ScriptReader::replaceIncludes(std::string& script, const DataDict& data)
174174
// Replace all include patterns in the line
175175
while (std::regex_search(script, match, include_pattern))
176176
{
177-
std::filesystem::path file_path(match[1].str());
178-
std::string file_content = readScriptFile((script_path_.parent_path() / file_path.string()).string(), data);
177+
std::filesystem::path relative_file_path(match[1].str());
178+
std::string file_content =
179+
readScriptFile((script_path_.parent_path() / relative_file_path.string()).string(), data);
179180
script.replace(match.position(0), match.length(0), file_content);
180181
}
181182
}

0 commit comments

Comments
 (0)