Skip to content

Instantly share code, notes, and snippets.

@masterl
Last active August 29, 2015 14:20
Show Gist options
  • Save masterl/9be45bfeba1cd0ee8994 to your computer and use it in GitHub Desktop.
Save masterl/9be45bfeba1cd0ee8994 to your computer and use it in GitHub Desktop.
Helping out to fix a C substring generator
// The MIT License (MIT)
// Copyright (c) 2015 Vanderci Curvelo Jr, Leonardo de Oliveira Lourenço
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/* Original: https://github.com/vandercijr/cplus_strings/blob/master/cplus_strings.c
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
* char *substr(char *str, int start, int length)
* extrai uma substring de uma string
* retorna um ponteiro char para a substring
*
*/
char *substr(char *str, int start, int length)
{
char *_substr;
//aloca memoria para a substring (+1 para o \0)
_substr = (char *) malloc((length + 1) * sizeof(char));
if(_substr != NULL)
{
//copia a substring para a variavel
strncpy(_substr, str+start, length);
//finaliza a string com o caracter nulo
_substr[length] = '\0';
}
return _substr;
}
/*
* char **split(char *str, char delimiter)
* transforma uma string em um array baseado em caracteres separadores
* retorna um array char
*
*/
char **split(char *str, char delimiter)
{
char **tokens;
char *current;
char *previous = NULL;
char separator[2];
int length;
int length_substr;
int separators_found = 0;
int offset = 0;
length = (int) strlen(str);
separator[0] = delimiter;
separator[1] = '\0';
//conta as ocorrencias de caracteres separadores
do
{
current = strstr(str+offset, separator);
if(current != NULL)
{
offset = (current - str) + 1;
separators_found++;
}
} while(current != NULL);
//aloca memoria para a o array de tokens (substrings)
tokens = (char **) malloc((separators_found+2) * sizeof(char *));
if(tokens != NULL)
{
tokens[separators_found+1] = NULL;
// tokens[separators_found] = NULL;
offset = 0;
for(int i = 0; i < (separators_found+1); ++i)
{
//aponta para o caracter encontrado
current = strstr(str+offset, separator);
//calcula o tamanho da string que está entre os caracteres de separação
length_substr = (current != NULL) ? current - ((previous != NULL) ? (previous+1) : str) : (str+length) - (previous-1);
tokens[i] = substr(str, offset, length_substr);
offset = (current != NULL) ? (current - str) + 1 : 0;
previous = current;
}
}
return tokens;
}
void free_tokens(char ***tokens)
{
for(int i = 0; (*tokens)[i] != NULL; ++i)
{
free((*tokens)[i]);
}
free(*tokens);
*tokens = NULL;
}
/*
*
* Teste das funções de string
*
*/
int main(void)
{
char *msg;
char **arr;
msg = "DEVICE 0|W|abcdefghijkl|FFF321";
arr = split(msg, '|');
if(arr == NULL)
{
printf("Split error!");
return 1;
}
for(int i = 0; arr[i] != NULL; ++i)
{
printf(" token: [%s]\n", arr[i]);
}
free_tokens(&arr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment