1 //
2 //      aegis - project change supervisor
3 //      Copyright (C) 1998, 2003-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/nstring.h>
21 #include <common/trace.h>
22 #include <common/wstring/list.h>
23 #include <libaegis/sub.h>
24 #include <libaegis/sub/trim_directo.h>
25 
26 
27 wstring
sub_trim_directory(sub_context_ty * scp,const wstring_list & arg)28 sub_trim_directory(sub_context_ty *scp, const wstring_list &arg)
29 {
30     trace(("sub_trim_directory()\n{\n"));
31     wstring dir;
32     long n = 1;
33     switch (arg.size())
34     {
35     default:
36         scp->error_set(i18n("requires one argument"));
37         trace(("return NULL;\n"));
38         trace(("}\n"));
39         return wstring();
40 
41     case 2:
42         n = 1;
43         dir = arg[1];
44         break;
45 
46     case 3:
47         {
48             nstring s = arg[1].to_nstring();
49             n = atol(s.c_str());
50             trace(("n = %ld;\n", n));
51             dir = arg[2];
52         }
53         break;
54     }
55 
56     //
57     // Skip the given number of leading directory components.
58     //
59     const wchar_t *wcp = dir.c_str();
60     const wchar_t *wep = wcp + dir.size();
61     while (n-- > 0)
62     {
63         const wchar_t *ep = wcp;
64         while (ep < wep && *ep != '/')
65             ++ep;
66         if (ep >= wep)
67             break;
68         wcp = ep + 1;
69     }
70 
71     //
72     // Build the result.
73     //
74     wstring result(wcp, wep - wcp);
75     trace(("return %p;\n", result.get_ref()));
76     trace(("}\n"));
77     return result;
78 }
79 
80 
81 // vim: set ts=8 sw=4 et :
82