1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
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 | {
|
---|
15 | if ( m_elements[0].length == 0 || m_elements[0].str == nullptr ) return;
|
---|
16 | string_view spath( m_elements[0].str, m_elements[0].length );
|
---|
17 | m_count = 0;
|
---|
18 | size_t point = spath.find( '.' );
|
---|
19 |
|
---|
20 | while ( point != string_view::npos )
|
---|
21 | {
|
---|
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( '.' );
|
---|
27 | }
|
---|
28 |
|
---|
29 | m_elements[m_count].str = spath.data();
|
---|
30 | m_elements[m_count].length = spath.length();
|
---|
31 | m_count++;
|
---|
32 | }
|
---|
33 |
|
---|
34 | void lua::path::push( nv::uint32 value )
|
---|
35 | {
|
---|
36 | m_elements[ m_count ].value = value;
|
---|
37 | m_elements[ m_count ].length = 0;
|
---|
38 | m_count++;
|
---|
39 | }
|
---|
40 |
|
---|
41 | void nv::lua::path::push( string_view p )
|
---|
42 | {
|
---|
43 | m_elements[ m_count ].str = p.data();
|
---|
44 | m_elements[ m_count ].length = p.length();
|
---|
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 ( uint32 i = 0; i < m_count; ++i )
|
---|
53 | {
|
---|
54 | if ( lua_istable( L, -1 ) )
|
---|
55 | {
|
---|
56 | if ( m_elements[i].length > 0 )
|
---|
57 | {
|
---|
58 | lua_pushlstring( L, m_elements[i].str, m_elements[i].length );
|
---|
59 | }
|
---|
60 | else
|
---|
61 | {
|
---|
62 | lua_pushunsigned( L, m_elements[i].value );
|
---|
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 | char buffer[64];
|
---|
79 | char* start = buffer;
|
---|
80 | char* current = buffer;
|
---|
81 | bool dot = false;
|
---|
82 | bool oos = false;
|
---|
83 | for ( const element& e : m_elements )
|
---|
84 | {
|
---|
85 | if ( current - start > 48 ) { oos = true; break; }
|
---|
86 | if ( dot ) *current++ = '.';
|
---|
87 | if ( e.length == 0 )
|
---|
88 | {
|
---|
89 | *current++ = '[';
|
---|
90 | current += uint32_to_buffer( array_ref< char >( current, current - start ), e.value );
|
---|
91 | *current++ = ']';
|
---|
92 | dot = false;
|
---|
93 | }
|
---|
94 | else
|
---|
95 | {
|
---|
96 | if ( size_t(current - start) + e.length > 60 ) { oos = true; break; }
|
---|
97 | nvmemcpy( current, e.str, e.length );
|
---|
98 | current += e.length;
|
---|
99 | dot = true;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | if (oos)
|
---|
103 | {
|
---|
104 | *current++ = '.';
|
---|
105 | *current++ = '.';
|
---|
106 | *current++ = '.';
|
---|
107 | }
|
---|
108 | *current++ = '\0';
|
---|
109 | return std::string( buffer, size_t(current - start - 1) );
|
---|
110 | }
|
---|