This repository has been archived by the owner on Apr 1, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TruncatingShortener. transpath: translate paths.
- Loading branch information
Showing
7 changed files
with
131 additions
and
11 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
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,37 @@ | ||
|
||
#include "TruncatingShortener.h" | ||
#include <cassert> | ||
#include <boost/lexical_cast.hpp> | ||
|
||
using namespace std; | ||
|
||
TruncatingShortener::TruncatingShortener(size_t maxNameLength) | ||
: maxNameLength_(maxNameLength) | ||
{ | ||
// ensure minimum length | ||
} | ||
|
||
|
||
string | ||
TruncatingShortener::operator()(const std::string& longName) { | ||
if ( longName.size() < maxNameLength_ ) { | ||
shortNames_.insert(longName); | ||
return longName; | ||
} | ||
|
||
string prefix(longName.substr(0, maxNameLength_ - 1)); | ||
string shortName; | ||
|
||
for ( int i = 1; ; ++i ) { | ||
const string counter(boost::lexical_cast<string>(i)); | ||
prefix.resize(maxNameLength_ - counter.size()); | ||
shortName = prefix + counter; | ||
|
||
if ( shortNames_.insert(shortName).second ) { | ||
return shortName; | ||
} | ||
} | ||
|
||
assert( !"Error in TruncatingShortener" ); | ||
} | ||
|
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,21 @@ | ||
|
||
#ifndef TRUNCATING_SHORTENER_INCLUDED_H_ | ||
#define TRUNCATING_SHORTENER_INCLUDED_H_ | ||
|
||
#include "NameShortener.h" | ||
#include <set> | ||
#include <string> | ||
|
||
class TruncatingShortener : public NameShortener { | ||
public: | ||
TruncatingShortener( size_t maxNameLength ); | ||
virtual std::string operator()(const std::string& longName); | ||
|
||
private: | ||
std::set<std::string> shortNames_; | ||
const size_t maxNameLength_; | ||
}; | ||
|
||
#endif // TRUNCATING_SHORTENER_INCLUDED_H_ | ||
|
||
|
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 |
---|---|---|
@@ -1,12 +1,61 @@ | ||
|
||
//#include "DirectoryMetadata.h" | ||
#include "DirectoryMetadata.h" | ||
#include "TruncatingShortener.h" | ||
#include <map> | ||
#include <utility> | ||
#include <boost/filesystem.hpp> | ||
#include <boost/foreach.hpp> | ||
#include <boost/scoped_ptr.hpp> | ||
|
||
using namespace std; | ||
|
||
namespace fs = boost::filesystem; | ||
#define foreach BOOST_FOREACH | ||
|
||
int | ||
main( int /*argc*/, char* /*argv*/[] ) { | ||
main( int argc, char* argv[] ) | ||
{ | ||
if ( argc < 2 ) { | ||
cerr << "Usage: transpath path" << endl; | ||
return -1; | ||
} | ||
|
||
const fs::path path(argv[1]); | ||
boost::scoped_ptr<NameShortener> shortener( new TruncatingShortener(5) ); | ||
DirectoryMetadata::Ptr dm; | ||
|
||
|
||
fs::path prefixPath; | ||
|
||
if ( path.is_absolute() ) { | ||
prefixPath = "/"; | ||
} else { | ||
prefixPath = "."; | ||
} | ||
|
||
fs::path resultPath; | ||
dm = DirectoryMetadata::fromFilesystem(prefixPath.string(), *shortener); | ||
|
||
foreach( fs::path part, path ) { | ||
map<string, DirectoryMetadata::Entry> entryMap; | ||
|
||
// TODO use copy algo | ||
foreach( const DirectoryMetadata::Entry& e, make_pair(dm->cbegin(), dm->cend()) ) { | ||
entryMap[e.longName] = e; | ||
} | ||
|
||
resultPath /= entryMap[part.string()].shortName; | ||
|
||
prefixPath /= part; | ||
if ( fs::is_directory(prefixPath) ) { | ||
shortener.reset( new TruncatingShortener(5) ); | ||
dm = DirectoryMetadata::fromFilesystem(prefixPath.string(), *shortener); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
cout << path << " -> " << resultPath << endl; | ||
|
||
return 0; | ||
} | ||
|