1 /****************************************************************************** 2 3 Keeps the path of a running executable 4 5 Copyright: 6 Copyright (c) 2009-2016 dunnhumby Germany GmbH. 7 All rights reserved. 8 9 License: 10 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. 11 Alternatively, this file may be distributed under the terms of the Tango 12 3-Clause BSD License (see LICENSE_BSD.txt for details). 13 14 ******************************************************************************/ 15 16 module ocean.sys.CmdPath; 17 18 19 import ocean.meta.types.Qualifiers; 20 21 import ocean.sys.Environment; 22 23 import ocean.io.FilePath; 24 25 import PathUtil = ocean.io.Path: normalize; 26 27 /// 28 unittest 29 { 30 void main ( istring[] args ) 31 { 32 CmdPath cmdpath; 33 34 // set to path of running executable 35 cmdpath.set(args[0]); 36 37 // get absolute directory path of running executable 38 auto exepath = cmdpath.get(); 39 40 // get absolute path of file "config.ini" located in subdirectory 41 // "etc" of the running executable's directory 42 auto cfgpath = cmdpath.prepend(["etc", "config.ini"]); 43 } 44 } 45 46 /****************************************************************************** 47 48 MainExe structure 49 50 ******************************************************************************/ 51 52 struct CmdPath 53 { 54 /************************************************************************** 55 56 Directory of the executable 57 58 **************************************************************************/ 59 60 private istring dir; 61 62 /************************************************************************** 63 64 Sets the executable path. 65 66 Params: 67 exepath = executable path 68 69 Returns: 70 base directory 71 72 **************************************************************************/ 73 74 public istring set ( cstring exepath ) 75 { 76 scope path = new FilePath(exepath); 77 78 path.set(PathUtil.normalize(path.folder)); 79 80 this.dir = path.absolute(Environment.cwd()).toString(); 81 82 return this.get(); 83 } 84 85 /************************************************************************** 86 87 Returns the base directory. 88 89 Returns: 90 base directory 91 92 **************************************************************************/ 93 94 public istring get ( ) 95 { 96 return this.dir; 97 } 98 99 /************************************************************************** 100 101 Prepends the absolute base directory to "path" and joins the path. 102 103 Params: 104 path = input path 105 106 Returns: 107 joined path with prepended absolute base directory 108 109 **************************************************************************/ 110 111 public istring prepend ( istring[] path ... ) 112 { 113 return FilePath.join(this.dir ~ path); 114 } 115 }