Removed utils_split(), strtok_r() is now used instead

This commit is contained in:
Scoopta 2019-11-04 22:41:53 -08:00
parent 2ab1c53b4d
commit 07cb2a65a7
5 changed files with 33 additions and 52 deletions

View File

@ -32,6 +32,4 @@ void utils_sleep_millis(time_t millis);
char* utils_concat(size_t arg_count, ...); char* utils_concat(size_t arg_count, ...);
size_t utils_split(char* str, const char chr);
#endif #endif

View File

@ -135,17 +135,16 @@ void wofi_drun_init() {
data_dirs = "/usr/local/share:/usr/share"; data_dirs = "/usr/local/share:/usr/share";
} }
char* dirs = utils_concat(3, data_home, ":", data_dirs); char* dirs = utils_concat(3, data_home, ":", data_dirs);
char* original_dirs = dirs;
free(data_home); free(data_home);
size_t colon_count = utils_split(dirs, ':'); char* save_ptr;
for(size_t count = 0; count < colon_count; ++count) { char* str = strtok_r(dirs, ":", &save_ptr);
char* app_dir = utils_concat(2, dirs, "/applications"); do {
char* app_dir = utils_concat(2, str, "/applications");
insert_dir(app_dir, cached, entries); insert_dir(app_dir, cached, entries);
dirs += strlen(dirs) + 1;
free(app_dir); free(app_dir);
} } while((str = strtok_r(NULL, ":", &save_ptr)) != NULL);
free(original_dirs); free(dirs);
map_free(cached); map_free(cached);
map_free(entries); map_free(entries);
} }

View File

@ -36,19 +36,20 @@ void wofi_run_init() {
free(cache); free(cache);
char* path = strdup(getenv("PATH")); char* path = strdup(getenv("PATH"));
char* original_path = path;
size_t colon_count = utils_split(path, ':'); char* save_ptr;
for(size_t count = 0; count < colon_count; ++count) { char* str = strtok_r(path, ":", &save_ptr);
DIR* dir = opendir(path); do {
DIR* dir = opendir(str);
if(dir == NULL) { if(dir == NULL) {
goto cont; continue;
} }
struct dirent* entry; struct dirent* entry;
while((entry = readdir(dir)) != NULL) { while((entry = readdir(dir)) != NULL) {
if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue; continue;
} }
char* full_path = utils_concat(3, path, "/", entry->d_name); char* full_path = utils_concat(3, str, "/", entry->d_name);
struct stat info; struct stat info;
stat(full_path, &info); stat(full_path, &info);
if(access(full_path, X_OK) == 0 && S_ISREG(info.st_mode) && !map_contains(cached, full_path)) { if(access(full_path, X_OK) == 0 && S_ISREG(info.st_mode) && !map_contains(cached, full_path)) {
@ -59,10 +60,8 @@ void wofi_run_init() {
free(full_path); free(full_path);
} }
closedir(dir); closedir(dir);
cont: } while((str = strtok_r(NULL, ":", &save_ptr)) != NULL);
path += strlen(path) + 1; free(path);
}
free(original_path);
map_free(cached); map_free(cached);
} }

View File

@ -47,13 +47,3 @@ char* utils_concat(size_t arg_count, ...) {
va_end(args); va_end(args);
return buffer; return buffer;
} }
size_t utils_split(char* str, const char chr) {
char* split = strchr(str, chr);
size_t count = 1;
for(; split != NULL; ++count) {
*split = 0;
split = strchr(split + 1, chr);
}
return count;
}

View File

