Esta es la version de split() ya esta corregida debugeada y sirve en todo caso.
Quien no sepa usar Vectores en C esta muerto ( ** ), es decir Puntero a Puntero.
Código: Seleccionar todo
#include<stdlib.h>
#include<stdio.h>
#ifdef bool
typedef enum { false, true } bool;
#endif
unsigned int rectific_num(unsigned int vval,unsigned int vmin,unsigned int vmax);
bool fix_numbers_range(unsigned int* vIndexIni,unsigned int* vIndexEnd, unsigned int* vLen, unsigned int vMin, unsigned int vMax);
unsigned long strlen(char* string);
unsigned long instr(unsigned long start,char* string1,char* string2);
char* mid(char* str, unsigned int start, unsigned int len);
char* strcpy(char* str_dest,char* str_src);
char** split(char* str, char* delimiter, long* limit);
int main()
{
long lim=-1;
long limback=lim;
char **array = split((char*)"Este es un hola mundo desde InfrAngeluX-Soft",(char*)" ",&lim);
if ( array != 0 )
{
limback=lim;
for (; lim>-1 ;lim-- )
printf ("%d \t %s \n", lim , array[lim]);
for (; limback>-1 ;limback-- )
free (array[limback]);
free(array);
}
getchar();
return 1;
}
unsigned int rectific_num(unsigned int vval,unsigned int vmin,unsigned int vmax)
/*
Corrige [vVal] con respecto a un minimo y a un maximo valor.
By BlackZeroX ( http://Infrangelux.sytes.net/ )
*/
{
if (vval < vmin)
return vmin;
else if ( vval > vmax )
return vmax;
else
return vval;
}
bool fix_numbers_range(unsigned int* vIndexIni,unsigned int* vIndexEnd, unsigned int* vLen, unsigned int vMin, unsigned int vMax)
/*
Corrige los rangos de [vIndexIni],[vIndexEnd], [vLen] con respecto a un minimo y a un maximo valor.
[vLen] corresponde a la distancia entre [vIndexIni] y [vIndexEnd].
Se retorna false solo si [vMax] es menor que que [vIndexIni] o que [*vLen] es igual o menor a [0]
By BlackZeroX ( http://Infrangelux.sytes.net/ )
*/
{
if ( vMax >= *vIndexIni && *vLen != 0 )
{
*vIndexIni = rectific_num(*vIndexIni,vMin,vMax);
*vIndexEnd = rectific_num(*vIndexIni + --(*vLen),*vIndexIni,vMax);
*vLen= *vIndexEnd - *vIndexIni+1;
return ( (*vLen > 0) ? true: false);
} else {
return false;
}
}
unsigned long strlen(char* string)
/*
Retorna la longitud de [string]
By BlackZeroX ( http://Infrangelux.sytes.net/ )
*/
{
unsigned long i=0;
while (*(string++) != '\0') i++;
return i;
}
char* strcpy(char* str_dest,char* str_src)
/*
Copia una string a otra [*str_src] a [*str_dest].
Retorna el puntero a [str_dest]
By BlackZeroX ( http://Infrangelux.sytes.net/ )
*/
{
while( (*(str_dest++)=*(str_src++) == '\0' ) );
return (str_dest);
}
unsigned long instr(unsigned long start,char* string1,char* string2)
/*
[Start] indica la posicion inicial donde se empesara a buscar en [string1]
Retorna la posicion de [*string2] en [*string1].
By BlackZeroX ( http://Infrangelux.sytes.net/ )
*/
{
unsigned long q,c,limit;
q=c=limit=0;
long ls2len=0;
ls2len = strlen(string2) - 1;
if ( ls2len >= 0 )
{
limit = strlen(string1)-ls2len;
if ( limit > 1 )
{
q = start-1;
while ( q < limit )
{
while ( string1[q+c] == string2[c] )
if ( (c++) == (unsigned long)ls2len )
return q+1;
q+=c+1;
c=0;
}
}
} else if (*string1 > '\0') {
return 1;
}
return 0;
}
char* mid(char* str, unsigned int start, unsigned int len)
/*
Se obtiene el fragmento deseado de [*str]
[Start] Indica desde donde se empesara
[Len] Indica la distancia.
Retorna el puntero al clon del fragmento deseado de [*str]
By BlackZeroX ( http://Infrangelux.sytes.net/ )
*/
{
char* pch_t;
unsigned int ul_str=strlen(str);
unsigned int ul_end=start+len;
start--;len++;
if ( fix_numbers_range(&start,&ul_end,&len,0,ul_str) == true )
{
if ( (pch_t = (char*)malloc(sizeof(char)*--len)) != 0 )
{
for (ul_str=0;start <= ul_end;start++,ul_str++ )
pch_t[ul_str] = str[start];
pch_t[len]='\0';
return pch_t;
} else {
return 0;
}
} else {
return 0;
}
}
char** split(char* str, char* delimiter, long* limit)
/*
Separa a [*str] cada vez que se encuentre a [*delimiter] con un limite definido en [*limit]
Si [*limit] = -1 se crea un Maximo mayor a [-1] en el indice del vector retornado
Si [*limit] > -1 Se indica y establece un indice mayor [pero no fijo] del indice mayor del vector retornado
En ambos casos [*limit] retorna el Indice maximo del vector retornado por la funcion y la funcion retorna el puntero a el vector retultante.
En casos contrarios no crea un vector y retorna 0 y [*limit] = [-1]
By BlackZeroX ( http://Infrangelux.sytes.net/ )
*/
{
unsigned int ui_lp =1;
unsigned int ui_inc =1;
unsigned int ui_lns =0;
unsigned int ui_del =0;
unsigned long ui_ub =0;
char **pi_out =0;
if ( *limit >= -1 )
{
if ( strlen(delimiter) == 0 )
delimiter = (char*)" ";
ui_lns = strlen(str);
ui_del = strlen(delimiter);
pi_out = (char**)malloc(sizeof(char*));
for(;pi_out!=0;)
{
if ( ui_ub == *limit )
{
pi_out[ui_ub] = mid(str, ui_lp,ui_lns);
ui_ub = *limit;
break;
}
ui_inc = instr(ui_inc, str, delimiter);
if ( ui_inc == 0 )
{
if ( ui_lp != ui_lns || ui_lns == 1 )
pi_out[ui_ub] = mid(str, ui_lp,ui_lns);
break;
}
pi_out[ui_ub] = mid(str, ui_lp, ui_inc - ui_lp);
pi_out = (char**)realloc(pi_out,(sizeof(char*)*((++ui_ub)+1)));
ui_lp = ui_inc + ui_del;
ui_inc = ui_lp;
}
*limit = ui_ub;
return pi_out;
}
*limit=-1;
return 0;
}
Temibles Lunas!¡.