221 lines
3.1 KiB
C++
Executable File
221 lines
3.1 KiB
C++
Executable File
/*
|
|
* This file is a part of CMSLU -- Content Management System like Unix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2008, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
|
|
#include "dirs.h"
|
|
|
|
|
|
void Dirs::Clear()
|
|
{
|
|
dir_table.Clear();
|
|
|
|
// !! temporarily not clearing
|
|
//root.Clear();
|
|
root.id = -1;
|
|
}
|
|
|
|
|
|
void Dirs::ReadDirs()
|
|
{
|
|
Clear();
|
|
|
|
db.GetDirs(dir_table);
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Dirs::GetDir(const std::string & name, long parent, Item ** item)
|
|
{
|
|
DirContainer::ParentIterator i = dir_table.FindFirstParent(parent);
|
|
|
|
for( ; i!=dir_table.ParentEnd() ; i = dir_table.NextParent(i) )
|
|
if( i->second->url == name )
|
|
{
|
|
*item = &(*i->second);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
bool Dirs::ExtractName(const char * & s, std::string & name)
|
|
{
|
|
name.clear();
|
|
|
|
// skipping first slashes (can be more than one)
|
|
for( ; *s == '/' ; ++s);
|
|
|
|
for( ; *s != 0 && *s != '/' ; ++s)
|
|
name += *s;
|
|
|
|
return !name.empty();
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Dirs::GetDir(const std::string & path, Item ** item)
|
|
{
|
|
long parent = -1;
|
|
std::string name;
|
|
const char * s = path.c_str();
|
|
Item * pitem = &root;
|
|
|
|
while( ExtractName(s, name) )
|
|
{
|
|
if( !GetDir(name, parent, &pitem) )
|
|
return false;
|
|
}
|
|
|
|
*item = pitem;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Dirs::GetDirId(const std::string & path, long * id)
|
|
{
|
|
Item * pitem;
|
|
|
|
if( !GetDir(path, &pitem) )
|
|
return false;
|
|
|
|
*id = pitem->id;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool Dirs::GetDirId(const std::string & name, long parent, long * id)
|
|
{
|
|
Item * pitem;
|
|
|
|
if( !GetDir(name, parent, &pitem) )
|
|
return false;
|
|
|
|
*id = pitem->id;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
bool Dirs::IsDir(long id)
|
|
{
|
|
if( id == -1 )
|
|
// root directory
|
|
return true;
|
|
|
|
DirContainer::Iterator i = dir_table.FindId(id);
|
|
|
|
if( i == dir_table.End() )
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Dirs::GetDirChilds(long parent, std::vector<Item> & childs_table)
|
|
{
|
|
if( !IsDir(parent) )
|
|
return false;
|
|
|
|
DirContainer::ParentIterator i = dir_table.FindFirstParent(parent);
|
|
|
|
for( ; i != dir_table.ParentEnd() ; i = dir_table.NextParent(i) )
|
|
childs_table.push_back( *i->second );
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// dodatkowo moze metoda AppendPath dodajaca sciezke do biezacego stringa?
|
|
// albo tutaj stringa nie czyscic?
|
|
// O(m * log n) (m- how many parts are in 'id')
|
|
bool Dirs::MakePath(long id, std::string & path)
|
|
{
|
|
DirContainer::Iterator i;
|
|
|
|
path.clear();
|
|
|
|
while( true )
|
|
{
|
|
if( id == -1 )
|
|
return true;
|
|
|
|
i = dir_table.FindId(id);
|
|
|
|
if( i == dir_table.End() )
|
|
return false;
|
|
|
|
id = i->parent_id;
|
|
path.insert(path.begin(), '/');
|
|
path.insert(0, i->url);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// with exceptions
|
|
|
|
Item * Dirs::GetDir(const std::string & path)
|
|
{
|
|
Item * pitem;
|
|
|
|
if( !GetDir(path, &pitem) )
|
|
throw Error(Error::incorrect_dir);
|
|
|
|
return pitem;
|
|
}
|
|
|
|
|
|
Item * Dirs::GetDir(const std::string & name, long parent)
|
|
{
|
|
Item * pitem;
|
|
|
|
if( !GetDir(name, parent, &pitem) )
|
|
throw Error(Error::incorrect_dir);
|
|
|
|
return pitem;
|
|
|
|
}
|
|
|
|
|
|
long Dirs::GetDirId(const std::string & path)
|
|
{
|
|
long id;
|
|
|
|
if( !GetDirId(path, &id) )
|
|
throw Error(Error::incorrect_dir);
|
|
|
|
return id;
|
|
}
|
|
|
|
|
|
long Dirs::GetDirId(const std::string & name, long parent)
|
|
{
|
|
long id;
|
|
|
|
if( !GetDirId(name, parent, &id) )
|
|
throw Error(Error::incorrect_dir);
|
|
|
|
return id;
|
|
}
|
|
|
|
|
|
|