/*
 * ImapProxy - a caching IMAP proxy daemon
 * Copyright (C) 2002 Steven Van Acker
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */


#include <string.h>
#include <stdlib.h>

#include <splitstring.h>
#include <output.h>

/* initialise */
int init_split_string(Split_string *p)
{
    memset(p,0,sizeof(Split_string));
    return 0;
}

/* make a split string, from a given string
 * the string will be split up in words
 * delim is a string with all the delimitors
 */
int make_split_string(char *buf,Split_string *x, char *delim)
{
    char *p,*q,*newbuf;
    int i;

    p = buf;
    /* remove spaces to start with */
    //while(*p == ' ') p++;
    while(*p && strchr(delim,*p)) p++;
    
    while(p)
    {
	/* if string ends here, don't add anything and bail out */
	if(!*p)
	    return 0;
	
	/* find the first space */
	q = p;
	while(q && *q && !strchr(delim,*q))
	    q++;
	    
	//q = strchr(p,' ');

	/* create some room */
	x->numparts++;
	if(!(x->parts = (char **)realloc(x->parts,x->numparts * sizeof(char *))))
	{
	    debug("make_split_string(\"%s\",%p,\"%s\"): Out of memory.\n",buf,p,delim);
	    return -1;
	}

	i = x->numparts - 1;
	
	
	if(q)
	{
	    /* there are spaces behind the current word, so be carefull to not include them */

	    if(!(x->parts[i] = (char *)malloc(q - p + 1)))
	    {
		debug("make_split_string(\"%s\",%p,\"%s\"): Out of memory.\n",buf,p,delim);
		return -1;
	    }
	    
	    newbuf = x->parts[i];

	    strncpy(newbuf,p,q-p);
	    newbuf[q-p] = '\0';

	    /* skip spaces again */
	    //while(*q == ' ') q++;
	    while(*q && strchr(delim,*q)) q++;
	}
	else
	{
	    /* no spaces after this word, meaning this is the last word */
	    if(!(x->parts[i] = strdup(p)))
	    {
		debug("make_split_string(\"%s\",%p,\"%s\"): Out of memory.\n",buf,p,delim);
		return -1;
	    }
	}

	p = q;
    }
    
    return 0;
}

/* same function as above, except that the string is cut off right before the first \r and \n
 */
int make_split_string_strip_crlf(char *buf,Split_string *p,char *delim)
{
    char *q = NULL, *x = NULL;
    int ret = 0;
    
    q = strdup(buf);

    if(!q)
    {
	debug("make_split_string_strip_crlf(): Out of memory !\n");
	return -1;
    }
    
    if((x = strchr(q,'\r')))	*x = '\0';
    if((x = strchr(q,'\n')))  *x = '\0';
    
    ret = make_split_string(q,p,delim);

    free(q);

    return ret;
}

/* delete the split string, and release the allocated space */
int delete_split_string(Split_string *p)
{
    int i;
    
    if(!p->numparts)
	return 0;

    for(i = 0;i < p->numparts; i++)
    {
	free(p->parts[i]);
    }

    free(p->parts);
    p->parts = NULL;
    p->numparts = 0;

    return 0;
}

/* return the number of words */
int split_string_count_parts(Split_string *p)
{
    return p->numparts;
}

/* return the i'th word, or NULL if i is out of range */
char *split_string_get(Split_string *p,int i)
{
    if(i >= p->numparts || i < 0)
	return NULL;
    else return p->parts[i];
}



syntax highlighted by Code2HTML, v. 0.9.1