Branch
Hash :
adb9e235
Author :
Thomas de Grivel
Date :
2023-01-14T18:26:30
license
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
/* rtbuf
* Copyright 2018-2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software excepted
* on Apple computers granted the above copyright notice and
* this permission paragraph are included in all copies and
* substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include "../librtbuf/rtbuf.h"
#include "../librtbuf/lib.h"
#include "rtbuf_gtk3_library.h"
#include "queue.h"
s_rtbuf_gtk_library_tree *g_rtbuf_gtk_library_tree = NULL;
s_rtbuf_gtk_library_tree * rtbuf_gtk_library_tree_new
(const char *dir_name, const char *leaf_name,
s_rtbuf_gtk_library_tree *children,
s_rtbuf_gtk_library_tree *next)
{
s_rtbuf_gtk_library_tree *tree =
malloc(sizeof(s_rtbuf_gtk_library_tree));
if (tree) {
tree->dir_name = dir_name;
tree->leaf_name = leaf_name;
tree->children = children;
tree->next = next;
}
return tree;
}
void rtbuf_gtk_library_tree_delete (s_rtbuf_gtk_library_tree *tree)
{
s_queue q;
assert(tree);
queue_init(&q);
queue_enqueue(&q, tree);
while (q.length > 0) {
tree = queue_dequeue(&q);
if (tree->children)
queue_enqueue(&q, tree->children);
if (tree->next)
queue_enqueue(&q, tree->next);
free((void*) tree->dir_name);
tree->dir_name = 0;
free((void*) tree->leaf_name);
tree->leaf_name = 0;
free(tree);
}
queue_destroy(&q);
}
s_rtbuf_gtk_library_tree *
rtbuf_gtk_library_tree_find_dir_name
(s_rtbuf_gtk_library_tree *tree,
const char *dir_name,
size_t len)
{
while (tree) {
if (strncmp(tree->dir_name, dir_name, len) == 0)
return tree;
tree = tree->next;
}
return NULL;
}
s_rtbuf_gtk_library_tree *
rtbuf_gtk_library_tree_insert (s_rtbuf_gtk_library_tree **tree,
const char *dir_name,
const char *leaf_name)
{
s_rtbuf_gtk_library_tree *t;
assert(tree);
t = rtbuf_gtk_library_tree_new(dir_name, leaf_name, NULL, *tree);
*tree = t;
return t;
}
void rtbuf_gtk_library_load_file_1 (const char *path,
size_t len,
size_t prefix_len,
size_t suffix_len)
{
const char *dir_name;
const char *sep;
int leaf = 0;
s_rtbuf_gtk_library_tree **tree = &g_rtbuf_gtk_library_tree;
s_rtbuf_gtk_library_tree *found;
/* printf("Found library %s\n", path); */
dir_name = path + prefix_len;
while (!leaf) {
sep = strchr(dir_name, '/');
if (!sep) {
sep = path + len - suffix_len;
leaf = 1;
}
found = rtbuf_gtk_library_tree_find_dir_name(*tree,
dir_name,
sep - dir_name);
if (!found) {
char *m_leaf_name = 0;
size_t dir_len = sep - dir_name;
char *m_dir_name = malloc(dir_len + 1);
memcpy(m_dir_name, dir_name, dir_len);
m_dir_name[dir_len] = 0;
if (leaf) {
size_t leaf_len = len - prefix_len - suffix_len;
m_leaf_name = malloc(leaf_len + 1);
memcpy(m_leaf_name, path + prefix_len, leaf_len);
m_leaf_name[leaf_len] = 0;
}
found = rtbuf_gtk_library_tree_insert(tree, m_dir_name, m_leaf_name);
}
/* printf("dir_name %s\n", found->dir_name);
if (leaf)
printf("leaf_name %s\n", found->leaf_name); */
tree = &found->children;
dir_name = sep + 1;
}
}
void rtbuf_gtk_library_load_file (const char *path, size_t prefix_len)
{
size_t len = strlen(path);
/* printf("file %s\n", path); */
if (strncmp(&path[len - 3], ".so", 3) == 0)
rtbuf_gtk_library_load_file_1(path, len, prefix_len, 3);
else if (strncmp(&path[len - 7], ".so.0.0", 7) == 0)
rtbuf_gtk_library_load_file_1(path, len, prefix_len, 7);
else if (strncmp(&path[len - 6], "-0.dll", 6) == 0)
rtbuf_gtk_library_load_file_1(path, len, prefix_len, 6);
}
int is_directory (const char *path)
{
struct stat sb;
if (stat(path, &sb)) {
warn("stat: %s", path);
return -1;
}
return S_ISDIR(sb.st_mode);
}
void rtbuf_gtk_library_load_directory (const char *path, size_t prefix_len)
{
DIR *dir;
/* printf("dir %s\n", path); */
dir = opendir(path);
if (dir) {
struct dirent *dp;
char child_path[PATH_MAX];
const char *in = path;
char *out = child_path;
while (*in)
*out++ = *in++;
while ((dp = readdir(dir)) != NULL) {
char *name = dp->d_name;
char *filename = out;
int dir_p;
if (name[0] != '.') {
in = name;
while (*in)
*filename++ = *in++;
*filename = 0;
dir_p = is_directory(child_path);
if (dir_p < 0)
continue;
else if (dir_p) {
*filename++ = '/';
*filename = 0;
rtbuf_gtk_library_load_directory(child_path, prefix_len);
}
else
rtbuf_gtk_library_load_file(child_path, prefix_len);
}
}
}
}
void rtbuf_gtk_library_load ()
{
char **path = g_rtbuf_lib_path;
if (g_rtbuf_gtk_library_tree) {
rtbuf_gtk_library_tree_delete(g_rtbuf_gtk_library_tree);
g_rtbuf_gtk_library_tree = NULL;
}
while (*path) {
printf("Loading rtbuf library in %s\n", *path);
rtbuf_gtk_library_load_directory(*path, strlen(*path));
path++;
}
}