Skip to content

Commit

Permalink
Added utils/resources to split mime-like logic
Browse files Browse the repository at this point in the history
  • Loading branch information
karurochari committed Dec 15, 2024
1 parent 7d2f392 commit 1647173
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 78 deletions.
4 changes: 2 additions & 2 deletions include/cache/memory-storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <variant>

#include "commons.hpp"
#include "utils/paths.hpp"
#include "utils/resources.hpp"

namespace vs{
namespace cache{
Expand Down Expand Up @@ -61,7 +61,7 @@ namespace cache{
class mem_storage_t{
public:
struct entry_t{
typedef std::variant<vs::component_t, vs::script_t> format_t;
typedef std::variant<vs::res::component_t, vs::res::script_t> format_t;
std::shared_ptr<void> ref;
format_t format;
};
Expand Down
4 changes: 2 additions & 2 deletions include/fetcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace vs{


//Fetch any resource from any path to memory.
std::tuple<resolve_path::reason_t::t, cache::mem_storage_t::entry_it*, component_t> fetch_any(const app_env_t& env, const char* path, resolve_path::from_t scope, bool promote=false, bool preserve=false);
std::tuple<resolve_path::reason_t::t, cache::mem_storage_t::entry_it*, res::component_t> 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
Expand All @@ -28,7 +28,7 @@ std::tuple<resolve_path::reason_t::t, cache::mem_storage_t::entry_it*, component
- LIB skip, the library will be loaded on demand
- CNATIVE compile and save it as script. Technically it is not, but I am not going to compile it over and over and script works as a container
*/
std::tuple<resolve_path::reason_t::t, cache::mem_storage_t::entry_it*, component_t> fetch_component(const app_env_t& env, const char* path, resolve_path::from_t scope, bool promote=false, bool preserve=false);
std::tuple<resolve_path::reason_t::t, cache::mem_storage_t::entry_it*, res::component_t> fetch_component(const app_env_t& env, const char* path, resolve_path::from_t scope, bool promote=false, bool preserve=false);



Expand Down
44 changes: 2 additions & 42 deletions include/utils/paths.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <cstring>
#include <string>
#include <utils/resources.hpp>
#include <utils/policies.hpp>
#include <utils/arch.hpp>

Expand All @@ -21,53 +22,12 @@ namespace vs{
#define vprefix(b) if(strncmp((src),vpath_type_t::as_string(b),std::char_traits<char>::length(vpath_type_t::as_string(b)))==0){type=b;location=src+std::char_traits<char>::length(vpath_type_t::as_string(b));}
#define rprefix(b) if(strncmp((src),rpath_type_t::as_string(b),std::char_traits<char>::length(rpath_type_t::as_string(b)))==0){type=b;location=src+std::char_traits<char>::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<bool,component_t> boolean true if a file, false if a file inside the folder.
*/
std::pair<bool,component_t> next_component_attempt(std::pair<bool,component_t>);
std::pair<bool,res::component_t> next_component_attempt(std::pair<bool,res::component_t>);

struct vpath_type_t{
enum t{
Expand Down
59 changes: 59 additions & 0 deletions include/utils/resources.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once


#include <utility>

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<bool,component_t> boolean true if a file, false if a file inside the folder.
*/
std::pair<bool,component_t> next_component_attempt(std::pair<bool,component_t>);


}
}
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
14 changes: 8 additions & 6 deletions src/fetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
namespace vs{


std::tuple<resolve_path::reason_t::t, cache::mem_storage_t::entry_it*, component_t> fetch_component(){
std::tuple<resolve_path::reason_t::t, cache::mem_storage_t::entry_it*, res::component_t> fetch_component(){
//TODO:
}

std::tuple<resolve_path::reason_t::t,cache::buffer_t, scoped_rpath_t> 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<bool,component_t> current_trial = {true,component_t::NONE}; current_trial != std::pair<bool,component_t> {false,component_t::NONE}; current_trial = vs::next_component_attempt(current_trial)){
for(std::pair<bool,res::component_t> current_trial = {true,res::component_t::NONE}; current_trial != std::pair<bool,res::component_t> {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});
Expand All @@ -23,10 +25,10 @@ std::tuple<resolve_path::reason_t::t,cache::buffer_t, scoped_rpath_t> fetcher(gl
}
}
}
for(std::pair<bool,component_t> current_trial = {true,component_t::NONE}; current_trial != std::pair<bool,component_t> {false,component_t::NONE}; current_trial = vs::next_component_attempt(current_trial)){
for(std::pair<bool,res::component_t> current_trial = {true,res::component_t::NONE}; current_trial != std::pair<bool,res::component_t> {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;
Expand All @@ -39,7 +41,7 @@ std::tuple<resolve_path::reason_t::t,cache::buffer_t, scoped_rpath_t> 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;
Expand All @@ -51,7 +53,7 @@ std::tuple<resolve_path::reason_t::t,cache::buffer_t, scoped_rpath_t> 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;
Expand Down
4 changes: 2 additions & 2 deletions src/ui-tree.xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>(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++;
}
}
Expand All @@ -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>(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++;
}
}
Expand Down
31 changes: 7 additions & 24 deletions src/utils/paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace vs{

std::pair<bool,component_t> next_component_attempt(std::pair<bool,component_t> current){
std::pair<bool,res::component_t> next_component_attempt(std::pair<bool,res::component_t> current){
/*static constexpr std::pair<bool,component_t> next[] = {
{true, component_t::VS},
{true,component_t::XML},
Expand All @@ -18,32 +18,15 @@ std::pair<bool,component_t> next_component_attempt(std::pair<bool,component_t> c
{false,component_t::MARKDOWN},
{false,component_t::NONE},
};*/
if(current.second<component_t::CNATIVE)return {current.first,(component_t)((int)current.second+1)};
else if (current.first==true && current.second==component_t::CNATIVE)return {false,component_t::VS};
else if(current.first==false && current.second==component_t::CNATIVE)return {true,component_t::MARKDOWN};
else if (current.first==true && current.second==component_t::MARKDOWN)return {false,component_t::MARKDOWN};
else if(current.first==false && current.second==component_t::MARKDOWN)return {false,component_t::NONE};
if(current.second<res::component_t::CNATIVE)return {current.first,(res::component_t)((int)current.second+1)};
else if (current.first==true && current.second==res::component_t::CNATIVE)return {false,res::component_t::VS};
else if(current.first==false && current.second==res::component_t::CNATIVE)return {true,res::component_t::MARKDOWN};
else if (current.first==true && current.second==res::component_t::MARKDOWN)return {false,res::component_t::MARKDOWN};
else if(current.first==false && current.second==res::component_t::MARKDOWN)return {false,res::component_t::NONE};

return {false, component_t::NONE};
return {false, res::component_t::NONE};
}

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;
}

void scoped_vpath_t::from_string(const char* src){
vprefix(vpath_type_t::THIS)
Expand Down
26 changes: 26 additions & 0 deletions src/utils/resources.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <cstring>
#include <utils/resources.hpp>

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;
}

}
}

0 comments on commit 1647173

Please sign in to comment.