Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. bool speechmarkArg(char* str, char** args, int* index, int* argCount) {
  7.  
  8. static const char key[] = {"\""};
  9. int length = strlen(str);
  10.  
  11. // increment index to point to the first character of the argument
  12. *index += 1;
  13.  
  14. // get the length of the argument
  15. int argLength = strcspn(str + (*index), key);
  16.  
  17. // no closing speechmark was found, return an error
  18. if ((*index + argLength) == length) {
  19. return false;
  20. }
  21.  
  22. // store the new argument in the array of arguments
  23. args[*argCount] = malloc((argLength + 1) * sizeof(char));
  24. memcpy(args[*argCount], str + (*index), argLength);
  25. strcat(args[*argCount], "\0");
  26.  
  27. // update index to point to the closing speechmark. Upon returning
  28. // to the splut function, the for loop will immediately cycle past
  29. *argCount += 1;
  30. *index += argLength;
  31.  
  32. // return true on success
  33. return true;
  34. }
  35.  
  36. bool whitespaceArg(char* str, char** args, int* index, int* argCount) {
  37.  
  38. static const char key[] = {"\" "};
  39. char delim;
  40.  
  41. // get the length of the argument
  42. int argLength = strcspn(str + (*index), key);
  43.  
  44. // store the delimiting character
  45. delim = str[*index + argLength];
  46.  
  47. // store the new argument in the array of arguments
  48. args[*argCount] = malloc((argLength + 1) * sizeof(char));
  49. memcpy(args[*argCount], str + (*index), argLength);
  50. strcat(args[*argCount], "\0");
  51.  
  52. // update the index and argcount
  53. *index += argLength;
  54. *argCount += 1;
  55.  
  56. if (delim == ' ' || delim == '\0') {
  57. return true;
  58. } else {
  59. if (!speechmarkArg(str, args, index, argCount)) {
  60. return false;
  61. }
  62. }
  63.  
  64. // return true on success
  65. return true;
  66. }
  67.  
  68. int main() {
  69.  
  70. char** args = malloc(32 * sizeof(char*));
  71. char* str = " arg1 arg2\"the best arg\"arg3 \"another great argument\" arg4 arg5 ";
  72.  
  73. int length = strlen(str);
  74. int argCount = 0;
  75. for (int i = 0; i < length; i += 1) {
  76. if (str[i] == ' ') {
  77. continue;
  78. } else if (str[i] == '\"') {
  79. if (!speechmarkArg(str, args, &i, &argCount)) {
  80. puts("error: expected closing quotation mark");
  81. break;
  82. }
  83. } else {
  84. if (!whitespaceArg(str, args, &i, &argCount)) {
  85. puts("error: expected closing quotation mark");
  86. break;
  87. }
  88. }
  89. }
  90.  
  91. // probably wanna free here tbh
  92. // keep track of the fact the arguments weren't successfully closed
  93. for (int i = 0; i < argCount; i += 1) {
  94. puts(args[i]);
  95. }
  96.  
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement