doc: Add 1.9.18 release notes
[fw/altos] / ao-tools / ao-view / aoview_util.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
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, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "aoview.h"
20
21 char *
22 aoview_fullname (char *dir, char *file)
23 {
24         char    *new;
25         int     dlen = strlen (dir);
26         int     flen = strlen (file);
27         int     slen = 0;
28
29         if (dir[dlen-1] != '/')
30                 slen = 1;
31         new = malloc (dlen + slen + flen + 1);
32         if (!new)
33                 return 0;
34         strcpy(new, dir);
35         if (slen)
36                 strcat (new, "/");
37         strcat(new, file);
38         return new;
39 }
40
41 char *
42 aoview_basename(char *file)
43 {
44         char *b;
45
46         b = strrchr(file, '/');
47         if (!b)
48                 return file;
49         return b + 1;
50 }
51
52 int
53 aoview_mkdir(char *dir)
54 {
55         char    *slash;
56         char    *d;
57         char    *part;
58
59         d = dir;
60         for (;;) {
61                 slash = strchr (d, '/');
62                 if (!slash)
63                         slash = d + strlen(d);
64                 if (!*slash)
65                         break;
66                 part = strndup(dir, slash - dir);
67                 if (!access(part, F_OK))
68                         if (mkdir(part, 0777) < 0)
69                                 return -errno;
70                 free(part);
71                 d = slash + 1;
72         }
73         return 0;
74 }
75
76 GtkTreeViewColumn *
77 aoview_add_plain_text_column (GtkTreeView *view, const gchar *title, gint model_column, gint width)
78 {
79         GtkCellRenderer *renderer;
80         GtkTreeViewColumn *column;
81
82         renderer = gtk_cell_renderer_text_new ();
83         g_object_set(renderer, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
84         g_object_set(renderer, "width-chars", width, NULL);
85         column = gtk_tree_view_column_new_with_attributes (title, renderer,
86                                                            "text", model_column,
87                                                            NULL);
88         gtk_tree_view_column_set_resizable (column, FALSE);
89         gtk_tree_view_append_column (view, column);
90
91         return column;
92 }