From bug-request at octave dot org Wed Apr 5 02:02:55 2006 Subject: autoload (was: Re: maybe remount) From: "John W. Eaton" To: Paul Kienzle Cc: "bug at octave dot org mailing list" Date: Wed, 5 Apr 2006 03:02:20 -0400 On 31-Mar-2006, Paul Kienzle wrote: | Okay, I agree that 'which' is not an ideal solution. I'm still | unhappy with the hardcoded full path to the function though. It | means that I can't move the directories around and have it just work. | When we eventually put octave into an OS X octave.app, we will | not be able to rely on a fixed path to the file. | | Could we instead rely on the relative path between the script | containing the autoload command and the oct file we are loading | from? For example, assuming we had a function 'thisfile' which | returns the path to the currently running script: | | autoload('min', fullfile(fileparts(thisfile()),'max.oct') ) OK, how about the following function (which also has the "advantage" of being needed for compatibility)? With it, you can write the above, but instead of thisfile () use mfilename ("fullpath") So the only assumption is that the PKG_ADD file that contains the autoload command and the file named in the autoload command must be located in the same directory. I also fixed the construction of the PKG_ADD files for Octave so that they use this new method so we can generate the PKG_ADD files just once, when Octave is compiled. | It would be easier to use if the path transformation were | implicit, and autoload would add the correct path if the given | path were not anchored at the root: | | autoload('min', 'max.oct') OK, I suppose you could also make this work given the implementation of mfilename. Exercise left to the reader. :-) jwe src/ChangeLog: 2006-04-05 John W. Eaton * parse.y (Fmfilename): New function. Index: src/parse.y =================================================================== RCS file: /cvs/octave/src/parse.y,v retrieving revision 1.247 diff -u -r1.247 parse.y --- src/parse.y 15 Mar 2006 02:51:40 -0000 1.247 +++ src/parse.y 5 Apr 2006 06:50:12 -0000 at @ -3584,6 +3584,68 @@ unwind_protect::run_frame ("source_file"); } +DEFUN (mfilename, args, , + "-*- texinfo -*-\n\ + at deftypefn {Built-in Function} {} mfilename ()\n\ + at deftypefnx {Built-in Function} {} mfilename (@code{\"fullpath\"})\n\ + at deftypefnx {Built-in Function} {} mfilename (@code{\"fullpathext\"})\n\ +Return the name of the currently executing file. At the top-level,\n\ +return the empty string. Given the argument at code{\"fullpath\"},\n\ +include the directory part of the file name, but not the extension.\n\ +Given the argument at code{\"fullpathext\"}, include the directory part\n\ +of the file name and the extension.\n\ + at end deftypfn") +{ + octave_value retval; + + int nargin = args.length (); + + if (nargin > 1) + { + print_usage ("mfilename"); + return retval; + } + + std::string arg; + + if (nargin == 1) + { + arg = args(0).string_value (); + + if (error_state) + { + error ("mfilename: expecting argument to be a character string"); + return retval; + } + } + + std::string fname; + + if (curr_caller_function) + fname = curr_caller_function->fcn_file_name (); + + if (arg == "fullpathext") + retval = fname; + else + { + size_t pos = fname.rfind ('.'); + + fname = (pos != NPOS) ? fname.substr (0, pos) : fname; + + if (arg == "fullpath") + retval = fname; + else + { + pos = fname.rfind (file_ops::dir_sep_char); + + retval = (pos != NPOS) ? fname.substr (pos+1) : fname; + } + } + + return retval; +} + + DEFUN (source, args, , "-*- texinfo -*-\n\ at deftypefn {Built-in Function} {} source (@var{file})\n\ ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------