Skip to content
This repository has been archived by the owner on Apr 1, 2022. It is now read-only.

Commit

Permalink
Add TruncatingShortener. transpath: translate paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuerig committed Jun 2, 2011
1 parent 2439187 commit 1320f5d
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/CMakeLists.txt
Expand Up @@ -11,12 +11,18 @@ add_definitions(
)
# -Weffc++

set(
Shorteners
MD5Shortener.cpp
TruncatingShortener.cpp
)

add_executable(
dirmeta
dirmeta.cpp
DirectoryMetadata.cpp
Etc.cpp
MD5Shortener.cpp
${Shorteners}
)
target_link_libraries (
dirmeta
Expand All @@ -29,7 +35,7 @@ add_executable(
transpath.cpp
DirectoryMetadata.cpp
Etc.cpp
MD5Shortener.cpp
${Shorteners}
)
target_link_libraries (
transpath
Expand Down
12 changes: 7 additions & 5 deletions src/DirectoryMetadata.cpp
Expand Up @@ -37,16 +37,18 @@ DirectoryMetadata::fromFilesystem(
vector<Entry>& entries(dm->entries_);

for ( fs::directory_iterator it(path), end; it != end; ++it ) {
const string name = it->path().filename().string();
const string entryPath( it->path().string() );

struct stat st;
if ( lstat(name.c_str(), &st) < 0 ) {
util::throw_errno("lstat", it->path());
if ( lstat(entryPath.c_str(), &st) < 0 ) {
util::throw_errno("lstat", entryPath);
}

const string entryName( it->path().filename().string() );

Entry entry = {
longName : name,
shortName : shortener(name),
longName : entryName,
shortName : shortener(entryName),
uid : st.st_uid,
gid : st.st_gid,
mode : st.st_mode,
Expand Down
3 changes: 3 additions & 0 deletions src/DirectoryMetadata.h
Expand Up @@ -14,6 +14,9 @@
class Etc;

class DirectoryMetadata : public boost::noncopyable {
// TODO What traits does this class have to expose
// so that it works, e.g., with BOOST_FOREACH?

public:
typedef boost::shared_ptr<DirectoryMetadata> Ptr;
typedef boost::shared_ptr<Etc> EtcPtr;
Expand Down
37 changes: 37 additions & 0 deletions src/TruncatingShortener.cpp
@@ -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" );
}

21 changes: 21 additions & 0 deletions src/TruncatingShortener.h
@@ -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_


4 changes: 3 additions & 1 deletion src/dirmeta.cpp
Expand Up @@ -2,6 +2,7 @@
#include <iostream>
#include "DirectoryMetadata.h"
#include "MD5Shortener.h"
#include "TruncatingShortener.h"

using namespace std;

Expand All @@ -14,7 +15,8 @@ main( int argc, char* argv[] ) {
DirectoryMetadata::fromMetadataFile(".", argv[2]) :
DirectoryMetadata::fromMetadataFile(".");
} else {
MD5Shortener shortener;
// MD5Shortener shortener;
TruncatingShortener shortener(5);
md = DirectoryMetadata::fromFilesystem(".", shortener);
}

Expand Down
55 changes: 52 additions & 3 deletions src/transpath.cpp
@@ -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;
}

0 comments on commit 1320f5d

Please sign in to comment.