Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3835

Show
Ignore:
Timestamp:
08/03/08 13:02:39 (4 months ago)
Author:
kris
Message:

updated TempFile? to use char[] instead of FilePath?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/io/TempFile.d

    r3818 r3835  
    1616import tango.io.DeviceConduit : DeviceConduit; 
    1717import tango.io.FileConduit : FileConduit; 
    18 import tango.io.FilePath : FilePath/*, PathView*/
     18import tango.io.FilePath : FilePath
    1919import tango.stdc.stringz : toStringz, toString16z; 
    2020 
     
    2424version( Win32 ) 
    2525{ 
    26     import tango.sys.Common : DWORD, LONG
     26    import tango.sys.Common : DWORD, LONG, MAX_PATH, PCHAR, CP_UTF8
    2727 
    2828    enum : DWORD { FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000 } 
     
    3939            GetTempPathA, SetFilePointer, GetLastError, ERROR_SUCCESS; 
    4040 
    41         HANDLE CreateFile(FilePath fn, DWORD da, DWORD sm, 
     41        HANDLE CreateFile(char[] fn, DWORD da, DWORD sm, 
    4242                LPSECURITY_ATTRIBUTES sa, DWORD cd, DWORD faa, HANDLE tf) 
    4343        { 
    44             return CreateFileA(fn.cString.ptr, da, sm, sa, cd, faa, tf); 
     44            return CreateFileA(toStringz(fn), da, sm, sa, cd, faa, tf); 
    4545        } 
    4646 
     
    6161    { 
    6262        import tango.sys.Common : 
     63            MultiByteToWideChar, WideCharToMultiByte, 
    6364            GetVersionExW, OSVERSIONINFO, 
    6465            CreateFileW, GENERIC_READ, GENERIC_WRITE, 
     
    6970            GetTempPathW, SetFilePointer, GetLastError, ERROR_SUCCESS; 
    7071 
    71         import tango.text.convert.Utf : toString, toString16; 
    72  
    73         HANDLE CreateFile(FilePath fn, DWORD da, DWORD sm, 
     72        HANDLE CreateFile(char[] fn, DWORD da, DWORD sm, 
    7473                LPSECURITY_ATTRIBUTES sa, DWORD cd, DWORD faa, HANDLE tf) 
    7574        { 
    76             return CreateFileW(toString16(fn.cString).ptr, 
    77                     da, sm, sa, cd, faa, tf); 
     75                // convert into output buffer 
     76                wchar[MAX_PATH+1] tmp = void; 
     77                assert (fn.length < tmp.length); 
     78                auto i = MultiByteToWideChar (CP_UTF8, 0, cast(PCHAR) fn.ptr,  
     79                                              fn.length, tmp.ptr, tmp.length); 
     80                tmp[i] = 0; 
     81                return CreateFileW(tmp.ptr, da, sm, sa, cd, faa, tf); 
    7882        } 
    7983 
     
    8892            if( len == 0 ) 
    8993                throw new Exception("could not obtain temporary path"); 
    90             return Path.standard(toString(result[0..len])); 
     94 
     95            auto dir = new char [len * 3]; 
     96            auto i = WideCharToMultiByte (CP_UTF8, 0, result.ptr, len,  
     97                                          cast(PCHAR) dir.ptr, dir.length, null, null); 
     98            return Path.standard (dir[0..i]); 
    9199        } 
    92100    } 
     
    286294 
    287295    // Path to the temporary file 
    288     private /*PathView*/ FilePath _path; 
     296    private char[] _path; 
    289297 
    290298    // Style we've opened with 
     
    303311    this(char[] prefix, Style style = Style.init) 
    304312    { 
    305         this(FilePath(prefix), style); 
    306     } 
    307  
    308     /// 
    309     this(FilePath prefix, Style style = Style.init) 
    310     { 
    311         create(prefix.dup, style); 
     313        create (prefix, style); 
     314    } 
     315 
     316    /// deprecated: please use char[] version instead 
     317    deprecated this(FilePath prefix, Style style = Style.init) 
     318    { 
     319        this (prefix.toString.dup, style); 
    312320    } 
    313321 
     
    319327    /************************************************************************** 
    320328     * 
    321      * Returns a PathView to the temporary file.  Please note that depending 
     329     * Returns the path of the temporary file.  Please note that depending 
    322330     * on your platform, the returned path may or may not actually exist if 
    323331     * you specified a transient file. 
    324332     * 
    325333     **************************************************************************/ 
    326     /*PathView*/ FilePath path() 
     334    char[] path() 
    327335    { 
    328336        return _path; 
     
    341349    override char[] toString() 
    342350    { 
    343         if( path.toString.length > 0 ) 
    344             return path.toString
     351        if( path.length > 0 ) 
     352            return path
    345353        else 
    346354            return "<TempFile>"; 
     
    379387    } 
    380388 
    381     private void create(FilePath prefix, Style style) 
     389    private void create(char[] prefix, Style style) 
    382390    { 
    383391        for( size_t i=0; i<style.attempts; ++i ) 
    384392        { 
    385             if( create_path(prefix.dup.append(randomName), style) ) 
     393            if( create_path(Path.join(prefix, randomName), style) ) 
    386394                return; 
    387395        } 
     
    402410         * Returns the path to the temporary directory. 
    403411         */ 
    404         public static FilePath tempPath() 
    405         { 
    406             return FilePath(GetTempPath()).dup
     412        public static char[] tempPath() 
     413        { 
     414            return GetTempPath
    407415        } 
    408416 
     
    411419         * style. 
    412420         */ 
    413         private bool create_path(FilePath path, Style style) 
     421        private bool create_path(char[] path, Style style) 
    414422        { 
    415423            // TODO: Check permissions directly and throw an exception; 
     
    480488         * Returns the path to the temporary directory. 
    481489         */ 
    482         public static FilePath tempPath() 
     490        public static char[] tempPath() 
    483491        { 
    484492            // Check for TMPDIR; failing that, use /tmp 
    485493            if( auto tmpdir = Environment.get("TMPDIR") ) 
    486                 return FilePath(tmpdir).dup; 
     494                return tmpdir.dup; 
    487495            else 
    488                 return FilePath("/tmp/").dup
     496                return "/tmp/"
    489497        } 
    490498 
     
    493501         * style. 
    494502         */ 
    495         private bool create_path(FilePath path, Style style) 
     503        private bool create_path(char[] path, Style style) 
    496504        { 
    497505            // Check suitability 
    498506            { 
    499                 auto parent = path.path; 
     507                auto parent = Path.parse(path).path; 
    500508                auto parentz = toStringz(parent); 
    501509 
     
    528536                    | O_NOFOLLOW | O_RDWR; 
    529537 
    530                 auto pathz = path.cString.ptr
     538                auto pathz = toStringz(path)
    531539 
    532540                handle = open(pathz, flags, 0600); 
     
    579587         * 
    580588         **********************************************************************/ 
    581         FilePath tempPath(); 
     589        char[] tempPath(); 
    582590    } 
    583591    else