Enhanced directory detection in a way we don't actually need yet

This commit is contained in:
ridiculousfish
2012-02-17 18:08:08 -08:00
parent ac0b97a571
commit 1bedc16544
4 changed files with 19 additions and 14 deletions

View File

@@ -66,11 +66,12 @@ static const wchar_t * const highlight_var[] =
/** /**
Tests if the specified string is the prefix of any valid path in the system. Tests if the specified string is the prefix of any valid path in the system.
\require_dir Whether the valid path must be a directory
\return zero it this is not a valid prefix, non-zero otherwise \return zero it this is not a valid prefix, non-zero otherwise
*/ */
// PCA DOES_IO // PCA DOES_IO
static bool is_potential_path( const wcstring &cpath ) static bool is_potential_path( const wcstring &cpath, bool require_dir = false )
{ {
ASSERT_IS_BACKGROUND_THREAD(); ASSERT_IS_BACKGROUND_THREAD();
@@ -121,12 +122,11 @@ static bool is_potential_path( const wcstring &cpath )
} }
if( !has_magic && cleaned_path.length() ) if( !has_magic && ! cleaned_path.empty() )
{ {
int must_be_dir = 0; bool must_be_full_dir = cleaned_path[cleaned_path.length()-1] == L'/';
DIR *dir; DIR *dir;
must_be_dir = cleaned_path[cleaned_path.length()-1] == L'/'; if( must_be_full_dir )
if( must_be_dir )
{ {
dir = wopendir( cleaned_path ); dir = wopendir( cleaned_path );
res = !!dir; res = !!dir;
@@ -147,9 +147,10 @@ static bool is_potential_path( const wcstring &cpath )
else if( (dir = wopendir( dir_name)) ) else if( (dir = wopendir( dir_name)) )
{ {
wcstring ent; wcstring ent;
while (wreaddir(dir, ent)) bool is_dir;
while (wreaddir(dir, ent, &is_dir))
{ {
if( ent == base_name ) if (string_prefixes_string(base_name, ent) && (! require_dir || is_dir))
{ {
res = true; res = true;
break; break;
@@ -979,7 +980,9 @@ void highlight_shell( const wchar_t * const buff, int *color, int pos, wcstring_
{ {
for( i=tok_begin-buff; i < (tok_end-buff); i++ ) for( i=tok_begin-buff; i < (tok_end-buff); i++ )
{ {
color[i] |= HIGHLIGHT_VALID_PATH; // Don't color HIGHLIGHT_ERROR because it looks dorky. For example, trying to cd into a non-directory would show an underline and also red.
if (! (color[i] & HIGHLIGHT_ERROR))
color[i] |= HIGHLIGHT_VALID_PATH;
} }
} }
} }

View File

@@ -658,7 +658,7 @@ wchar_t *tok_first( const wchar_t *str )
CHECK( str, 0 ); CHECK( str, 0 );
tok_init( &t, str, 0 ); tok_init( &t, str, TOK_SQUASH_ERRORS);
switch( tok_last_type( &t ) ) switch( tok_last_type( &t ) )
{ {

View File

@@ -82,13 +82,15 @@ void wutil_destroy()
{ {
} }
std::wstring *wreaddir(DIR *dir, std::wstring &outPath) bool wreaddir(DIR *dir, std::wstring &outPath, bool *is_dir)
{ {
struct dirent *d = readdir( dir ); struct dirent *d = readdir( dir );
if ( !d ) return 0; if ( !d ) return false;
outPath = str2wcstring(d->d_name); outPath = str2wcstring(d->d_name);
return &outPath; if (is_dir)
*is_dir = (d->d_type == DT_DIR);
return true;
} }

View File

@@ -113,7 +113,7 @@ wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path);
/** /**
Wide character version of readdir() Wide character version of readdir()
*/ */
std::wstring *wreaddir(DIR *dir, std::wstring &outPath); bool wreaddir(DIR *dir, std::wstring &outPath, bool *outIsDirectory = NULL);
/** /**
Wide character version of dirname() Wide character version of dirname()