Skip to content

Commit

Permalink
Move string utility functions to stringutil
Browse files Browse the repository at this point in the history
  • Loading branch information
rikyoz committed Oct 15, 2023
1 parent d804484 commit 4b47e22
Show file tree
Hide file tree
Showing 30 changed files with 211 additions and 171 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ set( HEADERS
src/internal/stdinputitem.hpp
src/internal/streamextractcallback.hpp
src/internal/streamutil.hpp
src/internal/stringutil.hpp
src/internal/updatecallback.hpp
src/internal/util.hpp
src/internal/windows.hpp )
Expand Down Expand Up @@ -156,8 +157,8 @@ set( SOURCES
src/internal/renameditem.cpp
src/internal/stdinputitem.cpp
src/internal/streamextractcallback.cpp
src/internal/stringutil.cpp
src/internal/updatecallback.cpp
src/internal/util.cpp
src/internal/windows.cpp )

# library output file name options
Expand Down
3 changes: 2 additions & 1 deletion src/bit7zlibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
#include "bit7zlibrary.hpp"
#include "bitexception.hpp"
#include "bitformat.hpp"
#include "internal/com.hpp"
#include "internal/guids.hpp"
#include "internal/util.hpp"
#include "internal/stringutil.hpp"

#include <7zip/Archive/IArchive.h>

Expand Down
2 changes: 1 addition & 1 deletion src/bitarchiveeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "internal/fsitem.hpp"
#include "internal/renameditem.hpp"
#include "internal/stdinputitem.hpp"
#include "internal/util.hpp"
#include "internal/stringutil.hpp"

