From c467671ae8b6ec161c17e86f3383fd0625f755b8 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 19 Aug 2022 19:48:10 -0400 Subject: remove sol2 (will re-add as submodule) --- lib/sol2/examples/source/custom_reader.cpp | 95 ------------------------------ 1 file changed, 95 deletions(-) delete mode 100644 lib/sol2/examples/source/custom_reader.cpp (limited to 'lib/sol2/examples/source/custom_reader.cpp') diff --git a/lib/sol2/examples/source/custom_reader.cpp b/lib/sol2/examples/source/custom_reader.cpp deleted file mode 100644 index 3e0d430..0000000 --- a/lib/sol2/examples/source/custom_reader.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#define SOL_ALL_SAFETIES_ON 1 -#include - -#include "assert.hpp" - -#include -#include -#include - -struct custom_reader { - FILE* f; - // We use 2 here to demonstrate - // multiple calls to the function to - // parse whole file. - // Please don't use 2. - // PLEASE DO NOT USE A BUFFER - // OF SIZE 2! - char buffer[2]; - std::size_t current_size; - std::size_t read_count; - - custom_reader(FILE* f_) : f(f_), buffer(), current_size(0), read_count(0) { - - } - - bool read() { - std::cout << "custom read: read #" << ++read_count << std::endl; - current_size = fread( buffer, 1, 2, f ); - return current_size > 0 && ferror(f) == 0; - } - - ~custom_reader( ) { - std::fclose( f ); - } -}; - -// function must match signature found in type lua_Reader: -// const char* ( lua_State*, void*, size_t* ) -const char* custom_reader_function(lua_State*, void* pointer_to_my_object, size_t* data_size) { - custom_reader& cr = *(static_cast(pointer_to_my_object)); - if ( cr.read( ) ) { - *data_size = cr.current_size; - return cr.buffer; - } - else { - *data_size = 0; - return nullptr; - } -} - -int main( ) { - std::cout << "=== custom reader ===" << std::endl; - - // make a file to use for the custom reader - { - std::ofstream bjork("bjork.lua", std::ios::binary); - bjork << "print('hello!')\n"; - } - struct on_scope_exit { - ~on_scope_exit() { - // remove file when done - std::remove("bjork.lua"); - } - } remove_on_exit; - - - sol::state lua; - lua.open_libraries( sol::lib::base ); - - FILE* bjork_fp; -#ifdef _MSC_VER - if ( fopen_s( &bjork_fp, "bjork.lua", "r" ) != 0 ) { - std::cerr << "failed to open bjork.lua -- exiting" << std::endl; - return -1; - } -#else - bjork_fp = fopen( "bjork.lua", "r" ); -#endif - if (bjork_fp == nullptr) { - std::cerr << "failed to open bjork.lua -- exiting" << std::endl; - return -1; - } - custom_reader reader(bjork_fp); - - // load the code using our custom reader, then run it - auto result = lua.safe_script( custom_reader_function, &reader, sol::script_pass_on_error ); - // make sure we ran loaded and ran the code successfully - c_assert( result.valid( ) ); - - // note there are lua.load( ... ) variants that take a - // custom reader than JUST run the code, too! - - std::cout << std::endl; - return 0; -} -- cgit v1.2.3