/* * 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 "core/log.h" #include "core/item.h" #include "core/error.h" #include "functions.h" void FunctionParser::SkipEmptyString(const char * msg) { for( ; get_index != get_tab_len && cur->request->get_tab[get_index].empty() ; ++get_index ) log << log3 << msg << logend; } void FunctionParser::ParseDirectories() { Item * pdir = system->dirs.GetRootDir(); if( !pdir ) { // there is no the root dir cur->request->status = WINIX_ERR_NO_ROOT_DIR; return; } while( true ) { cur->request->dir_tab.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_tab_len ) break; pdir = system->dirs.GetDir(cur->request->get_tab[get_index], pdir->id); if( !pdir ) break; ++get_index; } cur->request->last_item = cur->request->dir_tab.back(); } void FunctionParser::ParseItem() { SkipEmptyString("FP: Item: skipped empty string"); if( get_index == get_tab_len ) return; // cur->request->dir_tab has at least one element long parent_id = cur->request->dir_tab.back()->id; const std::wstring & url = cur->request->get_tab[get_index]; cur->request->status = db->GetItem(parent_id, url, cur->request->item); if( cur->request->status == WINIX_ERR_OK ) { cur->request->last_item = &cur->request->item; if( cur->request->role == Request::authorizer && cur->request->item.file_type == WINIX_ITEM_FILETYPE_NONE ) { log << log1 << "FP: item.url: " << url << " exists but has not a static content (authorizer role)" << logend; cur->request->status = WINIX_ERR_NO_ITEM; return; } ++get_index; cur->request->is_item = true; log << log3 << "FP: Item: id: " << cur->request->item.id << ", url: " << cur->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_tab_len ) return; cur->request->function = functions->Find(cur->request->get_tab[get_index]); if( cur->request->function ) { ++get_index; log << log3 << "FP: Function: " << cur->request->function->fun.url << logend; } } void FunctionParser::ParseParams(const std::wstring & par) { Param param; size_t i; if( par.empty() ) return; // looking for the first colon ':' for(i=0 ; i 0 ) param.name = par.substr(0, i); if( i < par.size() - 1 ) param.value = par.substr(i+1); } cur->request->param_tab.push_back(param); log << log3 << "FP: Param: name=" << param.name; if( !param.value.empty() ) log << ", value=" << param.value; log << logend; } void FunctionParser::ParseParams() { for( ; true ; ++get_index ) { SkipEmptyString("FP: Params: skipped empty string"); if( get_index == get_tab_len ) break; ParseParams(cur->request->get_tab[get_index]); } } void FunctionParser::Parse(Cur * pcur, Db * pdb, Functions * pfunctions, System * psystem) { db = pdb; cur = pcur; system = psystem; functions = pfunctions; cur->request->status = WINIX_ERR_OK; get_index = 0; get_tab_len = cur->request->get_tab.size(); cur->request->function = 0; cur->request->is_item = false; ParseDirectories(); if( cur->request->status != WINIX_ERR_OK ) return; ParseFunction(); if( !cur->request->function ) { ParseItem(); if( cur->request->status != WINIX_ERR_OK ) return; ParseFunction(); if( !cur->request->function && get_index != get_tab_len ) { cur->request->status = WINIX_ERR_NO_FUNCTION; log << log3 << "FP: Parse: unknown function: \"" << cur->request->get_tab[get_index] << "\"" << logend; return; } } ParseParams(); }