winix/core/functionparser.cpp

226 lines
3.7 KiB
C++
Executable File

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2008-2010, Tomasz Sowa
* All rights reserved.
*
*/
#include "functionparser.h"
#include "log.h"
#include "item.h"
#include "error.h"
#include "data.h"
#include "db.h"
#include "request.h"
void FunctionParser::SkipEmptyString(const char * msg)
{
for( ; get_index != get_table_len && request.get_table[get_index].empty() ; ++get_index )
log << log3 << msg << logend;
}
void FunctionParser::ParseDirectories()
{
Item * pdir = data.dirs.GetRootDir();
if( !pdir )
{
// there is no the root dir
request.status = WINIX_ERR_NO_ROOT_DIR;
return;
}
while( true )
{
request.dir_table.push_back( pdir );
log << log3 << "FP: Directory: ";
if( pdir->parent_id == -1 )
log << "(root)" << logend;
else
log << pdir->url << logend;
SkipEmptyString("FP: Directory: skipped empty string");
if( get_index == get_table_len )
break;
pdir = data.dirs.GetDir(request.get_table[get_index], pdir->id);
if( !pdir )
break;
++get_index;
}
}
void FunctionParser::ParseItem()
{
SkipEmptyString("FP: Item: skipped empty string");
if( get_index == get_table_len )
return;
// request.dir_table has at least one element
long parent_id = request.dir_table.back()->id;
const std::string & url = request.get_table[get_index];
request.status = db.GetItem(parent_id, url, request.item);
if( request.status == WINIX_ERR_OK )
{
if( request.role == Request::authorizer && request.item.auth == Item::auth_none )
{
log << log1 << "FP: item.url: " << url << " exists but has not a static content (authorizer role)" << logend;
request.status = WINIX_ERR_NO_ITEM;
return;
}
++get_index;
request.is_item = true;
log << log3 << "FP: Item: id: " << request.item.id << ", url: " << request.item.url << logend;
}
else
{
log << log3 << "FP: No Item: url: " << url << logend;
}
}
void FunctionParser::ParseFunction()
{
SkipEmptyString("FP: Function: skipped empty string");
if( get_index == get_table_len )
return;
request.pfunction = data.functions.GetFunction(request.get_table[get_index]);
if( request.pfunction )
{
++get_index;
log << log3 << "FP: Function: " << request.pfunction->item.url << logend;
}
}
void FunctionParser::ParseParams(const std::string & par)
{
Param param;
size_t i;
if( par.empty() )
return;
// looking for the first colon ':'
for(i=0 ; i<par.size() && par[i] != ':' ; ++i);
if( i == par.size() )
{
// there is no a colon
param.name = par;
}
else
{
if( i > 0 )
param.name = par.substr(0, i);
if( i < par.size() - 1 )
param.value = par.substr(i+1);
}
request.param_table.push_back(param);
log << log3 << "FP: Param: name=" << param.name << ", value=" << param.value << logend;
}
void FunctionParser::ParseParams()
{
for( ; true ; ++get_index )
{
SkipEmptyString("FP: Params: skipped empty string");
if( get_index == get_table_len )
break;
ParseParams(request.get_table[get_index]);
}
}
void FunctionParser::Parse()
{
request.status = WINIX_ERR_OK;
get_index = 0;
get_table_len = request.get_table.size();
request.pfunction = 0;
request.is_item = false;
ParseDirectories();
if( request.status != WINIX_ERR_OK )
return;
ParseFunction();
if( !request.pfunction )
{
ParseItem();
if( request.status != WINIX_ERR_OK )
return;
ParseFunction();
if( !request.pfunction && get_index != get_table_len )
{
request.status = WINIX_ERR_NO_FUNCTION;
log << log3 << "FP: Parse: unknown function: \"" << request.get_table[get_index] << "\"" << logend;
return;
}
}
ParseParams();
}