1 //
2 // aegis - project change supervisor
3 // Copyright (C) 1999, 2002-2006, 2008, 2012 Peter Miller
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see
17 // <http://www.gnu.org/licenses/>.
18 //
19
20 #include <common/ac/string.h>
21
22 #include <libaegis/os.h>
23 #include <common/trace.h>
24
25
26 //
27 // NAME
28 // os_dirname_relative - take path apart
29 //
30 // SYNOPSIS
31 // string_ty *os_dirname_relative(string_ty *path);
32 //
33 // DESCRIPTION
34 // Os_dirname is used to extract the directory part
35 // of a pathname. If none, result is "."
36 //
37 // RETURNS
38 // pointer to dynamically allocated string.
39 //
40 // CAVEAT
41 // Use str_free() when you are done with the value returned.
42 //
43
44 string_ty *
os_dirname_relative(string_ty * path)45 os_dirname_relative(string_ty *path)
46 {
47 trace(("os_dirname_relative(path = %p)\n{\n", path));
48 trace_string(path->str_text);
49 const char *cp = strrchr(path->str_text, '/');
50 string_ty *s = 0;
51 if (!cp)
52 s = str_from_c(".");
53 else if (cp == path->str_text)
54 s = str_from_c("/");
55 else
56 s = str_n_from_c(path->str_text, cp - path->str_text);
57 trace_string(s->str_text);
58 trace(("}\n"));
59 return s;
60 }
61
62
63 nstring
os_dirname_relative(const nstring & path)64 os_dirname_relative(const nstring &path)
65 {
66 trace(("os_dirname_relative(path = \"%s\")\n{\n", path.c_str()));
67 const char *cp = strrchr(path.c_str(), '/');
68 if (!cp)
69 {
70 trace(("return \".\";\n}\n"));
71 return ".";
72 }
73 if (cp == path.c_str())
74 {
75 trace(("return \"/\";\n}\n"));
76 return "/";
77 }
78 nstring result(path.c_str(), cp - path.c_str());
79 trace(("return \"%s\";\n", result.c_str()));
80 trace(("}\n"));
81 return result;
82 }
83
84
85 // vim: set ts=8 sw=4 et :
86