Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //HEADER string_explode.h
- /**
- * @author Tibor Dancs (433610) @ fi.muni.cz
- */
- #ifndef STRING_EXPLODE.H
- #define STRING_EXPLODE.H
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- /**
- * @brief explode Takes char* in as formated input and const char *separator as priority heap of separators
- * @param in char* formated input
- * @param separator const char* priority heap of separators
- * @return original char array if no separator matched, single string if locateb between any {start of input, separator, end of string} or NULL if no strings to parse
- */
- char *explode(char *in, const char *separator);
- #endif
- //SOURCE string_explode.c
- /**
- * @author Tibor Dancs (433610) @ fi.muni.cz
- */
- #include "string_explode.h"
- char *explode(char *in, const char *separator) {
- static char *pos;
- if (!separator || !strcmp(separator,"")){
- if (pos){
- free(pos);
- pos = NULL;
- }
- return in;
- }
- char *ret;
- if (in) {
- pos = in;
- }
- if (!pos){
- return NULL;
- }
- //printf("\tDEB:%d:%s\t%d:%s\n",strlen(pos),pos,strlen(separator),separator);
- //printf("\tINFO:String ok:%s\n",pos);
- unsigned int i = 0;
- while (i < strlen(pos)){
- for (unsigned int j = 0;j < strlen(separator);j++){
- //printf("\t%c == %c\n",pos[i],separator[j]);
- if (pos[i] == separator[j]){
- ret = (char*)malloc(i+1);
- strncpy(ret,pos,i);
- ret[i] = '\0';
- //printf("\tINFO:copy string:%s\n",ret);
- char *tmp = (char*)malloc(strlen(pos) - i);
- strcpy(tmp,pos+i+1);
- //printf("\tINFO:copy temp:%s\n",tmp);
- if (pos) {
- free(pos);
- }
- pos = tmp;
- //printf("\tINFO:free pos\n");
- return ret;
- }
- }
- //printf("\t~\n");
- i++;
- }
- char *tmp = (char*)malloc(strlen(pos)+1);
- strcpy(tmp,pos);
- if (pos){
- free(pos);
- }
- pos = NULL;
- return tmp;
- }
Advertisement
Add Comment
Please, Sign In to add comment