You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ubl-settings-bootloader/source/util.cc

460 lines
17 KiB

#include "util.h"
namespace Utils {
array<vector<string>, 5> read_csv(const string& filename) {
array<vector<string>, 5> array_vectors;
vector<string> vec_option;
vector<string> vec_opcision;
ifstream file(filename);
string line;
char delimiter = '|';
getline(file, line);
while (getline(file, line)) {
stringstream stream(line);
string option;
string opcision;
getline(stream, option, delimiter);
string line_local = stream.str();
if (line_local.find("\"") != string::npos) {
string str_delimiter = "\"";
vector<int> point = find_all(line_local, str_delimiter);
size_t len = point.size();
if (len >= 2) {
int index_start = point[len-2];
int index_end = point[len-1];
opcision = line_local.substr(index_start, index_end);
index_end = opcision.find("\"");
if (opcision.find("\"") != string::npos) {
opcision.replace(index_end, opcision.length(), "");
}
}
else {
opcision = "error";
}
}
else{
getline(stream, opcision, delimiter);
}
vec_option.push_back(option);
vec_opcision.push_back(opcision);
}
array_vectors[0] = vec_option;
array_vectors[1] = vec_opcision;
return array_vectors;
}
void write_file(string path_name, string data) {
ofstream fout(path_name, ios_base::out | ios_base::trunc);
if (fout.is_open()) {
fout << data;
fout.close();
}
}
vector<tuple<string, string>> read_csv_melody(const string& filename) {
vector<tuple<string, string>> vec_music;
ifstream file(filename);
string line;
char delimiter = '|';
getline(file, line);
while (getline(file, line)) {
stringstream stream(line);
string name;
string code;
getline(stream, name, delimiter);
getline(stream, code, delimiter);
tuple<string, string> tuple_music(name, code);
vec_music.push_back(tuple_music);
}
return vec_music;
}
string call(string cmd) {
FILE *fp;
int status;
char path[PATH_MAX] = {0};
fp = popen(cmd.c_str(), "r");
if (fp == NULL) {
exit(1);
}
while (fgets(path, PATH_MAX, fp) != NULL) {
break;
}
status = pclose(fp);
if (status == -1) {
exit(1);
}
return path;
}
vector<int> find_all(string &str_ntp, string substr) {
size_t index = 0;
vector<int> sub_index;
while ((index = str_ntp.find(substr, index)) != std::string::npos) {
index += substr.length();
sub_index.push_back(index);
}
return sub_index;
}
void str_remove(std::string& source, std::string to_remove) {
string::size_type n = to_remove.length();
for (string::size_type i = source.find(to_remove);
i != string::npos;
i = source.find(to_remove))
source.erase(i, n);
}
void str_replace_all(std::string& str_base, string str_find, string sReplacement)
{
size_t pos = 0, fpos;
while ((fpos = str_base.find(str_find, pos)) != std::string::npos)
{
str_base.replace(fpos, str_find.size(), sReplacement);
pos = fpos + sReplacement.size();
}
}
std::vector<std::string> split(std::string text, char delim) {
std::string line;
std::vector<std::string> vec;
std::stringstream ss(text);
while(std::getline(ss, line, delim)) {
vec.push_back(line);
}
return vec;
}
char *yon_char_new(char *chr)
{
if (chr){
char *newchar = (char*)malloc(strlen(chr) + 1);
memset(newchar, 0, strlen(chr) + 1);
memcpy(newchar, chr, strlen(chr));
return newchar;
} else
return NULL;
}
char *yon_char_append(char *source, char *append)
{
if (source && append)
{
int size = strlen(source) + strlen(append) + 1;
char *final = (char*)malloc(size);
memset(final, 0, size);
// if (strstr(source, "%%"))
// sprintf(final, source, append);
// else
sprintf(final, "%s%s", source, append);
return final;
}
else
return NULL;
}
char *yon_char_unite(char *source, ...){
va_list arglist;
char *new_char=NULL;
char *unite_char=NULL;
new_char=yon_char_new(source);
va_start(arglist,source);
unite_char = va_arg(arglist,char*);
while(unite_char){
new_char = yon_char_append(new_char,unite_char);
unite_char = va_arg(arglist,char*);
}
va_end(arglist);
return new_char;
}
char *yon_char_divide(char *source, int dividepos)
{
if (source&&(int)strlen(source)>=dividepos){
char *cut = (char*)malloc(dividepos + 1);
memset(cut, 0, dividepos + 1);
memcpy(cut, source, dividepos);
char *left = (char*)malloc(strlen(source) - strlen(cut));
memset(left, 0, strlen(source) - strlen(cut));
memcpy(left, source + dividepos + 1, (strlen(source) - dividepos));
memset(source, 0, strlen(source));
memcpy(source, left, strlen(left));
return cut;
}
return NULL;
}
char *yon_char_divide_search(char *source, char *dividepos, int delete_divider)
{
if (delete_divider){};
if (source&&dividepos){
char *cut = strstr(source, dividepos);
if (cut)
{
int leng = strlen(source) - strlen(cut);
cut = yon_char_divide(source, leng);
return cut;
}
}
return source;
}
char **yon_char_parse(char *parameters, int *size, char *divider){
*size=0;
if (parameters){
char **string=NULL;
char *paramline=yon_char_new(parameters);
char *param = strstr(paramline,divider);
for (;param;param=strstr(paramline,divider)){
string = (!string) ? (char **)malloc(sizeof(char*)*((*size)+1)) : (char **)realloc(string, sizeof(char*)*((*size)+1));
string[(*size)]=yon_char_divide(paramline,strlen(paramline)-strlen(param));
*size=(*size)+1;
}
string = (!string) ? (char **)malloc(sizeof(char*)*((*size)+1)) : (char **)realloc(string, sizeof(char*)*((*size)+1));
string[(*size)]=paramline;
(*size)+=1;
return string;
}
return NULL;
}
#define check_window_config_setup if(__yon_window_config_target_window)
typedef struct {
char *parameter_name;
enum YON_TYPE containing_type;
GtkWidget *track_widget;
char *property_name;
} __yon_listener_parameter;
typedef struct {
char *parameter_name;
char *section;
enum YON_TYPE containing_type;
void *property;
} __yon_custom_parameter;
struct {
int x;
int y;
int width;
int height;
int fullscreen;
dictionary *custom_listeners;
dictionary *custom_parameters;
dictionary *deleted_parameters;
} __yon_main_window_config;
static GtkWindow *__yon_window_config_target_window = NULL;
static GKeyFile *__yon_window_config_file = NULL;
static char *__yon_window_config_path = NULL;
void yon_window_config_save();
void yon_window_config_save(){
g_key_file_set_integer(__yon_window_config_file,"window","WindowPosX",__yon_main_window_config.x);
g_key_file_set_integer(__yon_window_config_file,"window","WindowPosY",__yon_main_window_config.y);
g_key_file_set_integer(__yon_window_config_file,"window","WindowWidth",__yon_main_window_config.width);
g_key_file_set_integer(__yon_window_config_file,"window","WindowHeight",__yon_main_window_config.height);
g_key_file_set_integer(__yon_window_config_file,"window","fullscreen",__yon_main_window_config.fullscreen);
dictionary *dict=NULL;
if (__yon_main_window_config.deleted_parameters)
for_dictionaries(dict,__yon_main_window_config.deleted_parameters){
__yon_custom_parameter *param = yon_dictionary_get_data(dict,__yon_custom_parameter*);
g_key_file_remove_key(__yon_window_config_file,param->section,param->parameter_name,NULL);
}
if (__yon_main_window_config.custom_listeners)
for_dictionaries(dict,__yon_main_window_config.custom_listeners){
__yon_listener_parameter *param = yon_dictionary_get_data(dict,__yon_listener_parameter*);
GValue *val = (GValue*)g_malloc0(sizeof(GValue));
g_object_get_property(G_OBJECT(param->track_widget),param->property_name,val);
switch(param->containing_type){
case YON_TYPE_STRING:
g_key_file_set_string(__yon_window_config_file,"window",param->parameter_name, g_value_get_string(val));
break;
case YON_TYPE_INT:
g_key_file_set_integer(__yon_window_config_file,"window",param->parameter_name, g_value_get_int(val));
break;
case YON_TYPE_BOOLEAN:
g_key_file_set_boolean(__yon_window_config_file,"window",param->parameter_name, g_value_get_boolean(val));
break;
case YON_TYPE_STRING_LIST:break;
case YON_TYPE_OTHER:printf("\033[0;31mCannot save %s property with %s key\033[0m\n",param->property_name,param->parameter_name);break;
}
}
if (__yon_main_window_config.custom_parameters)
for_dictionaries(dict,__yon_main_window_config.custom_parameters){
__yon_custom_parameter *param = yon_dictionary_get_data(dict,__yon_custom_parameter*);
switch (param->containing_type){
case YON_TYPE_STRING:
g_key_file_set_string(__yon_window_config_file,param->section,param->parameter_name, (char*)param->property);
break;
case YON_TYPE_INT:
g_key_file_set_integer(__yon_window_config_file,param->section,param->parameter_name, *(int*)param->property);
break;
case YON_TYPE_BOOLEAN:
g_key_file_set_boolean(__yon_window_config_file,param->section,param->parameter_name, *(gboolean*)param->property);
break;
case YON_TYPE_STRING_LIST:break;
default:
break;
}
}
g_key_file_save_to_file(__yon_window_config_file,__yon_window_config_path,NULL);
}
void yon_get_is_fullscreen();
void yon_get_is_fullscreen(){
gtk_window_is_maximized(__yon_window_config_target_window);
__yon_main_window_config.fullscreen = gtk_window_is_maximized(__yon_window_config_target_window);
if (!__yon_main_window_config.fullscreen) gtk_window_get_position(__yon_window_config_target_window,&__yon_main_window_config.x,&__yon_main_window_config.y);
}
/**yon_on_configured_window_destroy(GtkWidget* self,GdkEvent* event)
* [EN]
*
* [RU]
* Сохраняет настройки основного окна. Вызывается когда основное окно уничтожается.
*/
void yon_on_configured_window_destroy(GtkWidget* self,GdkEvent* event);
void yon_on_configured_window_destroy(GtkWidget* self,GdkEvent* event){
if (self&&event){};
check_window_config_setup{
yon_get_is_fullscreen();
yon_window_config_save();
}
gtk_main_quit();
}
void __yon_window_config_on_resize();
void __yon_window_config_on_resize(){
int max=0;
max=gtk_window_is_maximized(__yon_window_config_target_window);
if(max==0){
gtk_window_get_size(__yon_window_config_target_window,&__yon_main_window_config.width,&__yon_main_window_config.height);
gtk_window_get_position(__yon_window_config_target_window,&__yon_main_window_config.x,&__yon_main_window_config.y);
}
}
void yon_window_config_setup(GtkWindow *window){
__yon_window_config_target_window = window;
g_signal_connect(G_OBJECT(window),"delete-event",G_CALLBACK(yon_on_configured_window_destroy),NULL);
g_signal_connect(G_OBJECT(window),"check-resize"/*"configure-event"*/,G_CALLBACK(__yon_window_config_on_resize),NULL);
}
void *_yon_maximize(void *data);
void *_yon_maximize(void *data){
if (data){};
g_usleep(G_USEC_PER_SEC/10);
if(__yon_main_window_config.fullscreen ==1) gtk_window_maximize(__yon_window_config_target_window);
return NULL;
}
int yon_window_config_load(char *path){
__yon_window_config_file = g_key_file_new();
__yon_window_config_path=yon_char_new(path);
if (!g_key_file_load_from_file(__yon_window_config_file,__yon_window_config_path,G_KEY_FILE_NONE,NULL)){
struct stat st;
int size;
config_str conf = yon_char_parse(yon_char_new(__yon_window_config_path),&size,(char*)"/");
char *path = yon_char_unite(conf[0],(char*)"/",conf[1],(char*)"/",conf[2],(char*)"/",conf[3],(char*)"/",conf[4],(char*)"/",NULL);
if (stat(path, &st) == -1) {
mkdir(path, 0777);
}
FILE *fp;
fp=fopen(__yon_window_config_path,"w");
chmod(__yon_window_config_path,0777);
fclose(fp);
g_key_file_load_from_file(__yon_window_config_file,__yon_window_config_path,G_KEY_FILE_NONE,NULL);
}
__yon_main_window_config.x = g_key_file_get_integer(__yon_window_config_file,"window","WindowPosX",NULL);
__yon_main_window_config.y = g_key_file_get_integer(__yon_window_config_file,"window","WindowPosY",NULL);
__yon_main_window_config.width = g_key_file_get_integer(__yon_window_config_file,"window","WindowWidth",NULL);
__yon_main_window_config.height = g_key_file_get_integer(__yon_window_config_file,"window","WindowHeight",NULL);
__yon_main_window_config.fullscreen = g_key_file_get_integer(__yon_window_config_file,"window","fullscreen",NULL);
dictionary *dict=NULL;
if (__yon_main_window_config.custom_listeners)
for_dictionaries(dict,__yon_main_window_config.custom_listeners){
__yon_listener_parameter *param = yon_dictionary_get_data(dict,__yon_listener_parameter*);
GValue *val = (GValue*)g_malloc0(sizeof(GValue));
g_object_get_property(G_OBJECT(param->track_widget),param->property_name,val);
gboolean res=0;
switch(param->containing_type){
case YON_TYPE_STRING:
g_value_set_string(val,g_key_file_get_string(__yon_window_config_file,"window",param->parameter_name, NULL));
break;
case YON_TYPE_INT:
g_value_set_int(val,g_key_file_get_integer(__yon_window_config_file,"window",param->parameter_name, NULL));
break;
case YON_TYPE_BOOLEAN:
res = g_key_file_get_boolean(__yon_window_config_file,"window",param->parameter_name, NULL);
g_value_set_boolean(val,res);
break;
case YON_TYPE_STRING_LIST:break;
case YON_TYPE_OTHER:break;
}
g_object_set_property(G_OBJECT(param->track_widget),param->property_name,val);
}
if (__yon_main_window_config.width==0) __yon_main_window_config.width=800;
if (__yon_main_window_config.height==0) __yon_main_window_config.height=600;
gtk_window_resize(__yon_window_config_target_window,__yon_main_window_config.width,__yon_main_window_config.height);
gtk_window_move(__yon_window_config_target_window,__yon_main_window_config.x,__yon_main_window_config.y);
pthread_t tid;
pthread_create(&tid, NULL, (&_yon_maximize), NULL);
return 1;
}
void yon_window_config_apply();
void yon_window_config_apply(){
gtk_window_move(__yon_window_config_target_window,__yon_main_window_config.x,__yon_main_window_config.y);
gtk_window_resize(__yon_window_config_target_window,__yon_main_window_config.width,__yon_main_window_config.height);
}
config_str yon_window_config_get_section(char *section, gsize *size){
config_str key = g_key_file_get_keys(__yon_window_config_file,section,size,NULL);
return key;
}
char *yon_ubl_user_get_home_directory(){
FILE *path = popen(get_home_dir_command,"r");
char *ret = (char*)malloc(4096);
memset(ret,0,4096);
if (fgets(ret,4096,path)){
ret=yon_char_divide_search(ret,(char*)"\n",-1);
return ret;
} else return NULL;
}
char *yon_ubl_root_user_get(){
char *user=NULL;
if (yon_ubl_check_root()){
user=getenv("SUDO_USER");
if (user&&strcmp(user,"")!=0){
return user;
}else {
FILE *file = popen("getent passwd $PKEXEC_UID | cut -d: -f1","r");
user=(char*)malloc(4096);
memset(user,0,4096);
if (fgets(user,4096,file)){
user=yon_char_divide_search(user,(char*)"\n",-1);
if (user) return user;
} else return NULL;
}
}
return getlogin();
}
int yon_ubl_check_root(){
if (getuid()==0) return 1;
else return 0;
}
}