📄 file_functions.hpp
字号:
, processheap_allocator<char_2_type>
, 1024
> buffer_t;
buffer_t buffer(static_cast<ss_typename_type_k buffer_t::size_type>(size));
DWORD dw;
if(!::ReadFile(h.get(), &buffer[0], buffer.size(), &dw, NULL))
{
STLSOFT_THROW_X(windows_exception("Read operation failed", ::GetLastError()));
}
else
{
contents.assign(&buffer[0], dw);
return size;
}
}
}
return 0;
}
#if !defined(STLSOFT_COMPILER_IS_DMC) && \
!defined(STLSOFT_COMPILER_IS_MWERKS) && \
( !defined(STLSOFT_COMPILER_IS_MSVC) || \
_MSC_VER != 1300)
template< ss_typename_param_k S2
>
inline ws_uint64_t load_text_file(ws_char_a_t const* fileName, S2 &contents)
{
return load_text_file_impl<ws_char_a_t const*, S2>(fileName, contents);
}
template< ss_typename_param_k S2
>
inline ws_uint64_t load_text_file(ws_char_w_t const* fileName, S2 &contents)
{
return load_text_file_impl<ws_char_w_t const*, S2>(fileName, contents);
}
#if 0
template< ss_typename_param_k S2
>
inline ws_uint64_t load_text_file(char fileName[], S2 &contents)
{
return load_text_file_impl<ws_char_a_t const*, S2>(fileName, contents);
}
template< ss_typename_param_k S2
>
inline ws_uint64_t load_text_file(ws_char_w_t *fileName, S2 &contents)
{
return load_text_file_impl<ws_char_w_t const*, S2>(fileName, contents);
}
#endif /* 0 */
#endif /* compiler */
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
#if 0
template< ss_typename_param_k S
>
struct trim_trailing_carriage_return
{
public:
S operator ()(S const& s)
{
ss_size_t len = stlsoft_ns_qual(c_str_len)(s);
if( len > 0 &&
'\r' == s[len])
{
return s;
}
return S(stlsoft_ns_qual(c_str_ptr)(s), len - 1);
}
};
#endif /* 0 */
template< ss_typename_param_k CH
, ss_typename_param_k C
>
void readlines_impl(CH const* p, ss_size_t len, C &container)
{
typedef CH char_t;
typedef ss_typename_type_k C::value_type value_t;
char_t const* p0 = p;
char_t const* p1 = p0;
char_t const* end = p + len;
while(end != stlsoft_ns_qual(find_next_token)(p0, p1, end, static_cast<char_t>('\n')))
{
if( p1 > p0 &&
'\r' == p1[-1])
{
--p1;
}
container.push_back(value_t(p0, static_cast<ws_size_t>(p1 - p0)));
if('\r' == *p1)
{
++p1;
}
}
}
/** \brief Reads the lines of a text-file into a sequence container
*
* \ingroup group__library__filesystem
*
* \param fileName The name of the text-file to load
* \param container Reference to the sequence container to which each line
* read from \c fileName will be appended (via its push_back() method)
*
* \returns The \c container reference
*
\code
std::vector<std::string> lines;
winstl::readlines("mytextfile.ext", lines);
\endcode
*
*
* \remarks The container type's <code>value_type</code> must provide
* a two-parameter constructor whose parameters types are
* <code>char_type const*</code> (where <code>char_type</code> is the
* <code>value_type</code> of the string type <code>S</code>) and
* <code>size_t</code>, indicating the pointer to the beginning and the
* length of the C-style string that represents the line read from the
* file denoted by <code>fileName</code>.
*
* \note When used with a compiler that does not support partial template
* specialisation, the use of string types for which explicit
* specialisations are not defined will fail. Hence, using
* <code>stlsoft::simple_string</code> (which is the specialisation
* <code>stlsoft::basic_simple_string<char></code>) will succeed
* because a specialisation of <code>stlsoft::string_traits</code> exists
* for that type. The same applies for
* <code>stlsoft::simple_wstring</code>, <code>std::string</code> and
* <code>std::wstring</code>. However, if you attempt to use a
* specialisation of a string class template for which an explicit
* specialisation of <code>stlsoft::string_traits</code> does not exist
* then you will experience a compile-time error in the implementation
* of <code>winstl::load_text_file_impl()</code>. To correct this, you
* must either provide an explicit specialisation of
* <code>stlsoft::string_traits</code> for your type, or use a type for
* which a specialisation of <code>stlsoft::string_traits</code> does
* exist.<br><br>This problem does not occur for compilers that support
* partial template specialisation.
*/
template< ss_typename_param_k S
, ss_typename_param_k C
>
C &readlines(S const& fileName, C &container)
{
S contents;
S delim;
// NOTE: doing these as characters skips the issue of ANSI vs Unicode
delim.append(1, '\n');
load_text_file(fileName, contents);
#if 0
stlsoft::string_tokeniser< S /* stlsoft::basic_string_view<ss_typename_type_k stlsoft::string_traits<S>::char_type> */
, S
, stlsoft::string_tokeniser_ignore_blanks<false>
> tokens(contents, delim);
std::transform(tokens.begin(), tokens.end(), std::back_inserter(container), trim_trailing_carriage_return</* ss_typename_type_k */ C::value_type>());
#else /* ? 0 */
readlines_impl(contents.c_str(), contents.size(), container);
#endif /* 0 */
return container;
}
/* /////////////////////////////////////////////////////////////////////////
* Unit-testing
*/
#ifdef STLSOFT_UNITTEST
# include "./unittest/file_functions_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace winstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* !WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -