source: trunk/src/lua/lua_path.cc @ 532

Last change on this file since 532 was 490, checked in by epyon, 9 years ago
  • temporary Lua 5.1 hardcode
File size: 2.1 KB
RevLine 
[395]1// Copyright (C) 2012-2015 ChaosForge Ltd
[181]2// http://chaosforge.org/
3//
[395]4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
[181]6
7#include "nv/lua/lua_path.hh"
8
9#include "nv/lua/lua_raw.hh"
10
11using namespace nv;
12
13void lua::path::parse()
14{
[359]15        if ( m_elements[0].length == 0 || m_elements[0].str == nullptr ) return;
[399]16        string_view spath( m_elements[0].str, m_elements[0].length );
[359]17        m_count = 0;
18        size_t point = spath.find( '.' );
[181]19
[399]20        while ( point != string_view::npos )
[181]21        {
[359]22                m_elements[m_count].str    = spath.data();
23                m_elements[m_count].length = point;
24                m_count++;
25                spath.remove_prefix( point + 1 );
26                point = spath.find( '.' );
[181]27        }
28
[359]29        m_elements[m_count].str    = spath.data();
30        m_elements[m_count].length = spath.length();
31        m_count++;
[181]32}
33
[380]34void lua::path::push( nv::uint32 value )
[181]35{
[359]36        m_elements[ m_count ].value  = value;
[181]37        m_elements[ m_count ].length = 0;
38        m_count++;
39}
40
[399]41void nv::lua::path::push( string_view p )
[181]42{
[359]43        m_elements[ m_count ].str    = p.data();
44        m_elements[ m_count ].length = p.length();
[181]45        m_count++;
46}
47
48bool lua::path::resolve( lua_State* L, bool global /*= true */ ) const
49{
50        if (m_count == 0) return false;
[490]51        if (global) nlua_pushglobaltable( L );
[380]52        for ( uint32 i = 0; i < m_count; ++i )
[181]53        {
54                if ( lua_istable( L, -1 ) )
55                {
56                        if ( m_elements[i].length > 0 )
57                        {
[359]58                                lua_pushlstring( L, m_elements[i].str, m_elements[i].length );
[181]59                        }
60                        else
61                        {
[490]62                                nlua_pushunsigned( L, m_elements[i].value );
[181]63                        }
64                        lua_gettable( L, -2 );
65                        if (i > 0 || global ) lua_replace( L, -2 );
66                }
67                else
68                {
69                        lua_pop(L, 1);
70                        return false;
71                }
72        }
73        return true;
74}
75
[440]76string128 nv::lua::path::to_string() const
[181]77{
[440]78        string128 buffer;
[181]79        bool dot = false;
[445]80        for ( size_t c = 0; c < m_count; ++c )
[181]81        {
[440]82                if ( dot ) buffer.append( "." );
[445]83                if ( m_elements[c].length == 0 )
[181]84                {
[445]85                        buffer.append( "["_ls + m_elements[c].value + "]"_ls );
[181]86                        dot = false;
87                }
88                else
89                {
[445]90                        buffer.append( m_elements[c].str, m_elements[c].length );
[181]91                        dot = true;
92                }
93        }
[440]94        return buffer;
[181]95}
Note: See TracBrowser for help on using the repository browser.