Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changeset 1708

Show
Ignore:
Timestamp:
07/01/10 21:14:49 (14 years ago)
Author:
kyllingstad
Message:

Improved file name/line number propagation in std.file (FileException? and cenforce())

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/phobos/std/file.d

    r1639 r1708  
    125125 */ 
    126126 
    127127class FileException : Exception 
    128128{ 
    129129/** 
    130130OS error code. 
    131131 */  
    132132    immutable uint errno; 
    133133     
    134134/** 
    135 Constructor taking the name of the file where error happened and the 
    136 error number ($(LUCKY GetLastError) in Windows, $(D getErrno) in 
    137 Posix). 
     135Constructor taking the name of the file where error happened and a 
     136message describing the error. 
    138137 */  
    139138    this(in char[] name, in char[] message) 
    140139    { 
    141140        super(text(name, ": ", message)); 
     141        errno = 0; 
     142    } 
     143 
     144    this(in char[] name, in char[] message, string sourceFile, int sourceLine) 
     145    { 
     146        super(text(name, ": ", message), sourceFile, sourceLine); 
    142147        errno = 0; 
    143148    } 
    144149 
    145150/** 
    146151Constructor taking the name of the file where error happened and the 
    147152error number ($(LUCKY GetLastError) in Windows, $(D getErrno) in 
    148153Posix). 
    149154 */  
    150     version(Windows) this(string name, uint errno = GetLastError) 
     155    version(Windows) this(in char[] name, uint errno = GetLastError) 
    151156    { 
    152157        this(name, sysErrorString(errno)); 
    153158        this.errno = errno; 
    154159    } 
    155160 
    156161    version(Posix) this(in char[] name, uint errno = .getErrno) 
    157162    { 
    158163        auto s = strerror(errno); 
    159164        this(name, to!string(s)); 
    160165        this.errno = errno; 
    161166    } 
     167 
     168    version(Windows) this(in char[] name, string sourceFile, int sourceLine, 
     169        uint errno = GetLastError) 
     170    { 
     171        this(name, sysErrorString(errno), sourceFile, sourceLine); 
     172        this.errno = errno; 
     173    } 
     174 
     175    version(Posix) this(in char[] name, string sourceFile, int sourceLine, 
     176        uint errno = .getErrno) 
     177    { 
     178        auto s = strerror(errno); 
     179        this(name, to!string(s), sourceFile, sourceLine); 
     180        this.errno = errno; 
     181    } 
    162182} 
    163183 
    164184private T cenforce(T, string file = __FILE__, uint line = __LINE__) 
    165185(T condition, lazy const(char)[] name) 
    166186{ 
    167187    if (!condition) 
    168188    { 
    169         throw new FileException( 
    170             text("In ", file, "(", line, "), data file ", name)); 
     189        throw new FileException(name, file, line); 
    171190    } 
    172191    return condition; 
    173192} 
    174193 
    175194/* ********************************** 
    176195 * Basic File operations. 
    177196 */ 
    178197 
    179198/******************************************** 
    180199Read entire contents of file $(D name).