Introduce starts_with() and starts_with_ci()

Create user-defined functions to check whether a string contains a
certain prefix instead of messing around with strncmp() and
strncasecmp().

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2015-02-24 11:13:38 +01:00
parent f91208c2f4
commit 52779d2ec6
4 changed files with 72 additions and 123 deletions

View File

@ -1089,6 +1089,8 @@ void print_recur_event(const char *, long, struct recur_event *);
void print_todo(const char *, struct todo *); void print_todo(const char *, struct todo *);
int vasprintf(char **, const char *, va_list); int vasprintf(char **, const char *, va_list);
int asprintf(char **, const char *, ...); int asprintf(char **, const char *, ...);
int starts_with(const char *, const char *);
int starts_with_ci(const char *, const char *);
/* vars.c */ /* vars.c */
extern int col, row; extern int col, row;

View File

@ -455,12 +455,10 @@ static int
ical_chk_header(FILE * fd, char *buf, char *lstore, unsigned *lineno, ical_chk_header(FILE * fd, char *buf, char *lstore, unsigned *lineno,
int *major, int *minor) int *major, int *minor)
{ {
const char icalheader[] = "BEGIN:VCALENDAR";
if (!ical_readline(fd, buf, lstore, lineno)) if (!ical_readline(fd, buf, lstore, lineno))
return 0; return 0;
if (strncasecmp(buf, icalheader, sizeof(icalheader) - 1) != 0) if (!starts_with_ci(buf, "BEGIN:VCALENDAR"))
return 0; return 0;
while (!sscanf(buf, "VERSION:%d.%d", major, minor)) { while (!sscanf(buf, "VERSION:%d.%d", major, minor)) {
@ -693,10 +691,6 @@ static long ical_compute_rpt_until(long start, ical_rpt_t * rpt)
static ical_rpt_t *ical_read_rrule(FILE * log, char *rrulestr, static ical_rpt_t *ical_read_rrule(FILE * log, char *rrulestr,
unsigned *noskipped, const int itemline) unsigned *noskipped, const int itemline)
{ {
const char daily[] = "DAILY";
const char weekly[] = "WEEKLY";
const char monthly[] = "MONTHLY";
const char yearly[] = "YEARLY";
const char count[] = "COUNT="; const char count[] = "COUNT=";
const char interv[] = "INTERVAL="; const char interv[] = "INTERVAL=";
unsigned interval; unsigned interval;
@ -717,23 +711,13 @@ static ical_rpt_t *ical_read_rrule(FILE * log, char *rrulestr,
mem_free(rpt); mem_free(rpt);
return NULL; return NULL;
} else { } else {
if (strncmp(freqstr, daily, sizeof(daily) - 1) == if (starts_with(freqstr, "DAILY")) {
0) {
rpt->type = RECUR_DAILY; rpt->type = RECUR_DAILY;
} else } else if (starts_with(freqstr, "WEEKLY")) {
if (strncmp
(freqstr, weekly,
sizeof(weekly) - 1) == 0) {
rpt->type = RECUR_WEEKLY; rpt->type = RECUR_WEEKLY;
} else } else if (starts_with(freqstr, "MONTHLY")) {
if (strncmp
(freqstr, monthly,
sizeof(monthly) - 1) == 0) {
rpt->type = RECUR_MONTHLY; rpt->type = RECUR_MONTHLY;
} else } else if (starts_with(freqstr, "YEARLY")) {
if (strncmp
(freqstr, yearly,
sizeof(yearly) - 1) == 0) {
rpt->type = RECUR_YEARLY; rpt->type = RECUR_YEARLY;
} else { } else {
ical_log(log, ICAL_VEVENT, itemline, ical_log(log, ICAL_VEVENT, itemline,
@ -893,16 +877,6 @@ ical_read_event(FILE * fdi, FILE * log, unsigned *noevents,
char *lstore, unsigned *lineno) char *lstore, unsigned *lineno)
{ {
const int ITEMLINE = *lineno; const int ITEMLINE = *lineno;
const char endevent[] = "END:VEVENT";
const char summary[] = "SUMMARY";
const char dtstart[] = "DTSTART";
const char dtend[] = "DTEND";
const char duration[] = "DURATION";
const char rrule[] = "RRULE";
const char exdate[] = "EXDATE";
const char alarm[] = "BEGIN:VALARM";
const char endalarm[] = "END:VALARM";
const char desc[] = "DESCRIPTION";
ical_vevent_e vevent_type; ical_vevent_e vevent_type;
char *p; char *p;
struct { struct {
@ -920,14 +894,15 @@ ical_read_event(FILE * fdi, FILE * log, unsigned *noevents,
skip_alarm = 0; skip_alarm = 0;
while (ical_readline(fdi, buf, lstore, lineno)) { while (ical_readline(fdi, buf, lstore, lineno)) {
if (skip_alarm) { if (skip_alarm) {
/* Need to skip VALARM properties because some keywords could /*
interfere, such as DURATION, SUMMARY,.. */ * Need to skip VALARM properties because some keywords
if (strncasecmp * could interfere, such as DURATION, SUMMARY,..
(buf, endalarm, sizeof(endalarm) - 1) == 0) */
if (starts_with_ci(buf, "END:VALARM"))
skip_alarm = 0; skip_alarm = 0;
continue; continue;
} }
if (strncasecmp(buf, endevent, sizeof(endevent) - 1) == 0) { if (starts_with_ci(buf, "END:VEVENT")) {
if (vevent.mesg) { if (vevent.mesg) {
if (vevent.rpt && vevent.rpt->count) if (vevent.rpt && vevent.rpt->count)
vevent.rpt->until = vevent.rpt->until =
@ -1019,8 +994,7 @@ ical_read_event(FILE * fdi, FILE * log, unsigned *noevents,
} }
return; return;
} else { } else {
if (strncasecmp(buf, dtstart, sizeof(dtstart) - 1) if (starts_with_ci(buf, "DTSTART")) {
== 0) {
if ((p = strchr(buf, ':')) != NULL) if ((p = strchr(buf, ':')) != NULL)
vevent.start = vevent.start =
ical_datetime2long(++p, ical_datetime2long(++p,
@ -1031,9 +1005,7 @@ ical_read_event(FILE * fdi, FILE * log, unsigned *noevents,
_("could not retrieve event start time.")); _("could not retrieve event start time."));
goto cleanup; goto cleanup;
} }
} else } else if (starts_with_ci(buf, "DTEND")) {
if (strncasecmp(buf, dtend, sizeof(dtend) - 1)
== 0) {
if ((p = strchr(buf, ':')) != NULL) if ((p = strchr(buf, ':')) != NULL)
vevent.end = vevent.end =
ical_datetime2long(++p, ical_datetime2long(++p,
@ -1044,38 +1016,26 @@ ical_read_event(FILE * fdi, FILE * log, unsigned *noevents,
_("could not retrieve event end time.")); _("could not retrieve event end time."));
goto cleanup; goto cleanup;
} }
} else } else if (starts_with_ci(buf, "DURATION")) {
if (strncasecmp
(buf, duration,
sizeof(duration) - 1) == 0) {
if ((vevent.dur = ical_dur2long(buf)) <= 0) { if ((vevent.dur = ical_dur2long(buf)) <= 0) {
ical_log(log, ICAL_VEVENT, ical_log(log, ICAL_VEVENT,
ITEMLINE, ITEMLINE,
_("item duration malformed.")); _("item duration malformed."));
goto cleanup; goto cleanup;
} }
} else } else if (starts_with_ci(buf, "RRULE")) {
if (strncasecmp(buf, rrule, sizeof(rrule) - 1)
== 0) {
vevent.rpt = vevent.rpt =
ical_read_rrule(log, buf, noskipped, ical_read_rrule(log, buf, noskipped,
ITEMLINE); ITEMLINE);
} else } else if (starts_with_ci(buf, "EXDATE")) {
if (strncasecmp
(buf, exdate, sizeof(exdate) - 1) == 0) {
ical_read_exdate(&vevent.exc, log, buf, ical_read_exdate(&vevent.exc, log, buf,
noskipped, ITEMLINE); noskipped, ITEMLINE);
} else } else if (starts_with_ci(buf, "SUMMARY")) {
if (strncasecmp
(buf, summary, sizeof(summary) - 1) == 0) {
vevent.mesg = ical_read_summary(buf); vevent.mesg = ical_read_summary(buf);
} else } else if (starts_with_ci(buf, "BEGIN:VALARM")) {
if (strncasecmp(buf, alarm, sizeof(alarm) - 1)
== 0) {
skip_alarm = 1; skip_alarm = 1;
vevent.has_alarm = 1; vevent.has_alarm = 1;
} else if (strncasecmp(buf, desc, sizeof(desc) - 1) } else if (starts_with_ci(buf, "DESCRIPTION")) {
== 0) {
vevent.note = vevent.note =
ical_read_note(buf, noskipped, ical_read_note(buf, noskipped,
ICAL_VEVENT, ITEMLINE, ICAL_VEVENT, ITEMLINE,
@ -1104,11 +1064,6 @@ ical_read_todo(FILE * fdi, FILE * log, unsigned *notodos,
unsigned *noskipped, char *buf, char *lstore, unsigned *noskipped, char *buf, char *lstore,
unsigned *lineno) unsigned *lineno)
{ {
const char endtodo[] = "END:VTODO";
const char summary[] = "SUMMARY";
const char alarm[] = "BEGIN:VALARM";
const char endalarm[] = "END:VALARM";
const char desc[] = "DESCRIPTION";
const int LOWEST = 9; const int LOWEST = 9;
const int ITEMLINE = *lineno; const int ITEMLINE = *lineno;
struct { struct {
@ -1121,14 +1076,15 @@ ical_read_todo(FILE * fdi, FILE * log, unsigned *notodos,
skip_alarm = 0; skip_alarm = 0;
while (ical_readline(fdi, buf, lstore, lineno)) { while (ical_readline(fdi, buf, lstore, lineno)) {
if (skip_alarm) { if (skip_alarm) {
/* Need to skip VALARM properties because some keywords could /*
interfere, such as DURATION, SUMMARY,.. */ * Need to skip VALARM properties because some keywords
if (strncasecmp * could interfere, such as DURATION, SUMMARY,..
(buf, endalarm, sizeof(endalarm) - 1) == 0) */
if (starts_with_ci(buf, "END:VALARM"))
skip_alarm = 0; skip_alarm = 0;
continue; continue;
} }
if (strncasecmp(buf, endtodo, sizeof(endtodo) - 1) == 0) { if (starts_with_ci(buf, "END:VTODO")) {
if (!vtodo.has_priority) if (!vtodo.has_priority)
vtodo.priority = LOWEST; vtodo.priority = LOWEST;
if (vtodo.mesg) { if (vtodo.mesg) {
@ -1144,9 +1100,7 @@ ical_read_todo(FILE * fdi, FILE * log, unsigned *notodos,
} else { } else {
int tmpint; int tmpint;
if (strncasecmp if (starts_with_ci(buf, "PRIORITY:")) {
(buf, "PRIORITY:",
sizeof("PRIORITY:") - 1) == 0) {
sscanf(buf, "%d", &tmpint); sscanf(buf, "%d", &tmpint);
if (tmpint <= 9 && tmpint >= 1) { if (tmpint <= 9 && tmpint >= 1) {
vtodo.priority = tmpint; vtodo.priority = tmpint;
@ -1157,16 +1111,11 @@ ical_read_todo(FILE * fdi, FILE * log, unsigned *notodos,
"(must be between 1 and 9).")); "(must be between 1 and 9)."));
vtodo.priority = LOWEST; vtodo.priority = LOWEST;
} }
} else } else if (starts_with_ci(buf, "SUMMARY")) {
if (strncasecmp
(buf, summary, sizeof(summary) - 1) == 0) {
vtodo.mesg = ical_read_summary(buf); vtodo.mesg = ical_read_summary(buf);
} else } else if (starts_with_ci(buf, "BEGIN:VALARM")) {
if (strncasecmp(buf, alarm, sizeof(alarm) - 1)
== 0) {
skip_alarm = 1; skip_alarm = 1;
} else if (strncasecmp(buf, desc, sizeof(desc) - 1) } else if (starts_with_ci(buf, "DESCRIPTION")) {
== 0) {
vtodo.note = vtodo.note =
ical_read_note(buf, noskipped, ical_read_note(buf, noskipped,
ICAL_VTODO, ITEMLINE, ICAL_VTODO, ITEMLINE,
@ -1193,8 +1142,6 @@ ical_import_data(FILE * stream, FILE * log, unsigned *events,
unsigned *apoints, unsigned *todos, unsigned *lines, unsigned *apoints, unsigned *todos, unsigned *lines,
unsigned *skipped) unsigned *skipped)
{ {
const char vevent[] = "BEGIN:VEVENT";
const char vtodo[] = "BEGIN:VTODO";
char buf[BUFSIZ], lstore[BUFSIZ]; char buf[BUFSIZ], lstore[BUFSIZ];
int major, minor; int major, minor;
@ -1208,10 +1155,10 @@ ical_import_data(FILE * stream, FILE * log, unsigned *events,
while (ical_readline(stream, buf, lstore, lines)) { while (ical_readline(stream, buf, lstore, lines)) {
(*lines)++; (*lines)++;
if (strncasecmp(buf, vevent, sizeof(vevent) - 1) == 0) { if (starts_with_ci(buf, "BEGIN:VEVENT")) {
ical_read_event(stream, log, events, apoints, ical_read_event(stream, log, events, apoints,
skipped, buf, lstore, lines); skipped, buf, lstore, lines);
} else if (strncasecmp(buf, vtodo, sizeof(vtodo) - 1) == 0) { } else if (starts_with_ci(buf, "BEGIN:VTODO")) {
ical_read_todo(stream, log, todos, skipped, buf, ical_read_todo(stream, log, todos, skipped, buf,
lstore, lines); lstore, lines);
} }

View File

@ -275,48 +275,36 @@ void keys_remove_binding(int key, enum key action)
int keys_str2int(const char *key) int keys_str2int(const char *key)
{ {
const char CONTROL_KEY[] = "C-";
const char TAB_KEY[] = "TAB";
const char SPACE_KEY[] = "SPC";
const char ESCAPE_KEY[] = "ESC";
const char CURSES_KEY_UP[] = "UP";
const char CURSES_KEY_DOWN[] = "DWN";
const char CURSES_KEY_LEFT[] = "LFT";
const char CURSES_KEY_RIGHT[] = "RGT";
const char CURSES_KEY_HOME[] = "KEY_HOME";
const char CURSES_KEY_END[] = "KEY_END";
if (!key) if (!key)
return -1; return -1;
if (strlen(key) == 1) {
if (strlen(key) == 1)
return (int)key[0]; return (int)key[0];
} else {
if (key[0] == '^') if (key[0] == '^')
return CTRL((int)key[1]); return CTRL((int)key[1]);
else if (!strncmp else if (starts_with(key, "C-"))
(key, CONTROL_KEY, sizeof(CONTROL_KEY) - 1)) return CTRL((int)key[strlen("C-")]);
return CTRL((int)key[sizeof(CONTROL_KEY) - 1]); else if (!strcmp(key, "TAB"))
else if (!strcmp(key, TAB_KEY)) return TAB;
return TAB; else if (!strcmp(key, "ESC"))
else if (!strcmp(key, ESCAPE_KEY)) return ESCAPE;
return ESCAPE; else if (!strcmp(key, "SPC"))
else if (!strcmp(key, SPACE_KEY)) return SPACE;
return SPACE; else if (!strcmp(key, "UP"))
else if (!strcmp(key, CURSES_KEY_UP)) return KEY_UP;
return KEY_UP; else if (!strcmp(key, "DWN"))
else if (!strcmp(key, CURSES_KEY_DOWN)) return KEY_DOWN;
return KEY_DOWN; else if (!strcmp(key, "LFT"))
else if (!strcmp(key, CURSES_KEY_LEFT)) return KEY_LEFT;
return KEY_LEFT; else if (!strcmp(key, "RGT"))
else if (!strcmp(key, CURSES_KEY_RIGHT)) return KEY_RIGHT;
return KEY_RIGHT; else if (!strcmp(key, "KEY_HOME"))
else if (!strcmp(key, CURSES_KEY_HOME)) return KEY_HOME;
return KEY_HOME; else if (!strcmp(key, "KEY_END"))
else if (!strcmp(key, CURSES_KEY_END)) return KEY_END;
return KEY_END;
else return -1;
return -1;
}
} }
const char *keys_int2str(int key) const char *keys_int2str(int key)

View File

@ -1602,3 +1602,15 @@ asprintf(char **str, const char *format, ...)
return n; return n;
} }
int starts_with(const char *s, const char *p)
{
for (; *p && *p == *s; s++, p++);
return (*p == '\0');
}
int starts_with_ci(const char *s, const char *p)
{
for (; *p && tolower(*p) == tolower(*s); s++, p++);
return (*p == '\0');
}