Add RStrBuf api

This commit is contained in:
pancake 2013-12-03 04:46:53 +01:00
parent 8559ea2d0b
commit 8717542596
4 changed files with 64 additions and 2 deletions

View File

@ -602,6 +602,19 @@ R_API void r_strht_clear(RStrHT *s);
R_API void r_strht_del(RStrHT *s, const char *key);
R_API int r_is_heap (void *p);
typedef struct {
int len;
char *ptr;
char buf[64];
} RStrBuf;
R_API RStrBuf *r_strbuf_new();
R_API void r_strbuf_set(RStrBuf *sb, const char *s);
R_API void r_strbuf_append(RStrBuf *sb, const char *s);
R_API char *r_strbuf_get(RStrBuf *sb);
R_API void r_strbuf_free(RStrBuf *sb);
#ifdef __cplusplus
}
#endif

View File

@ -8,7 +8,7 @@ OBJS+=prof.o cache.o sys.o buf.o w32-sys.o base64.o base85.o name.o
OBJS+=list.o flist.o ht.o ht64.o mixed.o btree.o chmod.o graph.o
OBJS+=regex/regcomp.o regex/regerror.o regex/regexec.o uleb128.o
OBJS+=sandbox.o calc.o thread.o lock.o strpool.o bitmap.o strht.o
OBJS+=p_date.o p_format.o print.o p_seven.o slist.o randomart.o
OBJS+=p_date.o p_format.o print.o p_seven.o slist.o randomart.o strbuf.o
# DO NOT BUILD r_big api (not yet used and its buggy)
ifeq (1,0)

View File

@ -1237,4 +1237,3 @@ R_API void r_str_truncate_cmd(char *string) {
}
}
// STATIC/DYNAMIC STRINGS API

50
libr/util/strbuf.c Normal file
View File

@ -0,0 +1,50 @@
/* radare - LGPL - Copyright 2013 - pancake */
// STATIC/DYNAMIC STRINGS API
#include "r_types.h"
#include "r_util.h"
#include <stdio.h>
R_API RStrBuf *r_strbuf_new() {
return R_NEW0 (RStrBuf);
}
R_API void r_strbuf_set(RStrBuf *sb, const char *s) {
int l = strlen (s);
if (l>=sizeof (sb->buf)) {
free (sb->ptr);
sb->ptr = malloc (l+1);
memcpy (sb->ptr, s, l+1);
} else {
sb->ptr = NULL;
memcpy (sb->buf, s, l+1);
}
sb->len = l;
}
R_API void r_strbuf_append(RStrBuf *sb, const char *s) {
int l = strlen (s);
if ((sb->len+l+1)<sizeof (sb->buf)) {
strcpy (sb->buf+sb->len, s);
} else {
char *p = malloc (sb->len+l+1);
strcpy (p, sb->ptr?sb->ptr:sb->buf);
strcpy (p+sb->len, s);
}
sb->len += l;
}
R_API char *r_strbuf_get(RStrBuf *sb) {
if (sb) {
if (sb->ptr)
return sb->ptr;
return sb->buf;
}
return NULL;
}
R_API void r_strbuf_free(RStrBuf *sb) {
if (sb && sb->ptr)
free (sb->ptr);
free (sb);
}