05 мая 2006

Имя файла и его расширение

Почему-то boost::filesystem::path не понимает wide chars :(
Пришлось писать свои шаблоны:

template <typename StringType>
typename StringType::const_iterator FindFileName(const StringType& FilePath)
{
typename StringType::const_reverse_iterator rend(FilePath.rend()), rit(FilePath.rbegin());
for (; rit != rend; ++rit)
{
switch (*rit)
{
case '/':
case '\\':
return rit.base();
}
}

return FilePath.begin();
}

template <typename StringType>
typename StringType::const_iterator FindFileExtension(const StringType& FilePath)
{
typename StringType::const_reverse_iterator rend(FilePath.rend()), rit(FilePath.rbegin());
for (; rit != rend; ++rit)
{
switch (*rit)
{
case '/':
case '\\':
return FilePath.end();

case '.':
return rit.base();
}
}

return FilePath.end();
}

template <typename StringType>
StringType ExtractFileName(const StringType& FilePath)
{
return StringType(FindFileName(FilePath), FilePath.end());
}

template <typename StringType>
StringType ExtractFileExtension(const StringType& FilePath)
{
return StringType(FindFileExtension(FilePath), FilePath.end());
}

Использование:

std::wstring Path = L"c:\\foo\\bar.xml";
std::wcout << L"file: " << ExtractFileName(Path) << L", ext: " << ExtractFileExtension(Path) << std::endl;


Используемо с basic_string, vector и другими контейнерами.
Понимает как char, так и wchar_t.

1 комментарий:

CyberZX комментирует...

В boost 1.34 ситуация улучшилась.
Значительно переработан дизайн библиотеки, убраны эти жуткие проверки путей, которые на корректных значениях кидали исключения, тип path сделан шаблонным, для поддержки юникода ну и еще много чего приятного добавили. Вот полный список:

Version 1.34.0
* Internationalization, provided by class templates basic_path, basic_filesystem_error, and basic_directory_iterator.
* Simplification of the path interface, including elimination of distinction between native and generic formats, and separation of name checking functionality from general path functionality.
* Rationalization of predicate function design, including the addition of several new functions.
* Preservation of existing user code whenever possible. Deprecated features (symbolic_link_exists(), etc.) remain available for legacy code.
* Clearer specification, by reference to [POSIX-01], the ISO/IEEE Single Unix Standard, with provisions for Windows and other operating systems.
* New functions status, symlink_status, is_file, is_symlink, create_hard_link, create_symlink, path member and non-member swap, path inserter, path extractor, additional path relational and "/" operator overloads, additional path member template functions taking iterator arguments.
* More efficient operations when iterating over directories.
* Separation within the implementation of the distinction between the native operating system path syntax and API. This is important for CYGWIN users because it allows them to build for either the Windows or POSIX API's.
* Numerous small implementation fixes.