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

Changeset 3638

Show
Ignore:
Timestamp:
06/18/08 22:14:59 (4 months ago)
Author:
kris
Message:

FileConduit? now based around char[], and exposes the open() method for use.

Files:

Legend:

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

    r3559 r3638  
    2323private import  tango.io.DeviceConduit; 
    2424 
     25private import  stdc = tango.stdc.stringz; 
     26 
    2527private import  Utf = tango.text.convert.Utf; 
    2628 
     
    237239 
    238240        // the file we're working with  
    239         private PathView path_; 
     241        private char[] path_; 
    240242 
    241243        // the style we're opened with 
    242         private Style    style_; 
     244        private Style   style_; 
     245 
     246        /*********************************************************************** 
     247         
     248                Create a FileConduit for use with open() 
     249 
     250        ***********************************************************************/ 
     251 
     252        this () 
     253        { 
     254        } 
    243255 
    244256        /*********************************************************************** 
     
    248260        ***********************************************************************/ 
    249261 
    250         this (char[] name, Style style = ReadExisting) 
    251         { 
    252                 this (new FilePath(name), style); 
     262        this (char[] path, Style style = ReadExisting) 
     263        { 
     264                open (path, style); 
    253265        } 
    254266 
     
    257269                Create a FileConduit with the provided path and style. 
    258270 
    259         ***********************************************************************/ 
    260  
    261         this (PathView path, Style style = ReadExisting) 
    262         { 
    263                 // remember who we are 
    264                 path_ = path; 
    265  
    266                 // open the file 
    267                 open (this.style_ = style); 
     271                Deprecated: use char[] ctor instead 
     272 
     273        ***********************************************************************/ 
     274 
     275        deprecated this (PathView path, Style style = ReadExisting) 
     276        { 
     277                this (path.toString, style); 
    268278        }     
    269279 
     
    272282                Return the PathView used by this file. 
    273283 
    274         ***********************************************************************/ 
    275  
    276         PathView path () 
    277         { 
    278                 return path_; 
     284                We're removing PathView here, in favour of toString() 
     285 
     286        ***********************************************************************/ 
     287 
     288        deprecated PathView path () 
     289        { 
     290                // return something useful in the interim 
     291                return new FilePath (path_); 
    279292        }                
    280293 
     
    298311        override char[] toString () 
    299312        { 
    300                 return path_.toString
     313                return path_
    301314        }                
    302315 
     
    346359                ***************************************************************/ 
    347360 
    348                 protected void open (Style style
     361                void open (char[] path, Style style = ReadExisting
    349362                { 
    350363                        DWORD   attr, 
     
    386399                                        FILE_FLAG_WRITE_THROUGH, 
    387400                                        ]; 
     401 
     402                        // remember our settings 
     403                        assert(path); 
     404                        path_ = path; 
     405                        style_ = style; 
    388406 
    389407                        attr   = Attr[style.cache]; 
     
    399417                             else 
    400418                                { 
    401                                 wchar[256] tmp = void; 
    402                                 auto name = Utf.toString16 (path.cString, tmp); 
    403                                 handle = CreateFileW (name.ptr, access, share, 
     419                                char[512] zero = void; 
     420                                wchar[512] convert = void; 
     421 
     422                                // zero terminate and convert to utf16 
     423                                auto name = stdc.toStringz (path, zero); 
     424                                auto wide = Utf.toString16 (name[0..path.length+1], convert); 
     425 
     426                                // open the file 
     427                                handle = CreateFileW (wide.ptr, access, share, 
    404428                                                      null, create,  
    405429                                                      attr | FILE_ATTRIBUTE_NORMAL, 
     
    408432 
    409433                        if (handle is INVALID_HANDLE_VALUE) 
    410                             error ()
     434                            error
    411435 
    412436                        // move to end of file? 
     
    445469                        // must have Generic_Write access 
    446470                        if (! SetEndOfFile (handle)) 
    447                               error ();                             
     471                              error;                             
    448472                }                
    449473 
     
    463487                        if (result is -1 &&  
    464488                            GetLastError() != ERROR_SUCCESS) 
    465                             error ()
     489                            error
    466490 
    467491                        return result + (cast(long) high << 32); 
     
    491515                ***************************************************************/ 
    492516 
    493                 protected void open (Style style
     517                void open (char[] path, Style style = ReadExisting
    494518                { 
    495519                        alias int[] Flags; 
     
    519543                                        ]; 
    520544                                                 
     545                        // remember our settings 
     546                        assert(path); 
     547                        path_ = path; 
     548                        style_ = style; 
     549 
     550                        // zero terminate and convert to utf16 
     551                        char[512] zero = void; 
     552                        auto name = stdc.toStringz (path, zero); 
    521553                        auto mode = Access[style.access] | Create[style.open]; 
    522554 
    523555                        // always open as a large file 
    524                         handle = posix.open (path.cString.ptr, mode | O_LARGEFILE, 0666); 
     556                        handle = posix.open (name, mode | O_LARGEFILE, 0666); 
    525557                        if (handle is -1) 
    526                             error ()
     558                            error
    527559                } 
    528560 
     
    539571                        // set filesize to be current seek-position 
    540572                        if (ftruncate (handle, cast(int) position) is -1) 
    541                             error ()
     573                            error
    542574                }                
    543575 
     
    553585                        long result = posix.lseek (handle, cast(int) offset, anchor); 
    554586                        if (result is -1) 
    555                             error ()
     587                            error
    556588                        return result; 
    557589                }