[DGD] Re: DGD LPC (?)

Mikael Lind z94lind at mtek.chalmers.se
Sun Aug 2 00:45:09 CEST 1998


On Sat, 1 Aug 1998, Danne Solli wrote:

> int file_size(string str) {
>   mixed *sizes;
> 
>   if (sizeof(sizes = get_dir(str)[1]) == 1)
>     return sizes[0];
>   else
>     return -1;
> }
> 
> ought to do it.

The problem with your solution is that get_dir()'s wildcard matching is
ignored...

get_dir("/a/*") returns the files "b", "c" and "*" but your
file_size("/a/*") returns -1. (Should return the size of "/a/*".) Probably
not a problem if your files have names clean from wildcards.

get_dir("/a/*") returns the file "b" and your file_size("/a/*") returns the
size of "/a/b". (Should return -1.) Might be a problem.

The following is a somewhat abbreviated version of the file_size() that I
use. It's similar to the file_size() in the Kernel Library, I think, although
Dworkin's solution probably is optimized.

int file_size(string path) {
    int      i;
    mixed  **dir;
    string  *comps, base;

    if (path == "/") {
        return -2;
    }
    comps = explode(path, "/");
    base = comps[sizeof(comps) - 1];
    dir = get_dir(path);
    i = sizeof(dir[0]);
    while (i--) {
        if (dir[0][i] == base) {
            return dir[1][i];
        }
    }
    return -1;
}

Still, file_size("/a/[a-z]") wouldn't return the size of the file
"/a/[a-z]", if such a file existed. File names containing range matches would
need to be handled separately. I think. I don't. :)

This may seem like a very costly way to determine the size of a file. I'm
however under the impression that you could put in a lot of checks and still
have the actual file I/O call as the major tick drainer. Especially if you
precompile. Am I wrong?

Anyway, file names containing wildcards are hardly recommendable...

L Mikael "eLeMeL" Lind                :: I wished for 4 cursed scrolls
z94lind at mtek.chalmers.se              :: of gold detection and all I
http://www.mtek.chalmers.se/~z94lind/ :: got was this lousy .signature



List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list