Explorar o código

ncd/NCDVal: add NCDValComposedStringResource_Cstring()

ambrop7 %!s(int64=13) %!d(string=hai) anos
pai
achega
ef2d69eab8
Modificáronse 2 ficheiros con 45 adicións e 19 borrados
  1. 30 19
      ncd/NCDVal.c
  2. 15 0
      ncd/NCDVal.h

+ 30 - 19
ncd/NCDVal.c

@@ -353,6 +353,25 @@ static NCDValRef NCDVal__CopyComposedStringToStored (NCDValRef val)
     return copy;
 }
 
+static const char * NCDVal__composedstring_cstring_func (const b_cstring *cstr, size_t offset, size_t *out_length)
+{
+    ASSERT(offset < cstr->length)
+    ASSERT(out_length)
+    ASSERT(cstr->func == NCDVal__composedstring_cstring_func)
+    
+    size_t str_offset = cstr->user1.size;
+    NCDVal_ComposedString_func_getptr func_getptr = (NCDVal_ComposedString_func_getptr)cstr->user2.fptr;
+    void *user = cstr->user3.ptr;
+    
+    const char *data;
+    func_getptr(user, str_offset + offset, &data, out_length);
+    
+    ASSERT(data)
+    ASSERT(*out_length > 0)
+    
+    return data;
+}
+
 #include "NCDVal_maptree.h"
 #include <structure/CAvl_impl.h>
 
@@ -1100,6 +1119,17 @@ void NCDValComposedStringResource_GetPtr (NCDValComposedStringResource resource,
     }
 }
 
+b_cstring NCDValComposedStringResource_Cstring (NCDValComposedStringResource resource, size_t offset, size_t length)
+{
+    b_cstring cstr;
+    cstr.length = length;
+    cstr.func = NCDVal__composedstring_cstring_func;
+    cstr.user1.size = offset;
+    cstr.user2.fptr = (void (*) (void))resource.func_getptr;
+    cstr.user3.ptr = resource.user;
+    return cstr;
+}
+
 void NCDVal_StringGetPtr (NCDValRef string, size_t offset, size_t max_length, const char **out_data, size_t *out_length)
 {
     ASSERT(NCDVal_IsString(string))
@@ -1147,25 +1177,6 @@ void NCDVal_StringGetPtr (NCDValRef string, size_t offset, size_t max_length, co
     }
 }
 
-static const char * NCDVal__composedstring_cstring_func (const b_cstring *cstr, size_t offset, size_t *out_length)
-{
-    ASSERT(offset < cstr->length)
-    ASSERT(out_length)
-    ASSERT(cstr->func == NCDVal__composedstring_cstring_func)
-    
-    size_t str_offset = cstr->user1.size;
-    NCDVal_ComposedString_func_getptr func_getptr = (NCDVal_ComposedString_func_getptr)cstr->user2.fptr;
-    void *user = cstr->user3.ptr;
-    
-    const char *data;
-    func_getptr(user, str_offset + offset, &data, out_length);
-    
-    ASSERT(data)
-    ASSERT(*out_length > 0)
-    
-    return data;
-}
-
 b_cstring NCDVal_StringCstring (NCDValRef string)
 {
     ASSERT(NCDVal_IsString(string))

+ 15 - 0
ncd/NCDVal.h

@@ -469,6 +469,21 @@ typedef struct {
  */
 void NCDValComposedStringResource_GetPtr (NCDValComposedStringResource resource, size_t offset, size_t max_length, const char **out_data, size_t *out_length);
 
+/**
+ * Returns a cstring referencing a range within a {@link NCDValComposedStringResource}.
+ * \a offset and \a length specify the range within the resource which the returned
+ * cstring will reference. To reference the contents of a ComposedString, use:
+ *   - resource = NCDVal_ComposedStringResource(composedstring),
+ *   - offset = NCDVal_ComposedStringOffset(composedstring),
+ *   - length = NCDVal_StringLength(composedstring).
+ * 
+ * The returned cstring is valid as long as the resource is not released. Note that
+ * a reference to resource.ref_target may need to be taken to ensure the resource
+ * is not released while it is being referenced by the returned cstring (unless
+ * resource.ref_target is NULL).
+ */
+b_cstring NCDValComposedStringResource_Cstring (NCDValComposedStringResource resource, size_t offset, size_t length);
+
 /**
  * Builds a new ComposedString from a string resource.
  * A reference to the underlying string resource via the {@link NCDRefTarget} object