comparison src/utils.cc @ 4216:e613ffa9f0e6

[project @ 2002-12-04 17:37:09 by jwe]
author jwe
date Wed, 04 Dec 2002 17:37:09 +0000
parents 8ad52ec4f374
children ccfdb55c8156
comparison
equal deleted inserted replaced
4215:bc6059c5ddc7 4216:e613ffa9f0e6
51 #include "oct-cmplx.h" 51 #include "oct-cmplx.h"
52 #include "oct-env.h" 52 #include "oct-env.h"
53 #include "pathsearch.h" 53 #include "pathsearch.h"
54 #include "str-vec.h" 54 #include "str-vec.h"
55 55
56 #include "Cell.h"
56 #include <defaults.h> 57 #include <defaults.h>
57 #include "defun.h" 58 #include "defun.h"
58 #include "dirfns.h" 59 #include "dirfns.h"
59 #include "error.h" 60 #include "error.h"
60 #include "gripes.h" 61 #include "gripes.h"
235 dir_path p (path); 236 dir_path p (path);
236 237
237 return octave_env::make_absolute (p.find (name), octave_env::getcwd ()); 238 return octave_env::make_absolute (p.find (name), octave_env::getcwd ());
238 } 239 }
239 240
241 // Find all locations of the given file in the path.
242
243 string_vector
244 search_path_for_all_files (const std::string& path, const std::string& name)
245 {
246 dir_path p (path);
247
248 string_vector sv = p.find_all (name);
249
250 int len = sv.length ();
251
252 for (int i = 0; i < len; i++)
253 sv[i] = octave_env::make_absolute (sv[i], octave_env::getcwd ());
254
255 return sv;
256 }
257
258 static string_vector
259 make_absolute (const string_vector& sv)
260 {
261 int len = sv.length ();
262
263 for (int i = 0; i < len; i++)
264 sv[i] = octave_env::make_absolute (sv[i], octave_env::getcwd ());
265
266 return sv;
267 }
268
240 DEFUN (file_in_loadpath, args, , 269 DEFUN (file_in_loadpath, args, ,
241 "-*- texinfo -*-\n\ 270 "-*- texinfo -*-\n\
242 @deftypefn {Built-in Function} {} file_in_loadpath (@var{name})\n\ 271 @deftypefn {Built-in Function} {} file_in_loadpath (@var{file})\n\
243 \n\ 272 @deftypefnx {Built-in Function} {} file_in_loadpath (@var{file}, \"all\")\n\
244 Look up @var{name} in Octave's @code{LOADPATH}.\n\ 273 \n\
274 Return the absolute name name of @var{file} if it can be found in\n\
275 the list of directories specified by @code{LOADPATH}.\n\
276 If no file is found, return an empty matrix.\n\
277 \n\
278 If the second optional argument @code{\"all\"} is supplied, return\n\
279 a cell array containing the list of all files that have the same\n\
280 name in the path. If no files are found, return an empty cell array.\n\
245 @end deftypefn\n\ 281 @end deftypefn\n\
246 @seealso{file_in_path}") 282 @seealso{file_in_path}")
247 { 283 {
248 octave_value_list retval; 284 octave_value retval;
249 285
250 int argc = args.length () + 1; 286 int argc = args.length () + 1;
251 287
252 string_vector argv = args.make_argv ("file_in_loadpath"); 288 string_vector argv = args.make_argv ("file_in_loadpath");
253 289
254 if (error_state) 290 if (error_state)
255 return retval; 291 return retval;
256 292
257 if (argc == 2) 293 if (argc == 2)
258 retval = octave_env::make_absolute (Vload_path_dir_path.find (argv[1]), 294 {
259 octave_env::getcwd ()); 295 std::string fname
296 = octave_env::make_absolute (Vload_path_dir_path.find (argv[1]),
297 octave_env::getcwd ());
298 if (fname.empty ())
299 retval = Matrix ();
300 else
301 retval = fname;
302 }
303 else if (argc == 3 && argv[2] == "all")
304 retval = Cell (make_absolute (Vload_path_dir_path.find_all (argv[1])));
260 else 305 else
261 print_usage ("file_in_loadpath"); 306 print_usage ("file_in_loadpath");
262 307
263 return retval; 308 return retval;
264 } 309 }
265 310
266 DEFUN (file_in_path, args, , 311 DEFUN (file_in_path, args, ,
267 "-*- texinfo -*-\n\ 312 "-*- texinfo -*-\n\
268 @deftypefn {Built-in Function} {} file_in_path (@var{path}, @var{file})\n\ 313 @deftypefn {Built-in Function} {} file_in_path (@var{path}, @var{file})\n\
314 @deftypefnx {Built-in Function} {} file_in_path (@var{path}, @var{file}, \"all\")\n\
269 Return the absolute name name of @var{file} if it can be found in\n\ 315 Return the absolute name name of @var{file} if it can be found in\n\
270 @var{path}. The value of @var{path} should be a colon-separated list of\n\ 316 @var{path}. The value of @var{path} should be a colon-separated list of\n\
271 directories in the format described for the built-in variable\n\ 317 directories in the format described for the built-in variable\n\
272 @code{LOADPATH}.\n\ 318 @code{LOADPATH}. If no file is found, return an empty matrix.\n\
273 \n\
274 If the file cannot be found in the path, an empty matrix is returned.\n\
275 For example,\n\ 319 For example,\n\
276 \n\ 320 \n\
277 @example\n\ 321 @example\n\
278 file_in_path (LOADPATH, \"nargchk.m\")\n\ 322 file_in_path (LOADPATH, \"nargchk.m\")\n\
279 @result{} \"@value{OCTAVEHOME}/share/octave/2.0/m/general/nargchk.m\"\n\ 323 @result{} \"@value{OCTAVEHOME}/share/octave/2.0/m/general/nargchk.m\"\n\
280 @end example\n\ 324 @end example\n\
325 \n\
326 If the third optional argument @code{\"all\"} is supplied, return\n\
327 a cell array containing the list of all files that have the same\n\
328 name in the path. If no files are found, return an empty cell array.\n\
281 @end deftypefn") 329 @end deftypefn")
282 { 330 {
283 octave_value_list retval; 331 octave_value retval;
284 332
285 int argc = args.length () + 1; 333 int argc = args.length () + 1;
286 334
287 string_vector argv = args.make_argv ("file_in_path"); 335 string_vector argv = args.make_argv ("file_in_path");
288 336
296 if (fname.empty ()) 344 if (fname.empty ())
297 retval = Matrix (); 345 retval = Matrix ();
298 else 346 else
299 retval = fname; 347 retval = fname;
300 } 348 }
349 else if (argc == 4 && argv[3] == "all")
350 retval = Cell (make_absolute (search_path_for_all_files (argv[1], argv[2])));
301 else 351 else
302 print_usage ("file_in_path"); 352 print_usage ("file_in_path");
303 353
304 return retval; 354 return retval;
305 } 355 }