Hi fellows,
thank you for helping me to implement explode
here's the fine implementation, with functions formerly implemented which
are required by explode itself, I thought these maybe useful for somebody
:-) in addition a main method is there for testing explode.
*==================================================*
/*
* This is a collection of the functions used by
* explode and explode itself
* and a main method for trying explode.
*/
/*
* since explode orignally returns an array of strings
* it was better to return a structure that contains a
* size member which identifies that array's size.
* explode_return is that structure
*/
struct explode_return {
char **val;
int size;
};
/*
* XString strpos implementation
* haystack is the search string
* neddle is the string that will be searched for
* if neddle = haystack 0 is returned
* start is the place from where the function will start to check (optional)
*/
int strpos(char *haystack, char *neddle, int start)
{
if (strcmp(haystack,neddle)==0)
{
return 0;
}
int nedlen=strlen(neddle);
int haylen=strlen(haystack);
if (start>haylen-1 || start<0) {
start=0;
}
int returned=-1;
int i=0;
int br=0;
for (i=start;i<haylen-nedlen+1;i++)
{
if (haystack[i]==neddle[0])
{
if (nedlen>1)
{
int b;
for (b=i+1;b<nedlen+i;b++)
{
if (haystack[b]!=neddle[b-i])
{
break;
}
if (haystack[b]==neddle[b-i] && b==nedlen+i-1)
{
returned=i;
br=1;
break;
}
}
}
else
{
returned=i;
br=1;
break;
}
}
if (br==1)
break;
}
return returned;
}
/*
* XString substr implementation
* target is the string that we will cut from
* start is the cut starting position
* length is the place where we will finish the cut
*/
char *substr(char *target, int start, int length)
{
if (start<0) {
start=0;
}
if (length>strlen(target)-start) {
length=strlen(target)-start;
}
int i=0;
char *temp=(char *) malloc(length*sizeof(char));
for (i=start;i<length+start;i++) {
temp[i-start]=target[i];
}
return temp;
}
/*
* XString explode implementation
* sep is the separator string
* target is the string that will be exploded
* start is the index from which you want to start exploding
* limit is the number of elements returned (-1 = unlimited)
*/
struct explode_return explode(char *sep, char *target, int start, int limit)
{
/* calculate the length of strings */
int seplen=strlen(sep);
int tarlen=strlen(target);
/* make the initial vars */
char **temp;
int num_occ=0;
int f_start=0;
int cutlen;
start-=seplen;
/* the main loop */
while ((start=strpos(target,sep,start+seplen))!=-1 && f_start<tarlen &&
(num_occ<limit || limit==-1)) {
char *cut=substr(target,f_start,start-f_start);
f_start=start+seplen;
cutlen=strlen(cut);
if (cutlen==0) {
continue;
}
temp = (char **) realloc((num_occ>0) ? (temp) :
(0),(num_occ+1)*sizeof(char*));
temp[num_occ]=(char*) malloc(cutlen*sizeof(char));
strcpy(temp[num_occ],cut);
num_occ++;
free(cut);
}
if (f_start<tarlen) {
temp = (char **) realloc((num_occ>0) ? (temp) :
(0),(num_occ+1)*sizeof(char*));
temp[num_occ]=substr(target,f_start,tarlen-f_start);
num_occ++;
}
struct explode_return returned;
returned.val=temp;
returned.size=num_occ;
return returned;
}
/* this is a main for trying explode */
int main() {
struct explode_return f=explode(",","h,h,,h,h,j,u,o,p,ye",0,-1);
int i;
for (i=0;i<f.size;i++) {
printf("%s\n",f.val[i]);
free(f.val[i]);
}
free(f.val);
return 0;
}
*==================================================*
Regards,
Khalid Al-Kary.
_________________________________________________________________
Add photos to your e-mail with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail
|