// dir.h
class DirAdapter
{
char separator;
public:
DirAdapter(char sep = '\\') : separator(sep) {};
virtual ~DirAdapter() {}
typedef string CompositeHandle;
typedef string SimpleHandle;
//
void
getHandles(Directory &dir, vector<CompositeHandle> &cvec,
vector<SimpleHandle> &svec);
bool
moveDownTo(Directory &dir, const CompositeHandle &ch);
bool moveUp(Directory &dir);
};
// dir.cpp
void
DirAdapter::getHandles(Directory &dir,
vector<CompositeHandle> &cvec, vector<SimpleHandle> &svec)
{
dir.getSubdirNames(cvec);
dir.getFileNames(svec);
}
bool
DirAdapter::moveDownTo(Directory &dir, const CompositeHandle &ch)
{
string newpath = dir.getCurrentPath();
// append separator if needed
if(newpath[newpath.size()-1] != separator)
newpath += separator;
newpath += ch;
return dir.setCurrentPath(newpath);
}
bool DirAdapter::moveUp(Directory &dir)
{
string newpath = dir.getCurrentPath();
// lop off last directory name from path
// don't try if there isn't a name there
if(newpath.size() >= 2)
{
for(int i = newpath.size()-2; i >= 0; --i)
{
if(newpath[i] == separator)
{
string::iterator j = newpath.begin() + i;
newpath.erase(j, newpath.end());
break;
}
}
return dir.setCurrentPath(newpath);
}
return false;
}