std.file new functions/classes
jicman
jicman_member at pathlink.com
Thu Mar 16 12:26:44 PST 2006
Thanks Regan. :-)
Regan Heath says...
>
>------------fv5ryjRCvHY5uuvcLCI9oc
>Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15
>Content-Transfer-Encoding: 8bit
>
>On Tue, 14 Mar 2006 21:46:00 +0000 (UTC), jicman
><jicman_member at pathlink.com> wrote:
>> Regan Heath says...
>>
>>>> I hope Walter would, someday, also support the same functions that are
>>>> in DirEntry for a file.
>>>
>>> It should be fairly simple to write a function to produce a DirEntry,
>>> you
>>> could give it a go yourself. Heck, I might give it a go if I have time.
>>> I
>>> get the impression Walter is concentrating on the compiler as opposed to
>>> Phobos at the moment which might explain why std.file is a very basic
>>> implementation at present.
>>
>> Go ahead, make my day. ;-) Je je je je...
>
>LOL :)
>
>Here's a little something, not exactly what you wanted but you might find
>it useful.
>
>main.d
>etc\direntry.d
>etc\stat.d
>
>It uses 'stat' which has some limitations. I do not think it handles
>unicode filenames or files larger than int.max in size. MSDN defines
>_stati64 etc which, I believe, use M$ specific 64 bit integers to overcome
>the file size problem. Of course, DMC and thus DMD does not have access to
>these functions.
>
>Regan
>------------fv5ryjRCvHY5uuvcLCI9oc
>Content-Disposition: attachment; filename=main.d
>Content-Type: application/octet-stream; name=main.d
>Content-Transfer-Encoding: 8bit
>
>import etc.direntry;
>import std.stdio;
>
>void main(char[][] args)
>{
> DirEntryR d = new DirEntryR(args[1]);
> writefln("[",d.name,"]:");
> if (d.exists()) writefln("Exists");
> if (d.isDir()) writefln("Directory");
> if (d.isFile()) writefln("File");
>
> d = DirEntryR.getcwd();
> writefln("[",d.name,"]:");
> if (d.exists()) writefln("Exists");
> if (d.isDir()) writefln("Directory");
> if (d.isFile()) writefln("File");
>}
>
>------------fv5ryjRCvHY5uuvcLCI9oc
>Content-Disposition: attachment; filename=direntry.d
>Content-Type: application/octet-stream; name=direntry.d
>Content-Transfer-Encoding: 8bit
>
>module etc.direntry;
>private import etc.stat;
>private import std.string;
>private import std.c.stdlib;
>private import std.file;
>
>//should be defined in std.c.errno or similar
>uint ENOENT = 2;
>
>class DirEntryR
>{
>private:
> char[] _name;
> struct_stat data;
> bool doneStat;
> bool _exists;
>
> void doStat()
> {
> if (doneStat) return ;
> _exists = (stat(toStringz(_name),&data) == 0);
> doneStat = true;
> }
>
>public:
> this(char[] string)
> {
> name = string;
> }
>
> ~this()
> {
> }
>
> char[] name()
> {
> return _name;
> }
>
> char[] name(char[] string)
> {
> _name = string;
> refresh();
> return name;
> }
>
> void refresh()
> {
> doneStat = false;
> }
>
> bool exists()
> {
> doStat();
> return _exists;
> }
>
> bool isFile()
> {
> doStat();
> return _exists && !cast(bool)(data.st_mode & _S_IFDIR);
> }
>
> bool isDir()
> {
> doStat();
> return _exists && cast(bool)(data.st_mode & _S_IFDIR);
> }
>
> int size()
> {
> doStat();
> return data.st_size;
> }
>
> void[] read()
> {
> return std.file.read(_name);
> }
>
> void write(void[] buffer)
> {
> std.file.write(_name,buffer);
> }
>
> void append(void[] buffer)
> {
> std.file.append(_name,buffer);
> }
>
> void remove()
> {
> std.file.remove(_name);
> }
>
> void rename(char[] to)
> {
> std.file.rename(_name,to);
> }
>
> void rename(DirEntryR to)
> {
> std.file.rename(_name,to._name);
> }
>
> void chdir()
> {
> std.file.chdir(_name);
> }
>
> void mkdir()
> {
> std.file.mkdir(_name);
> }
>
> void rmdir()
> {
> std.file.rmdir(_name);
> }
>
> void copy(char[] to)
> {
> std.file.copy(_name,to);
> }
>
> void copy(DirEntryR to)
> {
> std.file.copy(_name,to._name);
> }
>
> static DirEntryR getcwd()
> {
> return new DirEntryR(std.file.getcwd());
> }
>}
>
>------------fv5ryjRCvHY5uuvcLCI9oc
>Content-Disposition: attachment; filename=stat.d
>Content-Type: application/octet-stream; name=stat.d
>Content-Transfer-Encoding: 8bit
>
>module etc.stat;
>
>extern(C):
>
>version(Windows)
>{
> struct struct_stat {
> short st_dev;
> ushort st_ino;
> ushort st_mode;
> short st_nlink;
> ushort st_uid;
> ushort st_gid;
> short st_rdev;
> int st_size;
> int st_atime;
> int st_mtime;
> int st_ctime;
> }
>
> ushort _S_IFMT = 0xF000;
> ushort _S_IFREG = 0x8000;
> ushort _S_IFBLK = 0x6000;
> ushort _S_IFNAM = 0x5000;
> ushort _S_IFDIR = 0x4000;
> ushort _S_IFCHR = 0x2000;
> ushort _S_IREAD = 0x0100;
> ushort _S_IWRITE = 0x0080;
> ushort _S_IEXEC = 0x0040;
>
> int fstat(int, struct_stat *);
> int stat(char *, struct_stat *);
>}
>
>version(linux)
>{
> import std.c.linux.linux;
>}
>
>------------fv5ryjRCvHY5uuvcLCI9oc--
More information about the Digitalmars-d-learn
mailing list