[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Couple comments about image_viewer and path behavior
From: |
John W. Eaton |
Subject: |
Re: Couple comments about image_viewer and path behavior |
Date: |
Mon, 27 Aug 2007 22:59:52 -0400 |
On 26-Aug-2007, Daniel J Sebald wrote:
| Quentin Spencer wrote:
|
| > I have always wondered why we had a separate path for images. I'm all
| > for getting rid of it.
|
| I'm on board for that. The only issue I wonder about is that if someone has
a path in which there are very large numbers of database files it might slow
down searching for M files?
|
| John W. Eaton wrote:
|
| > In addition to .m files, Matlab searches "MATLABPATH" for files read
| > by load, opened by fopen, imread, etc. I suppose doing the same for
| > Octave would be OK, but to avoid confusion, I don't think we should do
| > so without a warning for files that have relative names and that are
| > not found in the current directory.
|
| I sort of see what you are getting at, but on further thought, why exactly?
If the user enters
|
| imread("./image1.jpg");
| imread("../image2.jpg");
|
| it is almost implicit that he or she has the current working directory in
mind. But not so perhaps for
|
| imread("xray/tibia2.jpg");
|
| The rule is always look from working directory first, right? So Octave would
look for "./xray/tibia2.jpg" then any other directories (sub "xray") afterward.
Yes, this is what Matlab apparently does. With the following patch,
I think Octave will do the same.
jwe
liboctave/ChangeLog:
2007-08-27 John W. Eaton <address@hidden>
* oct-env.cc (octave_env::rooted_relative_pathname,
octave_env::do_rooted_relative_pathname): New functions.
* oct-env.h: Provide decls.
src/ChangeLog:
2007-08-27 John W. Eaton <address@hidden>
* load-path.cc (load_path::do_find_file): Also files with non
rooted relative names.
* load-save.cc (find_file_to_load): Likewise. Also handle
appending .mat to files with relative names.
Index: liboctave/oct-env.cc
===================================================================
RCS file: /cvs/octave/liboctave/oct-env.cc,v
retrieving revision 1.28
diff -u -u -r1.28 oct-env.cc
--- liboctave/oct-env.cc 21 Nov 2006 19:14:14 -0000 1.28
+++ liboctave/oct-env.cc 28 Aug 2007 02:51:49 -0000
@@ -110,6 +110,13 @@
? instance->do_absolute_pathname (s) : false;
}
+bool
+octave_env::rooted_relative_pathname (const std::string& s)
+{
+ return (instance_ok ())
+ ? instance->do_rooted_relative_pathname (s) : false;
+}
+
std::string
octave_env::base_pathname (const std::string& s)
{
@@ -261,6 +268,29 @@
return false;
}
+bool
+octave_env::do_rooted_relative_pathname (const std::string& s) const
+{
+ size_t len = s.length ();
+
+ if (len == 0)
+ return false;
+
+ if (len == 1 && s[0] == '.')
+ return true;
+
+ if (len > 1 && s[0] == '.' && file_ops::is_dir_sep (s[1]))
+ return true;
+
+ if (len == 2 && s[0] == '.' && s[1] == '.')
+ return true;
+
+ if (len > 2 && s[0] == '.' && s[1] == '.' && file_ops::is_dir_sep (s[2]))
+ return true;
+
+ return false;
+}
+
// Return the `basename' of the pathname in STRING (the stuff after
// the last directory separator). If STRING is not a full pathname,
// simply return it.
Index: liboctave/oct-env.h
===================================================================
RCS file: /cvs/octave/liboctave/oct-env.h,v
retrieving revision 1.6
diff -u -u -r1.6 oct-env.h
--- liboctave/oct-env.h 27 Oct 2006 01:45:56 -0000 1.6
+++ liboctave/oct-env.h 28 Aug 2007 02:51:49 -0000
@@ -40,6 +40,8 @@
static bool absolute_pathname (const std::string& s);
+ static bool rooted_relative_pathname (const std::string& s);
+
static std::string base_pathname (const std::string& s);
static std::string make_absolute (const std::string& s,
@@ -75,6 +77,8 @@
bool do_absolute_pathname (const std::string& s) const;
+ bool do_rooted_relative_pathname (const std::string& s) const;
+
std::string do_base_pathname (const std::string& s) const;
std::string do_make_absolute (const std::string& s,
Index: src/file-io.cc
===================================================================
RCS file: /cvs/octave/src/file-io.cc,v
retrieving revision 1.187
diff -u -u -r1.187 file-io.cc
--- src/file-io.cc 28 May 2007 05:46:28 -0000 1.187
+++ src/file-io.cc 28 Aug 2007 02:51:53 -0000
@@ -394,7 +394,9 @@
{
std::string fname = file_ops::tilde_expand (name);
- if (! (md & std::ios::out || octave_env::absolute_pathname (fname)))
+ if (! (md & std::ios::out
+ || octave_env::absolute_pathname (fname)
+ || octave_env::rooted_relative_pathname (fname)))
{
file_stat fs (fname);
Index: src/load-path.cc
===================================================================
RCS file: /cvs/octave/src/load-path.cc,v
retrieving revision 1.20
diff -u -u -r1.20 load-path.cc
--- src/load-path.cc 24 Aug 2007 15:36:50 -0000 1.20
+++ src/load-path.cc 28 Aug 2007 02:51:53 -0000
@@ -743,40 +743,49 @@
{
std::string retval;
- if (octave_env::absolute_pathname (file))
+ if (file.find ('/') != NPOS)
{
- file_stat fs (file);
+ if (octave_env::absolute_pathname (file)
+ || octave_env::rooted_relative_pathname (file))
+ {
+ file_stat fs (file);
- if (fs.exists ())
- return file;
- }
+ if (fs.exists ())
+ return file;
+ }
+ else
+ {
+ for (const_dir_info_list_iterator p = dir_info_list.begin ();
+ p != dir_info_list.end ();
+ p++)
+ {
+ std::string tfile = p->dir_name + file_ops::dir_sep_str + file;
- std::string dir_name;
+ file_stat fs (tfile);
- for (const_dir_info_list_iterator p = dir_info_list.begin ();
- p != dir_info_list.end ();
- p++)
+ if (fs.exists ())
+ return tfile;
+ }
+ }
+ }
+ else
{
- string_vector all_files = p->all_files;
+ for (const_dir_info_list_iterator p = dir_info_list.begin ();
+ p != dir_info_list.end ();
+ p++)
+ {
+ string_vector all_files = p->all_files;
- octave_idx_type len = all_files.length ();
+ octave_idx_type len = all_files.length ();
- for (octave_idx_type i = 0; i < len; i++)
- {
- if (all_files[i] == file)
+ for (octave_idx_type i = 0; i < len; i++)
{
- dir_name = p->dir_name;
-
- goto done;
+ if (all_files[i] == file)
+ return p->dir_name + file_ops::dir_sep_str + file;
}
}
}
- done:
-
- if (! dir_name.empty ())
- retval = dir_name + file_ops::dir_sep_str + file;
-
return retval;
}
Index: src/load-save.cc
===================================================================
RCS file: /cvs/octave/src/load-save.cc,v
retrieving revision 1.227
diff -u -u -r1.227 load-save.cc
--- src/load-save.cc 27 Jun 2007 18:34:13 -0000 1.227
+++ src/load-save.cc 28 Aug 2007 02:51:53 -0000
@@ -594,7 +594,8 @@
{
std::string fname = name;
- if (! octave_env::absolute_pathname (fname))
+ if (! (octave_env::absolute_pathname (fname)
+ || octave_env::rooted_relative_pathname (fname)))
{
file_stat fs (fname);
@@ -612,8 +613,14 @@
}
}
- if (fname.rfind (".") == NPOS)
+ size_t dot_pos = fname.rfind (".");
+ size_t sep_pos = fname.find_last_of (file_ops::dir_sep_chars);
+
+ if (dot_pos == NPOS || (sep_pos != NPOS && dot_pos < sep_pos))
{
+ // Either no '.' in name or no '.' appears after last directory
+ // separator.
+
file_stat fs (fname);
if (! (fs.exists () && fs.is_reg ()))