I have written a program to parse a string depending on the string the user provides.
Any advise/criticism is appreciated.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define DEFAULT_SIZE 10
#define RESIZE_MULTI 2
/*compares value to see if it exists in parse. return 0 if exists*/
static uint8_t compare(const char value, const char *parse)
{
size_t i;
i = 0;
while(*(parse + i) != ' ')
{
if(*(parse + i) == value)
return 0;
i++;
}
return 1;
}
/*string must be null terminated. the string that needs to be split*/
/*parse must be null terminated. contains value that the string will be split at*/
/*matrix contains all the string that was split based on parse. the function return how how many string there are*/
size_t parse_string(const char *string, const char *parse, char *** const user_matrix)
{
/*how many spots there are*/
size_t temp_matrix_size;
/*how many acctually string there are*/
size_t temp_matrix_length;
char **temp_matrix;
size_t i;
size_t counter;
/*allocate initial memory. array of pointers*/
temp_matrix = malloc(sizeof(*temp_matrix) * DEFAULT_SIZE);
if(temp_matrix == NULL)
goto FAIL0;
temp_matrix_size = DEFAULT_SIZE;
/*since right now there 0 string aka pointers to strings*/
temp_matrix_length = 0;
i = counter = 0;
while(*(string + i) != ' ')
{
/*resize array of pointers*/
if(temp_matrix_length >= temp_matrix_size)
{
char **temp = realloc(temp_matrix, sizeof(*temp_matrix) * temp_matrix_size * RESIZE_MULTI);
if(temp == NULL)
goto FAIL0;
temp_matrix = temp;
temp_matrix_size = temp_matrix_size * RESIZE_MULTI;
}
if(compare(*(string + i), parse) == 0)
{
char *temp;
if(counter > 0)
{
/*need the plus 1 for terminating char*/
temp = malloc(sizeof(*temp) * (counter + 1));
if(temp == NULL)
goto FAIL0;
/*setting terminating char*/
*(temp + counter) = ' ';
/*copy over the string before the parsing char*/
memcpy(temp, string + (i - counter), counter);
/*store the string*/
*(temp_matrix + temp_matrix_length) = temp;
temp_matrix_length++;
}
/*store only the parsing char with null terminated char*/
temp = malloc(sizeof(*temp) * 2);
if(temp == NULL)
goto FAIL0;
*(temp + 0) = *(string + i);
*(temp + 1) = ' ';
*(temp_matrix + temp_matrix_length) = temp;
temp_matrix_length++;
i++;
counter = 0;
}
else
{
i++;
counter++;
}
}
/*special case*/
if(counter != 0)
{
char *temp = malloc(sizeof(*temp) * (counter + 1));
if(temp == NULL)
goto FAIL0;
/*setting terminating char*/
*(temp + counter) = ' ';
/*copy over the string before the parsing char*/
memcpy(temp, string + (i - counter), counter);
/*store the string*/
*(temp_matrix + temp_matrix_length) = temp;
temp_matrix_length++;
}
*user_matrix = temp_matrix;
return temp_matrix_length;
FAIL0:
if(temp_matrix != NULL)
{
/*free all the strings*/
for(i = 0; i < temp_matrix_length; i++)
{
free(*(temp_matrix + i));
}
/*free the array of pointers*/
free(temp_matrix);
}
*user_matrix = NULL;
return 0;
}
int main(void)
{
char **matrix;
size_t size;
/*can have anything you want*/
char *str = "Hello Word!!!";
/*can have any char you want*/
char *parse = "! ";
size = parse_string(str, parse, &matrix);
for(size_t i = 0; i < size; i++)
{
printf("%sn", *(matrix + i));
}
for(size_t i = 0; i < size; i++)
{
free(*(matrix + i));
}
free(matrix);
return 0;
}