From 430c912e0229fd3299766d73f01561275ed6bf1b Mon Sep 17 00:00:00 2001 From: kokkiemouse Date: Mon, 23 Nov 2020 08:01:11 +0900 Subject: [PATCH] added cmake src Signed-off-by: kokkiemouse --- tools/cpp/channel/src/CMakeLists.txt | 1 + tools/cpp/channel/src/cmdline.h | 809 ++++++++++++++++++++++++++++++ tools/cpp/channel/src/custom_exec.cpp | 62 +++ tools/cpp/channel/src/custom_exec.hpp | 15 + tools/cpp/channel/src/main.cpp | 25 + tools/cpp/channel/src/main.hpp | 3 + tools/cpp/channel/src/message.cpp | 59 +++ tools/cpp/channel/src/message.hpp | 36 ++ tools/cpp/channel/src/option_channels.hpp | 8 + 9 files changed, 1018 insertions(+) create mode 100644 tools/cpp/channel/src/CMakeLists.txt create mode 100644 tools/cpp/channel/src/cmdline.h create mode 100644 tools/cpp/channel/src/custom_exec.cpp create mode 100644 tools/cpp/channel/src/custom_exec.hpp create mode 100644 tools/cpp/channel/src/main.cpp create mode 100644 tools/cpp/channel/src/main.hpp create mode 100644 tools/cpp/channel/src/message.cpp create mode 100644 tools/cpp/channel/src/message.hpp create mode 100644 tools/cpp/channel/src/option_channels.hpp diff --git a/tools/cpp/channel/src/CMakeLists.txt b/tools/cpp/channel/src/CMakeLists.txt new file mode 100644 index 00000000..9b7b7fb0 --- /dev/null +++ b/tools/cpp/channel/src/CMakeLists.txt @@ -0,0 +1 @@ +add_executable(channel main.cpp message.cpp) diff --git a/tools/cpp/channel/src/cmdline.h b/tools/cpp/channel/src/cmdline.h new file mode 100644 index 00000000..de9eaf74 --- /dev/null +++ b/tools/cpp/channel/src/cmdline.h @@ -0,0 +1,809 @@ +/* + Copyright (c) 2009, Hideyuki Tanaka + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace cmdline{ + +namespace detail{ + +template +class lexical_cast_t{ +public: + static Target cast(const Source &arg){ + Target ret; + std::stringstream ss; + if (!(ss<>ret && ss.eof())) + throw std::bad_cast(); + + return ret; + } +}; + +template +class lexical_cast_t{ +public: + static Target cast(const Source &arg){ + return arg; + } +}; + +template +class lexical_cast_t{ +public: + static std::string cast(const Source &arg){ + std::ostringstream ss; + ss< +class lexical_cast_t{ +public: + static Target cast(const std::string &arg){ + Target ret; + std::istringstream ss(arg); + if (!(ss>>ret && ss.eof())) + throw std::bad_cast(); + return ret; + } +}; + +template +struct is_same { + static const bool value = false; +}; + +template +struct is_same{ + static const bool value = true; +}; + +template +Target lexical_cast(const Source &arg) +{ + return lexical_cast_t::value>::cast(arg); +} + +static inline std::string demangle(const std::string &name) +{ + int status=0; + char *p=abi::__cxa_demangle(name.c_str(), 0, 0, &status); + std::string ret(p); + free(p); + return ret; +} + +template +std::string readable_typename() +{ + return demangle(typeid(T).name()); +} + +template +std::string default_value(T def) +{ + return detail::lexical_cast(def); +} + +template <> +inline std::string readable_typename() +{ + return "string"; +} + +} // detail + +//----- + +class cmdline_error : public std::exception { +public: + cmdline_error(const std::string &msg): msg(msg){} + ~cmdline_error() throw() {} + const char *what() const throw() { return msg.c_str(); } +private: + std::string msg; +}; + +template +struct default_reader{ + T operator()(const std::string &str){ + return detail::lexical_cast(str); + } +}; + +template +struct range_reader{ + range_reader(const T &low, const T &high): low(low), high(high) {} + T operator()(const std::string &s) const { + T ret=default_reader()(s); + if (!(ret>=low && ret<=high)) throw cmdline::cmdline_error("range_error"); + return ret; + } +private: + T low, high; +}; + +template +range_reader range(const T &low, const T &high) +{ + return range_reader(low, high); +} + +template +struct oneof_reader{ + T operator()(const std::string &s){ + T ret=default_reader()(s); + if (std::find(alt.begin(), alt.end(), ret)==alt.end()) + throw cmdline_error(""); + return ret; + } + void add(const T &v){ alt.push_back(v); } +private: + std::vector alt; +}; + +template +oneof_reader oneof(T a1) +{ + oneof_reader ret; + ret.add(a1); + return ret; +} + +template +oneof_reader oneof(T a1, T a2) +{ + oneof_reader ret; + ret.add(a1); + ret.add(a2); + return ret; +} + +template +oneof_reader oneof(T a1, T a2, T a3) +{ + oneof_reader ret; + ret.add(a1); + ret.add(a2); + ret.add(a3); + return ret; +} + +template +oneof_reader oneof(T a1, T a2, T a3, T a4) +{ + oneof_reader ret; + ret.add(a1); + ret.add(a2); + ret.add(a3); + ret.add(a4); + return ret; +} + +template +oneof_reader oneof(T a1, T a2, T a3, T a4, T a5) +{ + oneof_reader ret; + ret.add(a1); + ret.add(a2); + ret.add(a3); + ret.add(a4); + ret.add(a5); + return ret; +} + +template +oneof_reader oneof(T a1, T a2, T a3, T a4, T a5, T a6) +{ + oneof_reader ret; + ret.add(a1); + ret.add(a2); + ret.add(a3); + ret.add(a4); + ret.add(a5); + ret.add(a6); + return ret; +} + +template +oneof_reader oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7) +{ + oneof_reader ret; + ret.add(a1); + ret.add(a2); + ret.add(a3); + ret.add(a4); + ret.add(a5); + ret.add(a6); + ret.add(a7); + return ret; +} + +template +oneof_reader oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7, T a8) +{ + oneof_reader ret; + ret.add(a1); + ret.add(a2); + ret.add(a3); + ret.add(a4); + ret.add(a5); + ret.add(a6); + ret.add(a7); + ret.add(a8); + return ret; +} + +template +oneof_reader oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7, T a8, T a9) +{ + oneof_reader ret; + ret.add(a1); + ret.add(a2); + ret.add(a3); + ret.add(a4); + ret.add(a5); + ret.add(a6); + ret.add(a7); + ret.add(a8); + ret.add(a9); + return ret; +} + +template +oneof_reader oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7, T a8, T a9, T a10) +{ + oneof_reader ret; + ret.add(a1); + ret.add(a2); + ret.add(a3); + ret.add(a4); + ret.add(a5); + ret.add(a6); + ret.add(a7); + ret.add(a8); + ret.add(a9); + ret.add(a10); + return ret; +} + +//----- + +class parser{ +public: + parser(){ + } + ~parser(){ + for (std::map::iterator p=options.begin(); + p!=options.end(); p++) + delete p->second; + } + + void add(const std::string &name, + char short_name=0, + const std::string &desc=""){ + if (options.count(name)) throw cmdline_error("multiple definition: "+name); + options[name]=new option_without_value(name, short_name, desc); + ordered.push_back(options[name]); + } + + template + void add(const std::string &name, + char short_name=0, + const std::string &desc="", + bool need=true, + const T def=T()){ + add(name, short_name, desc, need, def, default_reader()); + } + + template + void add(const std::string &name, + char short_name=0, + const std::string &desc="", + bool need=true, + const T def=T(), + F reader=F()){ + if (options.count(name)) throw cmdline_error("multiple definition: "+name); + options[name]=new option_with_value_with_reader(name, short_name, need, def, desc, reader); + ordered.push_back(options[name]); + } + + void footer(const std::string &f){ + ftr=f; + } + + void set_program_name(const std::string &name){ + prog_name=name; + } + + bool exist(const std::string &name) const { + if (options.count(name)==0) throw cmdline_error("there is no flag: --"+name); + return options.find(name)->second->has_set(); + } + + template + const T &get(const std::string &name) const { + if (options.count(name)==0) throw cmdline_error("there is no flag: --"+name); + const option_with_value *p=dynamic_cast*>(options.find(name)->second); + if (p==NULL) throw cmdline_error("type mismatch flag '"+name+"'"); + return p->get(); + } + + const std::vector &rest() const { + return others; + } + + bool parse(const std::string &arg){ + std::vector args; + + std::string buf; + bool in_quote=false; + for (std::string::size_type i=0; i=arg.length()){ + errors.push_back("unexpected occurrence of '\\' at end of string"); + return false; + } + } + + buf+=arg[i]; + } + + if (in_quote){ + errors.push_back("quote is not closed"); + return false; + } + + if (buf.length()>0) + args.push_back(buf); + + for (size_t i=0; i &args){ + int argc=static_cast(args.size()); + std::vector argv(argc); + + for (int i=0; i lookup; + for (std::map::iterator p=options.begin(); + p!=options.end(); p++){ + if (p->first.length()==0) continue; + char initial=p->second->short_name(); + if (initial){ + if (lookup.count(initial)>0){ + lookup[initial]=""; + errors.push_back(std::string("short option '")+initial+"' is ambiguous"); + return false; + } + else lookup[initial]=p->first; + } + } + + for (int i=1; i &args){ + if (!options.count("help")) + add("help", '?', "print this message"); + check(args.size(), parse(args)); + } + + void parse_check(int argc, char *argv[]){ + if (!options.count("help")) + add("help", '?', "print this message"); + check(argc, parse(argc, argv)); + } + + std::string error() const{ + return errors.size()>0?errors[0]:""; + } + + std::string error_full() const{ + std::ostringstream oss; + for (size_t i=0; imust()) + oss<short_description()<<" "; + } + + oss<<"[options] ... "<name().length()); + } + for (size_t i=0; ishort_name()){ + oss<<" -"<short_name()<<", "; + } + else{ + oss<<" "; + } + + oss<<"--"<name(); + for (size_t j=ordered[i]->name().length(); jdescription()<set()){ + errors.push_back("option needs value: --"+name); + return; + } + } + + void set_option(const std::string &name, const std::string &value){ + if (options.count(name)==0){ + errors.push_back("undefined option: --"+name); + return; + } + if (!options[name]->set(value)){ + errors.push_back("option value is invalid: --"+name+"="+value); + return; + } + } + + class option_base{ + public: + virtual ~option_base(){} + + virtual bool has_value() const=0; + virtual bool set()=0; + virtual bool set(const std::string &value)=0; + virtual bool has_set() const=0; + virtual bool valid() const=0; + virtual bool must() const=0; + + virtual const std::string &name() const=0; + virtual char short_name() const=0; + virtual const std::string &description() const=0; + virtual std::string short_description() const=0; + }; + + class option_without_value : public option_base { + public: + option_without_value(const std::string &name, + char short_name, + const std::string &desc) + :nam(name), snam(short_name), desc(desc), has(false){ + } + ~option_without_value(){} + + bool has_value() const { return false; } + + bool set(){ + has=true; + return true; + } + + bool set(const std::string &){ + return false; + } + + bool has_set() const { + return has; + } + + bool valid() const{ + return true; + } + + bool must() const{ + return false; + } + + const std::string &name() const{ + return nam; + } + + char short_name() const{ + return snam; + } + + const std::string &description() const { + return desc; + } + + std::string short_description() const{ + return "--"+nam; + } + + private: + std::string nam; + char snam; + std::string desc; + bool has; + }; + + template + class option_with_value : public option_base { + public: + option_with_value(const std::string &name, + char short_name, + bool need, + const T &def, + const std::string &desc) + : nam(name), snam(short_name), need(need), has(false) + , def(def), actual(def) { + this->desc=full_description(desc); + } + ~option_with_value(){} + + const T &get() const { + return actual; + } + + bool has_value() const { return true; } + + bool set(){ + return false; + } + + bool set(const std::string &value){ + try{ + actual=read(value); + has=true; + } + catch(const std::exception &e){ + return false; + } + return true; + } + + bool has_set() const{ + return has; + } + + bool valid() const{ + if (need && !has) return false; + return true; + } + + bool must() const{ + return need; + } + + const std::string &name() const{ + return nam; + } + + char short_name() const{ + return snam; + } + + const std::string &description() const { + return desc; + } + + std::string short_description() const{ + return "--"+nam+"="+detail::readable_typename(); + } + + protected: + std::string full_description(const std::string &desc){ + return + desc+" ("+detail::readable_typename()+ + (need?"":" [="+detail::default_value(def)+"]") + +")"; + } + + virtual T read(const std::string &s)=0; + + std::string nam; + char snam; + bool need; + std::string desc; + + bool has; + T def; + T actual; + }; + + template + class option_with_value_with_reader : public option_with_value { + public: + option_with_value_with_reader(const std::string &name, + char short_name, + bool need, + const T def, + const std::string &desc, + F reader) + : option_with_value(name, short_name, need, def, desc), reader(reader){ + } + + private: + T read(const std::string &s){ + return reader(s); + } + + F reader; + }; + + std::map options; + std::vector ordered; + std::string ftr; + + std::string prog_name; + std::vector others; + + std::vector errors; +}; + +} // cmdline diff --git a/tools/cpp/channel/src/custom_exec.cpp b/tools/cpp/channel/src/custom_exec.cpp new file mode 100644 index 00000000..d7591cb2 --- /dev/null +++ b/tools/cpp/channel/src/custom_exec.cpp @@ -0,0 +1,62 @@ +#include "custom_exec.hpp" +using namespace std; +namespace FascodeUtil{ + int custom_exec_v(std::vector args){ + char** argskun=NULL; + argskun=new char*[args.size() + 2]; + for(size_t i=0;i + int custom_exec(T... args){ + std::vector args_vector; + for(string i : std::initializer_list{args...}){ + args_vector.push_back(i); + } + return custom_exec_v(args_vector); + } + int custom_exec_v_no_wait(std::vector args){ + char** argskun=NULL; + argskun=new char*[args.size() + 2]; + for(size_t i=0;i + int custom_exec_no_wait(T... args){ + std::vector args_vector; + for(string i : std::initializer_list{args...}){ + args_vector.push_back(i); + } + return custom_exec_v_no_wait(args_vector); + } +} \ No newline at end of file diff --git a/tools/cpp/channel/src/custom_exec.hpp b/tools/cpp/channel/src/custom_exec.hpp new file mode 100644 index 00000000..5cb28484 --- /dev/null +++ b/tools/cpp/channel/src/custom_exec.hpp @@ -0,0 +1,15 @@ +#pragma once +#include +#include +#include +#include +#include +#include +namespace FascodeUtil{ + int custom_exec_v(std::vector args); + template + int custom_exec(T... args); + int custom_exec_v_no_wait(std::vector args); + template + int custom_exec_no_wait(T... args); +} \ No newline at end of file diff --git a/tools/cpp/channel/src/main.cpp b/tools/cpp/channel/src/main.cpp new file mode 100644 index 00000000..900821c9 --- /dev/null +++ b/tools/cpp/channel/src/main.cpp @@ -0,0 +1,25 @@ +#include "main.hpp" +int main(int argc,char* argv[]){ + cmdline::parser p; + p.footer("[command]"); + p.add("arch",'a',"Specify the architecture"); + p.add("help",'h',"This help message"); + p.add("nobuiltin",'b',"Exclude built-in channels"); + p.add("dirname",'d',"Display directory names of all channel as it is"); + p.add("fullpath",'f',"Display the full path of the channel (Use with -db)"); + p.add("kernel",'k',"Specify the supported kernel"); + p.add("multi",'m',"Ignore channel version"); + p.add("only-add",'o',"Only additional channels"); + p.add("version",'v',"Specifies the AlterISO version"); + p.add("nocheck",0,"Do not check the channel with desc command.This option helps speed up."); + if (!p.parse(argc, argv)||p.exist("help")){ + std::cout< +#include +#include +namespace FascodeUtil{ + enum MSG_TYPE{ + INFO=0, + ERR=1, + DEBUG=2, + WARN=3 + }; + enum COLOR_TYPE{ + BLACK=30, + RED=31, + GREEN=32, + YELLOW=33, + BLUE=34, + MAGENTA=35, + CYAN=36, + WHITE=37 + }; + class msg{ + public: + void print(MSG_TYPE,std::string appname,std::string character,std::string label,COLOR_TYPE color,int numberspace,std::string message); + void print(MSG_TYPE,std::string appname,std::string character,int numberspace,std::string message); + void print(MSG_TYPE,std::string appname,int numberspace,std::string message); + void print(MSG_TYPE,std::string appname,std::string character,std::string message); + void print(MSG_TYPE,std::string appname,std::string message); + + private: + void _print(std::ostream& out_stream,std::string appname,std::string character,std::string label,COLOR_TYPE color,int numberspace,std::string message); + void _echo_type(std::ostream& out_stream,std::string character,std::string label,int label_space,COLOR_TYPE color); + + }; + +} \ No newline at end of file diff --git a/tools/cpp/channel/src/option_channels.hpp b/tools/cpp/channel/src/option_channels.hpp new file mode 100644 index 00000000..532ae197 --- /dev/null +++ b/tools/cpp/channel/src/option_channels.hpp @@ -0,0 +1,8 @@ +#pragma once +#include +#include +struct option_channels{ + std::string arch; + std::string kernel_name; + std::string version; +}; \ No newline at end of file -- 2.11.0