Created
January 5, 2018 08:38
-
-
Save Romain-P/51ebad2190c06c4d1956cbe7f0e0e0bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <unistd.h> | |
| #include <string.h> | |
| #include "String.h" | |
| static void assign_s(String *this, const String *str); | |
| static void assign_c(String *this, const char *s); | |
| static void append_s(String *this, const String *ap); | |
| static void append_c(String *this, const char *ap); | |
| void StringInit(String *this, const char *s) | |
| { | |
| if (this) | |
| this->str = strdup(s); | |
| this->assign_s = &assign_s; | |
| this->assign_c = &assign_c; | |
| } | |
| void StringDestroy(String *this) | |
| { | |
| if (!this) | |
| return; | |
| if (this->str) | |
| free(this->str); | |
| free(this); | |
| } | |
| static void safe_free(void *data) | |
| { | |
| void **addr = (void **) data; | |
| if (addr && *addr) | |
| free(*addr); | |
| *addr = NULL; | |
| } | |
| static void assign_s(String *this, const String *str) | |
| { | |
| if (!this || !str) | |
| return; | |
| safe_free(&this->str); | |
| if (str->str) | |
| this->str = strdup(str->str); | |
| } | |
| static void assign_c(String *this, const char *s) | |
| { | |
| if (!this) | |
| return; | |
| safe_free(&this->str); | |
| if (s) | |
| this->str = strdup(s); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef STRING_H_ | |
| # define STRING_H_ | |
| typedef struct String | |
| { | |
| char *str; | |
| void (*assign_s)(struct String *this, const struct String *str); | |
| void (*assign_c)(struct String *this, const char *s); | |
| void (*append_s)(String *this, const String *ap); | |
| void (*append_c)(String *this, const char *ap); | |
| } String; | |
| void StringInit(String *this, const char *s); | |
| void StringDestroy(String *this); | |
| #endif /* !STRING_H_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment