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

Last change on this file since 198 was 198, checked in by epyon, 12 years ago
  • warning cleanup for clang and gcc
File size: 2.4 KB
Line 
1// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include "nv/lua/lua_path.hh"
8
9#include "nv/lua/lua_raw.hh"
10
11using namespace nv;
12
13lua::path::path( nv::string p )
14        : m_count(0), m_path( std::move( p ) )
15{
16        parse();
17}
18
19lua::path::path( const char* p )
20        : m_count(0), m_path( p )
21{
22        parse();
23}
24
25lua::path::path( unsigned i )
26        : m_count(0)
27{
28        push( i );
29}
30
31void lua::path::parse()
32{
33        size_t start = 0;
34        size_t point = m_path.find('.');
35
36        while( point != std::string::npos )
37        {
38                push( start, point-start );
39                start = point+1;
40                point = m_path.find( '.', start );
41        }
42
43        push( start, m_path.length() - start );
44}
45
46void lua::path::push( uint32 e )
47{
48        m_elements[ m_count ].value  = e;
49        m_elements[ m_count ].length = 0;
50        m_count++;
51}
52
53void lua::path::push( uint32 start, uint32 length )
54{
55        m_elements[ m_count ].value  = start;
56        m_elements[ m_count ].length = length;
57        m_count++;
58}
59
60void lua::path::push( const char* p, uint32 length )
61{
62        m_elements[ m_count ].value  = m_path.length();
63        m_elements[ m_count ].length = length;
64        m_path.append( p, length );
65        m_count++;
66}
67
68void lua::path::push( const std::string& s, uint32 length )
69{
70        m_elements[ m_count ].value  = m_path.length();
71        m_elements[ m_count ].length = length;
72        m_path.append( s, 0, length );
73        m_count++;
74}
75
76bool lua::path::resolve( lua_State* L, bool global /*= true */ ) const
77{
78        if (m_count == 0) return false;
79        if (global) lua_pushglobaltable( L );
80        for (int i = 0; i < m_count; ++i )
81        {
82                if ( lua_istable( L, -1 ) )
83                {
84                        if ( m_elements[i].length > 0 )
85                        {
86                                lua_pushlstring( L, m_path.c_str() + m_elements[i].value, m_elements[i].length );
87                        }
88                        else
89                        {
90                                lua_pushunsigned( L, m_elements[i].value );
91                        }
92                        lua_gettable( L, -2 );
93                        if (i > 0 || global ) lua_replace( L, -2 );
94                }
95                else
96                {
97                        lua_pop(L, 1);
98                        return false;
99                }
100        }
101        return true;
102}
103
104std::string nv::lua::path::to_string() const
105{
106        std::string result;
107        result.reserve( 2 * m_path.length() );
108        bool dot = false;
109        for ( const element& e : m_elements )
110        {
111                if ( dot ) result.append(".");
112                if ( e.length == 0 )
113                {
114                        result.append("[" + nv::to_string( e.value ) + "]" );
115                        dot = false;
116                }
117                else
118                {
119                        result.append( m_path, e.value, e.length );
120                        dot = true;
121                }
122        }
123        return result;
124}
Note: See TracBrowser for help on using the repository browser.