Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** String.c for ex00 in /media/gmardon/1aa9b3b8-3e24-4ea9-9b0d-d57254b2d1b9/guillaume.mardon/delivery/cpp_d03/ex00/
- **
- ** Made by Guillaume MARDON
- ** Login <guillaume.mardon@epitech.eu@epitech.eu>
- **
- ** Started on Fri Jan 6 09:08:07 2017 Guillaume MARDON
- ** Last update Fri Jan 6 21:32:29 2017 Guillaume MARDON
- */
- #include "String.h"
- #include <stdio.h>
- #include <stddef.h>
- #include <unistd.h>
- static void assign_s(String *this, String const *str);
- static void assign_c(String *this, const char *s);
- static void append_s(String* this, String const* ap);
- static void append_c(String* this, char const* ap);
- static char at(String* this, size_t pos);
- static void clear(String* this);
- static size_t size(String* this);
- static int compare_s(String *this, const String* str);
- static int compare_c(String *this, const char* str);
- 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 aff(String *this);
- void StringInit(String *this, char const *s)
- {
- if (!this)
- return;
- free(this->str);
- this->str = malloc(sizeof(char) * strlen(s));
- strcpy(this->str, s);
- 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_c = &compare_c;
- this->compare_s = &compare_s;
- this->copy = ©
- this->c_str = &c_str;
- this->empty = ∅
- this->find_s = &find_s;
- this->find_c = &find_c;
- this->aff = &aff;
- }
- void StringDestroy(String *this)
- {
- if (!this)
- return;
- free(this->str);
- free(this);
- }
- static void assign_s(String *this, String const *str)
- {
- if (!this || !str)
- return;
- free(this->str);
- this->str = malloc(sizeof(char) * strlen(str->str));
- strcpy(this->str, str->str);
- }
- static void assign_c(String *this, const char *s)
- {
- if (!this || !s)
- return;
- free(this->str);
- this->str = malloc(sizeof(char) * strlen(s));
- strcpy(this->str, s);
- }
- static void append_s(String* this, String const* ap)
- {
- char *str;
- if (!this || !ap)
- return;
- str = malloc(sizeof(char) * strlen(this->str));
- strcpy(str, this->str);
- free(this->str);
- this->str = malloc(sizeof(char) * (strlen(ap->str) + strlen(str)));
- strcat(this->str, str);
- strcat(this->str, ap->str);
- free(str);
- }
- static void append_c(String* this, char const* ap)
- {
- char *str;
- if (!this || !ap)
- return;
- str = malloc(sizeof(char) * strlen(this->str));
- strcpy(str, this->str);
- free(this->str);
- this->str = malloc(sizeof(char) * (strlen(ap) + strlen(str)));
- strcat(this->str, str);
- strcat(this->str, ap);
- free(str);
- }
- static char at(String* this, size_t pos)
- {
- if (!this || pos > strlen(this->str))
- return (-1);
- return (this->str[pos]);
- }
- static void clear(String* this)
- {
- if (!this)
- return;
- this->str[0] = 0;
- }
- static size_t size(String* this)
- {
- if (!this)
- return (0);
- return (strlen(this->str));
- }
- static int compare_s(String *this, const String* str)
- {
- if (!this || !str)
- return (0);
- return (strcmp(this->str, str->str));
- }
- static int compare_c(String *this, const char* str)
- {
- if (!this || !str)
- return (0);
- return (strcmp(this->str, str));
- }
- static size_t copy(String* this, char* s, size_t n, size_t pos)
- {
- size_t index;
- size_t length;
- index = 0;
- if (!this || !s)
- return (index);
- length = this->size(this);
- while (n-- && pos < length)
- s[index++] = this->str[pos++];
- return index;
- }
- static const char* c_str (String *this)
- {
- if (!this)
- return (NULL);
- return (this->str);
- }
- static int empty(String* this)
- {
- if (!this || !this->str || !this->str[0])
- return (1);
- return (-1);
- }
- static int find_s(String* this, const String *str, size_t pos)
- {
- char *find;
- char *in;
- char *found;
- int index;
- find = strdup(str->str);
- in = strdup(this->str + pos);
- found = strstr(in, find);
- if (!found)
- return (-1);
- index = found - in;
- free(find);
- free(in);
- return (index);
- }
- static int find_c(String* this, const char* str, size_t pos)
- {
- char *find;
- char *in;
- char *found;
- int index;
- find = strdup(str);
- in = strdup(this->str + pos);
- found = strstr(in, find);
- if (!found)
- return (-1);
- index = found - in;
- free(find);
- free(in);
- return (index);
- }
- static void aff(String *this)
- {
- if (!this || !this->str)
- return;
- write(STDOUT_FILENO, this->str, strlen(this->str));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement