1 // 2 // aegis - project change supervisor 3 // Copyright (C) 2002-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/trace.h> 21 #include <common/wstring/list.h> 22 #include <libaegis/os.h> 23 #include <libaegis/sub.h> 24 #include <libaegis/sub/switch.h> 25 26 27 wstring sub_switch(sub_context_ty * scp,const wstring_list & arg)28sub_switch(sub_context_ty *scp, const wstring_list &arg) 29 { 30 trace(("sub_switch()\n{\n")); 31 trace(("arg.size() => %d\n", int(arg.size()))); 32 if (arg.size() < 2) 33 { 34 scp->error_set(i18n("requires two or more arguments")); 35 trace(("}\n")); 36 return wstring(); 37 } 38 39 long n = 2 + arg[1].to_nstring().to_long(); 40 trace(("n = %ld\n", n)); 41 wstring result; 42 if (n < 2 || (size_t)n >= arg.size()) 43 { 44 result = arg.back(); 45 } 46 else 47 { 48 result = arg[n]; 49 } 50 trace(("return %p;\n", result.get_ref())); 51 trace(("}\n")); 52 return result; 53 } 54 55 56 // vim: set ts=8 sw=4 et : 57