mirror of
https://github.com/asdlokj1qpi233/subconverter.git
synced 2026-11-03 10:12:50 +00:00
Optimize codes
Move node renaming after removing Emojis.
This commit is contained in:
6 files changed
+34
-52
No files matched your search
+4
-29
@@ -1,5 +1,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -37,35 +38,9 @@ std::string getTime(int type)
|
|||||||
|
|
||||||
void writeLog(int type, std::string content, int level)
|
void writeLog(int type, std::string content, int level)
|
||||||
{
|
{
|
||||||
//placeholder
|
|
||||||
/*
|
|
||||||
if(print_debug_info)
|
|
||||||
std::cerr<<getTime(2)<<" [DEBUG] "<<content<<"\n";
|
|
||||||
*/
|
|
||||||
if(level > global_log_level)
|
if(level > global_log_level)
|
||||||
return;
|
return;
|
||||||
//std::cerr<<getTime(2)<<" ["<<getpid()<<"]";
|
const char *levels[] = {"[FATL]", "[ERRO]", "[WARN]", "[INFO]", "[DEBG]", "[VERB]"};
|
||||||
|
std::cerr<<getTime(2)<<" ["<<getpid()<<" "<<std::this_thread::get_id()<<"]"<<levels[level % 6];
|
||||||
switch(level)
|
std::cerr<<" "<<content<<"\n";
|
||||||
{
|
|
||||||
case LOG_LEVEL_VERBOSE:
|
|
||||||
std::cerr<<getTime(2)<<" ["<<getpid()<<"]"<<"[VERB]"<<" "<<content<<"\n";
|
|
||||||
break;
|
|
||||||
case LOG_LEVEL_DEBUG:
|
|
||||||
std::cerr<<getTime(2)<<" ["<<getpid()<<"]"<<"[DEBG]"<<" "<<content<<"\n";
|
|
||||||
break;
|
|
||||||
case LOG_LEVEL_INFO:
|
|
||||||
std::cerr<<getTime(2)<<" ["<<getpid()<<"]"<<"[INFO]"<<" "<<content<<"\n";
|
|
||||||
break;
|
|
||||||
case LOG_LEVEL_WARNING:
|
|
||||||
std::cerr<<getTime(2)<<" ["<<getpid()<<"]"<<"[WARN]"<<" "<<content<<"\n";
|
|
||||||
break;
|
|
||||||
case LOG_LEVEL_ERROR:
|
|
||||||
std::cerr<<getTime(2)<<" ["<<getpid()<<"]"<<"[ERRO]"<<" "<<content<<"\n";
|
|
||||||
break;
|
|
||||||
case LOG_LEVEL_FATAL:
|
|
||||||
std::cerr<<getTime(2)<<" ["<<getpid()<<"]"<<"[FATL]"<<" "<<content<<"\n";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
//std::cerr<<" "<<content<<"\n";
|
|
||||||
}
|
}
|
||||||
+22
-13
@@ -58,11 +58,25 @@ void chkArg(int argc, char *argv[])
|
|||||||
update_ruleset_on_request = true;
|
update_ruleset_on_request = true;
|
||||||
}
|
}
|
||||||
else if(strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--file") == 0)
|
else if(strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--file") == 0)
|
||||||
pref_path.assign(argv[++i]);
|
{
|
||||||
|
if(i < argc - 1)
|
||||||
|
pref_path.assign(argv[++i]);
|
||||||
|
}
|
||||||
else if(strcmp(argv[i], "-g") == 0 || strcmp(argv[i], "--gen") == 0)
|
else if(strcmp(argv[i], "-g") == 0 || strcmp(argv[i], "--gen") == 0)
|
||||||
|
{
|
||||||
generator_mode = true;
|
generator_mode = true;
|
||||||
|
}
|
||||||
else if(strcmp(argv[i], "--artifact") == 0)
|
else if(strcmp(argv[i], "--artifact") == 0)
|
||||||
gen_profile.assign(argv[++i]);
|
{
|
||||||
|
if(i < argc - 1)
|
||||||
|
gen_profile.assign(argv[++i]);
|
||||||
|
}
|
||||||
|
else if(strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--log") == 0)
|
||||||
|
{
|
||||||
|
if(i < argc - 1)
|
||||||
|
if(freopen(argv[++i], "a", stderr) == NULL)
|
||||||
|
std::cerr<<"Error redirecting output to file.\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,6 +99,10 @@ void signal_handler(int sig)
|
|||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
if(fileExist("pref.yml"))
|
||||||
|
pref_path = "pref.yml";
|
||||||
|
chkArg(argc, argv);
|
||||||
|
setcd(pref_path); //then switch to pref directory
|
||||||
writeLog(0, "SubConverter " VERSION " starting up..", LOG_LEVEL_INFO);
|
writeLog(0, "SubConverter " VERSION " starting up..", LOG_LEVEL_INFO);
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
@@ -95,6 +113,7 @@ int main(int argc, char *argv[])
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
UINT origcp = GetConsoleOutputCP();
|
UINT origcp = GetConsoleOutputCP();
|
||||||
|
defer(SetConsoleOutputCP(origcp);)
|
||||||
SetConsoleOutputCP(65001);
|
SetConsoleOutputCP(65001);
|
||||||
#else
|
#else
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
@@ -110,23 +129,13 @@ int main(int argc, char *argv[])
|
|||||||
std::string prgpath = argv[0];
|
std::string prgpath = argv[0];
|
||||||
setcd(prgpath); //first switch to program directory
|
setcd(prgpath); //first switch to program directory
|
||||||
#endif // _DEBUG
|
#endif // _DEBUG
|
||||||
if(fileExist("pref.yml"))
|
|
||||||
pref_path = "pref.yml";
|
|
||||||
chkArg(argc, argv);
|
|
||||||
setcd(pref_path); //then switch to pref directory
|
|
||||||
readConf();
|
readConf();
|
||||||
if(!update_ruleset_on_request)
|
if(!update_ruleset_on_request)
|
||||||
refreshRulesets(rulesets, ruleset_content_array);
|
refreshRulesets(rulesets, ruleset_content_array);
|
||||||
generateBase();
|
generateBase();
|
||||||
|
|
||||||
if(generator_mode)
|
if(generator_mode)
|
||||||
{
|
return simpleGenerator();
|
||||||
int retVal = simpleGenerator();
|
|
||||||
#ifdef _WIN32
|
|
||||||
SetConsoleOutputCP(origcp);
|
|
||||||
#endif // _WIN32
|
|
||||||
return retVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
append_response("GET", "/", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
|
append_response("GET", "/", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
|
||||||
{
|
{
|
||||||
|
|||||||
+4
-9
@@ -69,7 +69,7 @@ public:
|
|||||||
template <typename T> void define(const T &value)
|
template <typename T> void define(const T &value)
|
||||||
{
|
{
|
||||||
if(_M_VALUE == -1)
|
if(_M_VALUE == -1)
|
||||||
set(value);
|
*this = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool get(const bool &def_value = false)
|
bool get(const bool &def_value = false)
|
||||||
@@ -85,12 +85,6 @@ public:
|
|||||||
return _M_VALUE;
|
return _M_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool set(const tribool &value)
|
|
||||||
{
|
|
||||||
_M_VALUE = value._M_VALUE;
|
|
||||||
return _M_VALUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool set(const std::string &str)
|
bool set(const std::string &str)
|
||||||
{
|
{
|
||||||
switch(hash_(str))
|
switch(hash_(str))
|
||||||
@@ -185,11 +179,12 @@ template <typename T> static inline void eraseElements(T &target)
|
|||||||
T().swap(target);
|
T().swap(target);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> static inline T to_number(const std::string &str, T def_value)
|
template <typename T, typename U> static inline T to_number(const U &value, T def_value = T())
|
||||||
{
|
{
|
||||||
T retval = 0.0;
|
T retval = 0.0;
|
||||||
char c;
|
char c;
|
||||||
std::stringstream ss(str);
|
std::stringstream ss;
|
||||||
|
ss << value;
|
||||||
if(!(ss >> retval))
|
if(!(ss >> retval))
|
||||||
return def_value;
|
return def_value;
|
||||||
else if(ss >> c)
|
else if(ss >> c)
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
#ifndef SA_INTERRUPT
|
#ifndef SA_INTERRUPT
|
||||||
#define SA_INTERRUPT 0 //ignore this setting
|
#define SA_INTERRUPT 0 //ignore this setting
|
||||||
#endif
|
#endif
|
||||||
|
#define SD_BOTH SHUT_RDWR
|
||||||
#ifndef __hpux
|
#ifndef __hpux
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#endif /* __hpux */
|
#endif /* __hpux */
|
||||||
|
|||||||
+2
-1
@@ -991,10 +991,11 @@ void preprocessNodes(std::vector<nodeInfo> &nodes, extra_settings &ext)
|
|||||||
{
|
{
|
||||||
std::for_each(nodes.begin(), nodes.end(), [ext](nodeInfo &x)
|
std::for_each(nodes.begin(), nodes.end(), [ext](nodeInfo &x)
|
||||||
{
|
{
|
||||||
x.remarks = nodeRename(x, ext.rename_array);
|
|
||||||
if(ext.remove_emoji)
|
if(ext.remove_emoji)
|
||||||
x.remarks = trim(removeEmoji(x.remarks));
|
x.remarks = trim(removeEmoji(x.remarks));
|
||||||
|
|
||||||
|
x.remarks = nodeRename(x, ext.rename_array);
|
||||||
|
|
||||||
if(ext.add_emoji)
|
if(ext.add_emoji)
|
||||||
x.remarks = addEmoji(x, ext.emoji_array);
|
x.remarks = addEmoji(x, ext.emoji_array);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -287,6 +287,7 @@ int start_web_server_multi(void *argv)
|
|||||||
for (i = 0; i < nthreads; i++)
|
for (i = 0; i < nthreads; i++)
|
||||||
event_base_loopbreak(base[i]); //stop the loop
|
event_base_loopbreak(base[i]); //stop the loop
|
||||||
|
|
||||||
|
shutdown(nfd, SD_BOTH); //stop accept call
|
||||||
closesocket(nfd); //close listener socket
|
closesocket(nfd); //close listener socket
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in new issue
Block a user