@ -91,16 +91,16 @@ static GtkWidget* create_label(char* mode, char* text, char* search_text, char*
wofi_property_box_add_property(WOFI_PROPERTY_BOX(box), "action", action); wofi_property_box_add_property(WOFI_PROPERTY_BOX(box), "action", action);
if(allow_images) { if(allow_images) {
char* tmp = strdup(text); char* tmp = strdup(text);
char* original = tmp;
char* mode = NULL; char* mode = NULL;
size_t colon_count = utils_split(tmp, ':'); char* save_ptr;
for(size_t count = 0; count < colon_count; ++count) { char* str = strtok_r(tmp, ":", &save_ptr);
do {
if(mode == NULL) { if(mode == NULL) {
mode = tmp; mode = str;
} else { } else {
if(strcmp(mode, "img") == 0) { if(strcmp(mode, "img") == 0) {
GdkPixbuf* buf = gdk_pixbuf_new_from_file(tmp, NULL); GdkPixbuf* buf = gdk_pixbuf_new_from_file(str, NULL);
int width = gdk_pixbuf_get_width(buf); int width = gdk_pixbuf_get_width(buf);
int height = gdk_pixbuf_get_height(buf); int height = gdk_pixbuf_get_height(buf);
if(height > width) { if(height > width) {
@ -118,7 +118,7 @@ static GtkWidget* create_label(char* mode, char* text, char* search_text, char*
gtk_widget_set_name(img, "img"); gtk_widget_set_name(img, "img");
gtk_container_add(GTK_CONTAINER(box), img); gtk_container_add(GTK_CONTAINER(box), img);
} else if(strcmp(mode, "text") == 0) { } else if(strcmp(mode, "text") == 0) {
GtkWidget* label = gtk_label_new(tmp); GtkWidget* label = gtk_label_new(str);
gtk_widget_set_name(label, "text"); gtk_widget_set_name(label, "text");
gtk_label_set_use_markup(GTK_LABEL(label), allow_markup); gtk_label_set_use_markup(GTK_LABEL(label), allow_markup);
gtk_label_set_xalign(GTK_LABEL(label), 0); gtk_label_set_xalign(GTK_LABEL(label), 0);
@ -126,9 +126,8 @@ static GtkWidget* create_label(char* mode, char* text, char* search_text, char*
} }
mode = NULL; mode = NULL;
} }
tmp += strlen(tmp) + 1; } while((str = strtok_r(NULL, ":", &save_ptr)) != NULL);
} free(tmp);
free(original);
} else { } else {
GtkWidget* label = gtk_label_new(text); GtkWidget* label = gtk_label_new(text);
gtk_widget_set_name(label, "text"); gtk_widget_set_name(label, "text");
@ -154,7 +153,7 @@ static gboolean _insert_widget(gpointer data) {
return FALSE; return FALSE;
} }
static char* get_cache_path(char* mode) { static char* get_cache_path(const gchar* mode) {
if(cache_file != NULL) { if(cache_file != NULL) {
return strdup(cache_file); return strdup(cache_file);
} }
@ -243,7 +242,7 @@ void wofi_term_run(const char* cmd) {
exit(1); exit(1);
} }
static void execute_action(char* mode, const gchar* cmd) { static void execute_action(const gchar* mode, const gchar* cmd) {
struct mode* mode_ptr = map_get(modes, mode); struct mode* mode_ptr = map_get(modes, mode);
char* cache_path = get_cache_path(mode); char* cache_path = get_cache_path(mode);
struct wl_list lines; struct wl_list lines;
@ -436,19 +435,15 @@ static void add_mode(char* mode) {
} }
static void* start_thread(void* data) { static void* start_thread(void* data) {
char* modes = data; (void) data;
if(strchr(modes, ',') != NULL) { if(strchr(mode, ',') != NULL) {
char* save_ptr; char* save_ptr;
char* _mode = strtok_r(modes, ",", &save_ptr); char* str = strtok_r(mode, ",", &save_ptr);
if(mode == NULL) {
mode = _mode;
}
do { do {
add_mode(_mode); add_mode(str);
} while((_mode = strtok_r(NULL, ",", &save_ptr)) != NULL); } while((str = strtok_r(NULL, ",", &save_ptr)) != NULL);
} else { } else {
mode = modes; add_mode(mode);
add_mode(modes);
} }
return NULL; return NULL;
} }
@ -459,7 +454,7 @@ void wofi_init(struct map* config) {
x = strtol(config_get(config, "x", "-1"), NULL, 10); x = strtol(config_get(config, "x", "-1"), NULL, 10);
y = strtol(config_get(config, "y", "-1"), NULL, 10); y = strtol(config_get(config, "y", "-1"), NULL, 10);
bool normal_window = strcmp(config_get(config, "normal_window", "false"), "true") == 0; bool normal_window = strcmp(config_get(config, "normal_window", "false"), "true") == 0;
char* mode = map_get(config, "mode"); mode = map_get(config, "mode");
uint8_t orientation = config_get_mnemonic(config, "orientation", "vertical", 2, "vertical", "horizontal"); uint8_t orientation = config_get_mnemonic(config, "orientation", "vertical", 2, "vertical", "horizontal");
outer_orientation = config_get_mnemonic(config, "orientation", "vertical", 2, "horizontal", "vertical"); outer_orientation = config_get_mnemonic(config, "orientation", "vertical", 2, "horizontal", "vertical");
uint8_t halign = config_get_mnemonic(config, "halign", "fill", 4, "fill", "start", "end", "center"); uint8_t halign = config_get_mnemonic(config, "halign", "fill", 4, "fill", "start", "end", "center");
@ -551,7 +546,7 @@ void wofi_init(struct map* config) {
g_signal_connect(window, "key-press-event", G_CALLBACK(key_press), NULL); g_signal_connect(window, "key-press-event", G_CALLBACK(key_press), NULL);
pthread_t thread; pthread_t thread;
pthread_create(&thread, NULL, start_thread, mode); pthread_create(&thread, NULL, start_thread, NULL);
gtk_widget_grab_focus(scroll); gtk_widget_grab_focus(scroll);
gtk_window_set_title(GTK_WINDOW(window), prompt); gtk_window_set_title(GTK_WINDOW(window), prompt);
gtk_widget_show_all(window); gtk_widget_show_all(window);