Skip to content

Commit 51c82ae

Browse files
committed
add escape function
1 parent 0180f45 commit 51c82ae

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

components/core/src/clp_s/indexer/IndexManager.cpp

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,53 @@ void IndexManager::update_metadata(std::string const& archive_dir, std::string c
5353
}
5454
}
5555

56+
std::string IndexManager::escape_key_name(std::string_view const key_name) {
57+
std::string escaped_key_name;
58+
escaped_key_name.reserve(key_name.size());
59+
for (auto c : key_name) {
60+
switch (c) {
61+
case '\"':
62+
escaped_key_name += "\\\"";
63+
break;
64+
case '\\':
65+
escaped_key_name += "\\\\";
66+
break;
67+
case '\n':
68+
escaped_key_name += "\\n";
69+
break;
70+
case '\t':
71+
escaped_key_name += "\\t";
72+
break;
73+
case '\r':
74+
escaped_key_name += "\\r";
75+
break;
76+
case '\b':
77+
escaped_key_name += "\\b";
78+
break;
79+
case '\f':
80+
escaped_key_name += "\\f";
81+
break;
82+
case '.':
83+
escaped_key_name += "\\.";
84+
break;
85+
default:
86+
if (std::isprint(c)) {
87+
escaped_key_name += c;
88+
} else {
89+
char buffer[7];
90+
std::snprintf(
91+
buffer,
92+
sizeof(buffer),
93+
"\\u00%02x",
94+
static_cast<unsigned char>(c)
95+
);
96+
escaped_key_name += buffer;
97+
}
98+
}
99+
}
100+
return escaped_key_name;
101+
}
102+
56103
std::vector<std::pair<std::string, clp_s::NodeType>> IndexManager::traverse_schema_tree(
57104
std::shared_ptr<SchemaTree> const& schema_tree
58105
) {
@@ -73,18 +120,18 @@ std::vector<std::pair<std::string, clp_s::NodeType>> IndexManager::traverse_sche
73120
}
74121
}
75122

76-
while (!s.empty()) {
123+
while (false == s.empty()) {
77124
auto [node_id, path_length] = s.top();
78125
s.pop();
79126

80-
auto& node = schema_tree->get_node(node_id);
127+
auto const& node = schema_tree->get_node(node_id);
81128
auto& children_ids = node.get_children_ids();
82129
auto node_type = node.get_type();
83130
path_buffer.resize(path_length);
84131
if (false == path_buffer.empty()) {
85132
path_buffer += ".";
86133
}
87-
path_buffer += node.get_key_name();
134+
path_buffer += escape_key_name(node.get_key_name());
88135
if (children_ids.empty() && clp_s::NodeType::Object != node_type
89136
&& clp_s::NodeType::Unknown != node_type)
90137
{

components/core/src/clp_s/indexer/IndexManager.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef CLP_S_INDEXER_TABLEMETADATAMANAGER_HPP
2-
#define CLP_S_INDEXER_TABLEMETADATAMANAGER_HPP
1+
#ifndef CLP_S_INDEXER_INDEXMANAGER_HPP
2+
#define CLP_S_INDEXER_INDEXMANAGER_HPP
33

44
#include "../../clp/GlobalMetadataDBConfig.hpp"
55
#include "../ArchiveReader.hpp"
@@ -47,6 +47,13 @@ class IndexManager {
4747
void update_metadata(std::string const& archive_dir, std::string const& archive_id);
4848

4949
private:
50+
/**
51+
* Escapes a key name
52+
* @param key_name
53+
* @return the escaped key name
54+
*/
55+
static std::string escape_key_name(std::string_view const key_name);
56+
5057
/**
5158
* Traverses the schema tree and returns a list of path names and their types
5259
* @param schema_tree
@@ -60,4 +67,4 @@ class IndexManager {
6067
OutputType m_output_type{OutputType::Database};
6168
};
6269
} // namespace clp_s::indexer
63-
#endif // CLP_S_INDEXER_TABLEMETADATAMANAGER_HPP
70+
#endif // CLP_S_INDEXER_INDEXMANAGER_HPP

components/core/src/clp_s/indexer/MySQLIndexStorage.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef CLP_S_INDEXER_MYSQLTABLEMETADATADB_HPP
2-
#define CLP_S_INDEXER_MYSQLTABLEMETADATADB_HPP
1+
#ifndef CLP_S_INDEXER_MYSQLINDEXSTORAGE_HPP
2+
#define CLP_S_INDEXER_MYSQLINDEXSTORAGE_HPP
33

44
#include "../../clp/MySQLDB.hpp"
55
#include "../../clp/MySQLPreparedStatement.hpp"
@@ -89,4 +89,4 @@ class MySQLIndexStorage {
8989
};
9090
} // namespace clp_s::indexer
9191

92-
#endif // CLP_S_INDEXER_MYSQLTABLEMETADATADB_HPP
92+
#endif // CLP_S_INDEXER_MYSQLINDEXSTORAGE_HPP

components/core/src/clp_s/indexer/indexer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ int main(int argc, char const* argv[]) {
4141
SPDLOG_ERROR("Failed to update metadata: {}", e.what());
4242
return 1;
4343
}
44+
return 0;
4445
}

0 commit comments

Comments
 (0)