Skip to content

Commit

Permalink
Strip leading & trailing whitespace from file paths. Refs #7277
Browse files Browse the repository at this point in the history
Allows for copying in extra characters by accident when finding a file.
  • Loading branch information
martyngigg committed Jul 12, 2013
1 parent 1da3d36 commit cddab9f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
8 changes: 4 additions & 4 deletions Code/Mantid/Framework/API/inc/MantidAPI/FileFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ namespace Mantid
class MANTID_API_DLL FileFinderImpl
{
public:
std::string getFullPath(const std::string& fName)const;
std::string getFullPath(const std::string& filename)const;
std::string getPath(const std::vector<IArchiveSearch_sptr>& archs, const std::set<std::string>& filename, const std::vector<std::string>& extensions)const;
/// DO NOT USE! MADE PUBLIC FOR TESTING ONLY.
std::string makeFileName(const std::string& hint, const Kernel::InstrumentInfo& instrument)const;
void setCaseSensitive(const bool cs);
bool getCaseSensitive() const;
std::string findRun(const std::string& hint,const std::set<std::string> *exts)const;
std::string findRun(const std::string& hint,const std::vector<std::string> &exts = std::vector<std::string>())const;
std::vector<std::string> findRuns(const std::string& hint)const;
std::string findRun(const std::string& hintstr,const std::set<std::string> *exts)const;
std::string findRun(const std::string& hintstr,const std::vector<std::string> &exts = std::vector<std::string>())const;
std::vector<std::string> findRuns(const std::string& hintstr)const;
/// DO NOT USE! MADE PUBLIC FOR TESTING ONLY.
const Kernel::InstrumentInfo getInstrument(const std::string& hint) const;
/// DO NOT USE! MADE PUBLIC FOR TESTING ONLY.
Expand Down
23 changes: 14 additions & 9 deletions Code/Mantid/Framework/API/src/FileFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/FacilityInfo.h"
#include "MantidKernel/Glob.h"
#include "MantidKernel/InstrumentInfo.h"
#include "MantidKernel/LibraryManager.h"
#include "MantidKernel/Glob.h"
#include "MantidKernel/Strings.h"

#include <Poco/Path.h>
#include <Poco/File.h>
Expand Down Expand Up @@ -100,12 +101,13 @@ namespace Mantid

/**
* Return the full path to the file given its name
* @param fName :: A full file name (without path) including extension
* @param filename :: A file name (without path) including extension
* @return The full path if the file exists and can be found in one of the search locations
* or an empty string otherwise.
*/
std::string FileFinderImpl::getFullPath(const std::string& fName) const
std::string FileFinderImpl::getFullPath(const std::string& filename) const
{
std::string fName = Kernel::Strings::strip(filename);
g_log.debug() << "getFullPath(" << fName << ")\n";
// If this is already a full path, nothing to do
if (Poco::Path(fName).isAbsolute())
Expand Down Expand Up @@ -352,15 +354,16 @@ namespace Mantid
/**
* Find the file given a hint. If the name contains a dot(.) then it is assumed that it is already a file stem
* otherwise calls makeFileName internally.
* @param hint :: The name hint, format: [INSTR]1234[.ext]
* @param hintstr :: The name hint, format: [INSTR]1234[.ext]
* @param exts :: Optional list of allowed extensions. Only those extensions found in both
* facilities extension list and exts will be used in the search. If an extension is given in hint
* this argument is ignored.
* @return The full path to the file or empty string if not found
*/
std::string FileFinderImpl::findRun(const std::string& hint, const std::set<std::string> *exts) const
std::string FileFinderImpl::findRun(const std::string& hintstr, const std::set<std::string> *exts) const
{
g_log.debug() << "set findRun(\'" << hint << "\', exts[" << exts->size() << "])\n";
std::string hint = Kernel::Strings::strip(hintstr);
g_log.debug() << "set findRun(\'" << hintstr << "\', exts[" << exts->size() << "])\n";
if (hint.empty())
return "";
std::vector<std::string> exts_v;
Expand Down Expand Up @@ -412,8 +415,9 @@ namespace Mantid
return "";
}

std::string FileFinderImpl::findRun(const std::string& hint,const std::vector<std::string> &exts)const
std::string FileFinderImpl::findRun(const std::string& hintstr,const std::vector<std::string> &exts)const
{
std::string hint = Kernel::Strings::strip(hintstr);
g_log.debug() << "vector findRun(\'" << hint << "\', exts[" << exts.size() << "])\n";

//if partial filename or run number is not supplied, return here
Expand Down Expand Up @@ -570,15 +574,16 @@ namespace Mantid

/**
* Find a list of files file given a hint. Calls findRun internally.
* @param hint :: Comma separated list of hints to findRun method.
* @param hintstr :: Comma separated list of hints to findRun method.
* Can also include ranges of runs, e.g. 123-135 or equivalently 123-35.
* Only the beginning of a range can contain an instrument name.
* @return A vector of full paths or empty vector
* @throw std::invalid_argument if the argument is malformed
* @throw Exception::NotFoundError if a file could not be found
*/
std::vector<std::string> FileFinderImpl::findRuns(const std::string& hint) const
std::vector<std::string> FileFinderImpl::findRuns(const std::string& hintstr) const
{
std::string hint = Kernel::Strings::strip(hintstr);
g_log.debug() << "findRuns hint = " << hint << "\n";
std::vector < std::string > res;
Poco::StringTokenizer hints(hint, ",",
Expand Down
20 changes: 12 additions & 8 deletions Code/Mantid/Framework/API/src/FileProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
//-----------------------------------------------------------------
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/FileFinder.h"
#include "MantidKernel/FileValidator.h"
#include "MantidKernel/DirectoryValidator.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/DirectoryValidator.h"
#include "MantidKernel/FacilityInfo.h"
#include "MantidKernel/FileValidator.h"
#include "MantidKernel/Strings.h"

#include <Poco/Path.h>
#include <Poco/File.h>
#include <cctype>
Expand Down Expand Up @@ -124,36 +126,38 @@ bool FileProperty::isOptional() const
*/
std::string FileProperty::setValue(const std::string & propValue)
{
std::string strippedValue = Kernel::Strings::strip(propValue);

// Empty value is allowed if optional
if( propValue.empty() )
if( strippedValue.empty() )
{
PropertyWithValue<std::string>::setValue("");
return isEmptyValueValid();
}

// If this looks like an absolute path then don't do any searching but make sure the
// directory exists for a Save property
if( Poco::Path(propValue).isAbsolute() )
if( Poco::Path(strippedValue).isAbsolute() )
{
std::string error("");
if( isSaveProperty() )
{
error = createDirectory(propValue);
error = createDirectory(strippedValue);
if( !error.empty() ) return error;
}

return PropertyWithValue<std::string>::setValue(propValue);
return PropertyWithValue<std::string>::setValue(strippedValue);
}

std::string errorMsg("");
// For relative paths, differentiate between load and save types
if( isLoadProperty() )
{
errorMsg = setLoadProperty(propValue);
errorMsg = setLoadProperty(strippedValue);
}
else
{
errorMsg = setSaveProperty(propValue);
errorMsg = setSaveProperty(strippedValue);
}
return errorMsg;
}
Expand Down

0 comments on commit cddab9f

Please sign in to comment.