winix/core/dircontainer.cpp

210 lines
3.6 KiB
C++
Executable File

/*
* This file is a part of CMSLU -- Content Management System like Unix
* and is not publicly distributed
*
* Copyright (c) 2008-2009, Tomasz Sowa
* All rights reserved.
*
*/
#include "dircontainer.h"
#include "log.h"
DirContainer::DirContainer()
{
is_root = false;
}
DirContainer::Iterator DirContainer::GetRoot()
{
if( !is_root )
return table.end();
return root_iter;
}
DirContainer::Iterator DirContainer::Begin()
{
return table.begin();
}
DirContainer::Iterator DirContainer::End()
{
return table.end();
}
DirContainer::SizeType DirContainer::Size()
{
return table.size();
}
bool DirContainer::Empty()
{
return table.empty();
}
DirContainer::Iterator DirContainer::PushBack(const Item & item)
{
if( item.parent_id == -1 && is_root )
{
log << log1 << "DirCont: more than one root dir - skipped, id: " << item.id << logend;
return root_iter;
}
Iterator last_iter = table.insert(table.end(), item);
if( item.parent_id == -1 )
{
is_root = true;
root_iter = last_iter;
}
log << log2 << "DirCont: added dir, url: " << item.url << ", id: " << item.id << ", parent_id: " << item.parent_id << logend;
table_id.insert( std::make_pair(last_iter->id, last_iter) );
table_parent.insert( std::make_pair(last_iter->parent_id, last_iter) );
log << log3 << "DirCont: added indexes to dir, id: " << last_iter->id << ", parent_id: " << last_iter->parent_id << logend;
return last_iter;
}
void DirContainer::Clear()
{
table.clear();
table_id.clear();
table_parent.clear();
is_root = false;
}
DirContainer::Iterator DirContainer::FindId(long id)
{
TableId::iterator i = table_id.find(id);
if( i == table_id.end() )
return table.end();
return i->second;
}
DirContainer::ParentIterator DirContainer::ParentBegin()
{
return table_parent.begin();
}
DirContainer::ParentIterator DirContainer::ParentEnd()
{
return table_parent.end();
}
DirContainer::ParentSizeType DirContainer::ParentSize()
{
return table_parent.size();
}
bool DirContainer::ParentEmpty()
{
return table_parent.empty();
}
DirContainer::ParentIterator DirContainer::FindFirstParent(long parent)
{
ParentIterator i = table_parent.lower_bound(parent);
if( i == table_parent.end() || i->first != parent )
return table_parent.end();
return i;
}
DirContainer::ParentIterator DirContainer::NextParent(ParentIterator i)
{
if( i == table_parent.end() )
return table_parent.end();
long parent = i->first;
++i;
if( i == table_parent.end() || i->first != parent )
return table_parent.end();
return i;
}
bool DirContainer::DelById(long id)
{
TableId::iterator i = table_id.find(id);
if( i == table_id.end() )
{
log << log1 << "DirCont: delete: there is no directory with id: " << id << logend;
return false;
}
long parent_id = i->second->parent_id;
TableParent::iterator z = table_parent.lower_bound(parent_id);
bool found = false;
for( ; z != table_parent.end() && z->first == parent_id ; ++z )
{
if( z->second == i->second )
{
log << log2 << "DirCont: deleted directory id: " << id << ", url: " << i->second->url;
if( i->second->parent_id == -1 )
{
log << log2 << " (root directory)";
is_root = false;
}
log << log2 << logend;
table.erase(i->second);
log << log3 << "DirCont: deleted indexes into directory id: " << id << logend;
table_id.erase(i);
table_parent.erase(z);
found = true;
break;
}
}
if( !found )
{
log << log1 << "DirCont: can't find an index_parent into directory id: " << id << ", url: " << i->second->url << " (deleting skipped)" << logend;
return false;
}
return true;
}