daily pastebin goal
51%
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 209 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ** String.c for  in /home/romain.pillot/projects/cpp_d03/ex00
  3. **
  4. ** Made by romain pillot
  5. ** Login   <romain.pillot@epitech.net>
  6. **
  7. ** Started on  Fri Jan  5 08:07:34 2018 romain pillot
  8. ** Last update Fri Jan  5 13:00:49 2018 romain pillot
  9. */
  10.  
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <stdbool.h>
  16. #include "String.h"
  17.  
  18. static void safe_free(void **data);
  19. static void assign_s(String *this, const String *str);
  20. static void assign_c(String *this, const char *s);
  21. static void append_s(String *this, const String *ap);
  22. static void append_c(String *this, const char *ap);
  23. static char at(String *this, size_t pos);
  24. static void clear(String *this);
  25. static int size(String *this);
  26. static int compare_s(String *this, const String *str);
  27. static int compare_c(String *this, const char *s);
  28. static size_t copy(String *this, char *s, size_t n, size_t pos);
  29. static const char *c_str(String *this);
  30. static int empty(String *this);
  31. static int find_s(String *this, const String *str, size_t pos);
  32. static int find_c(String *this, const char *str, size_t pos);
  33. static void insert_s(String *this, size_t pos, const String *str);
  34. static void insert_c(String *this, size_t pos, const char *s);
  35. static int to_int(String *this);
  36. static String *split_s(String *this, char separator);
  37. static char **split_c(String *this, char separator);
  38. static void aff(String *this);
  39. static void join_s(String *this, char delim, const String *tab);
  40. static void join_c(String *this, char delim, const char **tab);
  41. static void substr(String *this, int offset, int length);
  42.  
  43. #define FREE(addr) safe_free((void **) &(addr))
  44.  
  45. void    StringInit(String *this, const char *s)
  46. {
  47.   if (!this)
  48.     return;
  49.   this->str = s ? strdup(s) : NULL;
  50.   this->assign_s = &assign_s;
  51.   this->assign_c = &assign_c;
  52.   this->append_s = &append_s;
  53.   this->append_c = &append_c;
  54.   this->at = &at;
  55.   this->clear = &clear;
  56.   this->size = &size;
  57.   this->compare_s = &compare_s;
  58.   this->compare_c = &compare_c;
  59.   this->copy = &copy;
  60.   this->c_str = &c_str;
  61.   this->empty = &empty;
  62.   this->find_s = &find_s;
  63.   this->find_c = &find_c;
  64.   this->insert_s = &insert_s;
  65.   this->insert_c = &insert_c;
  66.   this->to_int = &to_int;
  67.   this->split_s = &split_s;
  68.   this->split_c = &split_c;
  69.   this->aff = &aff;
  70.   this->join_c = &join_c;
  71.   this->join_s = &join_s;
  72.   this->substr = &substr;
  73. }
  74.  
  75. void    StringDestroy(String *this)
  76. {
  77.   if (!this)
  78.     return;
  79.   FREE(this->str);
  80.   free(this);
  81. }
  82.  
  83. static void safe_free(void **addr)
  84. {
  85.   if (!addr || !(*addr))
  86.     return;
  87.   free(*addr);
  88.   *addr = NULL;
  89. }
  90.  
  91. static void assign_s(String *this, const String *str)
  92. {
  93.   if (!this || !str)
  94.     return;
  95.   FREE(this->str);
  96.   if (str->str)
  97.     this->str = strdup(str->str);
  98. }
  99.  
  100. static void assign_c(String *this, const char *s)
  101. {
  102.   if (!this)
  103.     return;
  104.   FREE(this->str);
  105.   if (s)
  106.     this->str = strdup(s);
  107. }
  108.  
  109. static void append_s(String *this, const String *ap)
  110. {
  111.   if (ap)
  112.     append_c(this, ap->str);
  113. }
  114.  
  115. static void append_c(String *this, const char *ap)
  116. {
  117.   char          *new;
  118.   int           len;
  119.  
  120.   if (!this || !ap)
  121.     return;
  122.   len = this->str ? strlen(this->str) : 0;
  123.   len += ap ? strlen(ap) : 0;
  124.   new = malloc(sizeof(char) * (len + 1));
  125.   strcat(new, this->str ? this->str : "");
  126.   strcat(new, ap ? ap : "");
  127.   FREE(this->str);
  128.   this->str = new;
  129. }
  130.  
  131. static char at(String *this, size_t pos)
  132. {
  133.   if (!this || !this->str || pos >= strlen(this->str))
  134.     return (-1);
  135.   return (this->str[pos]);
  136. }
  137.  
  138. static void clear(String *this)
  139. {
  140.   if (this && this->str)
  141.     memset(this->str, 0, strlen(this->str));
  142. }
  143.  
  144. static int  size(String *this)
  145. {
  146.   return (this && this->str ? (int) strlen(this->str) : -1);
  147. }
  148.  
  149. static int  compare_s(String *this, const String *str)
  150. {
  151.   if (!this || !str || !this->str || !str->str)
  152.     return (0);
  153.   return (strcmp(this->str, str->str));
  154. }
  155.  
  156. static int  compare_c(String *this, const char *s)
  157. {
  158.   if (!this || !s || this->str)
  159.     return (0);
  160.   return (strcmp(this->str, s));
  161. }
  162.  
  163. static size_t   copy(String *this, char *s, size_t n, size_t pos)
  164. {
  165.   size_t    len;
  166.  
  167.   if (!this || !this->str || !s || n <= 0 || pos > strlen(this->str))
  168.     return (0);
  169.   strncpy(s, this->str + pos, n);
  170.   len = strlen(this->str + pos);
  171.   return (n > len ? len : n);
  172. }
  173.  
  174. static const char *c_str(String *this)
  175. {
  176.   if (!this || !this->str)
  177.     return (NULL);
  178.   return (this->str);
  179. }
  180.  
  181. static int empty(String *this)
  182. {
  183.   return (this && this->str && this->str[0] ? -1 : 1);
  184. }
  185.  
  186. static int find_s(String *this, const String *str, size_t pos)
  187. {
  188.   if (!str)
  189.     return (-1);
  190.   return (find_c(this, str->str, pos));
  191. }
  192.  
  193. static int  find_c(String *this, const char *str, size_t pos)
  194. {
  195.   if (!this || !this->str || !str || strlen(str) > strlen(this->str) || pos >= strlen(this->str))
  196.     return (-1);
  197.   return (strstr(this->str + pos, str) - this->str);
  198. }
  199.  
  200. static void insert_s(String *this, size_t pos, const String *str)
  201. {
  202.   if (str)
  203.     insert_c(this, pos, str->str);
  204. }
  205.  
  206. static void insert_c(String *this, size_t pos, const char *s)
  207. {
  208.   size_t    len;
  209.   size_t    slen;
  210.  
  211.   if (!this || !this->str || !s)
  212.     return;
  213.   len = strlen(this->str);
  214.   slen = strlen(s);
  215.   if (pos > len)
  216.     pos = len;
  217.   if (pos + slen > len) {
  218.     this->str = realloc(this->str, pos + slen + 1);
  219.     strcpy(this->str + pos, s);
  220.   } else
  221.     for (int i = 0; s[i]; i++)
  222.       this->str[pos + i] = s[i];
  223. }
  224.  
  225. static int  to_int(String *this)
  226. {
  227.   if (this && this->str)
  228.     return (atoi(this->str));
  229.   return (0);
  230. }
  231.  
  232. static size_t   count_char(char const *str, char c)
  233. {
  234.   int       i = 0;
  235.  
  236.   while (str && *str)
  237.     if (*str++ == c)
  238.       ++i;
  239.   return (i);
  240. }
  241.  
  242. static String   *split_s(String *this, char separator)
  243. {
  244.   char      **array;
  245.   String    *casted;
  246.   int       i = 0;
  247.  
  248.   array = split_c(this, separator);
  249.   if (!array)
  250.     return (NULL);
  251.   while (array[i])
  252.     i++;
  253.   casted = malloc(sizeof(String) * (i + 1));
  254.   casted[i].str = NULL;
  255.   for (int j = 0; j < i; j++){
  256.     StringInit(casted + j, array[j]);
  257.   }
  258.   FREE(*array);
  259.   FREE(array);
  260.   return (casted);
  261. }
  262.  
  263. static char **split_c(String *this, char separator)
  264. {
  265.   char  **tab;
  266.   char  *str;
  267.   char  *hold;
  268.   int   i = -1;
  269.   int   j;
  270.   int   k = 0;
  271.  
  272.   if (!this || !this->str)
  273.     return (NULL);
  274.   str = strdup(this->str);
  275.   hold = str;
  276.   tab = malloc(sizeof(char *) * ((count_char(str, separator) + 2)));
  277.   while (str[++i]) {
  278.       if ((str[(j = i)] == separator || !(str[(j = i + 1)]))) {
  279.       tab[k++] = hold;
  280.       hold = str + j + 1;
  281.       str[j] = 0;
  282.     }
  283.     }
  284.   tab[k] = 0;
  285.   return (tab);
  286. }
  287.  
  288. static void aff(String *this)
  289. {
  290.   if (!this || !this->str)
  291.     return;
  292.   printf("%s", this->str);
  293. }
  294.  
  295. static void join_c(String *this, char delim, const char **tab)
  296. {
  297.   int       len = 0;
  298.  
  299.   if (!this)
  300.     return;
  301.   for (int i = 0; tab[i]; i++)
  302.     len += strlen(tab[i]);
  303.   FREE(this->str);
  304.   this->str = malloc(sizeof(char) * (len + 1));
  305.   this->str[len] = 0;
  306.   for (int i = 0; tab[i]; i++) {
  307.     if (i > 0)
  308.       strcat(this->str, (char[]){delim, 0});
  309.     strcat(this->str, tab[i]);
  310.   }
  311. }
  312.  
  313. static void join_s(String *this, char delim, const String *tab)
  314. {
  315.   int       len = 0;
  316.  
  317.   if (!this)
  318.     return;
  319.   for (int i = 0; tab[i].str; i++)
  320.     len += strlen(tab[i].str);
  321.   FREE(this->str);
  322.   this->str = malloc(sizeof(char) * (len + 1));
  323.   this->str[0] = 0;
  324.   this->str[len] = 0;
  325.   for (int i = 0; tab[i].str; i++) {
  326.     if (i > 0)
  327.       strcat(this->str, (char[]){delim, 0});
  328.     strcat(this->str, tab[i].str);
  329.   }
  330. }
  331.  
  332. static String   *substr(String *this, int offset, int length)
  333. {
  334.  
  335.   return (NULL);
  336. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top