Use the extra-data API and fix memory leak.

miika
From c07abdee1c9c775524a12f2aadf0834b260a0125 Mon Sep 17 00:00:00 2001
From: Miika Turkia <[email protected]>
Date: Sun, 9 Nov 2014 20:03:45 +0800
Subject: [PATCH 1/2] Use the new extra data interface for Poseidon import

Rather than overflowing the notes field, let's add all the details from
DC using the extra data API.

Signed-off-by: Miika Turkia <[email protected]>
---
 file.c | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)

diff --git a/file.c b/file.c
index fa684dc..685a80b 100644
--- a/file.c
+++ b/file.c
@@ -432,6 +432,22 @@ char *parse_mkvi_value(const char *haystack, const char *needle)
 	return ret;
 }
 
+char *next_mkvi_key(const char *haystack)
+{
+	char *valueptr, *endptr, *ret = NULL;
+
+	if ((valueptr = strstr(haystack, "\n")) != NULL) {
+		valueptr += 1;
+	}
+	if ((endptr = strstr(valueptr, ": ")) != NULL) {
+		*endptr = 0;
+		ret = strdup(valueptr);
+		*endptr = ':';
+
+	}
+	return ret;
+}
+
 int parse_txt_file(const char *filename, const char *csv)
 {
 	struct memblock memtxt, memcsv;
@@ -449,7 +465,7 @@ int parse_txt_file(const char *filename, const char *csv)
 		int hh = 0, mm = 0, ss = 0;
 		int prev_depth = 0, cur_sampletime = 0, prev_setpoint = -1;
 		bool has_depth = false, has_setpoint = false;
-		char *lineptr;
+		char *lineptr, *key, *value;
 		int diluent_pressure = 0, cylinder_pressure = 0, cur_cylinder_index = 0;
 
 		struct dive *dive;
@@ -492,8 +508,19 @@ int parse_txt_file(const char *filename, const char *csv)
 		cur_cylinder_index++;
 
 		lineptr = strstr(memtxt.buffer, "Dive started at");
-		if (lineptr)
-			dive->notes = strdup(lineptr);
+		while (lineptr && *lineptr && (lineptr = strchr(lineptr, '\n')) && ++lineptr) {
+			key = next_mkvi_key(lineptr);
+			if (!key)
+				break;
+			value = parse_mkvi_value(lineptr, key);
+			if (!value) {
+				free(key);
+				break;
+			}
+			add_extra_data(&dive->dc, key, value);
+			free(key);
+			free(value);
+		}
 		dc = &dive->dc;
 
 		/*
-- 
2.1.0

From 73e165af2c4cf79b09e25b4483dc193e6be37424 Mon Sep 17 00:00:00 2001
From: Miika Turkia <[email protected]>
Date: Sun, 9 Nov 2014 20:17:29 +0800
Subject: [PATCH 2/2] Fix memory leak

Signed-off-by: Miika Turkia <[email protected]>
---
 file.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/file.c b/file.c
index 685a80b..29bbea5 100644
--- a/file.c
+++ b/file.c
@@ -487,7 +487,9 @@ int parse_txt_file(const char *filename, const char *csv)
 		dive = alloc_dive();
 		dive->when = utc_mktime(&cur_tm);;
 		dive->dc.model = strdup("Poseidon MkVI Discovery");
-		dive->dc.deviceid = atoi(parse_mkvi_value(memtxt.buffer, "Rig Serial number"));
+		value = parse_mkvi_value(memtxt.buffer, "Rig Serial number");
+		dive->dc.deviceid = atoi(value);
+		free(value);
 		dive->dc.dctype = CCR;
 		dive->dc.no_o2sensors = 2;
 
@@ -502,8 +504,12 @@ int parse_txt_file(const char *filename, const char *csv)
 		dive->cylinder[cur_cylinder_index].type.size.mliter = 3000;
 		dive->cylinder[cur_cylinder_index].type.workingpressure.mbar = 200000;
 		dive->cylinder[cur_cylinder_index].type.description = strdup("3l Mk6");
-		he = atoi(parse_mkvi_value(memtxt.buffer, "Helium percentage"));
-		dive->cylinder[cur_cylinder_index].gasmix.o2.permille = (100 - atoi(parse_mkvi_value(memtxt.buffer, "Nitrogen percentage")) - he) * 10;
+		value = parse_mkvi_value(memtxt.buffer, "Helium percentage");
+		he = atoi(value);
+		free(value);
+		value = parse_mkvi_value(memtxt.buffer, "Nitrogen percentage");
+		dive->cylinder[cur_cylinder_index].gasmix.o2.permille = (100 - atoi(value) - he) * 10;
+		free(value);
 		dive->cylinder[cur_cylinder_index].gasmix.he.permille = he * 10;
 		cur_cylinder_index++;
 
-- 
2.1.0

_______________________________________________
subsurface mailing list
[email protected]
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to