All pastes #2066041 Raw Edit

Mine

public cpp v1 · immutable
#2066041 ·published 2011-05-20 23:45 UTC
rendered paste body
#define BOOST_FILESYSTEM_VERSION 3#define BOOST_FILESYSTEM_NO_DEPRECATED#include <boost/filesystem.hpp>namespace fs = boost::filesystem;#include <array>#include <iomanip>#include <iostream>#include <locale>#include <regex>#include <string>using namespace std;#include <Windows.h>wstring g_version	= L"VSClean 0.3.4, (c) 2008-2010 GATS";wstring g_usage		= L"Usage: vsclean [-(h|help|?)] [folder]";regex g_extensionsExp( "\\.(ncb|sdf|old)$" );std::array<string,1> g_files = { "UpgradeLog.XML" };std::array<string,11> g_folders = { 	"Debug",	"Release",	"Debug DLL",	"Release DLL",	"ReleaseDLL",	"DebugDLL",	"Static Debug",	"Static Release",	"x64",	"ipch",	"_UpgradeReport_Files"};struct CommandArgs {	fs::path	rootPath_;	bool		showHelp_;};/** Locate possible switches and folders in the command line. */CommandArgs parse_commandline( int argc, char* argv[] ) {	CommandArgs ca = { fs::path("."), false };	regex switches( "-(h|help|\\?)" );	for( int i = 1; i < argc; ++i ) {		if( regex_match( argv[i], switches ) ) {			ca.showHelp_ = true;		}		else {			ca.rootPath_ = fs::path( argv[1] );		}	}		ca.rootPath_ = fs::system_complete( ca.rootPath_ );	return ca;}/** Remove a read-only directory. */void remove_directory( fs::path const& dir ) {	WIN32_FILE_ATTRIBUTE_DATA fad;	BOOL resFrom = GetFileAttributesEx( dir.string<std::wstring>().c_str(), GetFileExInfoStandard, &fad );	if( !resFrom )		return;	SetFileAttributes( dir.string<std::wstring>().c_str(), FILE_ATTRIBUTE_NORMAL );	if( !RemoveDirectory( dir.string<std::wstring>().c_str() ) )		wcout << "Cannot delete: " << dir.filename().c_str() << endl;}/** Remove a read-only file. */void remove_file( fs::path const& file ) {	WIN32_FILE_ATTRIBUTE_DATA fad;	BOOL resFrom = GetFileAttributesEx( file.string<std::wstring>().c_str(), GetFileExInfoStandard, &fad );	if( !resFrom )		return;	SetFileAttributes( file.string<std::wstring>().c_str(), FILE_ATTRIBUTE_NORMAL );	if( !DeleteFile( file.string<std::wstring>().c_str() ) )		wcout << "Cannot delete: " << file.filename().c_str() << endl;}/**	Remove all the files in the specified folder.	@param dir [in] the folder to remove.	@param fileSize [out] the size of all files removed.	@return the number of files deleted.*/unsigned remove_folder( fs::path const& dir, boost::uintmax_t& fileSize ) {	unsigned nFiles = 0;	fs::directory_iterator endIter;	for( fs::directory_iterator dirIter( dir ); dirIter != endIter; ++dirIter ) {		if( is_directory( *dirIter ) ) {			nFiles += remove_folder( *dirIter, fileSize );		}		else if( is_regular_file( *dirIter ) ) {				wcout << L"Removing: " << dirIter->path().filename().c_str() << endl;				fileSize += fs::file_size( dirIter->path() );				++nFiles;				remove_file( dirIter->path() );		}	}	remove_directory( dir );	return nFiles;}/** Scan a folder system for Debug and Release directories.*/boost::uintmax_t scan( fs::path const& dir ) {	boost::uintmax_t fileSize = 0;	fs::directory_iterator endIter;	for( fs::directory_iterator dirIter( dir ); dirIter != endIter; ++dirIter ) {		if( is_directory( *dirIter ) ) {			auto dirName = dirIter->path().filename();			if( find( g_folders.cbegin(), g_folders.cend(), dirName ) != g_folders.cend()				) {				wcout << L"Removing directory: " << dirIter->path().c_str() << endl;				wcout << L" -> " << remove_folder( dirIter->path(), fileSize ) << L" files deleted" << endl;			}			else				fileSize += scan( *dirIter );		}		else if( is_regular_file( *dirIter ) ) {			auto ext = dirIter->path().extension();			if( regex_match( ext.string(), g_extensionsExp ) ||				find( g_files.cbegin(), g_files.cend(), dirIter->path().filename() ) != g_files.cend() ) {				wcout << L"Removing: " << dirIter->path().filename().c_str() << endl;				fileSize += fs::file_size( dirIter->path() );				remove_file( dirIter->path().string<std::wstring>().c_str() );			}		}	}	return fileSize;}int main( int argc, char* argv[] ) {	CommandArgs args = parse_commandline( argc, argv );	wcout << g_version << endl;	// display help is requested	if( args.showHelp_ ) {		wcout << g_usage << endl;		return 0;	}	// check path	if( !fs::exists( args.rootPath_ ) ) {		wcout << L"Path <" << args.rootPath_ << L"> not found" << endl;		return 1;	}	if( !fs::is_directory( args.rootPath_ ) ) {		wcout << L"Path <" << args.rootPath_ << L"> is not a folder." << endl;		return 2;	}	// Scan directory list.	wcout << L"Scanning: " << args.rootPath_.string().c_str() << endl;	std::locale here("");	cout.imbue( here );	boost::uintmax_t recovered = scan( args.rootPath_ );	// report bytes recovered and in multiples of bits.	wcout << recovered << L" bytes recovered";	boost::uintmax_t const KIBIBYTE = 1024;	boost::uintmax_t const MEBIBYTE = 1024 * 1024;	boost::uintmax_t const GIBIBYTE = 1024 * 1024 * 1024;	wcout << setprecision(2) << fixed;	if( recovered >= GIBIBYTE ) {		wcout << L" (" << recovered / static_cast<double>(GIBIBYTE) << L" GiB)";	}	else if( recovered >= MEBIBYTE ) {		wcout << L" (" << recovered / static_cast<double>(MEBIBYTE) << L" MiB)";	}	else if( recovered >= KIBIBYTE ) {		wcout << L" (" << recovered / static_cast<double>(KIBIBYTE) << L" KiB)";	}	wcout << endl;	return 0;}