Implement {apoint,event,todo}_tostr()
Add functions to serialize non-recurrent objects without immediately writing them to stdout. Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
parent
ab54c861dc
commit
d118beceee
30
src/apoint.c
30
src/apoint.c
@ -136,30 +136,42 @@ void apoint_sec2str(struct apoint *o, long day, char *start, char *end)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void apoint_write(struct apoint *o, FILE * f)
|
char *apoint_tostr(struct apoint *o)
|
||||||
{
|
{
|
||||||
|
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, <);
|
localtime_r(&t, <);
|
||||||
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, <);
|
localtime_r(&t, <);
|
||||||
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);
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
void apoint_write(struct apoint *o, FILE * f)
|
||||||
|
{
|
||||||
|
char *str = apoint_tostr(o);
|
||||||
|
fprintf(f, "%s\n", str);
|
||||||
|
mem_free(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct apoint *apoint_scan(FILE * f, struct tm start, struct tm end,
|
struct apoint *apoint_scan(FILE * f, struct tm start, struct tm end,
|
||||||
|
@ -683,6 +683,7 @@ void apoint_llist_free(void);
|
|||||||
struct apoint *apoint_new(char *, char *, long, long, char);
|
struct apoint *apoint_new(char *, char *, long, long, char);
|
||||||
unsigned apoint_inday(struct apoint *, long *);
|
unsigned apoint_inday(struct apoint *, long *);
|
||||||
void apoint_sec2str(struct apoint *, long, char *, char *);
|
void apoint_sec2str(struct apoint *, long, char *, char *);
|
||||||
|
char *apoint_tostr(struct apoint *);
|
||||||
void apoint_write(struct apoint *, FILE *);
|
void apoint_write(struct apoint *, FILE *);
|
||||||
struct apoint *apoint_scan(FILE *, struct tm, struct tm, char, char *,
|
struct apoint *apoint_scan(FILE *, struct tm, struct tm, char, char *,
|
||||||
struct item_filter *);
|
struct item_filter *);
|
||||||
@ -776,6 +777,7 @@ void event_llist_init(void);
|
|||||||
void event_llist_free(void);
|
void event_llist_free(void);
|
||||||
struct event *event_new(char *, char *, long, int);
|
struct event *event_new(char *, char *, long, int);
|
||||||
unsigned event_inday(struct event *, long *);
|
unsigned event_inday(struct event *, long *);
|
||||||
|
char *event_tostr(struct event *);
|
||||||
void event_write(struct event *, FILE *);
|
void event_write(struct event *, FILE *);
|
||||||
struct event *event_scan(FILE *, struct tm, int, char *, struct item_filter *);
|
struct event *event_scan(FILE *, struct tm, int, char *, struct item_filter *);
|
||||||
void event_delete(struct event *);
|
void event_delete(struct event *);
|
||||||
@ -1002,6 +1004,7 @@ int string_printf(struct string *, const char *, ...);
|
|||||||
extern llist_t todolist;
|
extern llist_t todolist;
|
||||||
struct todo *todo_get_item(int);
|
struct todo *todo_get_item(int);
|
||||||
struct todo *todo_add(char *, int, char *);
|
struct todo *todo_add(char *, int, char *);
|
||||||
|
char *todo_tostr(struct todo *);
|
||||||
void todo_write(struct todo *, FILE *);
|
void todo_write(struct todo *, FILE *);
|
||||||
void todo_delete_note(struct todo *);
|
void todo_delete_note(struct todo *);
|
||||||
void todo_delete(struct todo *);
|
void todo_delete(struct todo *);
|
||||||
|
21
src/event.c
21
src/event.c
@ -104,19 +104,30 @@ unsigned event_inday(struct event *i, long *start)
|
|||||||
return (i->day < *start + DAYINSEC && i->day >= *start);
|
return (i->day < *start + DAYINSEC && i->day >= *start);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Write to file the event in user-friendly format */
|
char *event_tostr(struct event *o)
|
||||||
void event_write(struct event *o, FILE * f)
|
|
||||||
{
|
{
|
||||||
|
struct string s;
|
||||||
struct tm lt;
|
struct tm lt;
|
||||||
time_t t;
|
time_t t;
|
||||||
|
|
||||||
|
string_init(&s);
|
||||||
|
|
||||||
t = o->day;
|
t = o->day;
|
||||||
localtime_r(&t, <);
|
localtime_r(&t, <);
|
||||||
fprintf(f, "%02u/%02u/%04u [%d] ", lt.tm_mon + 1, lt.tm_mday,
|
string_catf(&s, "%02u/%02u/%04u [%d] ", lt.tm_mon + 1, lt.tm_mday,
|
||||||
1900 + lt.tm_year, o->id);
|
1900 + lt.tm_year, o->id);
|
||||||
if (o->note != NULL)
|
if (o->note != NULL)
|
||||||
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 event_write(struct event *o, FILE * f)
|
||||||
|
{
|
||||||
|
char *str = event_tostr(o);
|
||||||
|
fprintf(f, "%s\n", str);
|
||||||
|
mem_free(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Load the events from file */
|
/* Load the events from file */
|
||||||
|
20
src/todo.c
20
src/todo.c
@ -79,13 +79,23 @@ struct todo *todo_add(char *mesg, int id, char *note)
|
|||||||
return todo;
|
return todo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *todo_tostr(struct todo *todo)
|
||||||
|
{
|
||||||
|
char *res;
|
||||||
|
|
||||||
|
if (todo->note)
|
||||||
|
asprintf(&res, "[%d]>%s %s", todo->id, todo->note, todo->mesg);
|
||||||
|
else
|
||||||
|
asprintf(&res, "[%d] %s", todo->id, todo->mesg);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
void todo_write(struct todo *todo, FILE * f)
|
void todo_write(struct todo *todo, FILE * f)
|
||||||
{
|
{
|
||||||
if (todo->note)
|
char *str = todo_tostr(todo);
|
||||||
fprintf(f, "[%d]>%s %s\n", todo->id, todo->note,
|
fprintf(f, "%s\n", str);
|
||||||
todo->mesg);
|
mem_free(str);
|
||||||
else
|
|
||||||
fprintf(f, "[%d] %s\n", todo->id, todo->mesg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Delete a note previously attached to a todo item. */
|
/* Delete a note previously attached to a todo item. */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user