Rev 8 | Blame | Last modification | View Log | RSS feed
/*
* Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "tprjresources.h"
#include "terror.h"
using std::string;
using std::vector;
TPrjResources::TPrjResources(vector<RESOURCE_LIST_T>& list)
: mResources(list)
{
DECL_TRACER("TPrjResources::TPrjResources(vector<RESOURCE_LIST_T>& list)");
}
TPrjResources::~TPrjResources()
{
DECL_TRACER("TPrjResources::~TPrjResources()");
}
RESOURCE_LIST_T TPrjResources::findResourceType(const std::string& type)
{
DECL_TRACER("TPrjResources::findResourceType(const std::string& type)");
vector<RESOURCE_LIST_T>::iterator iter;
for (iter = mResources.begin(); iter != mResources.end(); iter++)
{
if (iter->type.compare(type) == 0)
return *iter;
}
return RESOURCE_LIST_T();
}
RESOURCE_T TPrjResources::findResource(int idx, const std::string& name)
{
DECL_TRACER("TPrjResources::findResource(int idx, const std::string& name)");
if (idx < 1 || mResources.size() < (size_t)idx)
{
MSG_ERROR("Invalid index " << idx << "!");
return RESOURCE_T();
}
RESOURCE_LIST_T list = mResources[idx-1];
if (list.type.empty())
{
MSG_ERROR("Resource list " << idx << " is empty!");
return RESOURCE_T();
}
vector<RESOURCE_T>::iterator iter;
for (iter = list.ressource.begin(); iter != list.ressource.end(); iter++)
{
MSG_DEBUG("Resource: " << iter->name);
if (iter->name.compare(name) == 0)
return *iter;
}
MSG_WARNING("Resource " << name << " not found!");
return RESOURCE_T();
}
RESOURCE_T TPrjResources::findResource(const std::string& type, const std::string& name)
{
DECL_TRACER("TPrjResources::findResource(const std::string& type, const std::string& name)");
RESOURCE_LIST_T list = findResourceType(type);
if (list.type.empty())
return RESOURCE_T();
vector<RESOURCE_T>::iterator iter;
for (iter = list.ressource.begin(); iter != list.ressource.end(); iter++)
{
if (iter->name.compare(name) == 0)
return *iter;
}
return RESOURCE_T();
}
RESOURCE_T TPrjResources::findResource(const std::string& name)
{
DECL_TRACER("TPrjResources::findResource(const std::string& name)");
if (mResources.size() == 0 || mResources[0].ressource.size() == 0)
return RESOURCE_T();
vector<RESOURCE_T> list = mResources[0].ressource;
vector<RESOURCE_T>::iterator iter;
for (iter = list.begin(); iter != list.end(); iter++)
{
if (iter->name.compare(name) == 0)
return *iter;
}
return RESOURCE_T();
}
bool TPrjResources::setResource(const string& name, const string& scheme, const string& host, const string& path, const string& file, const string& user, const string& pw, int refresh)
{
DECL_TRACER("TPrjResources::setResource(const string& name, const string& scheme, const string& host, const string& path, const string& file, const string& user, const string& pw, int refresh)");
if (mResources.size() == 0 || mResources[0].ressource.size() == 0)
return false;
vector<RESOURCE_T> list = mResources[0].ressource;
vector<RESOURCE_T>::iterator iter;
for (iter = list.begin(); iter != list.end(); iter++)
{
if (iter->name.compare(name) == 0)
{
RESOURCE_T res = *iter;
if (!scheme.empty())
res.protocol = scheme;
if (!host.empty())
res.host = host;
if (!path.empty())
res.path = path;
if (!file.empty())
res.file = file;
if (!user.empty())
res.user = user;
if (!pw.empty())
res.password = pw;
if (refresh >= 0)
res.refresh = refresh;
list.erase(iter);
list.push_back(res);
RESOURCE_LIST_T rlist = mResources[0];
rlist.ressource.clear();
rlist.ressource = list;
mResources.erase(mResources.begin());
mResources.push_back(rlist);
return true;
}
}
return false;
}