summaryrefslogtreecommitdiff
path: root/kicad
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2016-10-28 09:05:07 (GMT)
committerWerner Almesberger <werner@almesberger.net>2016-10-28 09:05:07 (GMT)
commit87cad97c4decd0c7c69af1951cd3f8f4a211e6d8 (patch)
tree1fb60b98a4ce1dac90608d20a7f4ba7d226e3d23 /kicad
parent4c118f17efcec21be53a325c2327cd0a9a14bba0 (diff)
downloadeeshow-87cad97c4decd0c7c69af1951cd3f8f4a211e6d8.zip
eeshow-87cad97c4decd0c7c69af1951cd3f8f4a211e6d8.tar.gz
eeshow-87cad97c4decd0c7c69af1951cd3f8f4a211e6d8.tar.bz2
kicad/pl-render.c: complex date formatting (for eeplot -d, WIP)
Diffstat (limited to 'kicad')
-rw-r--r--kicad/pl-render.c110
1 files changed, 109 insertions, 1 deletions
diff --git a/kicad/pl-render.c b/kicad/pl-render.c
index c50815a..c84213c 100644
--- a/kicad/pl-render.c
+++ b/kicad/pl-render.c
@@ -10,11 +10,14 @@
* (at your option) any later version.
*/
+#define _XOPEN_SOURCE /* for strptime */
+#define _XOPEN_SOURCE_EXTENDED /* to not lose strdup */
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
+#include <time.h>
#include <math.h>
#include "version.h"
@@ -37,6 +40,9 @@ bool suppress_page_layout = 0;
const char *date_override = NULL;
+#define DEFAULT_DATE_FORMAT "%F %T %z"
+#define KICAD_DATE_FORMAT "%F"
+
/* ----- Coordinate transform ---------------------------------------------- */
@@ -59,6 +65,104 @@ static int coord(int v, int d, int o, int e)
}
+/* ----- Date formatting --------------------------------------------------- */
+
+
+static char *format_date(const char *fmt, const char *sheet_date)
+{
+ char *s = stralloc("");
+ time_t user_time = -1;
+ const char *p = fmt;
+ char *arg = NULL;
+ struct tm tm;
+ const char *end;
+ time_t t;
+
+ while (*p) {
+ int len;
+
+ free(arg);
+ arg = NULL;
+
+ len = strlen(s);
+
+ if (p[0] != '%' || p[1] == '%') {
+ if (*p == '%')
+ p++;
+ s = realloc_size(s, len + 2);
+ s[len] = *p;
+ s[len + 1] = 0;
+ p++;
+ continue;
+ }
+ if (*++p == '{') {
+ const char *start = ++p;
+
+ while (*p && *p != '}') {
+ if (p[0] == '%' && p[1] == '}')
+ p++;
+ p++;
+ }
+ if (!*p) {
+ error("incomplete argument in \"%s\"", fmt);
+ return s;
+ }
+ arg = alloc_size(p - start + 1);
+ memcpy(arg, start, p - start);
+ arg[p - start] = 0;
+ p++;
+ }
+ if (*p == '!') {
+ p++;
+ if (user_time != -1) {
+ p++;
+ continue;
+ }
+ }
+ switch (*p++) {
+ case 't': /* local time */
+ time(&t);
+ break;
+ case 'i': /* parse sheet_date */
+ memset(&tm, 0, sizeof(tm));
+ end = strptime(sheet_date,
+ arg ? arg : KICAD_DATE_FORMAT, &tm);
+ if (end && !*end)
+ user_time = mktime(&tm);
+ else
+ user_time = -1;
+ continue;
+ case 'u': /* user time */
+ if (user_time == -1)
+ continue;
+ t = user_time;
+ break;
+ case 0:
+ default:
+ error("unrecognized date \"%c\"", p[-1]);
+ continue;
+ }
+
+ char buf[1000]; /* @@@ enough :) */
+ size_t size;
+
+ if (t == -1)
+ continue;
+ if (!localtime_r(&t, &tm))
+ continue;
+ size = strftime(buf, sizeof(buf),
+ arg ? arg : DEFAULT_DATE_FORMAT, &tm);
+ s = realloc_size(s, len + size + 1);
+ memcpy(s + len, buf, size);
+ s[len + size] = 0;
+ }
+
+ free(arg);
+
+ return s;
+}
+
+
/* ----- String expansion -------------------------------------------------- */
@@ -96,7 +200,11 @@ static char *expand(const struct pl_ctx *pl, const char *s,
}
break;
case 'D': // date
- cx = date_override ? date_override : sheet->date;
+ if (date_override)
+ x = format_date(date_override,
+ sheet->date ? sheet->date : "");
+ else
+ cx = sheet->date;
break;
case 'F': // file name
cx = sheet->file;