[319] | 1 | // Copyright (C) 2012-2014 ChaosForge Ltd
|
---|
[181] | 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 | void lua::path::parse()
|
---|
| 14 | {
|
---|
[359] | 15 | if ( m_elements[0].length == 0 || m_elements[0].str == nullptr ) return;
|
---|
| 16 | string_ref spath( m_elements[0].str, m_elements[0].length );
|
---|
| 17 | m_count = 0;
|
---|
| 18 | size_t point = spath.find( '.' );
|
---|
[181] | 19 |
|
---|
[359] | 20 | while ( point != std::string::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 |
|
---|
[359] | 34 | void lua::path::push( size_t value )
|
---|
[181] | 35 | {
|
---|
[359] | 36 | m_elements[ m_count ].value = value;
|
---|
[181] | 37 | m_elements[ m_count ].length = 0;
|
---|
| 38 | m_count++;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[359] | 41 | void nv::lua::path::push( string_ref 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 |
|
---|
| 48 | bool lua::path::resolve( lua_State* L, bool global /*= true */ ) const
|
---|
| 49 | {
|
---|
| 50 | if (m_count == 0) return false;
|
---|
| 51 | if (global) lua_pushglobaltable( L );
|
---|
| 52 | for (int i = 0; i < m_count; ++i )
|
---|
| 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 | {
|
---|
[198] | 62 | lua_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 |
|
---|
| 76 | std::string nv::lua::path::to_string() const
|
---|
| 77 | {
|
---|
| 78 | std::string result;
|
---|
[359] | 79 | result.reserve( 64 );
|
---|
[181] | 80 | bool dot = false;
|
---|
| 81 | for ( const element& e : m_elements )
|
---|
| 82 | {
|
---|
| 83 | if ( dot ) result.append(".");
|
---|
| 84 | if ( e.length == 0 )
|
---|
| 85 | {
|
---|
| 86 | result.append("[" + nv::to_string( e.value ) + "]" );
|
---|
| 87 | dot = false;
|
---|
| 88 | }
|
---|
| 89 | else
|
---|
| 90 | {
|
---|
[359] | 91 | result.append( e.str, e.length );
|
---|
[181] | 92 | dot = true;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | return result;
|
---|
| 96 | }
|
---|