Skip to content
Browse files

Cleaned up indexing of entries by longName.

Thanks to Jonathan Wakely on accu-general.
  • Loading branch information...
1 parent 1320f5d commit 87b13eef4c112256c205226c9adbbe8d44b21b6a @mschuerig committed
Showing with 28 additions and 17 deletions.
  1. +5 −5 src/DirectoryMetadata.h
  2. +2 −1 src/TruncatingShortener.cpp
  3. +7 −10 src/transpath.cpp
  4. +14 −1 src/util.h
View
10 src/DirectoryMetadata.h
@@ -29,9 +29,9 @@ class DirectoryMetadata : public boost::noncopyable {
mode_t mode;
time_t mtime;
};
- typedef std::vector<Entry>::const_iterator EntryIter;
-
-
+ typedef Entry value_type;
+ typedef std::vector<Entry>::const_iterator const_iterator;
+
static const std::string metadataFilename;
DirectoryMetadata(const EtcPtr etc);
@@ -64,11 +64,11 @@ class DirectoryMetadata : public boost::noncopyable {
void write() const;
void write(std::ostream& os) const;
- EntryIter cbegin() const {
+ const_iterator begin() const {
return entries_.begin();
}
- EntryIter cend() const {
+ const_iterator end() const {
return entries_.end();
}
View
3 src/TruncatingShortener.cpp
@@ -8,7 +8,7 @@ using namespace std;
TruncatingShortener::TruncatingShortener(size_t maxNameLength)
: maxNameLength_(maxNameLength)
{
- // ensure minimum length
+ // TODO ensure minimum length
}
@@ -24,6 +24,7 @@ TruncatingShortener::operator()(const std::string& longName) {
for ( int i = 1; ; ++i ) {
const string counter(boost::lexical_cast<string>(i));
+ assert( counter.size() <= maxNameLength_ ); // TODO throw something sensible
prefix.resize(maxNameLength_ - counter.size());
shortName = prefix + counter;
View
17 src/transpath.cpp
@@ -1,6 +1,7 @@
#include "DirectoryMetadata.h"
#include "TruncatingShortener.h"
+#include "util.h"
#include <map>
#include <utility>
#include <boost/filesystem.hpp>
@@ -11,6 +12,7 @@ using namespace std;
namespace fs = boost::filesystem;
#define foreach BOOST_FOREACH
+
int
main( int argc, char* argv[] )
{
@@ -23,7 +25,6 @@ main( int argc, char* argv[] )
boost::scoped_ptr<NameShortener> shortener( new TruncatingShortener(5) );
DirectoryMetadata::Ptr dm;
-
fs::path prefixPath;
if ( path.is_absolute() ) {
@@ -36,18 +37,14 @@ main( int argc, char* argv[] )
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;
- }
-
+ map<string, DirectoryMetadata::Entry> entryMap =
+ util::index_by(*dm, &DirectoryMetadata::Entry::longName);
+
resultPath /= entryMap[part.string()].shortName;
prefixPath /= part;
if ( fs::is_directory(prefixPath) ) {
- shortener.reset( new TruncatingShortener(5) );
+ shortener.reset( new TruncatingShortener(5) ); // TODO I don't want to remember the args
dm = DirectoryMetadata::fromFilesystem(prefixPath.string(), *shortener);
} else {
break;
@@ -55,7 +52,7 @@ main( int argc, char* argv[] )
}
cout << path << " -> " << resultPath << endl;
-
+
return 0;
}
View
15 src/util.h
@@ -2,7 +2,8 @@
#ifndef UTIL_H_INCLUDED_
#define UTIL_H_INCLUDED_
-#include <errno.h>
+#include <cerrno>
+#include <map>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/system/error_code.hpp>
@@ -25,6 +26,18 @@ throw_errno(const std::string& msg, int err = errno) {
);
}
+template<typename T, typename R>
+std::map<R, typename T::value_type>
+index_by(const T& t, R T::value_type::*ptr_to_mem)
+{
+ std::map<R, typename T::value_type> idx;
+ for ( typename T::const_iterator i = t.begin(), end = t.end();
+ i != end;
+ ++i ) {
+ idx[((*i).*ptr_to_mem)] = *i;
+ }
+ return idx;
+}
} // namespace util

0 comments on commit 87b13ee

Please sign in to comment.
Something went wrong with that request. Please try again.