namespace bit7z {

Expand Down
2 changes: 1 addition & 1 deletion src/bitarchiveitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "bitarchiveitem.hpp"
#include "internal/fsutil.hpp"
#include "internal/util.hpp"
#include "internal/stringutil.hpp"

// For checking posix file attributes
#include <sys/stat.h>
Expand Down
1 change: 1 addition & 0 deletions src/bitinputarchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "internal/fixedbufferextractcallback.hpp"
#include "internal/streamextractcallback.hpp"
#include "internal/opencallback.hpp"
#include "internal/stringutil.hpp"
#include "internal/util.hpp"

#ifdef BIT7Z_AUTO_FORMAT
Expand Down
2 changes: 1 addition & 1 deletion src/bititemsvector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "internal/bufferitem.hpp"
#include "internal/fsindexer.hpp"
#include "internal/stdinputitem.hpp"
#include "internal/util.hpp"
#include "internal/stringutil.hpp"

using namespace bit7z;
using filesystem::FilesystemItem;
Expand Down
2 changes: 1 addition & 1 deletion src/bitoutputarchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#include "internal/archiveproperties.hpp"
#include "internal/cbufferoutstream.hpp"
#include "internal/cmultivolumeoutstream.hpp"
#include "internal/fsutil.hpp"
#include "internal/genericinputitem.hpp"
#include "internal/stringutil.hpp"
#include "internal/updatecallback.hpp"
#include "internal/util.hpp"

Expand Down
3 changes: 2 additions & 1 deletion src/bitpropvariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
#include "biterror.hpp"
#include "bitpropvariant.hpp"
#include "internal/dateutil.hpp"
#include "internal/windows.hpp"

#if defined( BIT7Z_USE_NATIVE_STRING ) && defined( _WIN32 ) // Windows
#define BSTR_TO_TSTRING( bstr ) std::wstring( bstr, ::SysStringLen( bstr ) )
#else
#include "internal/util.hpp"
#include "internal/stringutil.hpp"
#define BSTR_TO_TSTRING( bstr ) bit7z::narrow( bstr, SysStringLen( bstr ) )
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/bittypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "bittypes.hpp"

#if defined( _WIN32 ) && !defined( BIT7Z_USE_NATIVE_STRING )
#include "internal/util.hpp"
#include "internal/stringutil.hpp"
#endif

namespace bit7z {
Expand Down
1 change: 1 addition & 0 deletions src/internal/bufferextractcallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "internal/bufferextractcallback.hpp"
#include "internal/cbufferoutstream.hpp"
#include "internal/fs.hpp"
#include "internal/stringutil.hpp"
#include "internal/util.hpp"

using namespace std;
Expand Down
1 change: 1 addition & 0 deletions src/internal/bufferitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "internal/bufferitem.hpp"
#include "internal/cbufferinstream.hpp"
#include "internal/dateutil.hpp"
#include "internal/stringutil.hpp"
#include "internal/util.hpp"

using std::vector;
Expand Down
2 changes: 1 addition & 1 deletion src/internal/cfileinstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "bitexception.hpp"
#include "internal/cfileinstream.hpp"
#include "internal/util.hpp"
#include "internal/stringutil.hpp"

#if defined( _WIN32 ) && defined( __GLIBCXX__ ) && defined( _WIO_DEFINED )
#include "internal/fsutil.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/internal/cfileoutstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "bitexception.hpp"
#include "internal/cfileoutstream.hpp"
#include "internal/util.hpp"
#include "internal/stringutil.hpp"

#if defined( _WIN32 ) && defined( __GLIBCXX__ ) && defined( _WIO_DEFINED )
#include "internal/fsutil.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/internal/extractcallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "bitexception.hpp"
#include "internal/extractcallback.hpp"
#include "internal/operationcategory.hpp"
#include "internal/util.hpp"
#include "internal/stringutil.hpp"

namespace bit7z {

Expand Down
1 change: 1 addition & 0 deletions src/internal/fileextractcallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "bitexception.hpp"
#include "internal/fileextractcallback.hpp"
#include "internal/fsutil.hpp"
#include "internal/stringutil.hpp"
#include "internal/util.hpp"

using namespace std;
Expand Down
1 change: 1 addition & 0 deletions src/internal/fsitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "internal/cfileinstream.hpp"
#include "internal/csymlinkinstream.hpp"
#include "internal/fsitem.hpp"
#include "internal/stringutil.hpp"
#include "internal/util.hpp"

namespace bit7z { // NOLINT(modernize-concat-nested-namespaces)
Expand Down
2 changes: 1 addition & 1 deletion src/internal/fsutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#endif

#include "internal/fsutil.hpp"
#include "internal/util.hpp"
#include "internal/stringutil.hpp"

using namespace std;

Expand Down
2 changes: 1 addition & 1 deletion src/internal/genericinputitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

#include "internal/genericinputitem.hpp"
#include "util.hpp"
#include "internal/stringutil.hpp"

namespace bit7z {

Expand Down
1 change: 1 addition & 0 deletions src/internal/opencallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "bitexception.hpp"
#include "internal/cfileinstream.hpp"
#include "internal/opencallback.hpp"
#include "internal/stringutil.hpp"
#include "internal/util.hpp"

namespace bit7z {
Expand Down
2 changes: 1 addition & 1 deletion src/internal/renameditem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "internal/dateutil.hpp"
#include "internal/fsutil.hpp"
#include "internal/renameditem.hpp"
#include "internal/util.hpp"
#include "internal/stringutil.hpp"

namespace bit7z {

Expand Down
1 change: 1 addition & 0 deletions src/internal/stdinputitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "internal/cstdinstream.hpp"
#include "internal/dateutil.hpp"
#include "internal/stdinputitem.hpp"
#include "internal/stringutil.hpp"
#include "internal/util.hpp"

using std::istream;
Expand Down
3 changes: 2 additions & 1 deletion src/internal/util.cpp → src/internal/stringutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

#include <locale>

#include "internal/util.hpp"
#include "internal/stringutil.hpp"

#ifdef _WIN32
#include <Windows.h>
#ifdef BIT7Z_USE_SYSTEM_CODEPAGE
#define CODEPAGE CP_ACP
#define CODEPAGE_WC_FLAGS WC_NO_BEST_FIT_CHARS
Expand Down
79 changes: 79 additions & 0 deletions src/internal/stringutil.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* bit7z - A C++ static library to interface with the 7-zip shared libraries.
* Copyright (c) 2014-2023 Riccardo Ostani - All Rights Reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#ifndef STRINGUTIL_HPP
#define STRINGUTIL_HPP

#include "bittypes.hpp"
#include "internal/fsutil.hpp"

namespace bit7z {

#if defined( BIT7Z_USE_NATIVE_STRING ) && defined( _WIN32 )
// On Windows, with native strings enabled, strings are already wide!
# define WIDEN( tstr ) tstr
#else
# define WIDEN( tstr ) bit7z::widen(tstr)

auto narrow( const wchar_t* wideString, size_t size ) -> std::string;

auto widen( const std::string& narrowString ) -> std::wstring;
#endif

inline auto path_to_tstring( const fs::path& path ) -> tstring {
/* In an ideal world, we should only use fs::path's string< tchar >() function for converting a path to a tstring.
* However, MSVC converts paths to std::string using the system codepage instead of UTF-8,
* which is the default encoding of bit7z. */
#if defined( _WIN32 ) && defined( BIT7Z_USE_NATIVE_STRING )
return path.wstring();
#elif defined( _WIN32 ) && defined( BIT7Z_USE_SYSTEM_CODEPAGE )
/* If we encounter a path with Unicode characters, MSVC will throw an exception
* while converting from a fs::path to std::string if any character is invalid in the system codepage.
* Hence, here we use bit7z's own string conversion function, which substitutes invalid Unicode characters
* with '?' characters. */
const auto& native_path = path.native();
return narrow( native_path.c_str(), native_path.size() );
#else
return path.u8string();
#endif
}

#if defined( _MSC_VER ) && !defined( BIT7Z_USE_NATIVE_STRING ) && !defined( BIT7Z_USE_SYSTEM_CODEPAGE )
#define PATH_FROM_TSTRING( str ) fs::u8path( str )
#else
#define PATH_FROM_TSTRING( str ) fs::path{ str }
#endif

inline auto tstring_to_path( const tstring& str ) -> fs::path {
#if defined( _WIN32 ) && defined( BIT7Z_AUTO_PREFIX_LONG_PATHS )
auto result = PATH_FROM_TSTRING( str );
if ( filesystem::fsutil::should_format_long_path( result ) ) {
result = filesystem::fsutil::format_long_path( result );
}
return result;
#else
// By default, MSVC treats strings as encoded using the system codepage, but bit7z uses UTF-8.
return PATH_FROM_TSTRING( str );
#endif
}

inline auto path_to_wide_string( const fs::path& path ) -> std::wstring {
#if defined( _MSC_VER ) || !defined( BIT7Z_USE_STANDARD_FILESYSTEM )
return path.wstring();
#else
/* On some compilers and platforms (e.g., GCC before v12.3),
* the direct conversion of the fs::path to wstring might throw an exception due to unicode characters.
* So we simply convert to tstring, and then widen it if necessary. */
return WIDEN( path.string< tchar >() );
#endif
}

} // namespace bit7z

#endif //STRINGUTIL_HPP
1 change: 1 addition & 0 deletions src/internal/updatecallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "internal/cfileoutstream.hpp"
#include "internal/updatecallback.hpp"
#include "internal/stringutil.hpp"
#include "internal/util.hpp"

namespace bit7z {
Expand Down
Loading

0 comments on commit 4b47e22

Please sign in to comment.