Advertisement
Guest User

Untitled

a guest
Sep 18th, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. #include "input.h"
  2.  
  3. #include <stdarg.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. void getInput(char *str, ...)
  9. {
  10. va_list ap;
  11. va_start(ap, str);
  12.  
  13. char identifier;
  14. char *separator = NULL;
  15. long char_num = 0;
  16. for (int i = 1; i < strlen(str); i++)
  17. {
  18. int malloc_check = 0;
  19. free(separator);
  20. char_num = 0;
  21. separator = NULL;
  22. // to get identifier
  23. identifier = str[i];
  24. // to get char_num
  25. char *s = NULL;
  26. int j = 0; // size
  27. i++;
  28. while (str[i] != '}')
  29. {
  30. s = realloc(s, j + 2);
  31. if (s == NULL)
  32. {
  33. printf("Couldn't allocate memory 1\n");
  34. free(s);
  35. return;
  36. }
  37. s[j] = str[i];
  38. i++;
  39. j++;
  40. }
  41. if (j > 0) // to make sure that there is a string for char_num, because
  42. // if there is no char_num entered no point doing this
  43. {
  44. s[j] = '\0';
  45. char *tail;
  46. char_num = strtol(s, &tail, 10);
  47. if (*tail != '\0')
  48. {
  49. printf("The char_num has to be integers\n");
  50. free(s);
  51. return;
  52. }
  53. }
  54. free(s);
  55. s = NULL;
  56.  
  57. // for separator
  58. j = 0;
  59. i++;
  60. while (str[i - 1] == '\\' || i < strlen(str) && str[i] != '{')
  61. {
  62. separator = realloc(separator, j + 2);
  63. if (separator == NULL)
  64. {
  65. printf("Couldn't allocate memory 2\n");
  66. free(separator);
  67. return;
  68. }
  69. separator[j] = str[i];
  70. i++;
  71. j++;
  72. }
  73. if (j > 0) // to make sure that separator isn't nothing(if nothing: go to
  74. // next pointer) (if nothing on last {} keep going until EOF)
  75. {
  76. separator[j] = '\0';
  77. }
  78. int len = 0;
  79. if (separator != NULL) // initialize string to compare with separator
  80. {
  81. len = strlen(separator) - 1;
  82. s = malloc(len + 2); // len + 2 = strlen(separator) + 1( +1 for '\0')
  83. if (s == NULL)
  84. {
  85. printf("could not allocate memory 3\n");
  86. free(s);
  87. return;
  88. }
  89. s[len + 1] = '\0';
  90. }
  91. char *tail; // used to check if user inputs correct digits
  92. // Now doing the strings part etc
  93. char *get = NULL;
  94. if (identifier != 's')
  95. {
  96. get = malloc(char_num + 1);
  97. int k = 0;
  98. char c;
  99. while ((c = fgetc(stdin)) != EOF && c != '\n' && c != '\r' /*'/r' for MacOS*/)
  100. {
  101. if (separator != NULL)
  102. {
  103. for (int l = len; l >= 1; l--)
  104. {
  105. s[l - 1] = s[l];
  106. }
  107. s[len] = c;
  108. }
  109. if (separator != NULL && !strcmp(separator, s))
  110. {
  111. break;
  112. }
  113. if (char_num == 0)
  114. {
  115. get = realloc(get, k + 2);
  116. if (get == NULL)
  117. {
  118. printf("Couldn't allocate memory 5\n");
  119. free(get);
  120. return;
  121. }
  122. get[k] = c;
  123. k++;
  124. }
  125. else if (char_num != 0) // not "else" for clarity and choice ;)
  126. {
  127. get[k] = c;
  128. k++;
  129. if (k >= char_num)
  130. {
  131. break;
  132. }
  133. }
  134. }
  135. get[k] = '\0';
  136. }
  137. if (identifier == 's')
  138. {
  139. char *temp = NULL;
  140. char **value = va_arg(ap, char **);
  141.  
  142. int k = 0;
  143. char c;
  144. while ((c = fgetc(stdin)) != EOF && c != '\n' && c != '\r' /*'/r' for MacOS*/)
  145. {
  146. if (separator != NULL)
  147. {
  148. for (int l = 1; l <= len; l++)
  149. {
  150. s[l - 1] = s[l];
  151. }
  152. s[len] = c;
  153. }
  154. if (separator != NULL && !strcmp(separator, s))
  155. {
  156. break;
  157. }
  158. if (char_num != 0 && k < char_num)
  159. {
  160. // value = (char*)value; // so no warnings
  161. ((char *)value)[k] = c;
  162. }
  163. if (char_num == 0)
  164. {
  165. temp = realloc(temp, k + 2);
  166. if (temp == NULL)
  167. {
  168. printf("Couldn't allocate memory 5\n");
  169. free(temp);
  170. return;
  171. }
  172. temp[k] = c;
  173. }
  174. k++;
  175. }
  176. if (char_num == 0)
  177. {
  178. temp[k] = '\0';
  179. if (c == '\n' || c == '\r' || c == EOF)
  180. {
  181. *value = temp;
  182. malloc_check = 1;
  183. free(s);
  184. continue;
  185. }
  186. int length = strlen(temp) - strlen(separator) + 1 + 1;
  187. temp = realloc(temp, length);
  188. temp[length - 1] = '\0';
  189. *value = temp;
  190. }
  191. else
  192. {
  193. if (c == '\n' || c == '\r' || c == EOF)
  194. {
  195. ((char *)value)[k] = '\0';
  196. malloc_check = 1;
  197. free(s);
  198. continue;
  199. }
  200. ((char *)value)[k - strlen(separator) + 1] = '\0';
  201. }
  202. }
  203. else if (identifier == 'c')
  204. {
  205. char *value = va_arg(ap, char *);
  206. *value = get[0];
  207. }
  208. else if (identifier == 'l')
  209. {
  210. long *value = va_arg(ap, long *);
  211. *value = strtol(get, &tail, 10);
  212. }
  213. else if (identifier == 'L')
  214. {
  215. long long *value = va_arg(ap, long long *);
  216. *value = strtoll(get, &tail, 10);
  217. }
  218. else if (identifier == 'i')
  219. {
  220. int *value = va_arg(ap, int *);
  221. *value = strtol(get, &tail, 10);
  222. }
  223. else if (identifier == 'f')
  224. {
  225. float *value = va_arg(ap, float *);
  226. *value = strtof(get, &tail);
  227. }
  228. else if (identifier == 'd')
  229. {
  230. double *value = va_arg(ap, double *);
  231. *value = strtod(get, &tail);
  232. }
  233. else
  234. {
  235. printf("Invalid identifier\n");
  236. break;
  237. }
  238. if (identifier != 's' && identifier != 'c' && *tail != '\0')
  239. {
  240. printf("Enter correct digit\n");
  241. return;
  242. }
  243. if (identifier != 's')
  244. {
  245. free(get);
  246. }
  247. if (malloc_check == 0)
  248. {
  249. free(s);
  250. }
  251. }
  252. free(separator);
  253. va_end(ap);
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement