Published 2006-01-15 14:48:21
#include "ext/standard/php_smart_str.h"
typedef struct {
char *c; // data goes here..
size_t len; // the current length
size_t a; // the allocated size
} smart_str;
smart_str *sstr;
sstr = emalloc(sizeof(smart_str));
smart_str_sets(sstr, strdup("")); // start it clean.
.....
smart_str_free(sstr);
smart_str sstr = {0};
smart_str_alloc(&sstr, 100);
smart_str_sets(&sstr, strdup("This is a string"));
smart_str_free(&sstr);
void smart_str_alloc(smart_str *str, int len, int is_persistant);
smart_str *sstr;
sstr = emalloc(sizeof(smart_str));
smart_str_alloc(sstr, 100);
void smart_str_sets(smart_str *dest, char *src);
smart_str *sstr;Note: this does not copy the string (so freeing the string after using this will likely resulting in a double free error
sstr = emalloc(sizeof(smart_str));
smart_str_sets(sstr, strdup("This is a string"));
void smart_str_0(smart_str *str);
smart_str *sstr;
sstr = emalloc(sizeof(smart_str));
smart_str_appends(sstr, "Hello World");
smart_str_0(sstr);
printf("the string is %s", sstr->c);
smart_str_appendc(smart_str *dest, char c)
smart_str *sstr;
char c = "C";
sstr = emalloc(sizeof(smart_str));
smart_str_appendc(sstr, c);
smart_str_append(smart_str *dest, smart_str *add);
smart_str_appends(smart_str *dest, char *add);
smart_str_appendl(smart_str *dest, char *add, int n);
smart_str sstr1 = {0};Notes:
smart_str sstr2 = {0};
smart_str sstr3 = {0};
smart_str_appends(&sstr1, "the cat");
smart_str_appendl(&sstr1, " the dog", 3);
smart_str_appends(&sstr2, "the cat");
smart_str_appendl(&sstr2, " the dog", 3);
smart_str_append(&sstr3, &str1);
smart_str_append(&sstr3, &str2);
smart_str_append_long(smart_str *dest, long val);
smart_str_append_unsigned(smart_str *dest, unsigned long val);
smart_str_append_off_t(smart_str *dest, off_t val);
smart_str_append_long(sstr, 120);
smart_str_free(smart_str *str);
smart_str *sstr;
sstr = emalloc(sizeof(smart_str));
smart_str_sets(*sstr1, strdup("the cat"));
... do something ...
smart_str_free(sstr);
snprintf(char *buffer, int max_size, char *format, .....);