Boost logo

Boost Users :

Subject: [Boost-users] [filesystem] : How to get a parametrized basic_string from filesystem::path
From: Vincent R. (forumer_at_[hidden])
Date: 2014-08-28 10:34:37


Hi,

I am trying to modify an existing code to keep the possibility to use
string or wstring, so I have changed the
definition to parametrize it as shown below (Path inherits from path):

template <class TChar>
bool GetFileList(const std::basic_string<TChar>& i_strBaseDir,
                                                        std::vector<std::basic_string<TChar> >& o_FileList)
{
        boost::system::error_code ec;
        Path thePath(i_strBaseDir);
        boost::filesystem::directory_iterator iter(thePath, ec);
        if (! ec)
     {
         boost::filesystem::directory_iterator iter_end;
         for (; iter != iter_end; ++ iter)
         {
                        boost::system::error_code ec; // This is to avoid an exception in
case of any error
                        if(!boost::filesystem::is_directory(iter->path(), ec)) //find file
and not directory
                        {
                                std::basic_string<TChar> strFile = iter->path().????); // HOW TO DO
IT
                                    o_FileList.push_back(strFile);
                        }
         }
     }
        return (o_FileList.size() != 0);
}

My problem is with the following line :

std::basic_string<TChar> strFile = iter->path().??????;

I have solved like that (without testing it) :

template <typename CharT, typename T>
std::basic_string<CharT> string_cast(const T& r)
{
   std::basic_ostringstream<CharT> stream;
   stream.imbue(std::locale::classic());
   stream << r;
   return stream.str();
}

std::basic_string<TChar> strFile = string_cast<TChar>(iter->path());

but since it uses some stream we might loose some performance.

I have found another string_cast here :
http://codereview.stackexchange.com/questions/1205/c-string-cast-template-function
that might be better in terms of performance.

Do you have some other suggestion ?

Wouldn't be interesting to add a boost::string_cast that would allow to
convert from templated methods from/to string/wstring ?

Thanks


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net