-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from Gottox/feature/generic-collector
easy: use generic collector for easy directory
- Loading branch information
Showing
46 changed files
with
217 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions
1
subprojects/cextras-04517a4939153026a9ccdf2778ed11403be6f4e7/.meson-subproject-wrap-hash.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
c65f3da22779c3e7b3935233687cd5ed017464a7d70eef65eb95a29865d37099 |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
78 changes: 78 additions & 0 deletions
78
subprojects/cextras-04517a4939153026a9ccdf2778ed11403be6f4e7/lib/collection/collector.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @author : Enno Boland ([email protected]) | ||
* @file : iterator_collector | ||
* @created : Tuesday Aug 29, 2023 17:27:35 CEST | ||
*/ | ||
|
||
#include "../../include/cextras/collection.h" | ||
#include "../../include/cextras/error.h" | ||
|
||
int | ||
cx_collect(char ***target, cx_collector_next_t next, void *iterator) { | ||
int rv = 0; | ||
static const uintptr_t nullptr = 0; | ||
struct CxBuffer list = {0}; | ||
struct CxBuffer list_values = {0}; | ||
size_t elements = 0; | ||
char **result = NULL; | ||
|
||
rv = cx_buffer_init(&list); | ||
if (rv < 0) { | ||
goto out; | ||
} | ||
rv = cx_buffer_init(&list_values); | ||
if (rv < 0) { | ||
goto out; | ||
} | ||
|
||
for (;;) { | ||
const char *value = NULL; | ||
size_t size = 0; | ||
|
||
rv = next(iterator, &value, &size); | ||
if (rv < 0) { | ||
goto out; | ||
} | ||
if (value == NULL) { | ||
break; | ||
} | ||
|
||
size_t index = cx_buffer_size(&list_values); | ||
char *index_ptr = (void *)index; | ||
elements++; | ||
rv = cx_buffer_append(&list, (uint8_t *)&index_ptr, sizeof(char *)); | ||
if (rv < 0) { | ||
goto out; | ||
} | ||
rv = cx_buffer_append(&list_values, (uint8_t *)value, size); | ||
if (rv < 0) { | ||
goto out; | ||
} | ||
rv = cx_buffer_append(&list_values, (uint8_t *)&nullptr, sizeof(char)); | ||
if (rv < 0) { | ||
goto out; | ||
} | ||
} | ||
|
||
rv = cx_buffer_append(&list, (uint8_t *)&nullptr, sizeof(char *)); | ||
size_t base_size = cx_buffer_size(&list); | ||
|
||
const uint8_t *values_data = cx_buffer_data(&list_values); | ||
size_t values_size = cx_buffer_size(&list_values); | ||
|
||
rv = cx_buffer_append(&list, values_data, values_size); | ||
if (rv < 0) { | ||
goto out; | ||
} | ||
|
||
result = (char **)cx_buffer_unwrap(&list); | ||
for (cx_index_t i = 0; i < elements; i++) { | ||
result[i] += base_size + (uintptr_t)result; | ||
} | ||
*target = result; | ||
|
||
out: | ||
cx_buffer_cleanup(&list); | ||
cx_buffer_cleanup(&list_values); | ||
return rv; | ||
} |
File renamed without changes.
1 change: 1 addition & 0 deletions
1
...291d655c19fedb/lib/collection/meson.build → ...ed11403be6f4e7/lib/collection/meson.build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
collection_src = files( | ||
'buffer.c', | ||
'collector.c', | ||
'lru.c', | ||
'rc_hash_map.c', | ||
'rc_map.c', | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
104 changes: 104 additions & 0 deletions
104
subprojects/cextras-04517a4939153026a9ccdf2778ed11403be6f4e7/tests/collection/collector.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* BSD 2-Clause License | ||
* | ||
* Copyright (c) 2023, Enno Boland | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
/** | ||
* @author Enno Boland ([email protected]) | ||
* @file rc_map.c | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <cextras/collection.h> | ||
#include <cextras/types.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <testlib.h> | ||
|
||
#define LENGTH(x) (sizeof(x) / sizeof(*x)) | ||
|
||
struct TestCollectIter { | ||
cx_index_t index; | ||
char **array; | ||
size_t size; | ||
int rv; | ||
}; | ||
|
||
static int | ||
test_collect_next(void *iter, const char **value, size_t *size) { | ||
struct TestCollectIter *i = iter; | ||
if (i->index >= i->size) { | ||
return i->rv; | ||
} | ||
*value = i->array[i->index]; | ||
*size = strlen(i->array[i->index]); | ||
i->index++; | ||
return i->rv; | ||
} | ||
|
||
static void | ||
collector(void) { | ||
int rv = 0; | ||
char *values[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}; | ||
char **target = NULL; | ||
struct TestCollectIter iter = { | ||
.index = 0, | ||
.array = values, | ||
.size = LENGTH(values), | ||
}; | ||
|
||
rv = cx_collect(&target, test_collect_next, &iter); | ||
assert(rv == 0); | ||
|
||
for (size_t i = 0; i < LENGTH(values); i++) { | ||
assert(strcmp(values[i], target[i]) == 0); | ||
} | ||
assert(target[LENGTH(values)] == NULL); | ||
free(target); | ||
} | ||
|
||
static void | ||
collector_fail(void) { | ||
int rv = 0; | ||
char *values[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}; | ||
char **target = NULL; | ||
struct TestCollectIter iter = { | ||
.index = 0, | ||
.array = values, | ||
.size = LENGTH(values), | ||
.rv = -1, | ||
}; | ||
|
||
rv = cx_collect(&target, test_collect_next, &iter); | ||
assert(rv == -1); | ||
assert(target == NULL); | ||
} | ||
|
||
DECLARE_TESTS | ||
TEST(collector) | ||
TEST(collector_fail) | ||
END_TESTS |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion
1
subprojects/cextras-990106e5492c382669a7238a2e291d655c19fedb/.meson-subproject-wrap-hash.txt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters