Changeset 104

Show
Ignore:
Timestamp:
03/05/07 17:31:59 (2 years ago)
Author:
KirkMcDonald
Message:

Some configparse Tango support. (Not there yet.)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • misc/configparse.d

    r97 r104  
    3838module configparse; 
    3939 
    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; 
     40version (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
    4659 
    4760/// Thrown when configparse is unable to parse a file. 
     
    6982const char[] GLOBAL_SECTION = "General"; 
    7083 
    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/* 
     85A brief note about arg0: 
     86 
     87The first element of the args array differs based on operating system and which 
     88library (Phobos or Tango) is in use. In most cases, arg0 is the precise string 
     89used to start the program. In Phobos on Windows, arg0 is always the complete 
     90path to the EXE. 
     91*/ 
     92 
     93// Returns the name of the executable, minus directories. 
     94version (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    } 
    81116} 
    82117char[] env(char* var) { 
    83     return toString(getenv(var)); 
     118    char* s = getenv(var); 
     119    return s[0 .. strlen(s)]; 
    84120} 
    85121/++ 
     
    88124+/ 
    89125char[] same_dir(char[] arg0, char[] filename) { 
    90     version (Windows) { 
    91         return join(getDirName(arg0), filename); 
    92     } else { 
    93         char[] dir = getDirName(arg0); 
     126    version (Tango) { 
     127        scope binary = new FilePath(arg0); 
     128        scope file = new FilePath(filename); 
     129        char[] dir = binary.getPath(); 
     130        scope bin_name = new FilePath(binary.getFullName()); 
    94131        if (dir == "") { 
    95             char[][] paths = split(env("PATH"), pathsep); 
     132            char[][] paths = split(env("PATH"), SystemPathSeparator); 
    96133            foreach (path; paths) { 
    97                 if (exists(join(path, getBaseName(arg0)))) 
    98                     return join(path, filename); 
     134                if (new FileProxy(bin_name.join(path)).isExisting()) 
     135                    return file.join(path).toUtf8(); 
    99136            } 
    100137            return ""; 
    101138        } else { 
    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            } 
    103156        } 
    104157    } 
     
    131184        return [ 
    132185            same_dir(arg0, name), 
    133             join(env("HOMEDRIVE")~env("HOMEPATH"), name)
     186            env("HOMEDRIVE")~env("HOMEPATH")~"\\"~name
    134187            name 
    135188        ];