| 40 | | import std.stream : Stream, BufferedFile, FileMode; |
|---|
| 41 | | import std.string : find, strip, toString, tolower, split; |
|---|
| 42 | | import std.file : exists; |
|---|
| 43 | | import std.conv : toInt, toReal, ConvError; |
|---|
| 44 | | import std.path : getBaseName, getDirName, join, pathsep; |
|---|
| 45 | | import std.c.stdlib : getenv; |
|---|
| | 40 | version (Tango) { |
|---|
| | 41 | import tango.io.FileSystem : FileSystem; |
|---|
| | 42 | import tango.io.FilePath : FilePath; |
|---|
| | 43 | import tango.io.FileProxy : FileProxy; |
|---|
| | 44 | import tango.text.Util : locatePrior, split; |
|---|
| | 45 | import tango.stdc.stdlib : getenv, strlen; |
|---|
| | 46 | version (Windows) { |
|---|
| | 47 | const char[] SystemPathSeperator = ";"; |
|---|
| | 48 | } else { |
|---|
| | 49 | const char[] SystemPathSeperator = ":"; |
|---|
| | 50 | } |
|---|
| | 51 | } else { |
|---|
| | 52 | import std.stream : Stream, BufferedFile, FileMode; |
|---|
| | 53 | import std.string : find, strip, toString, tolower, split; |
|---|
| | 54 | import std.file : exists; |
|---|
| | 55 | import std.conv : toInt, toReal, ConvError; |
|---|
| | 56 | import std.path : getBaseName, getDirName, join, pathsep; |
|---|
| | 57 | import std.c.stdlib : getenv, strlen; |
|---|
| | 58 | } |
|---|
| 71 | | // Returns the name of the executable, minus directories or extensions. |
|---|
| 72 | | char[] get_program_name(char[] path) { |
|---|
| 73 | | version(Windows) { |
|---|
| 74 | | // (Unicode note: ".exe" only contains 4 code units, so this slice |
|---|
| 75 | | // should Just Work.) (Although it remains to be seen how robust |
|---|
| 76 | | // this code actually is.) |
|---|
| 77 | | assert(path[$-4 .. $] == ".exe"); |
|---|
| 78 | | path = path[0 .. $-4]; |
|---|
| 79 | | } |
|---|
| 80 | | return getBaseName(path); |
|---|
| | 84 | /* |
|---|
| | 85 | A brief note about arg0: |
|---|
| | 86 | |
|---|
| | 87 | The first element of the args array differs based on operating system and which |
|---|
| | 88 | library (Phobos or Tango) is in use. In most cases, arg0 is the precise string |
|---|
| | 89 | used to start the program. In Phobos on Windows, arg0 is always the complete |
|---|
| | 90 | path to the EXE. |
|---|
| | 91 | */ |
|---|
| | 92 | |
|---|
| | 93 | // Returns the name of the executable, minus directories. |
|---|
| | 94 | version (Tango) { |
|---|
| | 95 | char[] get_program_name(char[] path) { |
|---|
| | 96 | version(Windows) { |
|---|
| | 97 | char delimiter = '\\'; |
|---|
| | 98 | } else { |
|---|
| | 99 | char delimiter = '/'; |
|---|
| | 100 | } |
|---|
| | 101 | uint idx = locatePrior(path, delimiter); |
|---|
| | 102 | if (idx == path.length) return path; |
|---|
| | 103 | return path[idx+1 .. $]; |
|---|
| | 104 | } |
|---|
| | 105 | } else { |
|---|
| | 106 | char[] get_program_name(char[] path) { |
|---|
| | 107 | version(Windows) { |
|---|
| | 108 | // (Unicode note: ".exe" only contains 4 code units, so this slice |
|---|
| | 109 | // should Just Work.) (Although it remains to be seen how robust |
|---|
| | 110 | // this code actually is.) |
|---|
| | 111 | assert(path[$-4 .. $] == ".exe"); |
|---|
| | 112 | path = path[0 .. $-4]; |
|---|
| | 113 | } |
|---|
| | 114 | return getBaseName(path); |
|---|
| | 115 | } |
|---|
| 102 | | return join(dir, filename); |
|---|
| | 139 | return file.join(dir); |
|---|
| | 140 | } |
|---|
| | 141 | } else { |
|---|
| | 142 | version (Windows) { |
|---|
| | 143 | return join(getDirName(arg0), filename); |
|---|
| | 144 | } else { |
|---|
| | 145 | char[] dir = getDirName(arg0); |
|---|
| | 146 | if (dir == "") { |
|---|
| | 147 | char[][] paths = split(env("PATH"), pathsep); |
|---|
| | 148 | foreach (path; paths) { |
|---|
| | 149 | if (exists(join(path, getBaseName(arg0)))) |
|---|
| | 150 | return join(path, filename); |
|---|
| | 151 | } |
|---|
| | 152 | return ""; |
|---|
| | 153 | } else { |
|---|
| | 154 | return join(dir, filename); |
|---|
| | 155 | } |
|---|