diff options
author | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2008-11-11 18:03:26 +0300 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2008-11-11 18:03:26 +0300 |
commit | 3bb5fc9c7d10f7dec32f8d2a772fce69be468b15 (patch) | |
tree | 2acf79b72d49e35ac174cf8d122edcd8d15b1511 /source | |
parent | ade1495f0e6951bda3ae9852414d86e7c1fd624e (diff) |
* RNA: utility function to retrieve strings. It will use a fixed
size buffer if it's big enough, and otherwise allocate memory.
* Added BLI_dynstr_appendf() to construct strings easily in printf
style, this should make it easier to construct menu strings for
example.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenlib/BLI_dynstr.h | 12 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_dynstr.c | 59 | ||||
-rw-r--r-- | source/blender/makesrna/RNA_access.h | 1 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_access.c | 17 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_dependency.c | 3 |
5 files changed, 85 insertions, 7 deletions
diff --git a/source/blender/blenlib/BLI_dynstr.h b/source/blender/blenlib/BLI_dynstr.h index e5b94aecb4f..453e15a0f3b 100644 --- a/source/blender/blenlib/BLI_dynstr.h +++ b/source/blender/blenlib/BLI_dynstr.h @@ -56,7 +56,15 @@ DynStr* BLI_dynstr_new (void); * @param ds The DynStr to append to. * @param cstr The c-string to append. */ -void BLI_dynstr_append (DynStr *ds, char *cstr); +void BLI_dynstr_append (DynStr *ds, const char *cstr); + + /** + * Append a c-string to a DynStr, but with formatting like printf. + * + * @param ds The DynStr to append to. + * @param format The printf format string to use. + */ +void BLI_dynstr_appendf (DynStr *ds, const char *format, ...); /** * Find the length of a DynStr. @@ -69,7 +77,7 @@ int BLI_dynstr_get_len (DynStr *ds); /** * Get a DynStr's contents as a c-string. * <i> The returned c-string should be free'd - * using BLI_freeN. </i> + * using MEM_freeN. </i> * * @param ds The DynStr of interest. * @return The contents of @a ds as a c-string. diff --git a/source/blender/blenlib/intern/BLI_dynstr.c b/source/blender/blenlib/intern/BLI_dynstr.c index 43c80dd24f0..1c539af2957 100644 --- a/source/blender/blenlib/intern/BLI_dynstr.c +++ b/source/blender/blenlib/intern/BLI_dynstr.c @@ -28,6 +28,8 @@ * Dynamically sized string ADT */ +#include <stdarg.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -35,8 +37,10 @@ #include "BLI_blenlib.h" #include "BLI_dynstr.h" -#ifdef HAVE_CONFIG_H -#include <config.h> +#ifdef _WIN32 +#ifndef vsnprintf +#define vsnprintf _vsnprintf +#endif #endif /***/ @@ -63,7 +67,7 @@ DynStr *BLI_dynstr_new(void) { return ds; } -void BLI_dynstr_append(DynStr *ds, char *cstr) { +void BLI_dynstr_append(DynStr *ds, const char *cstr) { DynStrElem *dse= malloc(sizeof(*dse)); int cstrlen= strlen(cstr); @@ -79,6 +83,55 @@ void BLI_dynstr_append(DynStr *ds, char *cstr) { ds->curlen+= cstrlen; } +void BLI_dynstr_appendf(DynStr *ds, const char *format, ...) +{ + va_list args; + char *message, fixedmessage[256]; + int len= 256, maxlen= 65536, retval; + + while(1) { + if(len == sizeof(fixedmessage)) + message= fixedmessage; + else + message= MEM_callocN(sizeof(char)*len+1, "BLI_dynstr_appendf"); + + va_start(args, format); + retval= vsnprintf(message, len, format, args); + va_end(args); + + if(retval == -1) { + /* -1 means not enough space, but on windows it may also mean + * there is a formatting error, so we impose a maximum length */ + if(message != fixedmessage) + MEM_freeN(message); + message= NULL; + + len *= 2; + if(len > maxlen) { + fprintf(stderr, "BLI_dynstr_append text too long or format error.\n"); + break; + } + } + else if(retval > len) { + /* in C99 the actual length required is returned */ + if(message != fixedmessage) + MEM_freeN(message); + message= NULL; + + len= retval; + } + else + break; + } + + if(message) { + BLI_dynstr_append(ds, message); + + if(message != fixedmessage) + MEM_freeN(message); + } +} + int BLI_dynstr_get_len(DynStr *ds) { return ds->curlen; } diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index a5609ea4a7b..ed07120a619 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -66,6 +66,7 @@ float RNA_property_float_get_array(struct PropertyRNA *prop, struct PointerRNA * void RNA_property_float_set_array(struct PropertyRNA *prop, struct PointerRNA *ptr, int index, float value); void RNA_property_string_get(struct PropertyRNA *prop, struct PointerRNA *ptr, char *value); +char *RNA_property_string_get_alloc(struct PropertyRNA *prop, struct PointerRNA *ptr, char *fixedbuf, int fixedlen); int RNA_property_string_length(struct PropertyRNA *prop, struct PointerRNA *ptr); void RNA_property_string_set(struct PropertyRNA *prop, struct PointerRNA *ptr, const char *value); diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c index f666d2ce08b..05aafa81626 100644 --- a/source/blender/makesrna/intern/rna_access.c +++ b/source/blender/makesrna/intern/rna_access.c @@ -166,6 +166,23 @@ void RNA_property_string_get(PropertyRNA *prop, PointerRNA *ptr, char *value) sprop->get(ptr, value); } +char *RNA_property_string_get_alloc(PropertyRNA *prop, PointerRNA *ptr, char *fixedbuf, int fixedlen) +{ + char *buf; + int length; + + length= RNA_property_string_length(prop, ptr); + + if(length+1 < fixedlen) + buf= fixedbuf; + else + buf= MEM_callocN(sizeof(char)*(length+1), "RNA_string_get_alloc"); + + RNA_property_string_get(prop, ptr, buf); + + return buf; +} + int RNA_property_string_length(PropertyRNA *prop, PointerRNA *ptr) { StringPropertyRNA *sprop= (StringPropertyRNA*)prop; diff --git a/source/blender/makesrna/intern/rna_dependency.c b/source/blender/makesrna/intern/rna_dependency.c index bb97dd079c3..09d68e001d2 100644 --- a/source/blender/makesrna/intern/rna_dependency.c +++ b/source/blender/makesrna/intern/rna_dependency.c @@ -1,14 +1,13 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include "DNA_scene_types.h" #include "RNA_access.h" #include "RNA_types.h" -#include "BKE_main.h" - typedef struct RNAGenDeps { void *udata; PropDependencyCallback cb; |