Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** String.c for in /home/romain.pillot/projects/cpp_d03/ex00
- **
- ** Made by romain pillot
- ** Login <romain.pillot@epitech.net>
- **
- ** Started on Fri Jan 5 08:07:34 2018 romain pillot
- ** Last update Fri Jan 5 13:00:49 2018 romain pillot
- */
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include "String.h"
- static void safe_free(void **data);
- 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);
- static char at(String *this, size_t pos);
- static void clear(String *this);
- static int size(String *this);
- static int compare_s(String *this, const String *str);
- static int compare_c(String *this, const char *s);
- static size_t copy(String *this, char *s, size_t n, size_t pos);
- static const char *c_str(String *this);
- static int empty(String *this);
- static int find_s(String *this, const String *str, size_t pos);
- static int find_c(String *this, const char *str, size_t pos);
- static void insert_s(String *this, size_t pos, const String *str);
- static void insert_c(String *this, size_t pos, const char *s);
- static int to_int(String *this);
- static String *split_s(String *this, char separator);
- static char **split_c(String *this, char separator);
- static void aff(String *this);
- static void join_s(String *this, char delim, const String *tab);
- static void join_c(String *this, char delim, const char **tab);
- static void substr(String *this, int offset, int length);
- #define FREE(addr) safe_free((void **) &(addr))
- void StringInit(String *this, const char *s)
- {
- if (!this)
- return;
- this->str = s ? strdup(s) : NULL;
- this->assign_s = &assign_s;
- this->assign_c = &assign_c;
- this->append_s = &append_s;
- this->append_c = &append_c;
- this->at = &at;
- this->clear = &clear;
- this->size = &size;
- this->compare_s = &compare_s;
- this->compare_c = &compare_c;
- this->copy = ©
- this->c_str = &c_str;
- this->empty = ∅
- this->find_s = &find_s;
- this->find_c = &find_c;
- this->insert_s = &insert_s;
- this->insert_c = &insert_c;
- this->to_int = &to_int;
- this->split_s = &split_s;
- this->split_c = &split_c;
- this->aff = &aff;
- this->join_c = &join_c;
- this->join_s = &join_s;
- this->substr = &substr;
- }
- void StringDestroy(String *this)
- {
- if (!this)
- return;
- FREE(this->str);
- free(this);
- }
- static void safe_free(void **addr)
- {
- if (!addr || !(*addr))
- return;
- free(*addr);
- *addr = NULL;
- }
- static void assign_s(String *this, const String *str)
- {
- if (!this || !str)
- return;
- FREE(this->str);
- if (str->str)
- this->str = strdup(str->str);
- }
- static void assign_c(String *this, const char *s)
- {
- if (!this)
- return;
- FREE(this->str);
- if (s)
- this->str = strdup(s);
- }
- static void append_s(String *this, const String *ap)
- {
- if (ap)
- append_c(this, ap->str);
- }
- static void append_c(String *this, const char *ap)
- {
- char *new;
- int len;
- if (!this || !ap)
- return;
- len = this->str ? strlen(this->str) : 0;
- len += ap ? strlen(ap) : 0;
- new = malloc(sizeof(char) * (len + 1));
- strcat(new, this->str ? this->str : "");
- strcat(new, ap ? ap : "");
- FREE(this->str);
- this->str = new;
- }
- static char at(String *this, size_t pos)
- {
- if (!this || !this->str || pos >= strlen(this->str))
- return (-1);
- return (this->str[pos]);
- }
- static void clear(String *this)
- {
- if (this && this->str)
- memset(this->str, 0, strlen(this->str));
- }
- static int size(String *this)
- {
- return (this && this->str ? (int) strlen(this->str) : -1);
- }
- static int compare_s(String *this, const String *str)
- {
- if (!this || !str || !this->str || !str->str)
- return (0);
- return (strcmp(this->str, str->str));
- }
- static int compare_c(String *this, const char *s)
- {
- if (!this || !s || this->str)
- return (0);
- return (strcmp(this->str, s));
- }
- static size_t copy(String *this, char *s, size_t n, size_t pos)
- {
- size_t len;
- if (!this || !this->str || !s || n <= 0 || pos > strlen(this->str))
- return (0);
- strncpy(s, this->str + pos, n);
- len = strlen(this->str + pos);
- return (n > len ? len : n);
- }
- static const char *c_str(String *this)
- {
- if (!this || !this->str)
- return (NULL);
- return (this->str);
- }
- static int empty(String *this)
- {
- return (this && this->str && this->str[0] ? -1 : 1);
- }
- static int find_s(String *this, const String *str, size_t pos)
- {
- if (!str)
- return (-1);
- return (find_c(this, str->str, pos));
- }
- static int find_c(String *this, const char *str, size_t pos)
- {
- if (!this || !this->str || !str || strlen(str) > strlen(this->str) || pos >= strlen(this->str))
- return (-1);
- return (strstr(this->str + pos, str) - this->str);
- }
- static void insert_s(String *this, size_t pos, const String *str)
- {
- if (str)
- insert_c(this, pos, str->str);
- }
- static void insert_c(String *this, size_t pos, const char *s)
- {
- size_t len;
- size_t slen;
- if (!this || !this->str || !s)
- return;
- len = strlen(this->str);
- slen = strlen(s);
- if (pos > len)
- pos = len;
- if (pos + slen > len) {
- this->str = realloc(this->str, pos + slen + 1);
- strcpy(this->str + pos, s);
- } else
- for (int i = 0; s[i]; i++)
- this->str[pos + i] = s[i];
- }
- static int to_int(String *this)
- {
- if (this && this->str)
- return (atoi(this->str));
- return (0);
- }
- static size_t count_char(char const *str, char c)
- {
- int i = 0;
- while (str && *str)
- if (*str++ == c)
- ++i;
- return (i);
- }
- static String *split_s(String *this, char separator)
- {
- char **array;
- String *casted;
- int i = 0;
- array = split_c(this, separator);
- if (!array)
- return (NULL);
- while (array[i])
- i++;
- casted = malloc(sizeof(String) * (i + 1));
- casted[i].str = NULL;
- for (int j = 0; j < i; j++){
- StringInit(casted + j, array[j]);
- }
- FREE(*array);
- FREE(array);
- return (casted);
- }
- static char **split_c(String *this, char separator)
- {
- char **tab;
- char *str;
- char *hold;
- int i = -1;
- int j;
- int k = 0;
- if (!this || !this->str)
- return (NULL);
- str = strdup(this->str);
- hold = str;
- tab = malloc(sizeof(char *) * ((count_char(str, separator) + 2)));
- while (str[++i]) {
- if ((str[(j = i)] == separator || !(str[(j = i + 1)]))) {
- tab[k++] = hold;
- hold = str + j + 1;
- str[j] = 0;
- }
- }
- tab[k] = 0;
- return (tab);
- }
- static void aff(String *this)
- {
- if (!this || !this->str)
- return;
- printf("%s", this->str);
- }
- static void join_c(String *this, char delim, const char **tab)
- {
- int len = 0;
- if (!this)
- return;
- for (int i = 0; tab[i]; i++)
- len += strlen(tab[i]);
- FREE(this->str);
- this->str = malloc(sizeof(char) * (len + 1));
- this->str[len] = 0;
- for (int i = 0; tab[i]; i++) {
- if (i > 0)
- strcat(this->str, (char[]){delim, 0});
- strcat(this->str, tab[i]);
- }
- }
- static void join_s(String *this, char delim, const String *tab)
- {
- int len = 0;
- if (!this)
- return;
- for (int i = 0; tab[i].str; i++)
- len += strlen(tab[i].str);
- FREE(this->str);
- this->str = malloc(sizeof(char) * (len + 1));
- this->str[0] = 0;
- this->str[len] = 0;
- for (int i = 0; tab[i].str; i++) {
- if (i > 0)
- strcat(this->str, (char[]){delim, 0});
- strcat(this->str, tab[i].str);
- }
- }
- static String *substr(String *this, int offset, int length)
- {
- return (NULL);
- }
Add Comment
Please, Sign In to add comment