2008-12-10 05:42:49 +01:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-02-12 17:30:49 +01:00
|
|
|
* Copyright (c) 2008-2014, Tomasz Sowa
|
2008-12-10 05:42:49 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2008-12-10 05:42:49 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dircontainer.h"
|
2009-04-21 22:50:55 +02:00
|
|
|
#include "log.h"
|
2008-12-21 22:17:09 +01:00
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
std::wstring DirContainer::dir_etc = L"etc";
|
|
|
|
std::wstring DirContainer::dir_var = L"var";
|
2009-11-16 18:43:23 +01:00
|
|
|
|
|
|
|
|
2008-12-10 05:42:49 +01:00
|
|
|
DirContainer::DirContainer()
|
|
|
|
{
|
2008-12-22 17:03:03 +01:00
|
|
|
is_root = false;
|
2009-11-16 00:55:11 +01:00
|
|
|
is_etc = false;
|
2010-11-04 17:46:19 +01:00
|
|
|
is_var = false;
|
2008-12-22 17:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DirContainer::Iterator DirContainer::GetRoot()
|
|
|
|
{
|
|
|
|
if( !is_root )
|
|
|
|
return table.end();
|
2009-02-06 14:12:03 +01:00
|
|
|
|
|
|
|
return root_iter;
|
2008-12-10 05:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-16 00:55:11 +01:00
|
|
|
DirContainer::Iterator DirContainer::GetEtc()
|
|
|
|
{
|
|
|
|
if( !is_etc )
|
|
|
|
return table.end();
|
|
|
|
|
|
|
|
return etc_iter;
|
|
|
|
}
|
|
|
|
|
2008-12-10 05:42:49 +01:00
|
|
|
|
2010-11-04 17:46:19 +01:00
|
|
|
DirContainer::Iterator DirContainer::GetVar()
|
|
|
|
{
|
|
|
|
if( !is_var )
|
|
|
|
return table.end();
|
|
|
|
|
|
|
|
return var_iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-10 05:42:49 +01:00
|
|
|
DirContainer::Iterator DirContainer::Begin()
|
|
|
|
{
|
|
|
|
return table.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DirContainer::Iterator DirContainer::End()
|
|
|
|
{
|
|
|
|
return table.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DirContainer::SizeType DirContainer::Size()
|
|
|
|
{
|
added: uptime winix function prints how many sessions there are
changed: functions for text/numbers conversions
int Toi(const std::string & str, int base = 10);
int Toi(const std::wstring & str, int base = 10);
int Toi(const char * str, int base = 10);
int Toi(const wchar_t * str, int base = 10);
long Tol(const std::string & str, int base = 10);
long Tol(const std::wstring & str, int base = 10);
long Tol(const char * str, int base = 10);
long Tol(const wchar_t * str, int base = 10);
template<class CharType>
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10);
const wchar_t * Toa(unsigned int value, int base = 10);
const wchar_t * Toa(unsigned long value, int base = 10);
const wchar_t * Toa(int value, int base = 10);
const wchar_t * Toa(long value, int base = 10);
void Toa(int value, std::string & res, int base = 10, bool clear = true);
void Toa(long value, std::string & res, int base = 10, bool clear = true);
void Toa(int value, std::wstring & res, int base = 10, bool clear = true);
void Toa(long value, std::wstring & res, int base = 10, bool clear = true);
added: HtmlTextStream class (files htmltextstream.cpp htmltextstream.h in templates)
this is a special stream for automatically escaping html tags
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@682 e52654a7-88a9-db11-a3e9-0013d4bc506e
2010-11-25 02:34:46 +01:00
|
|
|
return table.size(); // !! warning: it has O(n)
|
2008-12-10 05:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DirContainer::Empty()
|
|
|
|
{
|
|
|
|
return table.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
bool DirContainer::IsNameOfSpecialFolder(const std::wstring & name)
|
|
|
|
{
|
|
|
|
return name == dir_etc || name == dir_var;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-16 00:55:11 +01:00
|
|
|
// looking for '/etc'
|
|
|
|
// 'root' is found beforehand
|
2009-11-16 18:43:23 +01:00
|
|
|
// CheckSpecialFolder() may not find everything (when the first is a special folder and then the root)
|
2009-11-16 00:55:11 +01:00
|
|
|
void DirContainer::FindSpecialFolders()
|
|
|
|
{
|
|
|
|
is_etc = false;
|
2010-11-04 17:46:19 +01:00
|
|
|
is_var = false;
|
2009-11-16 00:55:11 +01:00
|
|
|
|
|
|
|
if( !is_root )
|
|
|
|
return;
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
DirContainer::ParentIterator i = FindFirstChild(root_iter->id);
|
2009-11-16 00:55:11 +01:00
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
for( ; i!=ParentEnd() ; i = NextChild(i) )
|
2009-11-16 00:55:11 +01:00
|
|
|
{
|
2009-11-16 18:43:23 +01:00
|
|
|
if( i->second->url == dir_etc )
|
2009-11-16 00:55:11 +01:00
|
|
|
{
|
|
|
|
is_etc = true;
|
|
|
|
etc_iter = i->second;
|
2010-11-04 17:46:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( i->second->url == dir_var )
|
|
|
|
{
|
|
|
|
is_var = true;
|
|
|
|
var_iter = i->second;
|
2009-11-16 00:55:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-22 17:03:03 +01:00
|
|
|
|
2009-11-16 18:43:23 +01:00
|
|
|
// this is used with PushBack() method
|
|
|
|
void DirContainer::CheckSpecialFolder(const Item & item, Iterator iter)
|
|
|
|
{
|
|
|
|
if( item.parent_id == -1 )
|
|
|
|
{
|
|
|
|
is_root = true;
|
|
|
|
root_iter = iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !is_root )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( item.parent_id==root_iter->id && item.url==dir_etc )
|
|
|
|
{
|
|
|
|
is_etc = true;
|
|
|
|
etc_iter = iter;
|
2011-01-05 22:24:11 +01:00
|
|
|
log << log2 << "DirCont: added special folder: /etc" << logend;
|
2009-11-16 18:43:23 +01:00
|
|
|
}
|
2010-11-04 17:46:19 +01:00
|
|
|
|
|
|
|
if( item.parent_id==root_iter->id && item.url==dir_var )
|
|
|
|
{
|
|
|
|
is_var = true;
|
|
|
|
var_iter = iter;
|
2011-01-05 22:24:11 +01:00
|
|
|
log << log2 << "DirCont: added special folder: /var" << logend;
|
2010-11-04 17:46:19 +01:00
|
|
|
}
|
|
|
|
|
2009-11-16 18:43:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-20 00:13:21 +02:00
|
|
|
DirContainer::Iterator DirContainer::PushBack(const Item & item)
|
2008-12-10 05:42:49 +01:00
|
|
|
{
|
2009-02-06 14:12:03 +01:00
|
|
|
if( item.parent_id == -1 && is_root )
|
|
|
|
{
|
|
|
|
log << log1 << "DirCont: more than one root dir - skipped, id: " << item.id << logend;
|
2009-04-20 00:13:21 +02:00
|
|
|
return root_iter;
|
2009-02-06 14:12:03 +01:00
|
|
|
}
|
2008-12-22 17:03:03 +01:00
|
|
|
|
2009-02-06 14:12:03 +01:00
|
|
|
Iterator last_iter = table.insert(table.end(), item);
|
2009-11-16 18:43:23 +01:00
|
|
|
CheckSpecialFolder(item, last_iter);
|
2008-12-22 17:03:03 +01:00
|
|
|
|
2008-12-31 14:36:46 +01:00
|
|
|
log << log2 << "DirCont: added dir, url: " << item.url << ", id: " << item.id << ", parent_id: " << item.parent_id << logend;
|
2008-12-10 05:42:49 +01:00
|
|
|
|
2009-02-06 14:12:03 +01:00
|
|
|
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;
|
2009-04-20 00:13:21 +02:00
|
|
|
|
|
|
|
return last_iter;
|
2008-12-10 05:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-03-15 02:47:26 +01:00
|
|
|
bool DirContainer::ChangeParent(long dir_id, long new_parent_id)
|
|
|
|
{
|
|
|
|
Iterator i = FindId(dir_id);
|
|
|
|
|
|
|
|
if( i == table.end() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( i->parent_id == new_parent_id )
|
|
|
|
return true; // nothing to do
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
ParentIterator p = FindFirstChild(i->parent_id);
|
2010-03-15 02:47:26 +01:00
|
|
|
bool found = false;
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
for( ; p != table_parent.end() ; p = NextChild(p) )
|
2010-03-15 02:47:26 +01:00
|
|
|
{
|
|
|
|
if( p->second->id == dir_id )
|
|
|
|
{
|
|
|
|
table_parent.erase(p);
|
|
|
|
log << log3 << "DirCont: removed parent index to dir: " << i->id << logend;
|
|
|
|
|
|
|
|
i->parent_id = new_parent_id;
|
|
|
|
table_parent.insert( std::make_pair(new_parent_id, i) );
|
|
|
|
log << log3 << "DirCont: added parent index to dir, id: " << i->id << ", parent_id: " << i->parent_id << logend;
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
if( IsNameOfSpecialFolder(i->url) )
|
2010-03-15 02:47:26 +01:00
|
|
|
FindSpecialFolders();
|
|
|
|
|
|
|
|
break; // that iterator (p) is only one
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !found )
|
|
|
|
log << log1 << "DirCont: cannot find parent_id: " << i->parent_id << " in parent indexes" << logend;
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
void DirContainer::Clear()
|
|
|
|
{
|
|
|
|
table.clear();
|
|
|
|
table_id.clear();
|
|
|
|
table_parent.clear();
|
2009-02-06 14:12:03 +01:00
|
|
|
is_root = false;
|
2008-12-10 05:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
DirContainer::ParentIterator DirContainer::FindFirstChild(long parent)
|
2008-12-10 05:42:49 +01:00
|
|
|
{
|
|
|
|
ParentIterator i = table_parent.lower_bound(parent);
|
|
|
|
|
|
|
|
if( i == table_parent.end() || i->first != parent )
|
|
|
|
return table_parent.end();
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
DirContainer::ParentIterator DirContainer::NextChild(ParentIterator i)
|
2008-12-10 05:42:49 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2009-02-06 14:12:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2014-02-12 17:30:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Winix
|
|
|
|
|