-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic support to renaming of archived items (BitArchiveEditor)
Lots of refactoring of the internal classes are still needed. Close issue #43.
- Loading branch information
Showing
8 changed files
with
143 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// This is an open source non-commercial project. Dear PVS-Studio, please check it. | ||
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com | ||
|
||
/* | ||
* bit7z - A C++ static library to interface with the 7-zip DLLs. | ||
* Copyright (c) 2014-2020 Riccardo Ostani - All Rights Reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 2 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* Bit7z is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with bit7z; if not, see https://www.gnu.org/licenses/. | ||
*/ | ||
|
||
#ifndef BITARCHIVEEDITOR_HPP | ||
#define BITARCHIVEEDITOR_HPP | ||
|
||
#include "../include/bitarchivecreator.hpp" | ||
#include "../include/bittypes.hpp" | ||
|
||
namespace bit7z { | ||
using std::vector; | ||
|
||
class BitArchiveEditor : public BitArchiveCreator { | ||
public: | ||
BitArchiveEditor( const Bit7zLibrary& lib, | ||
const tstring& in_file, | ||
const BitInOutFormat& format, | ||
const tstring& password = TSTRING( "" ) ); | ||
|
||
void renameItem( unsigned index, const tstring& new_name ); | ||
|
||
void renameItem( const tstring& old_name, const tstring& new_name ); | ||
|
||
void applyChanges(); | ||
|
||
private: | ||
unique_ptr< BitInputArchive > mInputArchive; | ||
RenamedItems mRenameMap; | ||
}; | ||
} | ||
|
||
#endif //BITARCHIVEEDITOR_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* bit7z - A C++ static library to interface with the 7-zip DLLs. | ||
* Copyright (c) 2014-2020 Riccardo Ostani - All Rights Reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 2 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* Bit7z is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with bit7z; if not, see https://www.gnu.org/licenses/. | ||
*/ | ||
|
||
#include "../include/bitexception.hpp" | ||
#include "../include/bitarchiveeditor.hpp" | ||
|
||
#include "../include/fileupdatecallback.hpp" | ||
|
||
using bit7z::BitArchiveEditor; | ||
using bit7z::BitException; | ||
using bit7z::BitInFormat; | ||
|
||
BitArchiveEditor::BitArchiveEditor( const Bit7zLibrary& lib, | ||
const tstring& in_file, | ||
const BitInOutFormat& format, | ||
const tstring& password ) | ||
: BitArchiveCreator( lib, format, password ), | ||
mInputArchive( std::make_unique<BitInputArchive>( *this, in_file ) ) { | ||
mUpdateMode = true; | ||
} | ||
|
||
void BitArchiveEditor::renameItem( unsigned index, const tstring& new_name ) { | ||
if ( index >= mInputArchive->itemsCount() ) { | ||
throw BitException( "Invalid index " + std::to_string(index), std::make_error_code( std::errc::invalid_argument ) ); | ||
} | ||
mRenameMap[ index ] = new_name; | ||
} | ||
|
||
void BitArchiveEditor::renameItem( const tstring& old_name, const tstring& new_name ) { | ||
for ( const auto& archiveItem : *mInputArchive ) { | ||
if ( archiveItem.name() == old_name ) { | ||
mRenameMap[ archiveItem.index() ] = new_name; | ||
return; | ||
} | ||
} | ||
throw BitException("Could not find the file in the archive", | ||
std::make_error_code( std::errc::no_such_file_or_directory ), { old_name } ); | ||
} | ||
|
||
void BitArchiveEditor::applyChanges() { | ||
auto archive_path = mInputArchive->getArchivePath(); | ||
mInputArchive.reset(); | ||
CMyComPtr< UpdateCallback > update_callback = new FileUpdateCallback( *this, {} ); | ||
update_callback->setRenamedItems( std::move( mRenameMap ) ); | ||
BitArchiveCreator::compressToFile( archive_path, update_callback ); | ||
mRenameMap.clear(); | ||
mInputArchive = std::make_unique<BitInputArchive>( *this, archive_path ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters