[181] | 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 |
|
---|
| 11 | using namespace nv;
|
---|
| 12 |
|
---|
| 13 | lua::path::path( nv::string p )
|
---|
| 14 | : m_count(0), m_path( std::move( p ) )
|
---|
| 15 | {
|
---|
| 16 | parse();
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | lua::path::path( const char* p )
|
---|
| 20 | : m_count(0), m_path( p )
|
---|
| 21 | {
|
---|
| 22 | parse();
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[198] | 25 | lua::path::path( unsigned i )
|
---|
[181] | 26 | : m_count(0)
|
---|
| 27 | {
|
---|
| 28 | push( i );
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | void 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 |
|
---|
[204] | 46 | void lua::path::push( size_t e )
|
---|
[181] | 47 | {
|
---|
| 48 | m_elements[ m_count ].value = e;
|
---|
| 49 | m_elements[ m_count ].length = 0;
|
---|
| 50 | m_count++;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[204] | 53 | void lua::path::push( size_t start, size_t length )
|
---|
[181] | 54 | {
|
---|
| 55 | m_elements[ m_count ].value = start;
|
---|
| 56 | m_elements[ m_count ].length = length;
|
---|
| 57 | m_count++;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[204] | 60 | void lua::path::push( const char* p, size_t length )
|
---|
[181] | 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 |
|
---|
[204] | 68 | void lua::path::push( const std::string& s, size_t length )
|
---|
[181] | 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 |
|
---|
| 76 | bool 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 | {
|
---|
[198] | 90 | lua_pushunsigned( L, m_elements[i].value );
|
---|
[181] | 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 |
|
---|
| 104 | std::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 | }
|
---|