From e9799ba2480582cd4ab873be792fb8f292068986 Mon Sep 17 00:00:00 2001 From: Konstantin Isakov Date: Mon, 2 Feb 2009 15:01:48 +0000 Subject: [PATCH] FsEncoding has got a basename() function. The Lsa module now uses it instead of the system/local one. The implementation of FsEncoding::dirname() was also changed to stop relying on the system version. --- src/fsencoding.cc | 18 ++++++++++++++---- src/fsencoding.hh | 3 +++ src/lsa.cc | 22 +++------------------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/fsencoding.cc b/src/fsencoding.cc index fc3b4347..315638b5 100644 --- a/src/fsencoding.cc +++ b/src/fsencoding.cc @@ -5,7 +5,6 @@ #include #include #include -#include namespace FsEncoding { @@ -26,11 +25,22 @@ char separator() string dirname( string const & str ) { - std::vector< char > tmp( str.size() + 1 ); + size_t x = str.rfind( separator() ); - memcpy( &tmp.front(), str.c_str(), tmp.size() ); + if ( x == string::npos ) + return string( "." ); - return ::dirname( &tmp.front() ); + return string( str, 0, x ); +} + +string basename( string const & str ) +{ + size_t x = str.rfind( separator() ); + + if ( x == string::npos ) + return str; + + return string( str, x + 1 ); } } diff --git a/src/fsencoding.hh b/src/fsencoding.hh index 1ca8b130..68db0e58 100644 --- a/src/fsencoding.hh +++ b/src/fsencoding.hh @@ -26,6 +26,9 @@ char separator(); /// Returns the directory part of the given filename. string dirname( string const & ); +/// Returns the name part of the given filename. +string basename( string const & ); + } #endif diff --git a/src/lsa.cc b/src/lsa.cc index 8c380c13..dbd9ed3b 100644 --- a/src/lsa.cc +++ b/src/lsa.cc @@ -7,6 +7,7 @@ #include "folding.hh" #include "utf8.hh" #include "btreeidx.hh" +#include "fsencoding.hh" #include #include #include @@ -131,23 +132,6 @@ Entry::Entry( File::Class & f ) read * sizeof( uint16_t ) ); } -#ifdef __WIN32 - -// Win32 features the usual Posix basename() which may modify its input. We -// provide our local implementation here instead. - -char const * basename( char const * n ) -{ - char const * lastSep = strrchr( n, '\\' ); - - if ( !lastSep ) - return n; - - return lastSep + 1; -} - -#endif - class LsaDictionary: public BtreeIndexing::BtreeDictionary { File::Class idx; @@ -159,7 +143,7 @@ public: vector< string > const & dictionaryFiles ); virtual string getName() throw() - { return basename( string( getDictionaryFilenames()[ 0 ] ).c_str() ); } + { return FsEncoding::basename( getDictionaryFilenames()[ 0 ] ); } virtual map< Dictionary::Property, string > getProperties() throw() { return map< Dictionary::Property, string >(); } @@ -479,7 +463,7 @@ vector< sptr< Dictionary::Class > > Format::makeDictionaries( { // Building the index - initializing.indexingDictionary( basename( i->c_str() ) ); + initializing.indexingDictionary( FsEncoding::basename( *i ) ); File::Class idx( indexFile, "wb" );