Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. /*
  2. ** String.c for ex00 in /media/gmardon/1aa9b3b8-3e24-4ea9-9b0d-d57254b2d1b9/guillaume.mardon/delivery/cpp_d03/ex00/
  3. **
  4. ** Made by Guillaume MARDON
  5. ** Login <guillaume.mardon@epitech.eu@epitech.eu>
  6. **
  7. ** Started on Fri Jan 6 09:08:07 2017 Guillaume MARDON
  8. ** Last update Fri Jan 6 21:32:29 2017 Guillaume MARDON
  9. */
  10. #include "String.h"
  11. #include <stdio.h>
  12. #include <stddef.h>
  13. #include <unistd.h>
  14.  
  15. static void assign_s(String *this, String const *str);
  16. static void assign_c(String *this, const char *s);
  17. static void append_s(String* this, String const* ap);
  18. static void append_c(String* this, char const* ap);
  19. static char at(String* this, size_t pos);
  20. static void clear(String* this);
  21. static size_t size(String* this);
  22. static int compare_s(String *this, const String* str);
  23. static int compare_c(String *this, const char* str);
  24. static size_t copy(String* this, char* s, size_t n, size_t pos);
  25. static const char* c_str (String * this);
  26. static int empty(String* this);
  27. static int find_s(String* this, const String *str, size_t pos);
  28. static int find_c(String* this, const char* str, size_t pos);
  29. static void aff(String *this);
  30.  
  31. void StringInit(String *this, char const *s)
  32. {
  33. if (!this)
  34. return;
  35.  
  36. free(this->str);
  37. this->str = malloc(sizeof(char) * strlen(s));
  38. strcpy(this->str, s);
  39.  
  40. this->assign_s = &assign_s;
  41. this->assign_c = &assign_c;
  42. this->append_s = &append_s;
  43. this->append_c = &append_c;
  44. this->at = &at;
  45. this->clear = &clear;
  46. this->size = &size;
  47. this->compare_c = &compare_c;
  48. this->compare_s = &compare_s;
  49. this->copy = &copy;
  50. this->c_str = &c_str;
  51. this->empty = &empty;
  52. this->find_s = &find_s;
  53. this->find_c = &find_c;
  54. this->aff = &aff;
  55. }
  56.  
  57. void StringDestroy(String *this)
  58. {
  59. if (!this)
  60. return;
  61.  
  62. free(this->str);
  63. free(this);
  64. }
  65.  
  66. static void assign_s(String *this, String const *str)
  67. {
  68. if (!this || !str)
  69. return;
  70.  
  71. free(this->str);
  72. this->str = malloc(sizeof(char) * strlen(str->str));
  73. strcpy(this->str, str->str);
  74. }
  75.  
  76. static void assign_c(String *this, const char *s)
  77. {
  78. if (!this || !s)
  79. return;
  80.  
  81. free(this->str);
  82. this->str = malloc(sizeof(char) * strlen(s));
  83. strcpy(this->str, s);
  84. }
  85.  
  86. static void append_s(String* this, String const* ap)
  87. {
  88. char *str;
  89.  
  90. if (!this || !ap)
  91. return;
  92.  
  93. str = malloc(sizeof(char) * strlen(this->str));
  94. strcpy(str, this->str);
  95. free(this->str);
  96. this->str = malloc(sizeof(char) * (strlen(ap->str) + strlen(str)));
  97. strcat(this->str, str);
  98. strcat(this->str, ap->str);
  99. free(str);
  100. }
  101.  
  102. static void append_c(String* this, char const* ap)
  103. {
  104. char *str;
  105.  
  106. if (!this || !ap)
  107. return;
  108.  
  109. str = malloc(sizeof(char) * strlen(this->str));
  110. strcpy(str, this->str);
  111. free(this->str);
  112. this->str = malloc(sizeof(char) * (strlen(ap) + strlen(str)));
  113. strcat(this->str, str);
  114. strcat(this->str, ap);
  115. free(str);
  116. }
  117.  
  118. static char at(String* this, size_t pos)
  119. {
  120. if (!this || pos > strlen(this->str))
  121. return (-1);
  122. return (this->str[pos]);
  123. }
  124.  
  125. static void clear(String* this)
  126. {
  127. if (!this)
  128. return;
  129.  
  130. this->str[0] = 0;
  131. }
  132.  
  133. static size_t size(String* this)
  134. {
  135. if (!this)
  136. return (0);
  137.  
  138. return (strlen(this->str));
  139. }
  140.  
  141. static int compare_s(String *this, const String* str)
  142. {
  143. if (!this || !str)
  144. return (0);
  145.  
  146. return (strcmp(this->str, str->str));
  147. }
  148.  
  149. static int compare_c(String *this, const char* str)
  150. {
  151. if (!this || !str)
  152. return (0);
  153.  
  154. return (strcmp(this->str, str));
  155. }
  156.  
  157. static size_t copy(String* this, char* s, size_t n, size_t pos)
  158. {
  159. size_t index;
  160. size_t length;
  161.  
  162. index = 0;
  163. if (!this || !s)
  164. return (index);
  165.  
  166. length = this->size(this);
  167. while (n-- && pos < length)
  168. s[index++] = this->str[pos++];
  169. return index;
  170. }
  171.  
  172. static const char* c_str (String *this)
  173. {
  174. if (!this)
  175. return (NULL);
  176.  
  177. return (this->str);
  178. }
  179.  
  180. static int empty(String* this)
  181. {
  182. if (!this || !this->str || !this->str[0])
  183. return (1);
  184.  
  185. return (-1);
  186. }
  187.  
  188. static int find_s(String* this, const String *str, size_t pos)
  189. {
  190. char *find;
  191. char *in;
  192. char *found;
  193. int index;
  194.  
  195. find = strdup(str->str);
  196. in = strdup(this->str + pos);
  197. found = strstr(in, find);
  198. if (!found)
  199. return (-1);
  200. index = found - in;
  201. free(find);
  202. free(in);
  203. return (index);
  204. }
  205.  
  206. static int find_c(String* this, const char* str, size_t pos)
  207. {
  208. char *find;
  209. char *in;
  210. char *found;
  211. int index;
  212.  
  213. find = strdup(str);
  214. in = strdup(this->str + pos);
  215. found = strstr(in, find);
  216. if (!found)
  217. return (-1);
  218. index = found - in;
  219. free(find);
  220. free(in);
  221. return (index);
  222. }
  223.  
  224. static void aff(String *this)
  225. {
  226. if (!this || !this->str)
  227. return;
  228.  
  229. write(STDOUT_FILENO, this->str, strlen(this->str));
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement