Menu

See the  *  GNU General Public License for more details

A dynamic string data structure helper  implementation .
Filed under:.

— Leave a comment October 21

2014 In this article I will present to you a dynamic string data structure helper implementation.
In C, .

A string is just an array of characters terminated with the null character (NUL in ASCII)

Also, some times we handle a string by using a pointer to a buffer containing sequential characters terminated also with the null character.
The C standard liibrary provides various functions for string manipulation but it seems that whenever we want to extend a string by using pointers we have to perform all the time memory reallocation which leads to a bad evolution of replicated code.
In a recent project , I needed to create a string and append to it any number of new strings in order to extend it without using strcat, strcpy, strlen and realloc functions all the time.
So, I created a simple dynamic string data structure which solved my specific problem and I would like to share it with you people.
Below, is the header containing the data type definition of the dynamic string: /*  *  Copyright (C) 2014  Efstathios Chatzikyriakidis ([email protected]).
*  *  This program is free software : you can redistribute it and/or modify  *  it under the terms of the GNU General Public License as published by  *  the Free Software Foundation, either version 3 of the License, or  *  (at your option) any later version.
*  *  This program is distributed in the hope that it will be useful,  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the  *  GNU General Public License for more details.
*  *  You should have received a copy of the GNU General Public License  *  along with this program.
If not, see .
*/ #ifndef _DYNAMIC_STRING_HELPER_TYPES_H_ #define _DYNAMIC_STRING_HELPER_TYPES_H_ #include #include “shared-types.h” /*  * type definitions .
*/ // dynamic string type definition.
typedef struct dynamic_string_t {   string_t string;   size_t string_length;   size_t buffer_length; } dynamic_string_t; #endif // _DYNAMIC_STRING_HELPER_TYPES_H_ Next, follows the header of the source code unit: /*  *  Copyright (C) 2014  Efstathios Chatzikyriakidis ([email protected]).
*  *  This program is free software: you can redistribute it and/or modify  *  it under the terms of the GNU General Public License as published by  *  the Free Software Foundation, either version 3 of the License, or  *  (at your option) any later version.
*  *  This program is distributed in the hope that it will be useful,  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the  *  GNU General Public License for more details.
*  *  You should have received a copy of the GNU General Public License  *  along with this program.
If not, see .
*/ #ifndef _DYNAMIC_STRING_HELPER_UTILITY_H_ #define _DYNAMIC_STRING_HELPER_UTILITY_H_ #include “dynamic-string-helper-types.h” dynamic_string_t * const dynamic_string_create (size_t buffer_length); void dynamic_string_clear (dynamic_string_t * dynamic_string); bool_t dynamic_string_append (dynamic_string_t * const dynamic_string, const string_t const string); size_t dynamic_string_length (const dynamic_string_t * const dynamic_string); #endif // _DYNAMIC_STRING_HELPER_UTILITY_H_ Here is the implementation of the source code unit: /*  *  Copyright (C) 2014  Efstathios Chatzikyriakidis ([email protected]).
*  *  This program is free software: you can redistribute it and/or modify  *  it under the terms of the GNU General Public License as published by  *  the Free Software Foundation, either version 3 of the License, or  *  (at your option) any later version.
*  *  This program is distributed in the hope that it will be useful,  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the  *  GNU General Public License for more details.
*  *  You should have received a copy of the GNU General Public License  *  along with this program.
If not, see .
*/ #include #include #include “memory-management-helper-macros.h” #include “dynamic-string-helper-utility.h” /*  * functions.
*/ static unsigned int get_next_power_of_buffer_length (unsigned int current_length) { –current_length; current_length |= current_length >> 0x01; current_length |= current_length >> 0x02; current_length |= current_length >> 0x04; current_length |= current_length >> 0x08; current_length |= current_length >> 0x10; return ++current_length; } static bool_t dynamic_string_extend (dynamic_string_t * const dynamic_string, const size_t extra_length) { const size_t total_buffer_length = dynamic_string->string_length + extra_length; if (total_buffer_length <= dynamic_string->buffer_length) { return true; } const unsigned int next_power_of_buffer_length = get_next_power_of_buffer_length (total_buffer_length); const string_t const string = (string_t) realloc (dynamic_string->string, next_power_of_buffer_length); if (!string) { return false; } dynamic_string->buffer_length = next_power_of_buffer_length; dynamic_string->string = string; return true; } dynamic_string_t * const dynamic_string_create (size_t buffer_length) {   static const size_t initial_buffer_length = 32;   if (buffer_length < 0)   {     buffer_length = initial_buffer_length;   }   dynamic_string_t * dynamic_string = (dynamic_string_t *) calloc (1, sizeof (dynamic_string_t));   if (!dynamic_string)   {     return NULL;   }   dynamic_string->string = NULL;   dynamic_string->string_length = 0;   dynamic_string->buffer_length = buffer_length;   if (dynamic_string->buffer_length > 0)   {     dynamic_string->string = (string_t) malloc (dynamic_string->buffer_length);     if (!dynamic_string->string)     {       safe_free (dynamic_string);       return NULL;     }   }   return dynamic_string; } void dynamic_string_clear (dynamic_string_t * dynamic_string) {   if (dynamic_string)   {     safe_free(dynamic_string->string);     safe_free.
Rate this:.
Share this:.

Click to share on Facebook (Opens in new window)

Click to share on LinkedIn (Opens in new window)

Click to share on Twitter (Opens in new window)

Click to print (Opens in new window)

Click to email this to a friend (Opens in new window)

Like this:.
Like Loading.
Related.
Tags: dynamic, , , , , .

Comments RSS feed Leave a Reply Cancel reply

Enter your comment here.
Fill in your details below or click an icon to log in:.
Email (Address never made public) Name Website You are commenting using your WordPress.com account.
( Log Out /   ) You are commenting using your Google account.
( Log Out /   ) You are commenting using your Twitter account.
( Log Out /   ) You are commenting using your Facebook account.
( Log Out /   ) Cancel Connecting to %s Notify me of new comments via email.
Notify me of new posts via email.
« A useful function for getting the host name and service of a socket.
Arduino: Security Alarm System.
».
(79).
(21).
(15).
(26).
(4).
(7).
(55).
(24).
(4).
(16).
(14).
(4).
(7).
(10).
(78).
(11).
(9).
(1).
October 2014 M T W T F S S  12345 6789101112 13141516171819 20212223242526 2728293031   « Sep Mar ».
(2).
(4).
(1).
(1).
(2).
(1).
(1).
(1).
(2).
(1).
(9).
(1).
(8).
(1).
(1).
(2).
(4).
(7).
(1).
(1).
(1).
(8).
(12).
(1).
(2).
(1).
(2).
(1).
(2).
(1).
(1).
(4).
(20).
(13).
(5).
(2).
(10).
(13).
(10).
(10).
(20).
287,004 hits.
Send to Email Address Your Name Your Email Address Cancel Post was not sent – check your email addresses.
Email check failed.

Please try again Sorry

your blog cannot share posts by email.
%d bloggers like this:.

Tags: , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *