mirror of
https://github.com/subsurface/subsurface.git
synced 2025-01-31 22:43:25 +00:00
core: convert parse_mkvi_value() and parse_mkvi_key() to C++
This was a particularly funny one: It trampled on and then restored the buffer. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
cca4c8cae5
commit
6b054318e0
1 changed files with 21 additions and 40 deletions
|
@ -445,42 +445,33 @@ int try_to_open_csv(std::string &mem, enum csv_format type, struct divelog *log)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *parse_mkvi_value(char *haystack, const char *needle)
|
static std::string parse_mkvi_value(const char *haystack, const char *needle)
|
||||||
{
|
{
|
||||||
char *lineptr, *valueptr, *endptr, *ret = NULL;
|
const char *lineptr, *valueptr, *endptr;
|
||||||
|
|
||||||
if ((lineptr = strstr(haystack, needle)) != NULL) {
|
if ((lineptr = strstr(haystack, needle)) != NULL) {
|
||||||
if ((valueptr = strstr(lineptr, ": ")) != NULL) {
|
if ((valueptr = strstr(lineptr, ": ")) != NULL) {
|
||||||
valueptr += 2;
|
valueptr += 2;
|
||||||
}
|
}
|
||||||
if ((endptr = strstr(lineptr, "\n")) != NULL) {
|
if ((endptr = strstr(lineptr, "\n")) != NULL) {
|
||||||
char terminator = '\n';
|
if (*(endptr - 1) == '\r')
|
||||||
if (*(endptr - 1) == '\r') {
|
|
||||||
--endptr;
|
--endptr;
|
||||||
terminator = '\r';
|
return std::string(valueptr, endptr - valueptr);
|
||||||
}
|
|
||||||
*endptr = 0;
|
|
||||||
ret = copy_string(valueptr);
|
|
||||||
*endptr = terminator;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *next_mkvi_key(char *haystack)
|
static std::string next_mkvi_key(const char *haystack)
|
||||||
{
|
{
|
||||||
char *valueptr, *endptr, *ret = NULL;
|
const char *valueptr, *endptr;
|
||||||
|
|
||||||
if ((valueptr = strstr(haystack, "\n")) != NULL) {
|
if ((valueptr = strstr(haystack, "\n")) != NULL) {
|
||||||
valueptr += 1;
|
valueptr += 1;
|
||||||
if ((endptr = strstr(valueptr, ": ")) != NULL) {
|
if ((endptr = strstr(valueptr, ": ")) != NULL)
|
||||||
*endptr = 0;
|
return std::string(valueptr, endptr - valueptr);
|
||||||
ret = strdup(valueptr);
|
|
||||||
*endptr = ':';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ret;
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_txt_file(const char *filename, const char *csv, struct divelog *log)
|
int parse_txt_file(const char *filename, const char *csv, struct divelog *log)
|
||||||
|
@ -498,7 +489,7 @@ int parse_txt_file(const char *filename, const char *csv, struct divelog *log)
|
||||||
int hh = 0, mm = 0, ss = 0;
|
int hh = 0, mm = 0, ss = 0;
|
||||||
int prev_depth = 0, cur_sampletime = 0, prev_setpoint = -1, prev_ndl = -1;
|
int prev_depth = 0, cur_sampletime = 0, prev_setpoint = -1, prev_ndl = -1;
|
||||||
bool has_depth = false, has_setpoint = false, has_ndl = false;
|
bool has_depth = false, has_setpoint = false, has_ndl = false;
|
||||||
char *lineptr, *key, *value;
|
char *lineptr;
|
||||||
int prev_time = 0;
|
int prev_time = 0;
|
||||||
cylinder_t cyl = empty_cylinder;
|
cylinder_t cyl = empty_cylinder;
|
||||||
|
|
||||||
|
@ -506,12 +497,9 @@ int parse_txt_file(const char *filename, const char *csv, struct divelog *log)
|
||||||
struct divecomputer *dc;
|
struct divecomputer *dc;
|
||||||
struct tm cur_tm;
|
struct tm cur_tm;
|
||||||
|
|
||||||
value = parse_mkvi_value(memtxt.data(), "Dive started at");
|
std::string value = parse_mkvi_value(memtxt.data(), "Dive started at");
|
||||||
if (sscanf(value, "%d-%d-%d %d:%d:%d", &y, &m, &d, &hh, &mm, &ss) != 6) {
|
if (sscanf(value.c_str(), "%d-%d-%d %d:%d:%d", &y, &m, &d, &hh, &mm, &ss) != 6)
|
||||||
free(value);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
free(value);
|
|
||||||
cur_tm.tm_year = y;
|
cur_tm.tm_year = y;
|
||||||
cur_tm.tm_mon = m - 1;
|
cur_tm.tm_mon = m - 1;
|
||||||
cur_tm.tm_mday = d;
|
cur_tm.tm_mday = d;
|
||||||
|
@ -523,8 +511,7 @@ int parse_txt_file(const char *filename, const char *csv, struct divelog *log)
|
||||||
dive->when = utc_mktime(&cur_tm);;
|
dive->when = utc_mktime(&cur_tm);;
|
||||||
dive->dc.model = strdup("Poseidon MkVI Discovery");
|
dive->dc.model = strdup("Poseidon MkVI Discovery");
|
||||||
value = parse_mkvi_value(memtxt.data(), "Rig Serial number");
|
value = parse_mkvi_value(memtxt.data(), "Rig Serial number");
|
||||||
dive->dc.deviceid = atoi(value);
|
dive->dc.deviceid = atoi(value.c_str());
|
||||||
free(value);
|
|
||||||
dive->dc.divemode = CCR;
|
dive->dc.divemode = CCR;
|
||||||
dive->dc.no_o2sensors = 2;
|
dive->dc.no_o2sensors = 2;
|
||||||
|
|
||||||
|
@ -543,28 +530,22 @@ int parse_txt_file(const char *filename, const char *csv, struct divelog *log)
|
||||||
cyl.type.workingpressure.mbar = 200000;
|
cyl.type.workingpressure.mbar = 200000;
|
||||||
cyl.type.description = "3l Mk6";
|
cyl.type.description = "3l Mk6";
|
||||||
value = parse_mkvi_value(memtxt.data(), "Helium percentage");
|
value = parse_mkvi_value(memtxt.data(), "Helium percentage");
|
||||||
he = atoi(value);
|
he = atoi(value.c_str());
|
||||||
free(value);
|
|
||||||
value = parse_mkvi_value(memtxt.data(), "Nitrogen percentage");
|
value = parse_mkvi_value(memtxt.data(), "Nitrogen percentage");
|
||||||
cyl.gasmix.o2.permille = (100 - atoi(value) - he) * 10;
|
cyl.gasmix.o2.permille = (100 - atoi(value.c_str()) - he) * 10;
|
||||||
free(value);
|
|
||||||
cyl.gasmix.he.permille = he * 10;
|
cyl.gasmix.he.permille = he * 10;
|
||||||
add_cloned_cylinder(&dive->cylinders, cyl);
|
add_cloned_cylinder(&dive->cylinders, cyl);
|
||||||
|
|
||||||
lineptr = strstr(memtxt.data(), "Dive started at");
|
lineptr = strstr(memtxt.data(), "Dive started at");
|
||||||
while (!empty_string(lineptr) && (lineptr = strchr(lineptr, '\n'))) {
|
while (!empty_string(lineptr) && (lineptr = strchr(lineptr, '\n'))) {
|
||||||
++lineptr; // Skip over '\n'
|
++lineptr; // Skip over '\n'
|
||||||
key = next_mkvi_key(lineptr);
|
std::string key = next_mkvi_key(lineptr);
|
||||||
if (!key)
|
if (key.empty())
|
||||||
break;
|
break;
|
||||||
value = parse_mkvi_value(lineptr, key);
|
std::string value = parse_mkvi_value(lineptr, key.c_str());
|
||||||
if (!value) {
|
if (value.empty())
|
||||||
free(key);
|
|
||||||
break;
|
break;
|
||||||
}
|
add_extra_data(&dive->dc, key.c_str(), value.c_str());
|
||||||
add_extra_data(&dive->dc, key, value);
|
|
||||||
free(key);
|
|
||||||
free(value);
|
|
||||||
}
|
}
|
||||||
dc = &dive->dc;
|
dc = &dive->dc;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue