Implement recur_{apoint,event}_tostr()

Add functions to serialize recurrent items without immediately writing
them to stdout.

Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Lukas Fleischer 2016-01-11 20:12:00 +01:00
parent d118beceee
commit ae49ef1974
2 changed files with 53 additions and 27 deletions

View File

@ -964,7 +964,9 @@ struct recur_apoint *recur_apoint_scan(FILE *, struct tm, struct tm,
struct recur_event *recur_event_scan(FILE *, struct tm, int, char, struct recur_event *recur_event_scan(FILE *, struct tm, int, char,
int, struct tm, char *, llist_t *, int, struct tm, char *, llist_t *,
struct item_filter *); struct item_filter *);
char *recur_apoint_tostr(struct recur_apoint *);
void recur_apoint_write(struct recur_apoint *, FILE *); void recur_apoint_write(struct recur_apoint *, FILE *);
char *recur_event_tostr(struct recur_event *);
void recur_event_write(struct recur_event *, FILE *); void recur_event_write(struct recur_event *, FILE *);
void recur_save_data(FILE *); void recur_save_data(FILE *);
unsigned recur_item_find_occurrence(long, long, llist_t *, int, unsigned recur_item_find_occurrence(long, long, llist_t *, int,

View File

@ -308,7 +308,7 @@ int recur_char2def(char type)
} }
/* Write days for which recurrent items should not be repeated. */ /* Write days for which recurrent items should not be repeated. */
static void recur_write_exc(llist_t * lexc, FILE * f) static void recur_exc_append(struct string *s, llist_t *lexc)
{ {
llist_item_t *i; llist_item_t *i;
struct tm lt; struct tm lt;
@ -322,7 +322,7 @@ static void recur_write_exc(llist_t * lexc, FILE * f)
st_mon = lt.tm_mon + 1; st_mon = lt.tm_mon + 1;
st_day = lt.tm_mday; st_day = lt.tm_mday;
st_year = lt.tm_year + 1900; st_year = lt.tm_year + 1900;
fprintf(f, " !%02u/%02u/%04u", st_mon, st_day, st_year); string_catf(s, " !%02u/%02u/%04u", st_mon, st_day, st_year);
} }
} }
@ -456,59 +456,74 @@ struct recur_event *recur_event_scan(FILE * f, struct tm start, int id,
freq, tuntil, exc); freq, tuntil, exc);
} }
/* Writting of a recursive appointment into file. */ char *recur_apoint_tostr(struct recur_apoint *o)
void recur_apoint_write(struct recur_apoint *o, FILE * f)
{ {
struct string s;
struct tm lt; struct tm lt;
time_t t; time_t t;
string_init(&s);
t = o->start; t = o->start;
localtime_r(&t, &lt); localtime_r(&t, &lt);
fprintf(f, "%02u/%02u/%04u @ %02u:%02u", lt.tm_mon + 1, lt.tm_mday, string_catf(&s, "%02u/%02u/%04u @ %02u:%02u", lt.tm_mon + 1,
1900 + lt.tm_year, lt.tm_hour, lt.tm_min); lt.tm_mday, 1900 + lt.tm_year, lt.tm_hour, lt.tm_min);
t = o->start + o->dur; t = o->start + o->dur;
localtime_r(&t, &lt); localtime_r(&t, &lt);
fprintf(f, " -> %02u/%02u/%04u @ %02u:%02u", lt.tm_mon + 1, string_catf(&s, " -> %02u/%02u/%04u @ %02u:%02u", lt.tm_mon + 1,
lt.tm_mday, 1900 + lt.tm_year, lt.tm_hour, lt.tm_min); lt.tm_mday, 1900 + lt.tm_year, lt.tm_hour, lt.tm_min);
t = o->rpt->until; t = o->rpt->until;
if (t == 0) { /* We have an endless recurrent appointment. */ if (t == 0) {
fprintf(f, " {%d%c", o->rpt->freq, /* We have an endless recurrent appointment. */
string_catf(&s, " {%d%c", o->rpt->freq,
recur_def2char(o->rpt->type)); recur_def2char(o->rpt->type));
} else { } else {
localtime_r(&t, &lt); localtime_r(&t, &lt);
fprintf(f, " {%d%c -> %02u/%02u/%04u", o->rpt->freq, string_catf(&s, " {%d%c -> %02u/%02u/%04u", o->rpt->freq,
recur_def2char(o->rpt->type), lt.tm_mon + 1, recur_def2char(o->rpt->type), lt.tm_mon + 1,
lt.tm_mday, 1900 + lt.tm_year); lt.tm_mday, 1900 + lt.tm_year);
} }
recur_write_exc(&o->exc, f); recur_exc_append(&s, &o->exc);
fputs("} ", f); string_catf(&s, "} ");
if (o->note != NULL) if (o->note)
fprintf(f, ">%s ", o->note); string_catf(&s, ">%s ", o->note);
if (o->state & APOINT_NOTIFY) if (o->state & APOINT_NOTIFY)
fputc('!', f); string_catf(&s, "%c", '!');
else else
fputc('|', f); string_catf(&s, "%c", '|');
fprintf(f, "%s\n", o->mesg); string_catf(&s, "%s", o->mesg);
return string_buf(&s);
} }
/* Writting of a recursive event into file. */ void recur_apoint_write(struct recur_apoint *o, FILE * f)
void recur_event_write(struct recur_event *o, FILE * f)
{ {
char *str = recur_apoint_tostr(o);
fprintf(f, "%s\n", str);
mem_free(str);
}
char *recur_event_tostr(struct recur_event *o)
{
struct string s;
struct tm lt; struct tm lt;
time_t t; time_t t;
int st_mon, st_day, st_year; int st_mon, st_day, st_year;
int end_mon, end_day, end_year; int end_mon, end_day, end_year;
string_init(&s);
t = o->day; t = o->day;
localtime_r(&t, &lt); localtime_r(&t, &lt);
st_mon = lt.tm_mon + 1; st_mon = lt.tm_mon + 1;
st_day = lt.tm_mday; st_day = lt.tm_mday;
st_year = lt.tm_year + 1900; st_year = lt.tm_year + 1900;
t = o->rpt->until; t = o->rpt->until;
if (t == 0) { /* We have an endless recurrent event. */ if (t == 0) {
fprintf(f, "%02u/%02u/%04u [%d] {%d%c", st_mon, st_day, /* We have an endless recurrent event. */
string_catf(&s, "%02u/%02u/%04u [%d] {%d%c", st_mon, st_day,
st_year, o->id, o->rpt->freq, st_year, o->id, o->rpt->freq,
recur_def2char(o->rpt->type)); recur_def2char(o->rpt->type));
} else { } else {
@ -516,16 +531,25 @@ void recur_event_write(struct recur_event *o, FILE * f)
end_mon = lt.tm_mon + 1; end_mon = lt.tm_mon + 1;
end_day = lt.tm_mday; end_day = lt.tm_mday;
end_year = lt.tm_year + 1900; end_year = lt.tm_year + 1900;
fprintf(f, "%02u/%02u/%04u [%d] {%d%c -> %02u/%02u/%04u", string_catf(&s, "%02u/%02u/%04u [%d] {%d%c -> %02u/%02u/%04u",
st_mon, st_day, st_year, o->id, o->rpt->freq, st_mon, st_day, st_year, o->id, o->rpt->freq,
recur_def2char(o->rpt->type), end_mon, end_day, recur_def2char(o->rpt->type), end_mon, end_day,
end_year); end_year);
} }
recur_write_exc(&o->exc, f); recur_exc_append(&s, &o->exc);
fputs("} ", f); string_catf(&s, "} ");
if (o->note != NULL) if (o->note)
fprintf(f, ">%s ", o->note); string_catf(&s, ">%s ", o->note);
fprintf(f, "%s\n", o->mesg); string_catf(&s, "%s", o->mesg);
return string_buf(&s);
}
void recur_event_write(struct recur_event *o, FILE * f)
{
char *str = recur_event_tostr(o);
fprintf(f, "%s\n", str);
mem_free(str);
} }
/* Write recursive items to file. */ /* Write recursive items to file. */