From 1647173fa52bcf205290ad3cdf7e30ca8458343b Mon Sep 17 00:00:00 2001 From: karurochari Date: Sun, 15 Dec 2024 11:52:26 +0000 Subject: [PATCH] Added utils/resources to split mime-like logic --- include/cache/memory-storage.hpp | 4 +-- include/fetcher.hpp | 4 +-- include/utils/paths.hpp | 44 ++---------------------- include/utils/resources.hpp | 59 ++++++++++++++++++++++++++++++++ meson.build | 1 + src/fetcher.cpp | 14 ++++---- src/ui-tree.xml.cpp | 4 +-- src/utils/paths.cpp | 31 ++++------------- src/utils/resources.cpp | 26 ++++++++++++++ 9 files changed, 109 insertions(+), 78 deletions(-) create mode 100644 include/utils/resources.hpp create mode 100644 src/utils/resources.cpp diff --git a/include/cache/memory-storage.hpp b/include/cache/memory-storage.hpp index 45049e91..45c744ae 100644 --- a/include/cache/memory-storage.hpp +++ b/include/cache/memory-storage.hpp @@ -18,7 +18,7 @@ #include #include "commons.hpp" -#include "utils/paths.hpp" +#include "utils/resources.hpp" namespace vs{ namespace cache{ @@ -61,7 +61,7 @@ namespace cache{ class mem_storage_t{ public: struct entry_t{ - typedef std::variant format_t; + typedef std::variant format_t; std::shared_ptr ref; format_t format; }; diff --git a/include/fetcher.hpp b/include/fetcher.hpp index acb9c3e1..e1478847 100644 --- a/include/fetcher.hpp +++ b/include/fetcher.hpp @@ -16,7 +16,7 @@ namespace vs{ //Fetch any resource from any path to memory. -std::tuple fetch_any(const app_env_t& env, const char* path, resolve_path::from_t scope, bool promote=false, bool preserve=false); +std::tuple fetch_any(const app_env_t& env, const char* path, resolve_path::from_t scope, bool promote=false, bool preserve=false); /*Fully fetches a component by: - Looking for the best match which exists @@ -28,7 +28,7 @@ std::tuple fetch_component(const app_env_t& env, const char* path, resolve_path::from_t scope, bool promote=false, bool preserve=false); +std::tuple fetch_component(const app_env_t& env, const char* path, resolve_path::from_t scope, bool promote=false, bool preserve=false); diff --git a/include/utils/paths.hpp b/include/utils/paths.hpp index 357b0018..3585433e 100644 --- a/include/utils/paths.hpp +++ b/include/utils/paths.hpp @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -21,53 +22,12 @@ namespace vs{ #define vprefix(b) if(strncmp((src),vpath_type_t::as_string(b),std::char_traits::length(vpath_type_t::as_string(b)))==0){type=b;location=src+std::char_traits::length(vpath_type_t::as_string(b));} #define rprefix(b) if(strncmp((src),rpath_type_t::as_string(b),std::char_traits::length(rpath_type_t::as_string(b)))==0){type=b;location=src+std::char_traits::length(rpath_type_t::as_string(b));} -//TODO: Move script_t/component_t and possibly more somewhere else - -enum struct script_t{ - NONE, - C, - JS -}; - -enum struct component_t{ - NONE, - VS, - XML, - LIB, - WASM, - RISCV, - CNATIVE, - MARKDOWN, -}; -component_t component_t_i(const char* t); -constexpr const char* component_t_s(component_t t){ - if(t==component_t::NONE)return ""; - else if(t==component_t::VS)return ".vs"; - else if(t==component_t::XML)return ".xml"; - else if(t==component_t::LIB){ - //TODO:Add arch fragment? -# if defined(__linux__) - return ".so"; -# elif defined(_WIN32) || defined(_WIN64) - return ".dll"; -# elif defined(__APPLE__) - return ".dylib"; -# endif - } - else if(t==component_t::WASM)return ".wasm"; - else if(t==component_t::RISCV)return ".riscv"; - else if(t==component_t::CNATIVE)return ".c"; - else if(t==component_t::MARKDOWN)return ".md"; - else return nullptr; -} - - /** * @brief In case of a file inclusion without extension, like `this://component`, this function tells which one to look for next. * * @return std::pair boolean true if a file, false if a file inside the folder. */ -std::pair next_component_attempt(std::pair); +std::pair next_component_attempt(std::pair); struct vpath_type_t{ enum t{ diff --git a/include/utils/resources.hpp b/include/utils/resources.hpp new file mode 100644 index 00000000..5b7d30ce --- /dev/null +++ b/include/utils/resources.hpp @@ -0,0 +1,59 @@ +#pragma once + + +#include + +namespace vs{ +namespace res{ + +//TODO: Move script_t/component_t and possibly more somewhere else + +enum struct script_t{ + NONE, + C, + JS +}; + +enum struct component_t{ + NONE, + VS, + XML, + LIB, + WASM, + RISCV, + CNATIVE, + MARKDOWN, +}; +component_t component_t_i(const char* t); +constexpr const char* component_t_s(component_t t){ + if(t==component_t::NONE)return ""; + else if(t==component_t::VS)return ".vs"; + else if(t==component_t::XML)return ".xml"; + else if(t==component_t::LIB){ + //TODO:Add arch fragment? +# if defined(__linux__) + return ".so"; +# elif defined(_WIN32) || defined(_WIN64) + return ".dll"; +# elif defined(__APPLE__) + return ".dylib"; +# endif + } + else if(t==component_t::WASM)return ".wasm"; + else if(t==component_t::RISCV)return ".riscv"; + else if(t==component_t::CNATIVE)return ".c"; + else if(t==component_t::MARKDOWN)return ".md"; + else return nullptr; +} + + +/** + * @brief In case of a file inclusion without extension, like `this://component`, this function tells which one to look for next. + * + * @return std::pair boolean true if a file, false if a file inside the folder. + */ +std::pair next_component_attempt(std::pair); + + +} +} \ No newline at end of file diff --git a/meson.build b/meson.build index 184f1cd3..add9384e 100644 --- a/meson.build +++ b/meson.build @@ -237,6 +237,7 @@ vs_fltk = shared_library( './src/cache/res-storage.cpp', './src/cache/secrets.cpp', + './src/utils/resources.cpp', './src/utils/paths.cpp', './src/utils/strings.cpp', './src/utils/env.cpp', diff --git a/src/fetcher.cpp b/src/fetcher.cpp index 866aeced..1be143ff 100644 --- a/src/fetcher.cpp +++ b/src/fetcher.cpp @@ -6,14 +6,16 @@ namespace vs{ -std::tuple fetch_component(){ +std::tuple fetch_component(){ //TODO: } std::tuple fetcher(global_ctx_t& globals, resolve_path& base, resolve_path::from_t from,const char* src, bool promote, bool preserve){ auto ret = base(from,src); + //TODO: If the extension is present and valid, it should be taken and inverted to determine which resource type to apply. + //TODO: Fetcher right now should be made more general, as it only allows for components to be loaded. if(ret.first==resolve_path::reason_t::OK){ - for(std::pair current_trial = {true,component_t::NONE}; current_trial != std::pair {false,component_t::NONE}; current_trial = vs::next_component_attempt(current_trial)){ + for(std::pair current_trial = {true,res::component_t::NONE}; current_trial != std::pair {false,res::component_t::NONE}; current_trial = vs::next_component_attempt(current_trial)){ //If present, use the one cached. { auto found = globals.mem_storage.get({ret.second.location,0,cache::resource_t::BUFFER}); @@ -23,10 +25,10 @@ std::tuple fetcher(gl } } } - for(std::pair current_trial = {true,component_t::NONE}; current_trial != std::pair {false,component_t::NONE}; current_trial = vs::next_component_attempt(current_trial)){ + for(std::pair current_trial = {true,res::component_t::NONE}; current_trial != std::pair {false,res::component_t::NONE}; current_trial = vs::next_component_attempt(current_trial)){ if(ret.second.type==rpath_type_t::FS){ auto res = globals.mem_storage.fetch_from_fs({ - ret.second.location+(current_trial.first?vs::component_t_s(current_trial.second):std::string("/main")+vs::component_t_s(current_trial.second)), + ret.second.location+(current_trial.first?vs::res::component_t_s(current_trial.second):std::string("/main")+vs::res::component_t_s(current_trial.second)), 0,cache::resource_t::BUFFER,promote,preserve},current_trial.second); if(res==globals.mem_storage.end()){ continue; @@ -39,7 +41,7 @@ std::tuple fetcher(gl # ifdef HAS_CURL else if(ret.second.type==rpath_type_t::HTTP){ auto res = globals.mem_storage.fetch_from_http({ - ret.second.as_string()+(current_trial.first?vs::component_t_s(current_trial.second):std::string("/main")+vs::component_t_s(current_trial.second)), + ret.second.as_string()+(current_trial.first?vs::res::component_t_s(current_trial.second):std::string("/main")+vs::res::component_t_s(current_trial.second)), 0,cache::resource_t::BUFFER,promote,preserve},current_trial.second); if(res==globals.mem_storage.end()){ continue; @@ -51,7 +53,7 @@ std::tuple fetcher(gl } else if(ret.second.type==rpath_type_t::HTTPS){ auto res = globals.mem_storage.fetch_from_https({ - ret.second.as_string()+(current_trial.first?vs::component_t_s(current_trial.second):std::string("/main")+vs::component_t_s(current_trial.second)), + ret.second.as_string()+(current_trial.first?vs::res::component_t_s(current_trial.second):std::string("/main")+vs::res::component_t_s(current_trial.second)), 0,cache::resource_t::BUFFER,promote,preserve},current_trial.second); if(res==globals.mem_storage.end()){ continue; diff --git a/src/ui-tree.xml.cpp b/src/ui-tree.xml.cpp index fa4d49ce..513e13f7 100755 --- a/src/ui-tree.xml.cpp +++ b/src/ui-tree.xml.cpp @@ -449,7 +449,7 @@ void ui_tree_xml::_build_base_widget_extended_attr(const pugi::xml_node &root, u auto tmp = std::make_shared(cache::script_t{ compiler, symbols, frame_mode_t::NATIVE }); - globals.mem_storage.fetch_from_shared({this->fullname.as_string().c_str(),local_unique_counter+1,cache::resource_t::SCRIPT,false,false}, tmp, script_t::C); + globals.mem_storage.fetch_from_shared({this->fullname.as_string().c_str(),local_unique_counter+1,cache::resource_t::SCRIPT,false,false}, tmp, res::script_t::C); local_unique_counter++; } } @@ -471,7 +471,7 @@ void ui_tree_xml::_build_base_widget_extended_attr(const pugi::xml_node &root, u auto tmp = std::make_shared(cache::script_t{ compiler, symbols, frame_mode_t::QUICKJS }); - globals.mem_storage.fetch_from_shared({this->fullname.as_string().c_str(),local_unique_counter+1,cache::resource_t::SCRIPT,false,false}, tmp, script_t::JS); + globals.mem_storage.fetch_from_shared({this->fullname.as_string().c_str(),local_unique_counter+1,cache::resource_t::SCRIPT,false,false}, tmp, res::script_t::JS); local_unique_counter++; } } diff --git a/src/utils/paths.cpp b/src/utils/paths.cpp index 0b939e8e..a0e74ff2 100644 --- a/src/utils/paths.cpp +++ b/src/utils/paths.cpp @@ -2,7 +2,7 @@ namespace vs{ -std::pair next_component_attempt(std::pair current){ +std::pair next_component_attempt(std::pair current){ /*static constexpr std::pair next[] = { {true, component_t::VS}, {true,component_t::XML}, @@ -18,32 +18,15 @@ std::pair next_component_attempt(std::pair c {false,component_t::MARKDOWN}, {false,component_t::NONE}, };*/ - if(current.second +#include + +namespace vs{ +namespace res{ + +component_t component_t_i(const char* t){ + if(false); + else if(strcmp(t,".vs")==1)return component_t::VS; + else if(strcmp(t,".xml")==1)return component_t::XML; + else if(strcmp(t,".wasm")==1)return component_t::WASM; + else if(strcmp(t,".riscv")==1)return component_t::RISCV; +# if defined(__linux__) + else if(strcmp(t,".so")==1)return component_t::LIB; +# elif defined(_WIN32) || defined(_WIN64) + else if(strcmp(t,".dll")==1)return component_t::LIB; +# elif defined(__APPLE__) + else if(strcmp(t,".dylib")==1)return component_t::LIB; +# endif + else if(strcmp(t,".c")==1)return component_t::CNATIVE; + else if(strcmp(t,".md")==1)return component_t::MARKDOWN; + else return component_t::NONE; +} + +} +} \ No newline at end of file