www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.file new functions/classes

reply jicman <jicman_member pathlink.com> writes:
Walter, first of all, thanks for all the great work you've been doing!  You're
change my life. ;-)  Again, thanks.

Ok, for the rest of the folks, out there...

So, I love the new std.file, but I don't know how to get the "struct DirEntry"
to work.  Here is the help in Phobos:

|struct DirEntry;
|    Directory Entry
|
|    char[] name;
|        file or directory name
|
|    ulong size;
|        size of file in bytes
|
|    long creationTime;
|        time of file creation
|
|    long lastAccessTime;
|        time file was last accessed
|
|    long lastWriteTime;
|        time file was last written to
|
|    int isdir();
|        Return !=0 if DirEntry is a directory.
|
|    int isfile();
|        Return !=0 if DirEntry is a file. 

How do I use it against a file?  (say, c:\temp\file.txt)

thanks.

josé
Mar 09 2006
parent reply jicman <jicman_member pathlink.com> writes:
Has anyone figured out how to use the DirEntry entries for std.file?

jicman says...
Walter, first of all, thanks for all the great work you've been doing!  You're
change my life. ;-)  Again, thanks.

Ok, for the rest of the folks, out there...

So, I love the new std.file, but I don't know how to get the "struct DirEntry"
to work.  Here is the help in Phobos:

|struct DirEntry;
|    Directory Entry
|
|    char[] name;
|        file or directory name
|
|    ulong size;
|        size of file in bytes
|
|    long creationTime;
|        time of file creation
|
|    long lastAccessTime;
|        time file was last accessed
|
|    long lastWriteTime;
|        time file was last written to
|
|    int isdir();
|        Return !=0 if DirEntry is a directory.
|
|    int isfile();
|        Return !=0 if DirEntry is a file. 

How do I use it against a file?  (say, c:\temp\file.txt)

thanks.

josé
Mar 09 2006
parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Fri, 10 Mar 2006 07:56:36 +0000 (UTC), jicman  
<jicman_member pathlink.com> wrote:
 Has anyone figured out how to use the DirEntry entries for std.file?
I believe DirEntry is currently only used with listdir, eg. import std.file; import std.stdio; void main() { bool showFile(DirEntry* e) { if (e.isdir()) return true; writefln(" ",e.name); return true; } bool showDir(DirEntry* e) { if (!e.isdir()) return true; writefln(" ",e.name); return true; } writefln("Files:"); listdir(".",&showFile); writefln(""); writefln("Directories:"); listdir(".",&showDir); writefln(""); } Regan
Mar 10 2006
parent reply jicman <jicman_member pathlink.com> writes:
Gosh!  And I was so excited about std.file. ;-)  Well, it's a good thing that we
have std.recls. I hope Walter would, someday, also support the same functions
that are in DirEntry for a file.

Thanks Regan.


Regan Heath says...
On Fri, 10 Mar 2006 07:56:36 +0000 (UTC), jicman  
<jicman_member pathlink.com> wrote:
 Has anyone figured out how to use the DirEntry entries for std.file?
I believe DirEntry is currently only used with listdir, eg. import std.file; import std.stdio; void main() { bool showFile(DirEntry* e) { if (e.isdir()) return true; writefln(" ",e.name); return true; } bool showDir(DirEntry* e) { if (!e.isdir()) return true; writefln(" ",e.name); return true; } writefln("Files:"); listdir(".",&showFile); writefln(""); writefln("Directories:"); listdir(".",&showDir); writefln(""); } Regan
Mar 10 2006
parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Fri, 10 Mar 2006 15:11:47 +0000 (UTC), jicman  
<jicman_member pathlink.com> wrote:
 Gosh!  And I was so excited about std.file. ;-)  Well, it's a good thing  
 that we have std.recls.
There was a thread recently comparing them, you should read it to get Walter and Matthews opinions on the topic.
 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.
 Thanks Regan.
You're welcome. Regan
 Regan Heath says...
 On Fri, 10 Mar 2006 07:56:36 +0000 (UTC), jicman
 <jicman_member pathlink.com> wrote:
 Has anyone figured out how to use the DirEntry entries for std.file?
I believe DirEntry is currently only used with listdir, eg. import std.file; import std.stdio; void main() { bool showFile(DirEntry* e) { if (e.isdir()) return true; writefln(" ",e.name); return true; } bool showDir(DirEntry* e) { if (!e.isdir()) return true; writefln(" ",e.name); return true; } writefln("Files:"); listdir(".",&showFile); writefln(""); writefln("Directories:"); listdir(".",&showDir); writefln(""); } Regan
Mar 11 2006
parent reply jicman <jicman_member pathlink.com> writes:
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...
Mar 14 2006
parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Tue, 14 Mar 2006 21:46:00 +0000 (UTC), jicman  
<jicman_member 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
Mar 15 2006
parent jicman <jicman_member pathlink.com> writes:
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 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--
Mar 16 2006