Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ambrop72/badvpn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmbroz Bizjak <ambrop7@gmail.com>2014-11-19 00:49:14 +0300
committerAmbroz Bizjak <ambrop7@gmail.com>2014-11-19 00:49:14 +0300
commit98ed187623ff4c278b8afbd1f9a2be43460fc52a (patch)
tree4f1f8030120a744eb9f6469aa1863ffd1c98bb54 /examples
parent5d77b8559f69b09993369c07989d6d0e5a070d3e (diff)
ncd: Remove ComposedString type along with the buffer module.
It was a fancy feature, but with questionable utility and with a general maintenance burden.
Diffstat (limited to 'examples')
-rw-r--r--examples/ncdval_test.c148
1 files changed, 0 insertions, 148 deletions
diff --git a/examples/ncdval_test.c b/examples/ncdval_test.c
index 6933ed0..276353f 100644
--- a/examples/ncdval_test.c
+++ b/examples/ncdval_test.c
@@ -39,100 +39,6 @@
#define FORCE(cmd) if (!(cmd)) { fprintf(stderr, "failed\n"); exit(1); }
-struct composed_string {
- BRefTarget ref_target;
- size_t length;
- size_t chunk_size;
- char **chunks;
-};
-
-static void composed_string_ref_target_func_release (BRefTarget *ref_target)
-{
- struct composed_string *cs = UPPER_OBJECT(ref_target, struct composed_string, ref_target);
-
- size_t num_chunks = cs->length / cs->chunk_size;
- if (cs->length % cs->chunk_size) {
- num_chunks++;
- }
-
- for (size_t i = 0; i < num_chunks; i++) {
- BFree(cs->chunks[i]);
- }
-
- BFree(cs->chunks);
- BFree(cs);
-}
-
-static void composed_string_func_getptr (void *user, size_t offset, const char **out_data, size_t *out_length)
-{
- struct composed_string *cs = user;
- ASSERT(offset < cs->length)
-
- *out_data = cs->chunks[offset / cs->chunk_size] + (offset % cs->chunk_size);
- *out_length = cs->chunk_size - (offset % cs->chunk_size);
-}
-
-static NCDValRef build_composed_string (NCDValMem *mem, const char *data, size_t length, size_t chunk_size)
-{
- ASSERT(chunk_size > 0)
-
- struct composed_string *cs = BAlloc(sizeof(*cs));
- if (!cs) {
- goto fail0;
- }
-
- cs->length = length;
- cs->chunk_size = chunk_size;
-
- size_t num_chunks = cs->length / cs->chunk_size;
- if (cs->length % cs->chunk_size) {
- num_chunks++;
- }
-
- cs->chunks = BAllocArray(num_chunks, sizeof(cs->chunks[0]));
- if (!cs->chunk_size) {
- goto fail1;
- }
-
- size_t i;
- for (i = 0; i < num_chunks; i++) {
- cs->chunks[i] = BAlloc(cs->chunk_size);
- if (!cs->chunks[i]) {
- goto fail2;
- }
-
- size_t to_copy = length;
- if (to_copy > cs->chunk_size) {
- to_copy = cs->chunk_size;
- }
-
- memcpy(cs->chunks[i], data, to_copy);
- data += to_copy;
- length -= to_copy;
- }
-
- BRefTarget_Init(&cs->ref_target, composed_string_ref_target_func_release);
-
- NCDValComposedStringResource resource;
- resource.func_getptr = composed_string_func_getptr;
- resource.user = cs;
- resource.ref_target = &cs->ref_target;
-
- NCDValRef val = NCDVal_NewComposedString(mem, resource, 0, cs->length);
- BRefTarget_Deref(&cs->ref_target);
- return val;
-
-fail2:
- while (i-- > 0) {
- BFree(cs->chunks[i]);
- }
- BFree(cs->chunks);
-fail1:
- BFree(cs);
-fail0:
- return NCDVal_NewInvalid();
-}
-
static void test_string (NCDValRef str, const char *data, size_t length)
{
FORCE( !NCDVal_IsInvalid(str) )
@@ -320,60 +226,6 @@ int main ()
NCDValMem_Free(&mem);
- NCDValMem_Init(&mem);
-
- NCDValRef cstr1 = build_composed_string(&mem, "Hello World", 11, 3);
- test_string(cstr1, "Hello World", 11);
- FORCE( NCDVal_IsComposedString(cstr1) )
- FORCE( !NCDVal_IsContinuousString(cstr1) )
- FORCE( NCDVal_StringEquals(cstr1, "Hello World") )
- FORCE( !NCDVal_StringEquals(cstr1, "Hello World ") )
- FORCE( !NCDVal_StringEquals(cstr1, "Hello WorlD") )
-
- NCDValRef cstr2 = build_composed_string(&mem, "GoodBye", 7, 1);
- test_string(cstr2, "GoodBye", 7);
- FORCE( NCDVal_IsComposedString(cstr2) )
- FORCE( !NCDVal_IsContinuousString(cstr2) )
- FORCE( NCDVal_StringEquals(cstr2, "GoodBye") )
- FORCE( !NCDVal_StringEquals(cstr2, " GoodBye") )
- FORCE( !NCDVal_StringEquals(cstr2, "goodBye") )
-
- NCDValRef cstr3 = build_composed_string(&mem, "Bad\x00String", 10, 4);
- test_string(cstr3, "Bad\x00String", 10);
- FORCE( NCDVal_IsComposedString(cstr3) )
- FORCE( !NCDVal_IsContinuousString(cstr3) )
-
- FORCE( NCDVal_StringMemCmp(cstr1, cstr2, 1, 2, 3) < 0 )
- FORCE( NCDVal_StringMemCmp(cstr1, cstr2, 7, 1, 4) > 0 )
-
- char buf[10];
- NCDVal_StringCopyOut(cstr1, 1, 10, buf);
- FORCE( !memcmp(buf, "ello World", 10) )
-
- NCDValRef clist1 = NCDVal_NewList(&mem, 3);
- FORCE( !NCDVal_IsInvalid(clist1) )
- FORCE( NCDVal_ListAppend(clist1, cstr1) )
- FORCE( NCDVal_ListAppend(clist1, cstr2) )
- FORCE( NCDVal_ListAppend(clist1, cstr3) )
- FORCE( NCDVal_ListCount(clist1) == 3 )
-
- FORCE( NCDValMem_ConvertNonContinuousStrings(&mem, &clist1) )
- FORCE( NCDVal_ListCount(clist1) == 3 )
-
- NCDValRef fixed_str1 = NCDVal_ListGet(clist1, 0);
- NCDValRef fixed_str2 = NCDVal_ListGet(clist1, 1);
- NCDValRef fixed_str3 = NCDVal_ListGet(clist1, 2);
-
- FORCE( NCDVal_IsContinuousString(fixed_str1) )
- FORCE( NCDVal_IsContinuousString(fixed_str2) )
- FORCE( NCDVal_IsContinuousString(fixed_str3) )
-
- test_string(fixed_str1, "Hello World", 11);
- test_string(fixed_str2, "GoodBye", 7);
- test_string(fixed_str3, "Bad\x00String", 10);
-
- NCDValMem_Free(&mem);
-
NCDStringIndex_Free(&string_index);
return 0;