fx.io: update interface implementations
This commit is contained in:
+29
-29
@@ -64,8 +64,9 @@ static fx_result directory_delete(
|
|||||||
{
|
{
|
||||||
enum fx_status status = FX_SUCCESS;
|
enum fx_status status = FX_SUCCESS;
|
||||||
|
|
||||||
fx_iterator *it
|
fx_iterator *it = fx_directory_begin(
|
||||||
= fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_LAST);
|
dir,
|
||||||
|
FX_DIRECTORY_ITERATE_PARENT_LAST);
|
||||||
while (FX_OK(fx_iterator_get_status(it))) {
|
while (FX_OK(fx_iterator_get_status(it))) {
|
||||||
fx_iterator_erase(it);
|
fx_iterator_erase(it);
|
||||||
}
|
}
|
||||||
@@ -291,8 +292,9 @@ static fx_result directory_open(
|
|||||||
|
|
||||||
fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY);
|
fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY);
|
||||||
fx_path *cwd = NULL;
|
fx_path *cwd = NULL;
|
||||||
struct fx_directory_p *p
|
struct fx_directory_p *p = fx_object_get_private(
|
||||||
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
dir,
|
||||||
|
FX_TYPE_DIRECTORY);
|
||||||
|
|
||||||
if (!root) {
|
if (!root) {
|
||||||
cwd = fx_path_create_cwd();
|
cwd = fx_path_create_cwd();
|
||||||
@@ -358,8 +360,9 @@ fx_result fx_directory_open_temp(fx_directory **out)
|
|||||||
rpath,
|
rpath,
|
||||||
FX_DIRECTORY_OPEN_CREATE,
|
FX_DIRECTORY_OPEN_CREATE,
|
||||||
&dir);
|
&dir);
|
||||||
struct fx_directory_p *p
|
struct fx_directory_p *p = fx_object_get_private(
|
||||||
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
dir,
|
||||||
|
FX_TYPE_DIRECTORY);
|
||||||
|
|
||||||
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
|
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
|
||||||
fx_path_unref(rpath);
|
fx_path_unref(rpath);
|
||||||
@@ -408,8 +411,9 @@ const char *fx_directory_get_rel_path_cstr(const fx_directory *dir)
|
|||||||
|
|
||||||
fx_result fx_directory_delete(fx_directory *dir)
|
fx_result fx_directory_delete(fx_directory *dir)
|
||||||
{
|
{
|
||||||
struct fx_directory_p *p
|
struct fx_directory_p *p = fx_object_get_private(
|
||||||
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
dir,
|
||||||
|
FX_TYPE_DIRECTORY);
|
||||||
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
|
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
|
||||||
/* TODO allow object release functions to return a fx_result value */
|
/* TODO allow object release functions to return a fx_result value */
|
||||||
fx_directory_unref(dir);
|
fx_directory_unref(dir);
|
||||||
@@ -555,8 +559,9 @@ fx_iterator *fx_directory_begin(
|
|||||||
enum fx_directory_iterator_flags flags)
|
enum fx_directory_iterator_flags flags)
|
||||||
{
|
{
|
||||||
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
|
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
struct fx_directory_iterator_p *it
|
struct fx_directory_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(it_obj, FX_TYPE_DIRECTORY_ITERATOR);
|
it_obj,
|
||||||
|
FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
|
|
||||||
it->flags = flags;
|
it->flags = flags;
|
||||||
it->root = directory;
|
it->root = directory;
|
||||||
@@ -628,8 +633,9 @@ static const fx_iterator *iterator_cbegin(const fx_object *obj)
|
|||||||
|
|
||||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_directory_iterator_p *it
|
struct fx_directory_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
obj,
|
||||||
|
FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
if (!it || !it->fts) {
|
if (!it || !it->fts) {
|
||||||
return FX_ERR_NO_DATA;
|
return FX_ERR_NO_DATA;
|
||||||
}
|
}
|
||||||
@@ -677,10 +683,12 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
|||||||
|
|
||||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_directory_iterator_p *it
|
struct fx_directory_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
obj,
|
||||||
fx_result result
|
FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
= fx_directory_path_unlink(it->root, it->entry.filepath);
|
fx_result result = fx_directory_path_unlink(
|
||||||
|
it->root,
|
||||||
|
it->entry.filepath);
|
||||||
if (fx_result_is_error(result)) {
|
if (fx_result_is_error(result)) {
|
||||||
enum fx_status status = fx_error_get_status_code(result);
|
enum fx_status status = fx_error_get_status_code(result);
|
||||||
fx_error_discard(result);
|
fx_error_discard(result);
|
||||||
@@ -690,20 +698,13 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
|||||||
return iterator_move_next(obj);
|
return iterator_move_next(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
static fx_value iterator_get_value(const fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_directory_iterator_p *it
|
struct fx_directory_iterator_p *it = fx_object_get_private(
|
||||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
obj,
|
||||||
|
FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
|
|
||||||
return FX_ITERATOR_VALUE_PTR(&it->entry);
|
return FX_POINTER(&it->entry);
|
||||||
}
|
|
||||||
|
|
||||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
|
||||||
{
|
|
||||||
struct fx_directory_iterator_p *it
|
|
||||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
|
||||||
|
|
||||||
return FX_ITERATOR_VALUE_CPTR(&it->entry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
@@ -739,7 +740,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory_iterator)
|
|||||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||||
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||||
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
|
||||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||||
FX_TYPE_CLASS_END(fx_directory_iterator)
|
FX_TYPE_CLASS_END(fx_directory_iterator)
|
||||||
|
|
||||||
|
|||||||
+18
-9
@@ -6,6 +6,7 @@
|
|||||||
#include <fx/io/file.h>
|
#include <fx/io/file.h>
|
||||||
#include <fx/io/path.h>
|
#include <fx/io/path.h>
|
||||||
#include <fx/string.h>
|
#include <fx/string.h>
|
||||||
|
#include <fx/value.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
@@ -134,8 +135,8 @@ static enum fx_status path_get_directory(
|
|||||||
size_t dir_path_len = (size_t)(sep - path_cstr);
|
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_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
|
||||||
|
|
||||||
fx_path *dir_path
|
fx_path *dir_path = fx_path_create_from_cstr(
|
||||||
= fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s));
|
fx_string_get_cstr(dir_path_s));
|
||||||
fx_string_unref(dir_path_s);
|
fx_string_unref(dir_path_s);
|
||||||
|
|
||||||
*out_dir_path = dir_path;
|
*out_dir_path = dir_path;
|
||||||
@@ -284,13 +285,15 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct fx_path_p *result_p
|
struct fx_path_p *result_p = fx_object_get_private(
|
||||||
= fx_object_get_private(result, FX_TYPE_PATH);
|
result,
|
||||||
|
FX_TYPE_PATH);
|
||||||
|
|
||||||
for (size_t i = 0; i < nr_paths; i++) {
|
for (size_t i = 0; i < nr_paths; i++) {
|
||||||
if (paths[i]) {
|
if (paths[i]) {
|
||||||
struct fx_path_p *path_p
|
struct fx_path_p *path_p = fx_object_get_private(
|
||||||
= fx_object_get_private(paths[i], FX_TYPE_PATH);
|
paths[i],
|
||||||
|
FX_TYPE_PATH);
|
||||||
append_path(result_p, path_p);
|
append_path(result_p, path_p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -385,18 +388,24 @@ static void path_init(fx_object *obj, void *priv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void path_fini(fx_object *obj, void *priv)
|
static void path_fini(fx_object *obj, void *priv)
|
||||||
{
|
{
|
||||||
struct fx_path_p *path = priv;
|
struct fx_path_p *path = priv;
|
||||||
|
|
||||||
fx_string_unref(path->p_pathstr);
|
fx_string_unref(path->p_pathstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void path_to_string(const fx_object *obj, fx_stream *out)
|
static fx_status path_to_string(
|
||||||
|
const fx_value *obj,
|
||||||
|
fx_stream *out,
|
||||||
|
const char *format)
|
||||||
{
|
{
|
||||||
struct fx_path_p *path = fx_object_get_private(obj, 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);
|
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL);
|
||||||
|
return FX_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
|
|||||||
+6
-2
@@ -20,9 +20,13 @@ int main(int argc, const char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_iterator *it = fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
fx_iterator *it = fx_directory_begin(
|
||||||
fx_foreach(fx_directory_entry *, entry, it)
|
dir,
|
||||||
|
FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||||
|
fx_foreach(val, it)
|
||||||
{
|
{
|
||||||
|
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_ptr(entry->filepath));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user