io.c: Several minor simplifications

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2012-11-25 17:44:00 +01:00
parent 4871ae8e65
commit 87fb8cfec0

View File

@ -151,7 +151,6 @@ static void progress_bar(progress_bar_t type, int progress)
static FILE *get_export_stream(enum export_type type) static FILE *get_export_stream(enum export_type type)
{ {
FILE *stream; FILE *stream;
int cancel;
char *home, *stream_name; char *home, *stream_name;
const char *question = _("Choose the file used to export calcurse data:"); const char *question = _("Choose the file used to export calcurse data:");
const char *wrong_name = const char *wrong_name =
@ -169,8 +168,7 @@ static FILE *get_export_stream(enum export_type type)
while (stream == NULL) { while (stream == NULL) {
status_mesg(question, ""); status_mesg(question, "");
cancel = updatestring(win[STA].p, &stream_name, 0, 1); if (updatestring(win[STA].p, &stream_name, 0, 1)) {
if (cancel) {
mem_free(stream_name); mem_free(stream_name);
return NULL; return NULL;
} }
@ -798,7 +796,7 @@ void io_load_keys(const char *pager)
void io_check_dir(char *dir, int *missing) void io_check_dir(char *dir, int *missing)
{ {
if (read_only) if (read_only)
return; return -1;
errno = 0; errno = 0;
if (mkdir(dir, 0700) != 0) { if (mkdir(dir, 0700) != 0) {
@ -806,10 +804,11 @@ void io_check_dir(char *dir, int *missing)
fprintf(stderr, _("FATAL ERROR: could not create %s: %s\n"), dir, fprintf(stderr, _("FATAL ERROR: could not create %s: %s\n"), dir,
strerror(errno)); strerror(errno));
exit_calcurse(EXIT_FAILURE); exit_calcurse(EXIT_FAILURE);
} else {
return 1;
} }
} else { } else {
if (missing) return 0;
(*missing)++;
} }
} }
@ -817,34 +816,34 @@ unsigned io_file_exist(char *file)
{ {
FILE *fd; FILE *fd;
if (!file) if (file && (fd = fopen(file, "r")) != NULL) {
return 0;
if ((fd = fopen(file, "r")) == NULL)
return 0;
fclose(fd); fclose(fd);
return 1; return 1;
} }
else {
return 0;
}
}
void io_check_file(char *file, int *missing) unsigned io_check_file(char *file)
{ {
if (read_only) if (read_only)
return; return -1;
errno = 0; errno = 0;
if (!io_file_exist(file)) { if (io_file_exist(file)) {
return 1;
} else {
FILE *fd; FILE *fd;
if (missing)
(*missing)++;
if ((fd = fopen(file, "w")) == NULL) { if ((fd = fopen(file, "w")) == NULL) {
fprintf(stderr, _("FATAL ERROR: could not create %s: %s\n"), file, fprintf(stderr, _("FATAL ERROR: could not create %s: %s\n"), file,
strerror(errno)); strerror(errno));
exit_calcurse(EXIT_FAILURE); exit_calcurse(EXIT_FAILURE);
} }
file_close(fd, __FILE_POS__); file_close(fd, __FILE_POS__);
return 0;
} }
} }
@ -862,17 +861,15 @@ void io_check_file(char *file, int *missing)
*/ */
int io_check_data_files(void) int io_check_data_files(void)
{ {
int missing, missing_keys; int missing = 0;
missing = missing_keys = 0; missing += io_check_dir(path_dir) ? 0 : 1;
errno = 0; missing += io_check_dir(path_notes) ? 0 : 1;
io_check_dir(path_dir, &missing); missing += io_check_file(path_todo) ? 0 : 1;
io_check_dir(path_notes, &missing); missing += io_check_file(path_apts) ? 0 : 1;
io_check_file(path_todo, &missing); missing += io_check_file(path_conf) ? 0 : 1;
io_check_file(path_apts, &missing);
io_check_file(path_conf, &missing); if (!io_check_file(path_keys)) {
io_check_file(path_keys, &missing_keys);
if (missing_keys) {
missing++; missing++;
keys_dump_defaults(path_keys); keys_dump_defaults(path_keys);
} }
@ -897,14 +894,13 @@ void io_startup_screen(int no_data_file)
/* Export calcurse data. */ /* Export calcurse data. */
void io_export_data(enum export_type type) void io_export_data(enum export_type type)
{ {
FILE *stream; FILE *stream = NULL;
const char *success = _("The data were successfully exported"); const char *success = _("The data were successfully exported");
const char *enter = _("Press [ENTER] to continue"); const char *enter = _("Press [ENTER] to continue");
if (type < IO_EXPORT_ICAL || type >= IO_EXPORT_NBTYPES) if (type < IO_EXPORT_ICAL || type >= IO_EXPORT_NBTYPES)
EXIT(_("unknown export type")); EXIT(_("unknown export type"));
stream = 0;
switch (ui_mode) { switch (ui_mode) {
case UI_CMDLINE: case UI_CMDLINE:
stream = stdout; stream = stdout;
@ -933,21 +929,18 @@ void io_export_data(enum export_type type)
static FILE *get_import_stream(enum export_type type) static FILE *get_import_stream(enum export_type type)
{ {
FILE *stream; FILE *stream = NULL;
char *stream_name; char *stream_name;
const char *ask_fname = _("Enter the file name to import data from:"); const char *ask_fname = _("Enter the file name to import data from:");
const char *wrong_file = const char *wrong_file =
_("The file cannot be accessed, please enter another file name."); _("The file cannot be accessed, please enter another file name.");
const char *press_enter = _("Press [ENTER] to continue."); const char *press_enter = _("Press [ENTER] to continue.");
int cancel;
stream = NULL;
stream_name = mem_malloc(BUFSIZ); stream_name = mem_malloc(BUFSIZ);
memset(stream_name, 0, BUFSIZ); memset(stream_name, 0, BUFSIZ);
while (stream == NULL) { while (stream == NULL) {
status_mesg(ask_fname, ""); status_mesg(ask_fname, "");
cancel = updatestring(win[STA].p, &stream_name, 0, 1); if (updatestring(win[STA].p, &stream_name, 0, 1)) {
if (cancel) {
mem_free(stream_name); mem_free(stream_name);
return NULL; return NULL;
} }
@ -1085,13 +1078,10 @@ void io_log_print(struct io_file *log, int line, const char *msg)
void io_log_display(struct io_file *log, const char *msg, const char *pager) void io_log_display(struct io_file *log, const char *msg, const char *pager)
{ {
int ans;
RETURN_IF(log == NULL, _("No log file to display!")); RETURN_IF(log == NULL, _("No log file to display!"));
if (ui_mode == UI_CMDLINE) { if (ui_mode == UI_CMDLINE) {
printf("\n%s [y/n] ", msg); printf("\n%s [y/n] ", msg);
ans = fgetc(stdin); if (fgetc(stdin) == 'y') {
if (ans == 'y') {
const char *arg[] = { pager, log->name, NULL }; const char *arg[] = { pager, log->name, NULL };
int pid; int pid;
@ -1120,9 +1110,7 @@ static pthread_t io_t_psave;
/* Thread used to periodically save data. */ /* Thread used to periodically save data. */
static void *io_psave_thread(void *arg) static void *io_psave_thread(void *arg)
{ {
int delay; int delay = conf.periodic_save;
delay = conf.periodic_save;
EXIT_IF(delay < 0, _("Invalid delay")); EXIT_IF(delay < 0, _("Invalid delay"));
for (;;) { for (;;) {