Skip to content

Commit e4a314c

Browse files
committed
added input file security & fixed dynamic .csv path storage
no comment.
1 parent 64e4108 commit e4a314c

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/conf.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,23 @@ bool updateNbAlt(config& conf, int new_nb_alt)
247247
return true;
248248
}
249249

250+
// ✅ function + comment verified.
251+
/**
252+
* @brief Updates the nb_alt field in both memory and the config file.
253+
*/
254+
bool updateMapPath(config& conf, std::string map_path)
255+
{
256+
auto config_map = readKeyValueFile(conf.config_file, true);
257+
if (config_map.empty()) return false;
258+
updateConfigMap(config_map, "map_path", map_path);
259+
if (!writeConfigFile(conf.config_file, config_map)) {
260+
logger("error: failed to write updated config to file.");
261+
console("error", "failed to write updated config to file.");
262+
return false;
263+
}
264+
return true;
265+
}
266+
250267
// ✅ function + comment verified.
251268
/**
252269
* @brief Loads or creates a new configuration file based on user input.
@@ -300,8 +317,17 @@ config getConfiguration(config conf)
300317
{
301318
ensureDirectory("graph");
302319

303-
std::cout << "\n > enter .csv map path (format: landmark_A,landmark_B,cost): " << std::flush;
304-
std::cin >> conf.map_path;
320+
while (true) {
321+
std::cout << "\n > enter .csv map path (format: landmark_A,landmark_B,cost): " << std::flush;
322+
std::cin >> conf.map_path;
323+
324+
if (fileExists(conf.map_path)) {
325+
break;
326+
} else {
327+
console("error", "could not open file : " + conf.map_path);
328+
logger("error: could not open file : " + conf.map_path);
329+
}
330+
}
305331

306332
std::cout << "\n" << std::flush;
307333

@@ -322,7 +348,15 @@ config getConfiguration(config conf)
322348
{
323349
if (verifyConfFile(conf)) {
324350
console("success", "config file detected! file path: " + conf.config_file);
351+
352+
std::string tmp_map_path = conf.map_path;
325353
conf = loadConfFile(conf);
354+
if(conf.map_path != tmp_map_path) {
355+
conf.map_path = tmp_map_path;
356+
updateMapPath(conf, tmp_map_path);
357+
console("info", "new map path updated inside configuration file.");
358+
}
359+
326360
console("success", "config file loaded!");
327361
return conf;
328362
}

0 commit comments

Comments
 (0)