aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukas Fleischer <calcurse@cryptocrack.de>2012-04-13 18:27:00 +0200
committerLukas Fleischer <calcurse@cryptocrack.de>2012-04-17 11:10:11 +0200
commitcd4f8bb68fbdd0983a2375202134c560878de3ac (patch)
tree5d09d0b9e2b7df88054fa4b5bdc68927d6f752ac
parent45f2b7628555bb8668649da4188174e95b76a737 (diff)
downloadcalcurse-cd4f8bb68fbdd0983a2375202134c560878de3ac.tar.gz
calcurse-cd4f8bb68fbdd0983a2375202134c560878de3ac.zip
src/utils.c: Break out format specifier parsing
Move the format specifier parser to a separate function. This is done in preparation for supporting long format specifiers. Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
-rw-r--r--src/utils.c96
1 files changed, 72 insertions, 24 deletions
diff --git a/src/utils.c b/src/utils.c
index 73c1eea..aa52977 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -49,6 +49,21 @@
#define ISLEAP(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
+enum format_specifier {
+ FS_STARTDATE,
+ FS_STARTDATESTR,
+ FS_DURATION,
+ FS_ENDDATE,
+ FS_ENDDATESTR,
+ FS_MESSAGE,
+ FS_NOTE,
+ FS_NOTEFILE,
+ FS_PRIORITY,
+ FS_PSIGN,
+ FS_EOF,
+ FS_UNKNOWN
+};
+
/* General routine to exit calcurse properly. */
void
exit_calcurse (int status)
@@ -1063,6 +1078,39 @@ print_escape (const char *s)
}
}
+/* Parse a format specifier. */
+static enum format_specifier
+parse_fs (const char *s)
+{
+ switch (*s)
+ {
+ case 's':
+ return FS_STARTDATE;
+ case 'S':
+ return FS_STARTDATESTR;
+ case 'd':
+ return FS_DURATION;
+ case 'e':
+ return FS_ENDDATE;
+ case 'E':
+ return FS_ENDDATESTR;
+ case 'm':
+ return FS_MESSAGE;
+ case 'n':
+ return FS_NOTE;
+ case 'N':
+ return FS_NOTEFILE;
+ case 'p':
+ return FS_PRIORITY;
+ case '%':
+ return FS_PSIGN;
+ case '\0':
+ return FS_EOF;
+ default:
+ return FS_UNKNOWN;
+ }
+}
+
/* Print a formatted appointment to stdout. */
void
print_apoint (const char *format, long day, struct apoint *apt)
@@ -1076,36 +1124,36 @@ print_apoint (const char *format, long day, struct apoint *apt)
{
if (*p == '%') {
p++;
- switch (*p)
+ switch (parse_fs (p))
{
- case 's':
+ case FS_STARTDATE:
printf ("%ld", apt->start);
break;
- case 'S':
+ case FS_STARTDATESTR:
printf ("%s", str_start);
break;
- case 'd':
+ case FS_DURATION:
printf ("%ld", apt->dur);
break;
- case 'e':
+ case FS_ENDDATE:
printf ("%ld", apt->start + apt->dur);
break;
- case 'E':
+ case FS_ENDDATESTR:
printf ("%s", str_end);
break;
- case 'm':
+ case FS_MESSAGE:
printf ("%s", apt->mesg);
break;
- case 'n':
+ case FS_NOTE:
printf ("%s", apt->note);
break;
- case 'N':
+ case FS_NOTEFILE:
print_notefile (stdout, apt->note, 1);
break;
- case '%':
+ case FS_PSIGN:
putchar ('%');
break;
- case '\0':
+ case FS_EOF:
return;
break;
default:
@@ -1130,21 +1178,21 @@ print_event (const char *format, long day, struct event *ev)
{
if (*p == '%') {
p++;
- switch (*p)
+ switch (parse_fs (p))
{
- case 'm':
+ case FS_MESSAGE:
printf ("%s", ev->mesg);
break;
- case 'n':
+ case FS_NOTE:
printf ("%s", ev->note);
break;
- case 'N':
+ case FS_NOTEFILE:
print_notefile (stdout, ev->note, 1);
break;
- case '%':
+ case FS_PSIGN:
putchar ('%');
break;
- case '\0':
+ case FS_EOF:
return;
break;
default:
@@ -1196,24 +1244,24 @@ print_todo (const char *format, struct todo *todo)
{
if (*p == '%') {
p++;
- switch (*p)
+ switch (parse_fs (p))
{
- case 'p':
+ case FS_PRIORITY:
printf ("%d", abs (todo->id));
break;
- case 'm':
+ case FS_MESSAGE:
printf ("%s", todo->mesg);
break;
- case 'n':
+ case FS_NOTE:
printf ("%s", todo->note);
break;
- case 'N':
+ case FS_NOTEFILE:
print_notefile (stdout, todo->note, 1);
break;
- case '%':
+ case FS_PSIGN:
putchar ('%');
break;
- case '\0':
+ case FS_EOF:
return;
break;
default: