From a02834cf163f36b39331d73359107bdd44bb5aad Mon Sep 17 00:00:00 2001 From: Enrico Zini Date: Sat, 27 Mar 2010 18:13:34 +0000 Subject: [PATCH] Drafted code to load log entries --- README | 1 + src/log.vala | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/README b/README index ea9db46..65bb847 100644 --- a/README +++ b/README @@ -194,6 +194,7 @@ using vala-dbus-binding-tool: if GPS time is more than 1 hour different than the system time, show a "SYNC" button that will sync it if pressed - log + - load log entry for showing it - write data to disk as log happens (to have at least partial logs if power is cut) - more detailed GPX data (dop, elev..) diff --git a/src/log.vala b/src/log.vala index 1a5eba9..607fffe 100644 --- a/src/log.vala +++ b/src/log.vala @@ -178,6 +178,90 @@ public class Log : Object } } +enum LogParserState { + NONE, + METADATA, + TRACK, + WPT, +} + +class LogParser: Object +{ + const MarkupParser parser = { // It's a structure, not an object + start,// when an element opens + end, // when an element closes + text, // when text is found + null, // when comments are found + null // when errors occur + }; + + MarkupParseContext context = null; + public Log result = null; + LogParserState state = LogParserState.NONE; + string cur_text = ""; + + construct + { + context = new MarkupParseContext( + parser, // the structure with the callbacks + 0, // MarkupParseFlags + this, // extra argument for the callbacks, methods in this case + destroy // when the parsing ends + ); + } + + void destroy() + { + cur_text = ""; + } + + public bool parse(string content, ssize_t len = -1) throws MarkupError + { + return context.parse(content, len); + } + + + /* + outfd.puts(" \n"); + outfd.printf(" %s\n", Markup.escape_text(title)); + outfd.puts(" \n"); + if (track != null) writeTrack(outfd); + if (entries != null) writeEntries(outfd); + outfd.puts(" \n"); + */ + + + void start (MarkupParseContext context, string name, + string[] attr_names, string[] attr_values) throws MarkupError + { + if (name == "gpx") + { + state = LogParserState.NONE; + result = new Log("TODO:TAG", "TODO:TITLE"); + } else if (name == "metadata") { + state = LogParserState.METADATA; + } else if (name == "wpt") { + state = LogParserState.WPT; + } else if (name == "trkseg") { + state = LogParserState.TRACK; + } + cur_text = ""; + } + + void end (MarkupParseContext context, string name) throws MarkupError + { + if (name == "state") + if (state == LogParserState.METADATA) + result.title = cur_text; + } + + void text (MarkupParseContext context, + string text, size_t text_len) throws MarkupError + { + cur_text += text; + } +} + public class Logger : Resource, Object { protected List logs; @@ -227,6 +311,16 @@ public class Logger : Resource, Object if (logs == null) end_trace(); } + public Log load(string fname) + { + string contents; + size_t length; + FileUtils.get_contents(fname, out contents, out length); + LogParser parser = new LogParser(); + parser.parse(contents, (ssize_t)length); + return parser.result; + } + public void instant(string tag, string msg) { var log = new Log(tag, msg); -- 2.39.5