* 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.
This commit is contained in:
Brecht Van Lommel
2008-11-11 15:03:26 +00:00
parent ade1495f0e
commit 3bb5fc9c7d
5 changed files with 85 additions and 7 deletions

View File

@@ -56,7 +56,15 @@ DynStr* BLI_dynstr_new (void);
* @param ds The DynStr to append to. * @param ds The DynStr to append to.
* @param cstr The c-string to append. * @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. * 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. * Get a DynStr's contents as a c-string.
* <i> The returned c-string should be free'd * <i> The returned c-string should be free'd
* using BLI_freeN. </i> * using MEM_freeN. </i>
* *
* @param ds The DynStr of interest. * @param ds The DynStr of interest.
* @return The contents of @a ds as a c-string. * @return The contents of @a ds as a c-string.

View File

@@ -28,6 +28,8 @@
* Dynamically sized string ADT * Dynamically sized string ADT
*/ */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -35,8 +37,10 @@
#include "BLI_blenlib.h" #include "BLI_blenlib.h"
#include "BLI_dynstr.h" #include "BLI_dynstr.h"
#ifdef HAVE_CONFIG_H #ifdef _WIN32
#include <config.h> #ifndef vsnprintf
#define vsnprintf _vsnprintf
#endif
#endif #endif
/***/ /***/
@@ -63,7 +67,7 @@ DynStr *BLI_dynstr_new(void) {
return ds; return ds;
} }
void BLI_dynstr_append(DynStr *ds, char *cstr) { void BLI_dynstr_append(DynStr *ds, const char *cstr) {
DynStrElem *dse= malloc(sizeof(*dse)); DynStrElem *dse= malloc(sizeof(*dse));
int cstrlen= strlen(cstr); int cstrlen= strlen(cstr);
@@ -79,6 +83,55 @@ void BLI_dynstr_append(DynStr *ds, char *cstr) {
ds->curlen+= cstrlen; 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) { int BLI_dynstr_get_len(DynStr *ds) {
return ds->curlen; return ds->curlen;
} }

View File

@@ -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_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); 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); 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); void RNA_property_string_set(struct PropertyRNA *prop, struct PointerRNA *ptr, const char *value);

View File

@@ -166,6 +166,23 @@ void RNA_property_string_get(PropertyRNA *prop, PointerRNA *ptr, char *value)
sprop->get(ptr, 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) int RNA_property_string_length(PropertyRNA *prop, PointerRNA *ptr)
{ {
StringPropertyRNA *sprop= (StringPropertyRNA*)prop; StringPropertyRNA *sprop= (StringPropertyRNA*)prop;

View File

@@ -1,14 +1,13 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "DNA_scene_types.h" #include "DNA_scene_types.h"
#include "RNA_access.h" #include "RNA_access.h"
#include "RNA_types.h" #include "RNA_types.h"
#include "BKE_main.h"
typedef struct RNAGenDeps { typedef struct RNAGenDeps {
void *udata; void *udata;
PropDependencyCallback cb; PropDependencyCallback cb;