tonnytg / 42_minishell

This project has objective to build a prompt with shell commands
Apache License 2.0
2 stars 2 forks source link

Convert str to token #44

Closed tonnytg closed 1 year ago

tonnytg commented 1 year ago

When execute command, the adapter receive char *arg but must be a list of words. This can be solved convert this unique sentence of string to a matrix of strings. char *str to char **strgs for example.

Can be a Matrix, Linked list, Array ...

Example of standard commands strtok this command convert string to token

include

include

char strtok_custom(char str, const char delim) { static char last = NULL; char *token;

  // If str is not NULL, start from the beginning
  if (str != NULL)
      last = str;

  // If last is NULL, there are no more tokens
  if (last == NULL)
      return NULL;

  // Skip leading delimiters
  last += strspn(last, delim);

  // If last points to the end of the string, there are no more tokens
  if (*last == '\0')
      return NULL;

  // Find the end of the current token
  token = last;
  last = strpbrk(last, delim);

  if (last != NULL) {
      // If a delimiter is found, replace it with a null terminator
      *last = '\0';
      last++;
  }

  return token;

}

tonnytg commented 1 year ago

@carlosrocha-dev PR to Approve https://github.com/tonnytg/42_minishell/pull/57

tonnytg commented 1 year ago

PR aprovada