#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLENGTH 200
#define WHITESPACE " \n\t"
#define MALLOC_ERROR_CODE -1
#define _CRT_SECURE_NO_WARNINGS
#define strdup _strdup
int tokenize_v1(char *str, char ***res);
int tokenize_v2(char *str, char ***res);
int count_tokens(char *str, char *delimiters);
void allocate_token_array(int num_of_tokens, char ***res);
void fill_token_array(char *str, char *delimiters, char **res);
void double_array_size(char ***res, int *current_capacity);
void main(){
char **tokens=NULL;
char str[] = "abc def xxx yyy 1111 555555 ajhjsd dkhfdkf dfjdkf dfkjdkfk dfkj dkfjdkf";
int num_of_tokens;
num_of_tokens = tokenize_v1(str, &tokens);
printf("================================================ \n");
num_of_tokens = tokenize_v2(str, &tokens);
}
int tokenize_v1(char *str, char ***res){
int num_of_tokens;
char *copy, *p;
copy = strdup(str);
if (!copy){
fprintf(stderr, "allocation error!\n");
exit(MALLOC_ERROR_CODE);
}
num_of_tokens = count_tokens(copy, WHITESPACE);
allocate_token_array(num_of_tokens, res);
strcpy(copy, str);
fill_token_array(copy, WHITESPACE, *res);
free(copy);
return (num_of_tokens);
}
int count_tokens(char *str, char *delimiters){
int num_of_tokens = 0;
char *p;
p = strtok(str, WHITESPACE);
while (p){
num_of_tokens++;
p = strtok(NULL, WHITESPACE);
}
return num_of_tokens;
}
void allocate_token_array(int num_of_tokens, char ***res){
*res = (char**)malloc(num_of_tokens*sizeof(char*));
if (!res){
fprintf(stderr, "allocation error!\n");
exit(MALLOC_ERROR_CODE);
}
}
void fill_token_array(char *str, char *delimiters, char **res){
int curr_token_idx = 0;
char *p;
p = strtok(str, delimiters);
while (p){
res[curr_token_idx] = (char*)malloc((strlen(p) + 1)*sizeof(char));
if (!res[curr_token_idx]){
fprintf(stderr, "allocation error!\n");
exit(MALLOC_ERROR_CODE);
}
strcpy(res[curr_token_idx], p);
curr_token_idx++;
p = strtok(NULL, delimiters);
}
}
int tokenize_v2(char *str, char ***res){
int num_of_tokens, current_capacity;
char *copy, *p;
*res = NULL;
copy = strdup(str);
if (!copy){
fprintf(stderr, "allocation error!\n");
exit(MALLOC_ERROR_CODE);
}
current_capacity = num_of_tokens = 0;
p = strtok(copy, WHITESPACE);
while (p){
if (num_of_tokens == current_capacity)
double_array_size(res, ¤t_capacity);
(*res)[num_of_tokens] = strdup(p);
p = strtok(NULL, WHITESPACE);
}
*res = (char**)realloc(*res, num_of_tokens*sizeof(char*));
free(copy);
return(num_of_tokens);
}
void double_array_size(char ***res, int *current_capacity){
*current_capacity = (*current_capacity) * 2 + 1;
printf("resizing\n");
*res = (char**)realloc(*res, *current_capacity*sizeof(char*));
if (!*(res)){
fprintf(stderr, "allocation error!\n");
exit(MALLOC_ERROR_CODE);
}
}