/* * This file is a part of Winix * and is distributed under the 2-Clause BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2012-2016, Tomasz Sowa * All rights reserved. * * 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. * */ #include "templates.h" namespace Winix { namespace TemplatesFunctions { bool are_spaces_the_same(const std::vector & params, const std::vector & spaces) { // last value from params is the parameter name (not a space) if( spaces.size() + 1 != params.size() ) return false; for(size_t i=0 ; i & params, std::vector & spaces) { if( !params.empty() ) { spaces.resize(params.size() - 1); for(size_t i=0 ; i & params, PT::Space & space, size_t level = 0) { if( level + 1 < params.size() ) { for(size_t i=0 ; iname == params[level].str ) return find_space(params, *space.spaces[i], level+1); } // there is no such a space return 0; } else { return &space; } } /* * * IMPROVE ME * add a private namespace? * */ struct SpaceInfo { bool inited; PT::Space * last_space; std::vector spaces; SpaceInfo() { inited = false; last_space = 0; } }; static std::map spaces_map; static size_t space_reqid = 0; /* struct SpaceKey { PT::Space * global_space; std::vector spaces_names; bool operator<(const SpaceKey & k) { if( global_space != k.global_space ) return global_space < k.global_space; if( spaces_names.size() != k.spaces_names.size() ) return spaces_names.size() < k.spaces_names.size(); for(size_t i=0 ; i space_map; void space_init(const std::vector & params, PT::Space & space, SpaceInfo & space_info) { if( !space_info.inited || !are_spaces_the_same(params, space_info.spaces) ) { space_info.inited = true; copy_space(params, space_info.spaces); space_info.last_space = find_space(params, space); } } void space_check_reqid() { if( space_reqid != cur->request->id ) { space_reqid = cur->request->id; spaces_map.clear(); } } void space_value(Info & i, PT::Space & space, bool escape) { space_check_reqid(); if( !i.params.empty() ) { SpaceInfo & space_info = spaces_map[&space]; space_init(i.params, space, space_info); if( space_info.last_space ) { const std::wstring & param = i.params.back().str; const std::wstring * value = space_info.last_space->GetValue(param); if( value ) { if( escape ) i.out << *value; else i.out << R(*value); } } } } /* stack item for [for] statement */ struct SpaceStackItem { // names of the spaces std::vector spaces; // table of values std::vector values; // an index to 'values' which is incremented every each [for] size_t value_index; SpaceStackItem() { value_index = 0; } }; /* each Space has its own SpacesTabInfo struct */ struct SpacesTabInfo { // a stack for [for] statements std::vector stack_tab; // current item from 'stack_tab' size_t cur_stack; SpacesTabInfo() { cur_stack = 0; } }; static std::map spaces_tab_info_map; static size_t spaces_tab_reqid = 0; bool spaces_tab_find_stack_item(std::vector & params, SpacesTabInfo & spaces_tab_info) { bool found = false; spaces_tab_info.cur_stack = spaces_tab_info.stack_tab.size(); for(size_t i=0 ; i & params, PT::Space & space, SpacesTabInfo & spaces_tab_info) { if( !params.empty() ) { // adding a new item at the end (with the default constructor) spaces_tab_info.stack_tab.resize(spaces_tab_info.stack_tab.size() + 1); spaces_tab_info.cur_stack = spaces_tab_info.stack_tab.size() - 1; SpaceStackItem & stack_item = spaces_tab_info.stack_tab.back(); copy_space(params, stack_item.spaces); space.ListText(params.back().str, stack_item.values); stack_item.value_index = 0; } } void spaces_tab_check_reqid() { if( spaces_tab_reqid != cur->request->id ) { spaces_tab_reqid = cur->request->id; spaces_tab_info_map.clear(); } } void spaces_tab_init(std::vector & params, PT::Space & space, SpacesTabInfo & spaces_tab_info) { PT::Space * child_space = find_space(params, space); if( child_space ) { if( !spaces_tab_find_stack_item(params, spaces_tab_info) ) { // add a new value to the stack spaces_tab_add_to_stack(params, *child_space, spaces_tab_info); } } else { // there is not such a space // space_list_tab() will return false immediately spaces_tab_info.cur_stack = spaces_tab_info.stack_tab.size(); } } void space_list_tab(Info & i, PT::Space & space) { spaces_tab_check_reqid(); if( !i.params.empty() ) { SpacesTabInfo & spaces_tab_info = spaces_tab_info_map[&space]; spaces_tab_init(i.params, space, spaces_tab_info); if( spaces_tab_info.cur_stack < spaces_tab_info.stack_tab.size() ) { SpaceStackItem & stack_item = spaces_tab_info.stack_tab[spaces_tab_info.cur_stack]; // i.iter is different for each [for] statement (implemented in EZC) // so we don't have to remember it in our stack // for each the same space we have only one item in the stack, e.g. // [for item_meta "space1" "space2" "value"] // [for item_meta "space1" "space2" "value"] // [end] // [end] // above two [for]s use the same item on our stack stack_item.value_index = i.iter; i.res = stack_item.value_index < stack_item.values.size(); } } } void spaces_tab_value_print(HtmlTextStream & out, SpacesTabInfo & spaces_tab_info) { if( spaces_tab_info.cur_stack < spaces_tab_info.stack_tab.size() ) { SpaceStackItem & stack_item = spaces_tab_info.stack_tab[spaces_tab_info.cur_stack]; if( stack_item.value_index < stack_item.values.size() ) out << stack_item.values[stack_item.value_index]; } } void space_list_tab_value(Info & i, PT::Space & space) { spaces_tab_check_reqid(); SpacesTabInfo & spaces_tab_info = spaces_tab_info_map[&space]; if( i.params.empty() ) { // value from last [for ...] statement spaces_tab_value_print(i.out, spaces_tab_info); } else { size_t cur_stack_old = spaces_tab_info.cur_stack; if( spaces_tab_find_stack_item(i.params, spaces_tab_info) ) spaces_tab_value_print(i.out, spaces_tab_info); spaces_tab_info.cur_stack = cur_stack_old; } } bool spaces_tab_has_next(SpacesTabInfo & spaces_tab_info) { if( spaces_tab_info.cur_stack < spaces_tab_info.stack_tab.size() ) { SpaceStackItem & val = spaces_tab_info.stack_tab[spaces_tab_info.cur_stack]; return val.value_index + 1 < val.values.size(); } return false; } void space_list_tab_has_next(Info & i, PT::Space & space) { spaces_tab_check_reqid(); SpacesTabInfo & spaces_tab_info = spaces_tab_info_map[&space]; if( i.params.empty() ) { // value from last [for ...] statement i.res = spaces_tab_has_next(spaces_tab_info); } else { size_t cur_stack_old = spaces_tab_info.cur_stack; if( spaces_tab_find_stack_item(i.params, spaces_tab_info) ) i.res = spaces_tab_has_next(spaces_tab_info); spaces_tab_info.cur_stack = cur_stack_old; } } struct Space2StackItem : public Ezc::FunData { std::vector values; }; void space2_list_tab(Info & i, PT::Space & space) { if( !i.stack.fun_data ) { if( !i.params.empty() ) { i.stack.for_name = new std::wstring(L"space_list_tab"); Space2StackItem * stack_item = new Space2StackItem(); i.stack.fun_data = stack_item; PT::Space * dst_space = find_space(i.params, space); if( dst_space ) dst_space->ListText(i.params.back().str, stack_item->values); i.res = i.iter < stack_item->values.size(); } } else { auto stack_item = reinterpret_cast(i.stack.fun_data); i.res = i.iter < stack_item->values.size(); } } void space2_list_tab_value(Info & i, PT::Space & space) { Ezc::Stack * stack = i.FindLastFor(i.par, L"space_list_tab"); if( stack && stack->fun_data ) { auto stack_item = dynamic_cast(stack->fun_data); if( stack_item && stack->iter < stack_item->values.size() ) i.out << stack_item->values[stack->iter]; } } } // namespace TemplatesFunctions } // namespace Winix