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>
This commit is contained in:
Lukas Fleischer 2012-04-13 18:27:00 +02:00
parent 45f2b76285
commit cd4f8bb68f

View File

@ -49,6 +49,21 @@
#define ISLEAP(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) #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. */ /* General routine to exit calcurse properly. */
void void
exit_calcurse (int status) 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. */ /* Print a formatted appointment to stdout. */
void void
print_apoint (const char *format, long day, struct apoint *apt) 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 == '%') { if (*p == '%') {
p++; p++;
switch (*p) switch (parse_fs (p))
{ {
case 's': case FS_STARTDATE:
printf ("%ld", apt->start); printf ("%ld", apt->start);
break; break;
case 'S': case FS_STARTDATESTR:
printf ("%s", str_start); printf ("%s", str_start);
break; break;
case 'd': case FS_DURATION:
printf ("%ld", apt->dur); printf ("%ld", apt->dur);
break; break;
case 'e': case FS_ENDDATE:
printf ("%ld", apt->start + apt->dur); printf ("%ld", apt->start + apt->dur);
break; break;
case 'E': case FS_ENDDATESTR:
printf ("%s", str_end); printf ("%s", str_end);
break; break;
case 'm': case FS_MESSAGE:
printf ("%s", apt->mesg); printf ("%s", apt->mesg);
break; break;
case 'n': case FS_NOTE:
printf ("%s", apt->note); printf ("%s", apt->note);
break; break;
case 'N': case FS_NOTEFILE:
print_notefile (stdout, apt->note, 1); print_notefile (stdout, apt->note, 1);
break; break;
case '%': case FS_PSIGN:
putchar ('%'); putchar ('%');
break; break;
case '\0': case FS_EOF:
return; return;
break; break;
default: default:
@ -1130,21 +1178,21 @@ print_event (const char *format, long day, struct event *ev)
{ {
if (*p == '%') { if (*p == '%') {
p++; p++;
switch (*p) switch (parse_fs (p))
{ {
case 'm': case FS_MESSAGE:
printf ("%s", ev->mesg); printf ("%s", ev->mesg);
break; break;
case 'n': case FS_NOTE:
printf ("%s", ev->note); printf ("%s", ev->note);
break; break;
case 'N': case FS_NOTEFILE:
print_notefile (stdout, ev->note, 1); print_notefile (stdout, ev->note, 1);
break; break;
case '%': case FS_PSIGN:
putchar ('%'); putchar ('%');
break; break;
case '\0': case FS_EOF:
return; return;
break; break;
default: default:
@ -1196,24 +1244,24 @@ print_todo (const char *format, struct todo *todo)
{ {
if (*p == '%') { if (*p == '%') {
p++; p++;
switch (*p) switch (parse_fs (p))
{ {
case 'p': case FS_PRIORITY:
printf ("%d", abs (todo->id)); printf ("%d", abs (todo->id));
break; break;
case 'm': case FS_MESSAGE:
printf ("%s", todo->mesg); printf ("%s", todo->mesg);
break; break;
case 'n': case FS_NOTE:
printf ("%s", todo->note); printf ("%s", todo->note);
break; break;
case 'N': case FS_NOTEFILE:
print_notefile (stdout, todo->note, 1); print_notefile (stdout, todo->note, 1);
break; break;
case '%': case FS_PSIGN:
putchar ('%'); putchar ('%');
break; break;
case '\0': case FS_EOF:
return; return;
break; break;
default: default: