1 /* vifm
2  * Copyright (C) 2015 xaizek.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #include "ioe.h"
20 
21 #include <stddef.h> /* NULL size_t */
22 #include <stdlib.h> /* free() */
23 #include <string.h> /* strerror() */
24 
25 #include "../engine/text_buffer.h"
26 #include "../utils/path.h"
27 #include "private/ioe.h"
28 
29 void
ioe_errlst_init(ioe_errlst_t * elist)30 ioe_errlst_init(ioe_errlst_t *elist)
31 {
32 	static const ioe_errlst_t initializer = IOE_ERRLST_INIT;
33 	*elist = initializer;
34 }
35 
36 void
ioe_errlst_free(ioe_errlst_t * elist)37 ioe_errlst_free(ioe_errlst_t *elist)
38 {
39 	size_t i;
40 
41 	if(elist == NULL)
42 	{
43 		return;
44 	}
45 
46 	for(i = 0U; i < elist->error_count; ++i)
47 	{
48 		ioe_err_free(&elist->errors[i]);
49 	}
50 	free(elist->errors);
51 }
52 
53 char *
ioe_errlst_to_str(const ioe_errlst_t * elist)54 ioe_errlst_to_str(const ioe_errlst_t *elist)
55 {
56 	size_t i;
57 
58 	vle_textbuf *str = vle_tb_create();
59 	if(str == NULL)
60 	{
61 		return NULL;
62 	}
63 
64 	for(i = 0U; i < elist->error_count; ++i)
65 	{
66 		const ioe_err_t *const err = &elist->errors[i];
67 		const char *const path = replace_home_part(err->path);
68 		if(err->error_code == IO_ERR_UNKNOWN)
69 		{
70 			vle_tb_append_linef(str, "%s: %s", path, err->msg);
71 		}
72 		else if(err->msg[0] == '\0')
73 		{
74 			vle_tb_append_linef(str, "%s: %s", path, strerror(err->error_code));
75 		}
76 		else
77 		{
78 			vle_tb_append_linef(str, "%s: %s (%s)", path, err->msg,
79 					strerror(err->error_code));
80 		}
81 	}
82 
83 	return vle_tb_release(str);
84 }
85 
86 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
87 /* vim: set cinoptions+=t0 filetype=c : */
88