The Artima Developer Community
Sponsored Link

PHP Buzz Forum
smart_str API

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Alan Knowles

Posts: 390
Nickname: alank
Registered: Sep, 2004

Alan Knowles is Freelance Developer, works on PHP extensions and PEAR.
smart_str API Posted: Jan 14, 2006 11:39 PM
Reply to this message Reply

This post originated from an RSS feed registered with PHP Buzz by Alan Knowles.
Original Post: smart_str API
Feed Title: Smoking toooo much PHP
Feed URL: http://www.akbkhome.com/blog.php/RSS.xml
Feed Description: More than just a blog :)
Latest PHP Buzz Posts
Latest PHP Buzz Posts by Alan Knowles
Latest Posts From Smoking toooo much PHP

Advertisement
Well, for a change, a vaguely usefull blog post ;) - I was sitting down looking at the DBDO code, that I got ivan to look at a few months ago. Being a gtk guy, he had copied the gstring api into DBDO, which from my recollection of the PHP internals is pretty similar to smart_str. However, there is no documentation for smart_str, other than the source. So here is a first go at it....

** feel free to make coments / correction notes.

SMART STRING API

The Struct:

    typedef struct {
char *c; // data goes here..
size_t len; // the current length
size_t a; // the allocated size
} smart_str;
EXAMPLE:
    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);

If you are setting up a pointer to the smart_str, it's probably best to use the _sets method, to fix the memory


Creating Memory:

    void smart_str_alloc(smart_str *str, int len, int is_persistant)
EXAMPLE:
    smart_str *sstr;
sstr = emalloc(sizeof(smart_str));
smart_str_alloc(sstr, 100);

Note: This only allocates memory for the string inside the smart_str, if you are using a pointer to the smart_str you must still emalloc that.


Initialize:

     void smart_str_sets(smart_str *dest, char *src); 
EXAMPLE:
     smart_str *sstr;
sstr = emalloc(sizeof(smart_str));
smart_str_sets(sstr, strdup("This is a string"));
Note: this does not copy the string (so freeing the string after using this will likely resulting in a double free error

This is usefull when working with a pointer to the smart_str, where you can not initialize it as empty.

Closing a String

    void smart_str_0(smart_str *str)    
EXAMPLE:
    smart_str *sstr;
sstr = emalloc(sizeof(smart_str));

smart_str_appends(sstr, "Hello World");
smart_str_0();
printf("the string is %s", sstr->c);

Note: If you need to pass the string to a function that accepts \0 deliminated strings, you should use this function.


Add a Character to a string

    smart_str_appendc(smart_str *dest, char c)
EXAMPLE:
    smart_str *sstr;
char c = "C";

sstr = emalloc(sizeof(smart_str));
smart_str_appendc(sstr, c);

Notes: do we need to pre-allocate the string in any manner?


Add a string

    smart_str_appends(smart_str *dest, smart_str *add)
    smart_str_appends(smart_str *dest, char *add)
    smart_str_appendl(smart_str *dest, char *add, int n)

EXAMPLE:
    smart_str sstr1 = {0};
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);
Notes:
appendl: adds only the first n characters to the smart string.
appends: adds the new string to the smart string (assumes \0 terminated.

Adding Numbers to a smart string:

    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);

EXAMPLE:
    smart_str_append_long(sstr, 120);

Free the String:

    smart_str_free(smart_str *str)

Example:
    smart_str *sstr;
sstr = emalloc(sizeof(smart_str));
smart_str_sets(*sstr1, strdup("the cat"));
... do something ...

smart_str_free(sstr);
    

See Also:

autoallocating sprintf. (with \0 terminated added)
    snprintf(char *buffer, int max_size, char *format, .....);



Read: smart_str API

Topic: Easter Egg in PHP Previous Topic   Next Topic Topic: WordPress 2.x Hooks for Action - Comprehensive List for Plugin and Theme Developers

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use