Use strncasecmp() for case-insensitive comparison

Instead of converting everything to upper case and then using strncmp(),
use strncasecmp() which does case-insensitive comparison out of the box.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2012-12-22 17:25:00 +01:00
parent ef961f4927
commit de73d96188
3 changed files with 22 additions and 39 deletions

View File

@ -977,7 +977,6 @@ char *new_tempfile(const char *, int);
int parse_date(const char *, enum datefmt, int *, int *, int *, struct date *); int parse_date(const char *, enum datefmt, int *, int *, int *, struct date *);
int parse_time(const char *, unsigned *, unsigned *); int parse_time(const char *, unsigned *, unsigned *);
int parse_duration(const char *, unsigned *); int parse_duration(const char *, unsigned *);
void str_toupper(char *);
void file_close(FILE *, const char *); void file_close(FILE *, const char *);
void psleep(unsigned); void psleep(unsigned);
int fork_exec(int *, int *, const char *, const char *const *); int fork_exec(int *, int *, const char *, const char *const *);

View File

@ -34,6 +34,7 @@
* *
*/ */
#include <strings.h>
#include <sys/types.h> #include <sys/types.h>
#include "calcurse.h" #include "calcurse.h"
@ -428,8 +429,7 @@ ical_chk_header(FILE * fd, char *buf, char *lstore, unsigned *lineno,
if (!ical_readline(fd, buf, lstore, lineno)) if (!ical_readline(fd, buf, lstore, lineno))
return 0; return 0;
str_toupper(buf); if (strncasecmp(buf, icalheader, sizeof(icalheader) - 1) != 0)
if (strncmp(buf, icalheader, sizeof(icalheader) - 1) != 0)
return 0; return 0;
while (!sscanf(buf, "VERSION:%d.%d", major, minor)) { while (!sscanf(buf, "VERSION:%d.%d", major, minor)) {
@ -830,7 +830,7 @@ ical_read_event(FILE * fdi, FILE * log, unsigned *noevents, unsigned *noapoints,
const char endalarm[] = "END:VALARM"; const char endalarm[] = "END:VALARM";
const char desc[] = "DESCRIPTION"; const char desc[] = "DESCRIPTION";
ical_vevent_e vevent_type; ical_vevent_e vevent_type;
char *p, buf_upper[BUFSIZ]; char *p;
struct { struct {
llist_t exc; llist_t exc;
ical_rpt_t *rpt; ical_rpt_t *rpt;
@ -844,18 +844,14 @@ ical_read_event(FILE * fdi, FILE * log, unsigned *noevents, unsigned *noapoints,
memset(&vevent, 0, sizeof vevent); memset(&vevent, 0, sizeof vevent);
skip_alarm = 0; skip_alarm = 0;
while (ical_readline(fdi, buf, lstore, lineno)) { while (ical_readline(fdi, buf, lstore, lineno)) {
strncpy(buf_upper, buf, BUFSIZ);
buf_upper[BUFSIZ - 1] = '\0';
str_toupper(buf_upper);
if (skip_alarm) { if (skip_alarm) {
/* Need to skip VALARM properties because some keywords could /* Need to skip VALARM properties because some keywords could
interfere, such as DURATION, SUMMARY,.. */ interfere, such as DURATION, SUMMARY,.. */
if (strncmp(buf_upper, endalarm, sizeof(endalarm) - 1) == 0) if (strncasecmp(buf, endalarm, sizeof(endalarm) - 1) == 0)
skip_alarm = 0; skip_alarm = 0;
continue; continue;
} }
if (strncmp(buf_upper, endevent, sizeof(endevent) - 1) == 0) { if (strncasecmp(buf, endevent, sizeof(endevent) - 1) == 0) {
if (vevent.mesg) { if (vevent.mesg) {
if (vevent.rpt && vevent.rpt->count) if (vevent.rpt && vevent.rpt->count)
vevent.rpt->until = ical_compute_rpt_until(vevent.start, vevent.rpt); vevent.rpt->until = ical_compute_rpt_until(vevent.start, vevent.rpt);
@ -917,7 +913,7 @@ ical_read_event(FILE * fdi, FILE * log, unsigned *noevents, unsigned *noapoints,
} }
return; return;
} else { } else {
if (strncmp(buf_upper, dtstart, sizeof(dtstart) - 1) == 0) { if (strncasecmp(buf, dtstart, sizeof(dtstart) - 1) == 0) {
if ((p = strchr(buf, ':')) != NULL) if ((p = strchr(buf, ':')) != NULL)
vevent.start = ical_datetime2long(++p, &vevent_type); vevent.start = ical_datetime2long(++p, &vevent_type);
if (!vevent.start) { if (!vevent.start) {
@ -925,7 +921,7 @@ ical_read_event(FILE * fdi, FILE * log, unsigned *noevents, unsigned *noapoints,
_("could not retrieve event start time.")); _("could not retrieve event start time."));
goto cleanup; goto cleanup;
} }
} else if (strncmp(buf_upper, dtend, sizeof(dtend) - 1) == 0) { } else if (strncasecmp(buf, dtend, sizeof(dtend) - 1) == 0) {
if ((p = strchr(buf, ':')) != NULL) if ((p = strchr(buf, ':')) != NULL)
vevent.end = ical_datetime2long(++p, &vevent_type); vevent.end = ical_datetime2long(++p, &vevent_type);
if (!vevent.end) { if (!vevent.end) {
@ -933,21 +929,21 @@ ical_read_event(FILE * fdi, FILE * log, unsigned *noevents, unsigned *noapoints,
_("could not retrieve event end time.")); _("could not retrieve event end time."));
goto cleanup; goto cleanup;
} }
} else if (strncmp(buf_upper, duration, sizeof(duration) - 1) == 0) { } else 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, ITEMLINE, _("item duration malformed.")); ical_log(log, ICAL_VEVENT, ITEMLINE, _("item duration malformed."));
goto cleanup; goto cleanup;
} }
} else if (strncmp(buf_upper, rrule, sizeof(rrule) - 1) == 0) { } else if (strncasecmp(buf, rrule, sizeof(rrule) - 1) == 0) {
vevent.rpt = ical_read_rrule(log, buf, noskipped, ITEMLINE); vevent.rpt = ical_read_rrule(log, buf, noskipped, ITEMLINE);
} else if (strncmp(buf_upper, exdate, sizeof(exdate) - 1) == 0) { } else if (strncasecmp(buf, exdate, sizeof(exdate) - 1) == 0) {
ical_read_exdate(&vevent.exc, log, buf, noskipped, ITEMLINE); ical_read_exdate(&vevent.exc, log, buf, noskipped, ITEMLINE);
} else if (strncmp(buf_upper, summary, sizeof(summary) - 1) == 0) { } else if (strncasecmp(buf, summary, sizeof(summary) - 1) == 0) {
vevent.mesg = ical_read_summary(buf); vevent.mesg = ical_read_summary(buf);
} else if (strncmp(buf_upper, alarm, sizeof(alarm) - 1) == 0) { } else if (strncasecmp(buf, alarm, sizeof(alarm) - 1) == 0) {
skip_alarm = 1; skip_alarm = 1;
vevent.has_alarm = 1; vevent.has_alarm = 1;
} else if (strncmp(buf_upper, desc, sizeof(desc) - 1) == 0) { } else if (strncasecmp(buf, desc, sizeof(desc) - 1) == 0) {
vevent.note = ical_read_note(buf, noskipped, ICAL_VEVENT, vevent.note = ical_read_note(buf, noskipped, ICAL_VEVENT,
ITEMLINE, log); ITEMLINE, log);
} }
@ -980,7 +976,6 @@ ical_read_todo(FILE * fdi, FILE * log, unsigned *notodos, unsigned *noskipped,
const char desc[] = "DESCRIPTION"; const char desc[] = "DESCRIPTION";
const int LOWEST = 9; const int LOWEST = 9;
const int ITEMLINE = *lineno; const int ITEMLINE = *lineno;
char buf_upper[BUFSIZ];
struct { struct {
char *mesg, *note; char *mesg, *note;
int has_priority, priority; int has_priority, priority;
@ -990,17 +985,14 @@ ical_read_todo(FILE * fdi, FILE * log, unsigned *notodos, unsigned *noskipped,
memset(&vtodo, 0, sizeof vtodo); memset(&vtodo, 0, sizeof vtodo);
skip_alarm = 0; skip_alarm = 0;
while (ical_readline(fdi, buf, lstore, lineno)) { while (ical_readline(fdi, buf, lstore, lineno)) {
strncpy(buf_upper, buf, BUFSIZ);
buf_upper[BUFSIZ - 1] = '\0';
str_toupper(buf_upper);
if (skip_alarm) { if (skip_alarm) {
/* Need to skip VALARM properties because some keywords could /* Need to skip VALARM properties because some keywords could
interfere, such as DURATION, SUMMARY,.. */ interfere, such as DURATION, SUMMARY,.. */
if (strncmp(buf_upper, endalarm, sizeof(endalarm) - 1) == 0) if (strncasecmp(buf, endalarm, sizeof(endalarm) - 1) == 0)
skip_alarm = 0; skip_alarm = 0;
continue; continue;
} }
if (strncmp(buf_upper, endtodo, sizeof(endtodo) - 1) == 0) { if (strncasecmp(buf, endtodo, sizeof(endtodo) - 1) == 0) {
if (!vtodo.has_priority) if (!vtodo.has_priority)
vtodo.priority = LOWEST; vtodo.priority = LOWEST;
if (vtodo.mesg) { if (vtodo.mesg) {
@ -1015,7 +1007,8 @@ ical_read_todo(FILE * fdi, FILE * log, unsigned *notodos, unsigned *noskipped,
} else { } else {
int tmpint; int tmpint;
if (sscanf(buf_upper, "PRIORITY:%d", &tmpint) == 1) { if (strncasecmp(buf, "PRIORITY:", sizeof("PRIORITY:") - 1) == 0) {
sscanf(buf, "%d", &tmpint);
if (tmpint <= 9 && tmpint >= 1) { if (tmpint <= 9 && tmpint >= 1) {
vtodo.priority = tmpint; vtodo.priority = tmpint;
vtodo.has_priority = 1; vtodo.has_priority = 1;
@ -1025,11 +1018,11 @@ ical_read_todo(FILE * fdi, FILE * log, unsigned *notodos, unsigned *noskipped,
"(must be between 1 and 9).")); "(must be between 1 and 9)."));
vtodo.priority = LOWEST; vtodo.priority = LOWEST;
} }
} else if (strncmp(buf_upper, summary, sizeof(summary) - 1) == 0) { } else if (strncasecmp(buf, summary, sizeof(summary) - 1) == 0) {
vtodo.mesg = ical_read_summary(buf); vtodo.mesg = ical_read_summary(buf);
} else if (strncmp(buf_upper, alarm, sizeof(alarm) - 1) == 0) { } else if (strncasecmp(buf, alarm, sizeof(alarm) - 1) == 0) {
skip_alarm = 1; skip_alarm = 1;
} else if (strncmp(buf_upper, desc, sizeof(desc) - 1) == 0) { } else if (strncasecmp(buf, desc, sizeof(desc) - 1) == 0) {
vtodo.note = ical_read_note(buf, noskipped, ICAL_VTODO, ITEMLINE, log); vtodo.note = ical_read_note(buf, noskipped, ICAL_VTODO, ITEMLINE, log);
} }
} }
@ -1066,11 +1059,10 @@ ical_import_data(FILE * stream, FILE * log, unsigned *events, unsigned *apoints,
while (ical_readline(stream, buf, lstore, lines)) { while (ical_readline(stream, buf, lstore, lines)) {
(*lines)++; (*lines)++;
str_toupper(buf); if (strncasecmp(buf, vevent, sizeof(vevent) - 1) == 0) {
if (strncmp(buf, vevent, sizeof(vevent) - 1) == 0) {
ical_read_event(stream, log, events, apoints, skipped, buf, lstore, ical_read_event(stream, log, events, apoints, skipped, buf, lstore,
lines); lines);
} else if (strncmp(buf, vtodo, sizeof(vtodo) - 1) == 0) { } else if (strncasecmp(buf, vtodo, sizeof(vtodo) - 1) == 0) {
ical_read_todo(stream, log, todos, skipped, buf, lstore, lines); ical_read_todo(stream, log, todos, skipped, buf, lstore, lines);
} }
} }

View File

@ -845,14 +845,6 @@ int parse_duration(const char *string, unsigned *duration)
return 1; return 1;
} }
void str_toupper(char *s)
{
if (!s)
return;
for (; *s; s++)
*s = toupper(*s);
}
void file_close(FILE * f, const char *pos) void file_close(FILE * f, const char *pos)
{ {
EXIT_IF((fclose(f)) != 0, _("Error when closing file at %s"), pos); EXIT_IF((fclose(f)) != 0, _("Error when closing file at %s"), pos);