* Sébastien MARQUE -- Wednesday 22 October 2008:
> may you teach a young padawan about what it is intented to do
> and how it works, please? :D 

Don't look too closely at that. I was just fooling around, and
there was a bug in it, too. I only took the first version and
replaced some constants with equivalent but misleading values.
num() for nil,  0!=!0 for 1  (that is:  0 != !0), and the 9
has no meaning. It was only important that stat() and isdir()
used the same value.

The serious solution looks like this:

  var stat = (func { var _stat = stat; func(f) _stat(caller()[0][0] = f) })();
  var isdir = func directory(caller()[0][0]) != nil;


The background
--------------
io.stat(<path>) returns a vector with information about 
a file (see 'man 2 stat'), where the 3rd entry is the mode
field, which tells you whether a "file" is a regular file,
or a directory, etc. One uses it like this:

  var s = io.stat("/tmp/foo");
  if (s != nil and io.isdir(s[2]))
      print("/tmp/foo is a directory");


The problem
-----------
io.isdir() used to use the bits lib, but after the last Nasal
update the lib changed its behavior dramatically: bit #0 is now
no longer the rightmost bit (LSB). This broke isdir().


The ugly hack
-------------
I wasn't keen to fix that, as it could be that Andy reverts the
bits library change. So I suggested a hack that would let io.stat()
store the argument in a local variable (caller()[0][something])
where io.isdir() would pick it up and abuse directory() to check
if the file is a directory. It returns nil if it's not a directory.

Verbosely written, the same could have looked like this:

  var orig_stat = io.stat;

  io.stat = func(file) {
      caller()[0]["_stat_filename"] = file;     # store filename
                                                # in the calling function's
                                                # local variable space ...
      return orig_stat(file);
  }

  io.isdir = func(mode) {
      var file = caller()[0]["_stat_filename"]; # ... and pick it up again
      if (directory(file) == nil)
          return 0;               # file isn't a directory
      else
          return 1;               # file is a directory
  }


The real solution
-----------------
Don't use the bits lib at all, but simple arithmetic instead. This is
also twice as fast. And I should have done that immeditately, and not
play with ugly hacks.  :-)

m.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to