Skip to content

Commit

Permalink
AP_HAL_SITL: simplify lookup_location using null-terminated data
Browse files Browse the repository at this point in the history
  • Loading branch information
tpwrules committed Feb 23, 2024
1 parent c0a88f6 commit 2c98217
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions libraries/AP_HAL_SITL/SITL_cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,29 +701,22 @@ bool SITL_State::parse_home(const char *home_str, Location &loc, float &yaw_degr
bool SITL_State::lookup_location(const char *home_str, Location &loc, float &yaw_degrees)
{
const char *locations = "@ROMFS/locations.txt";
FileData *fd = AP::FS().load_file(locations);
FileData *fd = AP::FS().load_file(locations); // data is null-terminated
if (fd == nullptr) {
::printf("Missing %s\n", locations);
return false;
}
char *str = strndup((const char *)fd->data, fd->length);
if (!str) {
delete fd;
return false;
}
size_t len = strlen(home_str);
char *saveptr = nullptr;
for (char *s = strtok_r(str, "\r\n", &saveptr);
for (char *s = strtok_r((const char*)fd->data, "\r\n", &saveptr);
s;
s=strtok_r(nullptr, "\r\n", &saveptr)) {
if (strncasecmp(s, home_str, len) == 0 && s[len]=='=') {
bool ok = parse_home(&s[len+1], loc, yaw_degrees);
free(str);
delete fd;
return ok;
}
}
free(str);
delete fd;
::printf("Failed to find location '%s'\n", home_str);
return false;
Expand Down

0 comments on commit 2c98217

Please sign in to comment.