]> git.sur5r.net Git - glabels/blob - src/file-util.c
Imported Upstream version 3.0.0
[glabels] / src / file-util.c
1 /*
2  *  file-util.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "file-util.h"
24
25 #include <string.h>
26
27
28 /****************************************************************************/
29 /* Append ".glabels" extension to filename if needed.                       */
30 /****************************************************************************/
31 gchar *
32 gl_file_util_add_extension (const gchar *orig_filename)
33 {
34         gchar *new_filename, *extension;
35
36         extension = strrchr (orig_filename, '.');
37         if (extension == NULL) {
38                 new_filename = g_strconcat (orig_filename, ".glabels", NULL);
39         } else {
40                 if (g_ascii_strcasecmp (extension, ".glabels") != 0) {
41                         new_filename =
42                             g_strconcat (orig_filename, ".glabels", NULL);
43                 } else {
44                         new_filename = g_strdup (orig_filename);
45                 }
46         }
47
48         return new_filename;
49 }
50
51
52 /****************************************************************************/
53 /* Remove ".glabels" extension from filename if needed.                     */
54 /****************************************************************************/
55 gchar *
56 gl_file_util_remove_extension (const gchar *orig_filename)
57 {
58         gchar *new_filename, *extension;
59
60         new_filename = g_strdup (orig_filename);
61
62         extension = strrchr (new_filename, '.');
63         if (extension != NULL) {
64                 if (g_ascii_strcasecmp (extension, ".glabels") == 0) {
65                         *extension = 0; /* truncate string, rm extension */
66                 }
67         }
68
69         return new_filename;
70 }
71
72
73 /****************************************************************************/
74 /* Make sure we have an absolute path to filename.                          */
75 /****************************************************************************/
76 gchar *
77 gl_file_util_make_absolute (const gchar *filename)
78 {
79         gchar *pwd, *absolute_filename;
80
81         if (g_path_is_absolute (filename)) {
82                 absolute_filename = g_strdup (filename);
83         } else {
84                 pwd = g_get_current_dir ();
85                 absolute_filename = g_build_filename (pwd, filename, NULL);
86                 g_free (pwd);
87         }
88
89         return absolute_filename;
90 }
91
92
93 /****************************************************************************/
94 /* Test for given extension.                                                */
95 /****************************************************************************/
96 gboolean
97 gl_file_util_is_extension (const gchar       *filename,
98                            const gchar       *ext_test)
99 {
100         gchar *ext;
101
102         ext = strrchr (filename, '.');
103         if ( ext != NULL )
104         {
105                 return (g_ascii_strcasecmp (ext, ext_test) == 0);
106         }
107
108         return FALSE;
109 }
110
111
112
113 /*
114  * Local Variables:       -- emacs
115  * mode: C                -- emacs
116  * c-basic-offset: 8      -- emacs
117  * tab-width: 8           -- emacs
118  * indent-tabs-mode: nil  -- emacs
119  * End:                   -- emacs
120  */