Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. /*
  2. ** tools.c for tools in /home/tekm/tek2/PSU_2016_myirc
  3. **
  4. ** Made by leroy_0
  5. ** Login <maxime.leroy@epitech.eu>
  6. **
  7. ** Started on Mon May 22 15:35:15 2017 leroy_0
  8. ** Last update Wed Jun 7 19:58:35 2017 yan
  9. */
  10.  
  11. #include <ctype.h>
  12. #include "common.h"
  13.  
  14. char *epur_str(char *str)
  15. {
  16. char *tmp;
  17. int i;
  18. int k;
  19.  
  20. i = 0;
  21. k = 0;
  22. if ((tmp = malloc(sizeof(char) * (strlen(str) + 1))) == NULL)
  23. return (NULL);
  24. while ((str[i] == ' ') || (str[i] == '\t'))
  25. i++;
  26. while (str[i] != '\0')
  27. {
  28. tmp[k++] = str[i++];
  29. while ((str[i] == ' ') || (str[i] == '\t'))
  30. i++;
  31. if (((str[i - 1] == ' ') || (str[i - 1] == '\t')) && (str[i] != '\0'))
  32. {
  33. tmp[k] = ' ';
  34. k++;
  35. }
  36. }
  37. tmp[k] = '\0';
  38. free(str);
  39. return (tmp);
  40. }
  41.  
  42. int count_words(char *str, char *delim)
  43. {
  44. unsigned int x;
  45. unsigned int y;
  46. unsigned int z;
  47. unsigned int total;
  48.  
  49. x = 0;
  50. total = 0;
  51. if (!delim || !str)
  52. return (0);
  53. while (x <= strlen(str))
  54. {
  55. z = 0;
  56. y = 0;
  57. while (delim[z])
  58. {
  59. if (x + z <= strlen(str) && str[x + z] == delim[z])
  60. y++;
  61. z++;
  62. }
  63. x++;
  64. if (y == strlen(delim))
  65. total++;
  66. }
  67. total++;
  68. return (total);
  69. }
  70.  
  71. char **my_strtab(char *str, char *s)
  72. {
  73. char **tab;
  74. int x;
  75.  
  76. x = 1;
  77. if (!str)
  78. return (NULL);
  79. if ((tab = malloc(sizeof(char *) * (count_words(str, s) + 1))) == NULL)
  80. return (NULL);
  81. tab[0] = strtok(str, s);
  82. if (tab[0])
  83. tab[0] = strdup(tab[0]);
  84. while (tab && tab[x - 1])
  85. {
  86. tab[x] = strtok(NULL, s);
  87. if (tab[x])
  88. tab[x] = strdup(tab[x]);
  89. x++;
  90. }
  91. free(str);
  92. return (tab);
  93. }
  94.  
  95. bool check_validity(char **buff)
  96. {
  97. unsigned int x;
  98.  
  99. x = 0;
  100. if (strcmp(*buff, "") == 0
  101. || (*buff)[0] == '\n' || (*buff)[0] == '\r')
  102. return (false);
  103. if ((*buff)[strlen(*buff) - 1] == '\n')
  104. (*buff)[strlen(*buff) - 1] = '\0';
  105. while (*buff && (*buff)[x])
  106. {
  107. if ((*buff)[x] == ' ')
  108. x++;
  109. else
  110. break;
  111. }
  112. if (!(strlen(*buff) == x))
  113. *buff = epur_str(*buff);
  114. return (true);
  115. }
  116.  
  117. bool strcmp_insensible(char *src, char *to_cmp)
  118. {
  119. int delim;
  120. int x;
  121.  
  122. x = 0;
  123. if (src == NULL || to_cmp == NULL || strlen(src) != strlen(to_cmp))
  124. return (false);
  125. delim = strlen(to_cmp);
  126. while (x < delim)
  127. {
  128. if (toupper(src[x]) != toupper(to_cmp[x]))
  129. return (false);
  130. x++;
  131. }
  132. return (true);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement