Compare commits
15 Commits
f6320d64cd
...
c29c687076
| Author | SHA1 | Date | |
|---|---|---|---|
| c29c687076 | |||
| c41131eac8 | |||
| eb759982db | |||
| 0ee94af705 | |||
| 5b16d98e51 | |||
| 5156971945 | |||
| c887a18a10 | |||
| c0f139dde6 | |||
| ee11ff240f | |||
| 32d4d0abf2 | |||
| f093c0fb6e | |||
| 9145e3bac7 | |||
| fcf44cc288 | |||
| 4d6f2afa5a | |||
| 7cbd1eb162 |
+1
-1
@@ -48,7 +48,7 @@ enum fx_status fx_arglist_report_missing_option(
|
||||
}
|
||||
|
||||
fx_err("required option `" F_YELLOW "%s" F_RESET "` was not specified.",
|
||||
fx_stringstream_ptr(opt_name));
|
||||
fx_stringstream_get_cstr(opt_name));
|
||||
fx_i("usage: %s", fx_string_get_cstr(opt_string));
|
||||
fx_i("for more information, use `" F_YELLOW "--help" F_RESET "`");
|
||||
|
||||
|
||||
@@ -68,9 +68,14 @@ static fx_status convert_key_to_index(
|
||||
return status;
|
||||
}
|
||||
|
||||
size_t index = hash % table_size, i = 1;
|
||||
size_t base_index = hash % table_size;
|
||||
size_t index = base_index, i = 0;
|
||||
bool index_found = false;
|
||||
while (table[index].i_item && i <= table_size) {
|
||||
while (i <= table_size) {
|
||||
if (!table[index].i_item) {
|
||||
goto skip;
|
||||
}
|
||||
|
||||
const struct fx_hashtable_item_p *item = fx_object_get_private(
|
||||
table[index].i_item,
|
||||
FX_TYPE_HASHTABLE_ITEM);
|
||||
@@ -80,7 +85,8 @@ static fx_status convert_key_to_index(
|
||||
break;
|
||||
}
|
||||
|
||||
index = (index + (i * i)) % table_size;
|
||||
skip:
|
||||
index = (base_index + (i * i)) % table_size;
|
||||
i++;
|
||||
}
|
||||
|
||||
@@ -108,7 +114,7 @@ static fx_status allocate_index_for_key(
|
||||
size_t index = hash % table_size;
|
||||
bool ok = false;
|
||||
|
||||
for (size_t i = 1; i < table_size; i++) {
|
||||
for (size_t i = 0; i < table_size; i++) {
|
||||
if (!table[index].i_item) {
|
||||
ok = true;
|
||||
break;
|
||||
@@ -276,18 +282,27 @@ static fx_status hashtable_put(
|
||||
break;
|
||||
} while (1);
|
||||
|
||||
fx_hashtable_item *item = fx_object_create(FX_TYPE_HASHTABLE_ITEM);
|
||||
fx_hashtable_item *item = hashtable->t_items[index].i_item;
|
||||
struct fx_hashtable_item_p *item_p = NULL;
|
||||
if (!item) {
|
||||
item = fx_object_create(FX_TYPE_HASHTABLE_ITEM);
|
||||
if (!item) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
struct fx_hashtable_item_p *item_p
|
||||
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
|
||||
item_p = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
|
||||
|
||||
fx_value_copy(&item_p->i_key, key);
|
||||
fx_value_copy(&item_p->i_value, value);
|
||||
|
||||
hashtable->t_items[index].i_item = item;
|
||||
hashtable->t_count++;
|
||||
} else {
|
||||
item_p = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
|
||||
fx_value_unset(&item_p->i_value);
|
||||
}
|
||||
|
||||
fx_value_copy(&item_p->i_value, value);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ int main(int argc, const char **argv)
|
||||
fx_stream_read_all_bytes_s(fx_stdin, dest_stream, buf, &nr_read);
|
||||
|
||||
printf("done. read %zu bytes total.\n", nr_read);
|
||||
printf("%s\n", fx_stringstream_ptr(dest_stream));
|
||||
printf("%s\n", fx_stringstream_get_cstr(dest_stream));
|
||||
|
||||
fx_stringstream_unref(dest_stream);
|
||||
fx_stream_buffer_unref(buf);
|
||||
|
||||
@@ -158,8 +158,6 @@ static fx_status process_start(struct fx_process_p *proc)
|
||||
fx_string_unref(tmp);
|
||||
}
|
||||
|
||||
fprintf(stderr, "argc=%zu, argv=%p\n", argc, argv);
|
||||
fprintf(stderr, "envc=%zu, envp=%p\n", envc, envp);
|
||||
execve(fx_string_get_cstr(proc->proc_exec_path),
|
||||
(char *const *)argv,
|
||||
(char *const *)envp);
|
||||
|
||||
@@ -18,18 +18,25 @@ FX_TYPE_CLASS_DECLARATION_END(fx_path)
|
||||
|
||||
#define FX_RV_PATH(cstr) FX_RV(fx_path_create_from_cstr(cstr))
|
||||
|
||||
typedef enum fx_system_path {
|
||||
FX_PATH_ROOT = 0,
|
||||
FX_PATH_CWD,
|
||||
FX_PATH_HOME,
|
||||
FX_PATH_CONFIG,
|
||||
} fx_system_path;
|
||||
|
||||
struct fx_file_info;
|
||||
|
||||
FX_API fx_type_id fx_path_get_type(void);
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_path, FX_TYPE_PATH);
|
||||
|
||||
FX_API fx_path *fx_path_create_root();
|
||||
FX_API fx_path *fx_path_create_cwd();
|
||||
FX_API fx_path *fx_path_create_from_cstr(const char *path);
|
||||
FX_API fx_path *fx_path_get_system(fx_system_path path_type);
|
||||
FX_API fx_path *fx_path_duplicate(const fx_path *path);
|
||||
|
||||
FX_API fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths);
|
||||
FX_API fx_path *fx_path_join_array(const fx_value *values[], size_t nr_values);
|
||||
FX_API fx_path *fx_path_join_list(size_t count, ...);
|
||||
|
||||
FX_API fx_path *fx_path_make_absolute(const fx_path *in);
|
||||
FX_API fx_path *fx_path_make_relative(const fx_path *in);
|
||||
@@ -49,7 +56,7 @@ FX_API enum fx_status fx_path_get_filename(
|
||||
const fx_path *path,
|
||||
fx_string *out_name);
|
||||
|
||||
FX_API const char *fx_path_ptr(const fx_path *path);
|
||||
FX_API const char *fx_path_get_cstr(const fx_path *path);
|
||||
FX_API size_t fx_path_length(const fx_path *path);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
@@ -51,12 +51,12 @@ static const fx_path *directory_get_rel_path(const struct fx_directory_p *dir)
|
||||
|
||||
static const char *directory_get_path_cstr(const struct fx_directory_p *dir)
|
||||
{
|
||||
return fx_path_ptr(dir->d_path_abs);
|
||||
return fx_path_get_cstr(dir->d_path_abs);
|
||||
}
|
||||
|
||||
static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
|
||||
{
|
||||
return fx_path_ptr(dir->d_path_rel);
|
||||
return fx_path_get_cstr(dir->d_path_rel);
|
||||
}
|
||||
|
||||
static fx_result directory_delete(
|
||||
@@ -263,7 +263,7 @@ static fx_result directory_open(
|
||||
|
||||
int root_fd = root ? root->d_fd : AT_FDCWD;
|
||||
|
||||
const char *path_cstr = fx_path_ptr(path);
|
||||
const char *path_cstr = fx_path_get_cstr(path);
|
||||
if (root) {
|
||||
while (*path_cstr == '/') {
|
||||
path_cstr++;
|
||||
@@ -571,7 +571,7 @@ fx_iterator *fx_directory_begin(
|
||||
int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR;
|
||||
|
||||
const char *path_list[] = {
|
||||
fx_path_ptr(it->_p->d_path_abs),
|
||||
fx_path_get_cstr(it->_p->d_path_abs),
|
||||
NULL,
|
||||
};
|
||||
|
||||
|
||||
@@ -248,11 +248,11 @@ static enum fx_status file_swap_shadow(
|
||||
|
||||
int err;
|
||||
|
||||
err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path));
|
||||
err = rename(fx_path_get_cstr(main_file->path), fx_path_get_cstr(tmp_path));
|
||||
err = rename(
|
||||
fx_path_ptr(shadow_file->path),
|
||||
fx_path_ptr(main_file->path));
|
||||
err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path));
|
||||
fx_path_get_cstr(shadow_file->path),
|
||||
fx_path_get_cstr(main_file->path));
|
||||
err = rename(fx_path_get_cstr(tmp_path), fx_path_get_cstr(shadow_file->path));
|
||||
|
||||
fx_path_unref(tmp_path);
|
||||
|
||||
@@ -441,7 +441,7 @@ fx_result fx_file_open(
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
int fd = open(fx_path_ptr(abs_path), flags, 0644);
|
||||
int fd = open(fx_path_get_cstr(abs_path), flags, 0644);
|
||||
if (fd == -1) {
|
||||
fx_path_unref(abs_path);
|
||||
return FX_RESULT_STATUS(
|
||||
|
||||
+55
-42
@@ -41,7 +41,7 @@ static bool path_is_absolute(const struct fx_path_p *path)
|
||||
return s[0] == '/';
|
||||
}
|
||||
|
||||
static const char *path_ptr(const struct fx_path_p *path)
|
||||
static const char *path_get_cstr(const struct fx_path_p *path)
|
||||
{
|
||||
return fx_string_get_cstr(path->p_pathstr);
|
||||
}
|
||||
@@ -51,7 +51,7 @@ static enum fx_status path_stat(
|
||||
struct fx_file_info *out)
|
||||
{
|
||||
struct stat st;
|
||||
int err = stat(path_ptr(path), &st);
|
||||
int err = stat(path_get_cstr(path), &st);
|
||||
|
||||
if (err != 0) {
|
||||
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
|
||||
@@ -135,8 +135,8 @@ static enum fx_status path_get_directory(
|
||||
size_t dir_path_len = (size_t)(sep - path_cstr);
|
||||
fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
|
||||
|
||||
fx_path *dir_path = fx_path_create_from_cstr(
|
||||
fx_string_get_cstr(dir_path_s));
|
||||
fx_path *dir_path
|
||||
= fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s));
|
||||
fx_string_unref(dir_path_s);
|
||||
|
||||
*out_dir_path = dir_path;
|
||||
@@ -200,33 +200,6 @@ fx_path *fx_path_create_root()
|
||||
return path;
|
||||
}
|
||||
|
||||
fx_path *fx_path_create_cwd()
|
||||
{
|
||||
const long buf_len = 2048;
|
||||
char *buf = malloc(buf_len);
|
||||
if (!buf) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!getcwd(buf, buf_len)) {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fx_path *path = fx_path_create();
|
||||
if (!path) {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
|
||||
|
||||
fx_string_append_cstr(p->p_pathstr, buf);
|
||||
free(buf);
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
fx_path *fx_path_create_from_cstr(const char *cstr)
|
||||
{
|
||||
fx_path *path = fx_path_create();
|
||||
@@ -259,6 +232,49 @@ fx_path *fx_path_create_from_cstr(const char *cstr)
|
||||
return path;
|
||||
}
|
||||
|
||||
fx_path *fx_path_get_system(fx_system_path path_type)
|
||||
{
|
||||
char path_buf[4096];
|
||||
const char *path_value = NULL, *default_value = NULL;
|
||||
switch (path_type) {
|
||||
case FX_PATH_ROOT:
|
||||
path_value = "/";
|
||||
break;
|
||||
case FX_PATH_CWD:
|
||||
if (getcwd(path_buf, sizeof path_buf) == NULL) {
|
||||
path_value = path_buf;
|
||||
}
|
||||
break;
|
||||
case FX_PATH_HOME:
|
||||
path_value = getenv("HOME");
|
||||
default_value = "~";
|
||||
break;
|
||||
case FX_PATH_CONFIG: {
|
||||
const char *home = getenv("HOME");
|
||||
path_value = getenv("XDG_CONFIG_HOME");
|
||||
snprintf(
|
||||
path_buf,
|
||||
sizeof path_buf,
|
||||
"%s/.config",
|
||||
home ? home : "~");
|
||||
default_value = path_buf;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (path_value) {
|
||||
return fx_path_create_from_cstr(path_value);
|
||||
}
|
||||
|
||||
if (default_value) {
|
||||
return fx_path_create_from_cstr(default_value);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fx_path *fx_path_duplicate(const fx_path *path)
|
||||
{
|
||||
fx_path *new_path = fx_path_create();
|
||||
@@ -285,15 +301,13 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_path_p *result_p = fx_object_get_private(
|
||||
result,
|
||||
FX_TYPE_PATH);
|
||||
struct fx_path_p *result_p
|
||||
= fx_object_get_private(result, FX_TYPE_PATH);
|
||||
|
||||
for (size_t i = 0; i < nr_paths; i++) {
|
||||
if (paths[i]) {
|
||||
struct fx_path_p *path_p = fx_object_get_private(
|
||||
paths[i],
|
||||
FX_TYPE_PATH);
|
||||
struct fx_path_p *path_p
|
||||
= fx_object_get_private(paths[i], FX_TYPE_PATH);
|
||||
append_path(result_p, path_p);
|
||||
}
|
||||
}
|
||||
@@ -366,9 +380,9 @@ enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
|
||||
out_name);
|
||||
}
|
||||
|
||||
const char *fx_path_ptr(const fx_path *path)
|
||||
const char *fx_path_get_cstr(const fx_path *path)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path);
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_get_cstr, path);
|
||||
}
|
||||
|
||||
size_t fx_path_length(const fx_path *path)
|
||||
@@ -400,9 +414,8 @@ static fx_status path_to_string(
|
||||
fx_stream *out,
|
||||
const char *format)
|
||||
{
|
||||
struct fx_path_p *path = fx_object_get_private(
|
||||
obj->v_object,
|
||||
FX_TYPE_PATH);
|
||||
struct fx_path_p *path
|
||||
= fx_object_get_private(obj->v_object, FX_TYPE_PATH);
|
||||
|
||||
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL);
|
||||
return FX_SUCCESS;
|
||||
|
||||
+35
-29
@@ -53,12 +53,12 @@ static const fx_path *directory_get_rel_path(const struct fx_directory_p *dir)
|
||||
|
||||
static const char *directory_get_path_cstr(const struct fx_directory_p *dir)
|
||||
{
|
||||
return fx_path_ptr(dir->d_path_abs);
|
||||
return fx_path_get_cstr(dir->d_path_abs);
|
||||
}
|
||||
|
||||
static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
|
||||
{
|
||||
return fx_path_ptr(dir->d_path_rel);
|
||||
return fx_path_get_cstr(dir->d_path_rel);
|
||||
}
|
||||
|
||||
static fx_result directory_delete(
|
||||
@@ -90,12 +90,13 @@ static bool directory_path_exists(
|
||||
const struct fx_directory_p *root,
|
||||
const fx_path *path)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
|
||||
&FX_VALUE_OBJECT(path),
|
||||
};
|
||||
|
||||
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path *abs_path
|
||||
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return false;
|
||||
}
|
||||
@@ -110,12 +111,13 @@ static bool directory_path_is_file(
|
||||
const struct fx_directory_p *root,
|
||||
const fx_path *path)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
|
||||
&FX_VALUE_OBJECT(path),
|
||||
};
|
||||
|
||||
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path *abs_path
|
||||
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return false;
|
||||
}
|
||||
@@ -130,12 +132,13 @@ static bool directory_path_is_directory(
|
||||
const struct fx_directory_p *root,
|
||||
const fx_path *path)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
|
||||
&FX_VALUE_OBJECT(path),
|
||||
};
|
||||
|
||||
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path *abs_path
|
||||
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return false;
|
||||
}
|
||||
@@ -151,12 +154,13 @@ static fx_result directory_path_stat(
|
||||
const fx_path *path,
|
||||
struct fx_file_info *out)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
|
||||
&FX_VALUE_OBJECT(path),
|
||||
};
|
||||
|
||||
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path *abs_path
|
||||
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
@@ -171,12 +175,13 @@ static fx_result directory_path_unlink(
|
||||
const struct fx_directory_p *root,
|
||||
const fx_path *path)
|
||||
{
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
|
||||
&FX_VALUE_OBJECT(path),
|
||||
};
|
||||
|
||||
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path *abs_path
|
||||
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
@@ -264,7 +269,7 @@ static fx_result directory_open(
|
||||
|
||||
int root_fd = root ? root->d_fd : AT_FDCWD;
|
||||
|
||||
const char *path_cstr = fx_path_ptr(path);
|
||||
const char *path_cstr = fx_path_get_cstr(path);
|
||||
if (root) {
|
||||
while (*path_cstr == '/') {
|
||||
path_cstr++;
|
||||
@@ -298,15 +303,16 @@ static fx_result directory_open(
|
||||
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
||||
|
||||
if (!root) {
|
||||
cwd = fx_path_create_cwd();
|
||||
cwd = fx_path_get_system(FX_PATH_CWD);
|
||||
}
|
||||
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : cwd,
|
||||
path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root ? root->d_path_abs : cwd),
|
||||
&FX_VALUE_OBJECT(path),
|
||||
};
|
||||
|
||||
fx_path *new_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path *new_path
|
||||
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!new_path) {
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
@@ -570,7 +576,7 @@ fx_iterator *fx_directory_begin(
|
||||
int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR;
|
||||
|
||||
const char *path_list[] = {
|
||||
fx_path_ptr(it->_p->d_path_abs),
|
||||
fx_path_get_cstr(it->_p->d_path_abs),
|
||||
NULL,
|
||||
};
|
||||
|
||||
|
||||
+25
-18
@@ -86,13 +86,13 @@ static fx_result file_open_shadow(
|
||||
= fx_path_create_from_cstr(fx_string_get_cstr(filename));
|
||||
fx_string_unref(filename);
|
||||
|
||||
const fx_path *parts[] = {
|
||||
dir,
|
||||
shadow_filename,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(dir),
|
||||
&FX_VALUE_OBJECT(shadow_filename),
|
||||
};
|
||||
|
||||
fx_path *shadow_filepath
|
||||
= fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path_unref(dir);
|
||||
fx_path_unref(shadow_filename);
|
||||
|
||||
@@ -229,12 +229,14 @@ static enum fx_status file_swap_shadow(
|
||||
z__fx_io_generate_tmp_filename(tmp_name, sizeof tmp_name);
|
||||
fx_path *tmp_name_p = fx_path_create_from_cstr(tmp_name);
|
||||
|
||||
const fx_path *parts[] = {
|
||||
dir_path,
|
||||
tmp_name_p,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(dir_path),
|
||||
&FX_VALUE_OBJECT(tmp_name_p),
|
||||
};
|
||||
|
||||
tmp_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
tmp_path = fx_path_join_array(
|
||||
parts,
|
||||
sizeof parts / sizeof parts[0]);
|
||||
|
||||
fx_path_unref(tmp_name_p);
|
||||
|
||||
@@ -250,11 +252,15 @@ static enum fx_status file_swap_shadow(
|
||||
|
||||
int err;
|
||||
|
||||
err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path));
|
||||
err = rename(
|
||||
fx_path_ptr(shadow_file->path),
|
||||
fx_path_ptr(main_file->path));
|
||||
err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path));
|
||||
fx_path_get_cstr(main_file->path),
|
||||
fx_path_get_cstr(tmp_path));
|
||||
err = rename(
|
||||
fx_path_get_cstr(shadow_file->path),
|
||||
fx_path_get_cstr(main_file->path));
|
||||
err = rename(
|
||||
fx_path_get_cstr(tmp_path),
|
||||
fx_path_get_cstr(shadow_file->path));
|
||||
|
||||
fx_path_unref(tmp_path);
|
||||
|
||||
@@ -424,16 +430,17 @@ fx_result fx_file_open(
|
||||
if (root) {
|
||||
root_path = fx_directory_get_path(root);
|
||||
} else {
|
||||
root_path = fx_path_create_cwd();
|
||||
root_path = fx_path_get_system(FX_PATH_CWD);
|
||||
free_root_path = true;
|
||||
}
|
||||
|
||||
const fx_path *parts[] = {
|
||||
root_path,
|
||||
file_path,
|
||||
const fx_value *parts[] = {
|
||||
&FX_VALUE_OBJECT(root_path),
|
||||
&FX_VALUE_OBJECT(file_path),
|
||||
};
|
||||
|
||||
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path *abs_path
|
||||
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
|
||||
|
||||
if (free_root_path) {
|
||||
fx_path_unref((fx_path *)root_path);
|
||||
@@ -443,7 +450,7 @@ fx_result fx_file_open(
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
int fd = open(fx_path_ptr(abs_path), flags, 0644);
|
||||
int fd = open(fx_path_get_cstr(abs_path), flags, 0644);
|
||||
if (fd == -1) {
|
||||
fx_path_unref(abs_path);
|
||||
return FX_RESULT_STATUS(
|
||||
|
||||
+97
-42
@@ -91,22 +91,22 @@ static bool path_is_directory(const struct fx_path_p *path)
|
||||
return (info.attrib & FX_FILE_ATTRIB_DIRECTORY) != 0;
|
||||
}
|
||||
|
||||
static void append_path(struct fx_path_p *dest, const struct fx_path_p *src)
|
||||
static void append_path(struct fx_path_p *dest, const fx_string *src)
|
||||
{
|
||||
if (path_is_absolute(src)) {
|
||||
const char *src_cstr = fx_string_get_cstr(src);
|
||||
if (src_cstr[0] == '/') {
|
||||
fx_string_clear(dest->p_pathstr);
|
||||
fx_string_append_s(dest->p_pathstr, src->p_pathstr);
|
||||
fx_string_append_s(dest->p_pathstr, src);
|
||||
return;
|
||||
}
|
||||
|
||||
if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0
|
||||
&& fx_string_get_last_char(dest->p_pathstr) != '/'
|
||||
&& fx_string_get_first_char(src->p_pathstr) != '/') {
|
||||
char s[] = {'/', 0};
|
||||
fx_string_append_cstr(dest->p_pathstr, s);
|
||||
&& fx_string_get_first_char(src) != '/') {
|
||||
fx_string_append_c(dest->p_pathstr, '/');
|
||||
}
|
||||
|
||||
fx_string_append_s(dest->p_pathstr, src->p_pathstr);
|
||||
fx_string_append_s(dest->p_pathstr, src);
|
||||
}
|
||||
|
||||
static enum fx_status path_unlink(const struct fx_path_p *path)
|
||||
@@ -199,33 +199,6 @@ fx_path *fx_path_create_root()
|
||||
return path;
|
||||
}
|
||||
|
||||
fx_path *fx_path_create_cwd()
|
||||
{
|
||||
const long buf_len = 2048;
|
||||
char *buf = malloc(buf_len);
|
||||
if (!buf) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!getcwd(buf, buf_len)) {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fx_path *path = fx_path_create();
|
||||
if (!path) {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
|
||||
|
||||
fx_string_append_cstr(p->p_pathstr, buf);
|
||||
free(buf);
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
fx_path *fx_path_create_from_cstr(const char *cstr)
|
||||
{
|
||||
fx_path *path = fx_path_create();
|
||||
@@ -258,6 +231,49 @@ fx_path *fx_path_create_from_cstr(const char *cstr)
|
||||
return path;
|
||||
}
|
||||
|
||||
fx_path *fx_path_get_system(fx_system_path path_type)
|
||||
{
|
||||
char path_buf[4096];
|
||||
const char *path_value = NULL, *default_value = NULL;
|
||||
switch (path_type) {
|
||||
case FX_PATH_ROOT:
|
||||
path_value = "/";
|
||||
break;
|
||||
case FX_PATH_CWD:
|
||||
if (getcwd(path_buf, sizeof path_buf) == NULL) {
|
||||
path_value = path_buf;
|
||||
}
|
||||
break;
|
||||
case FX_PATH_HOME:
|
||||
path_value = getenv("HOME");
|
||||
default_value = "~";
|
||||
break;
|
||||
case FX_PATH_CONFIG: {
|
||||
const char *home = getenv("HOME");
|
||||
path_value = getenv("XDG_CONFIG_HOME");
|
||||
snprintf(
|
||||
path_buf,
|
||||
sizeof path_buf,
|
||||
"%s/.config",
|
||||
home ? home : "~");
|
||||
default_value = path_buf;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (path_value) {
|
||||
return fx_path_create_from_cstr(path_value);
|
||||
}
|
||||
|
||||
if (default_value) {
|
||||
return fx_path_create_from_cstr(default_value);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fx_path *fx_path_duplicate(const fx_path *path)
|
||||
{
|
||||
fx_path *new_path = fx_path_create();
|
||||
@@ -277,7 +293,7 @@ fx_path *fx_path_duplicate(const fx_path *path)
|
||||
return new_path;
|
||||
}
|
||||
|
||||
fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
|
||||
fx_path *fx_path_join_array(const fx_value *values[], size_t nr_values)
|
||||
{
|
||||
fx_path *result = fx_path_create();
|
||||
if (!result) {
|
||||
@@ -287,14 +303,53 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
|
||||
struct fx_path_p *result_p
|
||||
= fx_object_get_private(result, FX_TYPE_PATH);
|
||||
|
||||
for (size_t i = 0; i < nr_paths; i++) {
|
||||
if (paths[i]) {
|
||||
struct fx_path_p *path_p
|
||||
= fx_object_get_private(paths[i], FX_TYPE_PATH);
|
||||
append_path(result_p, path_p);
|
||||
}
|
||||
fx_stringstream *tmp = fx_stringstream_create();
|
||||
fx_string *value_s = fx_string_create();
|
||||
for (size_t i = 0; i < nr_values; i++) {
|
||||
if (!values[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
fx_stringstream_reset(tmp);
|
||||
fx_value_to_string(values[i], tmp, NULL);
|
||||
|
||||
fx_string_clear(value_s);
|
||||
fx_string_append_cstr(value_s, fx_stringstream_get_cstr(tmp));
|
||||
append_path(result_p, value_s);
|
||||
}
|
||||
|
||||
fx_string_unref(value_s);
|
||||
fx_stringstream_unref(tmp);
|
||||
return result;
|
||||
}
|
||||
|
||||
fx_path *fx_path_join_list(size_t count, ...)
|
||||
{
|
||||
fx_path *result = fx_path_create();
|
||||
if (!result) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_path_p *result_p
|
||||
= fx_object_get_private(result, FX_TYPE_PATH);
|
||||
va_list arg;
|
||||
va_start(arg, count);
|
||||
|
||||
fx_stringstream *tmp = fx_stringstream_create();
|
||||
fx_string *value_s = fx_string_create();
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
const fx_value *value = va_arg(arg, const fx_value *);
|
||||
|
||||
fx_stringstream_reset(tmp);
|
||||
fx_value_to_string(value, tmp, NULL);
|
||||
|
||||
fx_string_clear(value_s);
|
||||
fx_string_append_cstr(value_s, fx_stringstream_get_cstr(tmp));
|
||||
append_path(result_p, value_s);
|
||||
}
|
||||
|
||||
fx_string_unref(value_s);
|
||||
fx_stringstream_unref(tmp);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -363,7 +418,7 @@ enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
|
||||
out_name);
|
||||
}
|
||||
|
||||
const char *fx_path_ptr(const fx_path *path)
|
||||
const char *fx_path_get_cstr(const fx_path *path)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path);
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ enum fx_status fx_directory_open(
|
||||
}
|
||||
|
||||
HANDLE dir_handle = CreateFileA(
|
||||
fx_path_ptr(new_path), GENERIC_READ,
|
||||
fx_path_get_cstr(new_path), GENERIC_READ,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
|
||||
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, INVALID_HANDLE_VALUE);
|
||||
|
||||
@@ -197,7 +197,7 @@ static bool move_into_directory(struct fx_directory_iterator *it, const char *di
|
||||
|
||||
state = push_iteration_state(it->_z);
|
||||
state->search_path = dir_path;
|
||||
state->search = FindFirstFileA(fx_path_ptr(search_path), &state->data);
|
||||
state->search = FindFirstFileA(fx_path_get_cstr(search_path), &state->data);
|
||||
|
||||
fx_path_release(search_path);
|
||||
fx_path_release(wildcard);
|
||||
@@ -224,7 +224,7 @@ static bool move_into_directory(struct fx_directory_iterator *it, const char *di
|
||||
static bool move_to_first_item(
|
||||
struct fx_directory_iterator *it, const struct fx_path *root_dir)
|
||||
{
|
||||
bool has_results = move_into_directory(it, fx_path_ptr(root_dir));
|
||||
bool has_results = move_into_directory(it, fx_path_get_cstr(root_dir));
|
||||
|
||||
if (!has_results) {
|
||||
return false;
|
||||
|
||||
@@ -205,13 +205,13 @@ bool fx_path_is_absolute(const struct fx_path *path)
|
||||
|
||||
bool fx_path_exists(const struct fx_path *path)
|
||||
{
|
||||
DWORD attrib = GetFileAttributesA(fx_path_ptr(path));
|
||||
DWORD attrib = GetFileAttributesA(fx_path_get_cstr(path));
|
||||
return attrib != INVALID_FILE_ATTRIBUTES;
|
||||
}
|
||||
|
||||
bool fx_path_is_file(const struct fx_path *path)
|
||||
{
|
||||
DWORD attrib = GetFileAttributesA(fx_path_ptr(path));
|
||||
DWORD attrib = GetFileAttributesA(fx_path_get_cstr(path));
|
||||
if (attrib == INVALID_FILE_ATTRIBUTES) {
|
||||
return false;
|
||||
}
|
||||
@@ -221,7 +221,7 @@ bool fx_path_is_file(const struct fx_path *path)
|
||||
|
||||
bool fx_path_is_directory(const struct fx_path *path)
|
||||
{
|
||||
DWORD attrib = GetFileAttributesA(fx_path_ptr(path));
|
||||
DWORD attrib = GetFileAttributesA(fx_path_get_cstr(path));
|
||||
if (attrib == INVALID_FILE_ATTRIBUTES) {
|
||||
return false;
|
||||
}
|
||||
@@ -229,7 +229,7 @@ bool fx_path_is_directory(const struct fx_path *path)
|
||||
return (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
||||
}
|
||||
|
||||
const char *fx_path_ptr(const struct fx_path *path)
|
||||
const char *fx_path_get_cstr(const struct fx_path *path)
|
||||
{
|
||||
return fx_string_get_cstr(path->pathstr);
|
||||
}
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@ int main(int argc, const char **argv)
|
||||
{
|
||||
fx_directory_entry *entry = NULL;
|
||||
fx_value_get_pointer(val, (void **)&entry);
|
||||
printf("%s\n", fx_path_ptr(entry->filepath));
|
||||
printf("%s\n", fx_path_get_cstr(entry->filepath));
|
||||
}
|
||||
|
||||
fx_iterator_unref(it);
|
||||
|
||||
@@ -130,7 +130,7 @@ void write_tagged_datetime(fx_datetime *data)
|
||||
|
||||
fx_stringstream *new_data = fx_stringstream_create();
|
||||
fx_datetime_to_string(data, FX_DATETIME_FORMAT_RFC3339, new_data);
|
||||
fx_stream_write_cstr(fx_stdout, fx_stringstream_ptr(new_data), NULL);
|
||||
fx_stream_write_cstr(fx_stdout, fx_stringstream_get_cstr(new_data), NULL);
|
||||
|
||||
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
|
||||
|
||||
|
||||
@@ -50,6 +50,11 @@ enum {
|
||||
FX_KEY_BACKSPACE,
|
||||
FX_KEY_RETURN,
|
||||
|
||||
/* within this range, we further reserve a sub-range. applications can
|
||||
* define their own special keycode values within this range */
|
||||
FX_KEY_RESERVED_START = 0xFFF00,
|
||||
FX_KEY_RESERVED_END = 0xFFFFD,
|
||||
|
||||
FX_MOD_CTRL = 0x10000000,
|
||||
|
||||
FX_KEY_EOF = 0xFFFFFFFF,
|
||||
@@ -153,6 +158,7 @@ FX_API void fx_tty_set_vmode(
|
||||
struct fx_tty *tty,
|
||||
const struct fx_tty_vmode *vmode);
|
||||
FX_API void fx_tty_reset_vmode(struct fx_tty *tty);
|
||||
FX_API void fx_tty_flush(struct fx_tty *tty);
|
||||
|
||||
FX_API enum fx_status fx_tty_get_dimensions(
|
||||
struct fx_tty *tty,
|
||||
|
||||
+89
-8
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "../../tty.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <fx/term/tty.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -103,6 +104,7 @@ static void init_tty(struct fx_tty *tty, FILE *in, FILE *out)
|
||||
|
||||
tcgetattr(fd, &tty->t_ios_default);
|
||||
memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw);
|
||||
setvbuf(out, NULL, _IOLBF, 4096);
|
||||
|
||||
tty->t_ios_raw.c_iflag &= ~(ICRNL | IXON);
|
||||
tty->t_ios_raw.c_oflag &= ~(OPOST);
|
||||
@@ -207,8 +209,14 @@ void fx_tty_reset_vmode(struct fx_tty *tty)
|
||||
tty->t_vmode.v_attrib = FX_TTY_ATTRIB_NORMAL;
|
||||
}
|
||||
|
||||
void fx_tty_flush(struct fx_tty *tty)
|
||||
{
|
||||
fflush(tty->t_out);
|
||||
}
|
||||
|
||||
static int compare_colour(
|
||||
const struct fx_tty_colour *a, const struct fx_tty_colour *b)
|
||||
const struct fx_tty_colour *a,
|
||||
const struct fx_tty_colour *b)
|
||||
{
|
||||
if (a->c_mode != b->c_mode) {
|
||||
return -1;
|
||||
@@ -245,7 +253,9 @@ static int compare_colour(
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int compare_vmode(const struct fx_tty_vmode *a, const struct fx_tty_vmode *b)
|
||||
static int compare_vmode(
|
||||
const struct fx_tty_vmode *a,
|
||||
const struct fx_tty_vmode *b)
|
||||
{
|
||||
if (a->v_attrib != b->v_attrib) {
|
||||
return -1;
|
||||
@@ -273,7 +283,9 @@ static void put_ansi_attrib(struct fx_tty *tty, const char *s)
|
||||
}
|
||||
|
||||
static void set_attrib(
|
||||
struct fx_tty *tty, enum fx_tty_attrib old, enum fx_tty_attrib new)
|
||||
struct fx_tty *tty,
|
||||
enum fx_tty_attrib old,
|
||||
enum fx_tty_attrib new)
|
||||
{
|
||||
if (old == new) {
|
||||
return;
|
||||
@@ -311,7 +323,8 @@ static void set_attrib(
|
||||
}
|
||||
|
||||
static void set_fg(
|
||||
struct fx_tty *tty, const struct fx_tty_colour *old,
|
||||
struct fx_tty *tty,
|
||||
const struct fx_tty_colour *old,
|
||||
const struct fx_tty_colour *new)
|
||||
{
|
||||
if (compare_colour(old, new) == 0) {
|
||||
@@ -347,7 +360,8 @@ static void set_fg(
|
||||
}
|
||||
|
||||
static void set_bg(
|
||||
struct fx_tty *tty, const struct fx_tty_colour *old,
|
||||
struct fx_tty *tty,
|
||||
const struct fx_tty_colour *old,
|
||||
const struct fx_tty_colour *new)
|
||||
{
|
||||
if (compare_colour(old, new) == 0) {
|
||||
@@ -460,7 +474,10 @@ fx_keycode fx_tty_read_key(struct fx_tty *tty)
|
||||
return c;
|
||||
}
|
||||
|
||||
void fx_tty_move_cursor_x(struct fx_tty *tty, enum fx_tty_position_base base, int pos)
|
||||
void fx_tty_move_cursor_x(
|
||||
struct fx_tty *tty,
|
||||
enum fx_tty_position_base base,
|
||||
int pos)
|
||||
{
|
||||
if (base == FX_TTY_POS_CURSOR && pos < 0 && pos >= -4) {
|
||||
for (int i = 0; i > pos; i--) {
|
||||
@@ -489,7 +506,10 @@ void fx_tty_move_cursor_x(struct fx_tty *tty, enum fx_tty_position_base base, in
|
||||
}
|
||||
}
|
||||
|
||||
void fx_tty_move_cursor_y(struct fx_tty *tty, enum fx_tty_position_base base, int pos)
|
||||
void fx_tty_move_cursor_y(
|
||||
struct fx_tty *tty,
|
||||
enum fx_tty_position_base base,
|
||||
int pos)
|
||||
{
|
||||
if (base == FX_TTY_POS_START) {
|
||||
/* we don't need this functionality right now */
|
||||
@@ -530,7 +550,9 @@ void fx_tty_clear(struct fx_tty *tty, enum fx_tty_clear_mode mode)
|
||||
}
|
||||
|
||||
enum fx_status fx_tty_get_dimensions(
|
||||
struct fx_tty *tty, unsigned int *w, unsigned int *h)
|
||||
struct fx_tty *tty,
|
||||
unsigned int *w,
|
||||
unsigned int *h)
|
||||
{
|
||||
if (!(tty->t_flags & FX_TTY_INTERACTIVE)) {
|
||||
return -1;
|
||||
@@ -552,3 +574,62 @@ enum fx_status fx_tty_get_dimensions(
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum fx_status fx_tty_get_cursor_position(
|
||||
struct fx_tty *tty,
|
||||
unsigned int *w,
|
||||
unsigned int *h)
|
||||
{
|
||||
if (!(tty->t_flags & FX_TTY_INTERACTIVE)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int in_fd = fileno(tty->t_in);
|
||||
int out_fd = fileno(tty->t_out);
|
||||
|
||||
write(out_fd, "\x1b[6n", 4);
|
||||
|
||||
char c;
|
||||
read(in_fd, &c, 1);
|
||||
read(in_fd, &c, 1);
|
||||
|
||||
char s[16] = {0};
|
||||
for (int i = 0; i < sizeof s - 1; i++) {
|
||||
read(in_fd, &c, 1);
|
||||
if (!isdigit(c)) {
|
||||
break;
|
||||
}
|
||||
|
||||
s[i] = c;
|
||||
s[i + 1] = 0;
|
||||
}
|
||||
|
||||
if (w) {
|
||||
if (s[0]) {
|
||||
*w = atoi(s);
|
||||
} else {
|
||||
*w = 0;
|
||||
}
|
||||
}
|
||||
|
||||
s[0] = 0;
|
||||
for (int i = 0; i < sizeof s - 1; i++) {
|
||||
read(in_fd, &c, 1);
|
||||
if (!isdigit(c)) {
|
||||
break;
|
||||
}
|
||||
|
||||
s[i] = c;
|
||||
s[i + 1] = 0;
|
||||
}
|
||||
|
||||
if (h) {
|
||||
if (s[0]) {
|
||||
*h = atoi(s);
|
||||
} else {
|
||||
*h = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+2
-5
@@ -1217,11 +1217,8 @@ static bool wchar_is_control(fx_wchar c)
|
||||
return true;
|
||||
}
|
||||
|
||||
if (c == 0xFEFF) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (c >= 0xFFF9 && c <= 0xFFFB) {
|
||||
/* treat the reserved range as control characters */
|
||||
if (c >= 0xF0000 && c <= 0xFFFFD) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
#include <fx/global.h>
|
||||
#include <fx/thread.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static fx_queue globals = FX_QUEUE_INIT;
|
||||
static fx_mutex globals_lock = FX_MUTEX_INIT;
|
||||
|
||||
struct global {
|
||||
fx_object *g_object;
|
||||
fx_queue_entry g_entry;
|
||||
};
|
||||
|
||||
void fx_cleanup_global(void)
|
||||
{
|
||||
fx_queue_entry *cur = fx_queue_first(&globals);
|
||||
while (cur) {
|
||||
struct global *global = fx_unbox(struct global, cur, g_entry);
|
||||
cur = fx_queue_next(cur);
|
||||
|
||||
fx_object_unref(global->g_object);
|
||||
free(global);
|
||||
}
|
||||
}
|
||||
|
||||
void fx_register_global(fx_object *obj)
|
||||
{
|
||||
struct global *global = malloc(sizeof *global);
|
||||
if (!global) {
|
||||
return;
|
||||
}
|
||||
|
||||
memset(global, 0x0, sizeof *global);
|
||||
|
||||
global->g_object = fx_object_ref(obj);
|
||||
fx_mutex_lock(&globals_lock);
|
||||
fx_queue_push_back(&globals, &global->g_entry);
|
||||
fx_mutex_unlock(&globals_lock);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#ifndef FX_GLOBAL_H_
|
||||
#define FX_GLOBAL_H_
|
||||
|
||||
#include <fx/misc.h>
|
||||
#include <fx/object.h>
|
||||
|
||||
FX_API void fx_register_global(fx_object *obj);
|
||||
FX_API void fx_cleanup_global(void);
|
||||
|
||||
#endif
|
||||
@@ -95,7 +95,7 @@ fx_string_insert_cstrf(fx_string *dest, size_t at, const char *format, ...);
|
||||
FX_API void fx_string_clear(fx_string *str);
|
||||
|
||||
FX_API fx_iterator *fx_string_tokenise(
|
||||
fx_string *str,
|
||||
const fx_string *str,
|
||||
const char *delims[],
|
||||
size_t nr_delims,
|
||||
fx_string_tokenise_flags flags);
|
||||
|
||||
@@ -27,7 +27,7 @@ FX_API fx_status fx_stringstream_reset(fx_stringstream *strv);
|
||||
FX_API fx_status
|
||||
fx_stringstream_reset_with_buffer(fx_stringstream *strv, char *buf, size_t max);
|
||||
|
||||
FX_API const char *fx_stringstream_ptr(const fx_stringstream *strv);
|
||||
FX_API const char *fx_stringstream_get_cstr(const fx_stringstream *strv);
|
||||
FX_API char *fx_stringstream_steal(fx_stringstream *strv);
|
||||
|
||||
FX_API size_t fx_stringstream_get_length(const fx_stringstream *strv);
|
||||
|
||||
+6
-5
@@ -56,7 +56,7 @@ struct fx_string_p {
|
||||
struct fx_string_iterator_p {
|
||||
int _m, _f;
|
||||
fx_string *_tmp;
|
||||
struct fx_string_p *_s_p, *_tmp_p;
|
||||
const struct fx_string_p *_s_p, *_tmp_p;
|
||||
|
||||
const char **_d;
|
||||
size_t _nd, _ds;
|
||||
@@ -478,7 +478,7 @@ static fx_status string_replace_all_with_stringstream(
|
||||
size_t new_len = fx_stringstream_get_length(new_data);
|
||||
string_reserve(str, new_len);
|
||||
char *dest = string_ptr(str);
|
||||
memcpy(dest, fx_stringstream_ptr(new_data), new_len);
|
||||
memcpy(dest, fx_stringstream_get_cstr(new_data), new_len);
|
||||
dest[new_len] = '\0';
|
||||
|
||||
str->s_len = new_len;
|
||||
@@ -513,7 +513,8 @@ static enum fx_status remove_ansi(
|
||||
memmove(removal_start, excess_src, excess_length);
|
||||
s[new_str_len] = '\0';
|
||||
|
||||
str->s_len = new_str_len;
|
||||
str->s_len -= length;
|
||||
str->s_codepoints -= length;
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
@@ -1023,7 +1024,7 @@ static enum fx_status find_next_token(struct fx_string_iterator_p *it)
|
||||
}
|
||||
|
||||
static fx_iterator *string_tokenise(
|
||||
struct fx_string_p *str,
|
||||
const struct fx_string_p *str,
|
||||
const char *delims[],
|
||||
size_t nr_delims,
|
||||
fx_string_tokenise_flags flags)
|
||||
@@ -1390,7 +1391,7 @@ void fx_string_clear(fx_string *str)
|
||||
}
|
||||
|
||||
fx_iterator *fx_string_tokenise(
|
||||
fx_string *str,
|
||||
const fx_string *str,
|
||||
const char *delims[],
|
||||
size_t nr_delims,
|
||||
fx_string_tokenise_flags flags)
|
||||
|
||||
+6
-3
@@ -141,7 +141,7 @@ static size_t stringstream_get_length(const struct fx_stringstream_p *strv)
|
||||
return strv->ss_len;
|
||||
}
|
||||
|
||||
static const char *stringstream_ptr(const struct fx_stringstream_p *ss)
|
||||
static const char *stringstream_get_cstr(const struct fx_stringstream_p *ss)
|
||||
{
|
||||
return ss->ss_buf;
|
||||
}
|
||||
@@ -236,9 +236,12 @@ enum fx_status fx_stringstream_reset_with_buffer(
|
||||
max);
|
||||
}
|
||||
|
||||
const char *fx_stringstream_ptr(const fx_stringstream *ss)
|
||||
const char *fx_stringstream_get_cstr(const fx_stringstream *ss)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRINGSTREAM, stringstream_ptr, ss);
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_TYPE_STRINGSTREAM,
|
||||
stringstream_get_cstr,
|
||||
ss);
|
||||
}
|
||||
|
||||
char *fx_stringstream_steal(fx_stringstream *ss)
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <fx/thread.h>
|
||||
#include <pthread.h>
|
||||
|
||||
bool fx_mutex_lock(fx_mutex *mut)
|
||||
{
|
||||
return pthread_mutex_lock(mut) == 0;
|
||||
}
|
||||
|
||||
bool fx_mutex_trylock(fx_mutex *mut)
|
||||
{
|
||||
return pthread_mutex_trylock(mut) == 0;
|
||||
}
|
||||
|
||||
bool fx_mutex_unlock(fx_mutex *mut)
|
||||
{
|
||||
return pthread_mutex_trylock(mut) == 0;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <fx/thread.h>
|
||||
#include <pthread.h>
|
||||
|
||||
bool fx_mutex_lock(fx_mutex *mut)
|
||||
{
|
||||
return pthread_mutex_lock(mut) == 0;
|
||||
}
|
||||
|
||||
bool fx_mutex_trylock(fx_mutex *mut)
|
||||
{
|
||||
return pthread_mutex_trylock(mut) == 0;
|
||||
}
|
||||
|
||||
bool fx_mutex_unlock(fx_mutex *mut)
|
||||
{
|
||||
return pthread_mutex_trylock(mut) == 0;
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <fx/bst.h>
|
||||
#include <fx/endian.h>
|
||||
#include <fx/global.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/object.h>
|
||||
#include <fx/reflection/function.h>
|
||||
@@ -16,6 +17,7 @@
|
||||
|
||||
static struct fx_bst type_idmap = FX_BST_INIT;
|
||||
static struct fx_namemap type_namemap = FX_NAMEMAP_INIT;
|
||||
static fx_once runtime_cleanup_init = FX_ONCE_INIT;
|
||||
static union fx_type_id zero_id = {0};
|
||||
|
||||
struct type_init_ctx {
|
||||
@@ -23,6 +25,37 @@ struct type_init_ctx {
|
||||
size_t ctx_instance_offset;
|
||||
};
|
||||
|
||||
static fx_result fx_type_destroy(struct fx_type_info *info);
|
||||
|
||||
static void type_namemap_cleanup(struct fx_namemap_entry *entry)
|
||||
{
|
||||
struct fx_type_info *ty
|
||||
= fx_unbox(struct fx_type_info, entry, ty_namemap_entry);
|
||||
fx_type_destroy(ty);
|
||||
}
|
||||
|
||||
static void function_namemap_cleanup(struct fx_namemap_entry *entry)
|
||||
{
|
||||
struct fx_type_function *func
|
||||
= fx_unbox(struct fx_type_function, entry, f_entry);
|
||||
fx_function_unref(func->f_func);
|
||||
free(func);
|
||||
}
|
||||
|
||||
static void property_namemap_cleanup(struct fx_namemap_entry *entry)
|
||||
{
|
||||
struct fx_type_property *prop
|
||||
= fx_unbox(struct fx_type_property, entry, p_entry);
|
||||
fx_property_unref(prop->p_prop);
|
||||
free(prop);
|
||||
}
|
||||
|
||||
static void runtime_cleanup(void)
|
||||
{
|
||||
fx_cleanup_global();
|
||||
// fx_namemap_cleanup(&type_namemap, type_namemap_cleanup);
|
||||
}
|
||||
|
||||
static inline int type_info_compare(
|
||||
const struct fx_type_info *a,
|
||||
const struct fx_type_info *b)
|
||||
@@ -56,10 +89,8 @@ static struct fx_type_info *get_type(
|
||||
{
|
||||
fx_bst_node *cur = tree->bst_root;
|
||||
while (cur) {
|
||||
struct fx_type_info *cur_node = fx_unbox(
|
||||
struct fx_type_info,
|
||||
cur,
|
||||
ty_idmap_node);
|
||||
struct fx_type_info *cur_node
|
||||
= fx_unbox(struct fx_type_info, cur, ty_idmap_node);
|
||||
int cmp = fx_type_id_compare(key, &cur_node->ty_id);
|
||||
|
||||
if (cmp > 0) {
|
||||
@@ -80,10 +111,8 @@ struct fx_type_component *fx_type_get_component(
|
||||
{
|
||||
fx_bst_node *cur = tree->bst_root;
|
||||
while (cur) {
|
||||
struct fx_type_component *cur_node = fx_unbox(
|
||||
struct fx_type_component,
|
||||
cur,
|
||||
c_node);
|
||||
struct fx_type_component *cur_node
|
||||
= fx_unbox(struct fx_type_component, cur, c_node);
|
||||
int cmp = fx_type_id_compare(key, &cur_node->c_type->ty_id);
|
||||
|
||||
if (cmp > 0) {
|
||||
@@ -151,16 +180,14 @@ static fx_result locate_interface(
|
||||
struct fx_type_info *dest,
|
||||
struct type_init_ctx *init_ctx)
|
||||
{
|
||||
struct fx_type_component *interface_comp = fx_type_get_component(
|
||||
&dest->ty_components,
|
||||
interface_id);
|
||||
struct fx_type_component *interface_comp
|
||||
= fx_type_get_component(&dest->ty_components, interface_id);
|
||||
if (interface_comp) {
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
struct fx_type_info *interface_reg = get_type(
|
||||
&type_idmap,
|
||||
interface_id);
|
||||
struct fx_type_info *interface_reg
|
||||
= get_type(&type_idmap, interface_id);
|
||||
if (!interface_reg) {
|
||||
return FX_RESULT_ERR(NO_ENTRY);
|
||||
}
|
||||
@@ -227,9 +254,8 @@ static fx_result find_type_components(struct fx_type_info *type)
|
||||
}
|
||||
|
||||
while (1) {
|
||||
struct fx_type_info *dep_class = get_type(
|
||||
&type_idmap,
|
||||
current_id);
|
||||
struct fx_type_info *dep_class
|
||||
= get_type(&type_idmap, current_id);
|
||||
if (!dep_class) {
|
||||
return FX_RESULT_ERR(NO_ENTRY);
|
||||
}
|
||||
@@ -302,6 +328,10 @@ static bool type_has_base_class(struct fx_type_info *info)
|
||||
|
||||
fx_result fx_type_register(struct fx_type_info *info)
|
||||
{
|
||||
if (fx_init_once(&runtime_cleanup_init)) {
|
||||
atexit(runtime_cleanup);
|
||||
}
|
||||
|
||||
if (!type_has_base_class(info)) {
|
||||
fx_type_id_copy(FX_TYPE_OBJECT, &info->ty_parent_id);
|
||||
}
|
||||
@@ -340,12 +370,34 @@ fx_result fx_type_register(struct fx_type_info *info)
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_result fx_type_destroy(struct fx_type_info *info)
|
||||
{
|
||||
info->ty_category = FX_TYPE_CLASS;
|
||||
|
||||
union fx_type_id metatype = {0};
|
||||
|
||||
fx_bst_node *cur = fx_bst_first(&info->ty_components);
|
||||
while (cur) {
|
||||
struct fx_type_component *component
|
||||
= fx_unbox(struct fx_type_component, cur, c_node);
|
||||
cur = fx_bst_next(cur);
|
||||
|
||||
free(component);
|
||||
}
|
||||
|
||||
fx_namemap_cleanup(&info->ty_properties, property_namemap_cleanup);
|
||||
fx_namemap_cleanup(&info->ty_functions, function_namemap_cleanup);
|
||||
|
||||
fx_type_unref(info->ty_metatype);
|
||||
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
fx_result fx_type_add_function(fx_type_info *info, struct _fx_object *func)
|
||||
{
|
||||
const char *name = fx_function_get_name(func);
|
||||
struct fx_namemap_entry *entry = fx_namemap_get(
|
||||
&info->ty_functions,
|
||||
name);
|
||||
struct fx_namemap_entry *entry
|
||||
= fx_namemap_get(&info->ty_functions, name);
|
||||
if (entry) {
|
||||
return FX_RESULT_ERR(NAME_EXISTS);
|
||||
}
|
||||
@@ -366,9 +418,8 @@ fx_result fx_type_add_function(fx_type_info *info, struct _fx_object *func)
|
||||
fx_result fx_type_add_property(fx_type_info *info, struct _fx_object *prop)
|
||||
{
|
||||
const char *name = fx_property_get_name(prop);
|
||||
struct fx_namemap_entry *entry = fx_namemap_get(
|
||||
&info->ty_functions,
|
||||
name);
|
||||
struct fx_namemap_entry *entry
|
||||
= fx_namemap_get(&info->ty_functions, name);
|
||||
if (entry) {
|
||||
return FX_RESULT_ERR(NAME_EXISTS);
|
||||
}
|
||||
@@ -410,10 +461,8 @@ fx_function *fx_type_info_get_function_by_name(
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_type_function *func = fx_unbox(
|
||||
struct fx_type_function,
|
||||
entry,
|
||||
f_entry);
|
||||
struct fx_type_function *func
|
||||
= fx_unbox(struct fx_type_function, entry, f_entry);
|
||||
return func->f_func;
|
||||
}
|
||||
|
||||
@@ -426,9 +475,7 @@ fx_function *fx_type_info_get_property_by_name(
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_type_property *prop = fx_unbox(
|
||||
struct fx_type_property,
|
||||
entry,
|
||||
p_entry);
|
||||
struct fx_type_property *prop
|
||||
= fx_unbox(struct fx_type_property, entry, p_entry);
|
||||
return prop->p_prop;
|
||||
}
|
||||
|
||||
+1
-1
@@ -181,7 +181,7 @@ static fx_status value_change_string(fx_value *in, fx_value *out)
|
||||
}
|
||||
|
||||
fx_string *result
|
||||
= fx_string_create_from_cstr(fx_stringstream_ptr(strm));
|
||||
= fx_string_create_from_cstr(fx_stringstream_get_cstr(strm));
|
||||
fx_stringstream_unref(strm);
|
||||
|
||||
if (!result) {
|
||||
|
||||
+18
-31
@@ -1,40 +1,27 @@
|
||||
#include <stdarg.h>
|
||||
#include <fx/diagnostics/process.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static int another_function(int a, double b, int c)
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
printf("a=%d, b=%lf, c=%d\n", a, b, c);
|
||||
return 2;
|
||||
printf("%d args:\n", argc);
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
printf("%s\n", argv[i]);
|
||||
}
|
||||
|
||||
static int test_function(int a, int b, int c, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, c);
|
||||
int d = va_arg(args, int);
|
||||
int e = va_arg(args, int);
|
||||
int f = va_arg(args, int);
|
||||
int g = va_arg(args, int);
|
||||
int h = va_arg(args, int);
|
||||
int i = va_arg(args, int);
|
||||
int j = va_arg(args, int);
|
||||
printf("a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d\n",
|
||||
a,
|
||||
b,
|
||||
c,
|
||||
d,
|
||||
e,
|
||||
f,
|
||||
g,
|
||||
h,
|
||||
i,
|
||||
j);
|
||||
return a + b + c + d + e;
|
||||
printf("environment:\n");
|
||||
fx_process *self = fx_process_get_self();
|
||||
const fx_hashtable *env = fx_process_get_environment(self);
|
||||
fx_object_to_string(env, fx_stdout, NULL);
|
||||
printf("\n");
|
||||
|
||||
printf("read from stdin...\n");
|
||||
char line[128];
|
||||
if (fgets(line, sizeof line, stdin)) {
|
||||
printf("read: %s\n", line);
|
||||
} else {
|
||||
printf("read failed\n");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_function(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
another_function(1, 2.5, 5);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user