/***********************************************
 *  util.c                                 
 *                                            
 *  Copyright 2001 Eric Kratzer.              
 *  Includes Xaudio software Copyright (C)
 *           1996-2000 Xaudio Corporation
 *           All Rights Reserved.              
 *
 *  This file is part of imp3sh.
 *                                            
 *  imp3sh is free software; you can distribute
 *  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.
 *
 *  imp3sh 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 recieved a copy of the GNU
 *  General Public License along with imp3sh; if
 *  not, write to the Free Software Foundation,
 *  Inc., 59 Temple Place, Suite 330, Boston MA
 *  02111-1307 USA
 ************************************************/

/************************************
 *          Include Files           *
 ************************************/

#include "imp3sh.h"

/************************************
 *        External References       *
 ************************************/

extern DCharBuf *args;

/************************************
 *        Utility functions         *
 ************************************/

int is_upper_c(char c)
{
    return (c > 64 && c < 90) ? TRUE : FALSE;
}

int any_upper_case(char *str)
{
    if (str) {
	while (*str != ZCHAR) {
	    if (is_upper_c(*str))
		return TRUE;
	    str++;
	}
    }

    return FALSE;
}

int is_all_digits(char *str)
{
    if (str) {
	while (*str != ZCHAR) {
	    if (!isdigit(*str))
		return FALSE;
	    str++;
	}
	return TRUE;
    }

    return FALSE;
}

int is_digit_range(char *s)
{
    char *dash, *str2, str[INFO_SIZE];

    safe_strcpy(str, s, INFO_SIZE);
    dash = strchr(str, '-');

    if (dash) {
	str2 = (dash + 1);
	*dash = ZCHAR;

	if ((*str == ZCHAR && *str2 == ZCHAR) ||
	    (*str != ZCHAR && !is_all_digits(str)) ||
	    (*str2 != ZCHAR && !is_all_digits(str2)))
	    return FALSE;
	else
	    return TRUE;
    }

    return FALSE;
}

void get_digit_range(char *str, int *low, int *high, int lrep, int hrep)
{
    char *dash, *str2;

    dash = strchr(str, '-');

    if (dash) {
	str2 = (dash + 1);
	*dash = ZCHAR;

	if (*str != ZCHAR)
	    *low = atoi(str);
	else
	    *low = lrep;

	if (*str2 != ZCHAR)
	    *high = atoi(str2);
	else
	    *high = hrep;
    } else
	*low = *high = -1;
}

void remove_end_space(char *str)
{
    char *t;

    t = NULL;

    if (str) {
	while (*str != ZCHAR) {
	    if (*str != TAB && *str != SPACE)
		t = str;
	    str++;
	}

	if (t) *(t + 1) = ZCHAR;
    }
}

int get_strlen(char *str)
{
    char *w;

    if (str) {
	w = str;
	while (*w++ != ZCHAR);
	return (w - str - 1);
    }

    return 0;
}

void remove_newline(char *str)
{
    int len;

    len = get_strlen(str);

    if (len && str[len - 1] == END_COMM)
	str[len - 1] = ZCHAR;
}

void get_fline(FILE * fd, char *str, int size)
{
    memset(str, ZCHAR, size);
    fgets(str, size, fd);
    remove_newline(str);
}

void make_printable(char *str)
{
    if (str) {
	while (*str != ZCHAR) {
	    if (*str < 0x20 || *str > 0x7e)
		*str = SPACE;
	    str++;
	}
    }
}

int is_all_space(char *str)
{
    if (str) {
	while (*str != ZCHAR) {
	    if (*str > 0x20)
		return FALSE;
	    str++;
	}
    }

    return TRUE;
}

int is_any_space(char *str)
{
    if (str) {
	while (*str != ZCHAR) {
	    if (*str == 0x20)
		return TRUE;
	    str++;
	}
    }

    return FALSE;
}

char *skip_space(char *str)
{
    if (str) {
	while (*str == SPACE || *str == TAB)
	    str++;

	if (*str != ZCHAR)
	    return str;
    }

    return NULL;
}

char *grep_str(char *pat, char *str)
{
    char *ret;
    int not, plen, (*compare_func)(const char *, const char *, size_t);

    ret = NULL;
    not = FALSE;
    plen = get_strlen(pat);
    compare_func = strncasecmp;

    if (!plen || !str || !pat)
	return NULL;

    if (*pat == '!') {
	not = TRUE;
	pat++;
	plen--;

	if (!plen)
	    return NULL;
    }

    if (any_upper_case(pat))
	compare_func = strncmp;

    while (*str != ZCHAR) {
	if (compare_func(pat, str, plen) == 0) {
	    ret = (str + plen);
	    break;
	}
	str++;
    }

    /* don't assume that grep_str() returns a
       writeable address if you are grep'ing
       with a '!'.                            */
    if (not) {
	if (ret)
	    ret = NULL;
	else
	    ret = "ok";
    }

    return ret;
}

int grep_against_args(int start, char *str)
{
    char *pat;

    pat = g_dbuf(args, start++);

    if (!pat)
	return FALSE;

    while (pat) {
	if (grep_str(pat, str) == NULL)
	    return FALSE;
	pat = g_dbuf(args, start++);
    }

    return TRUE;
}

char *tokenize(char *t, char **mem, char delim)
{
    char *ret, *end;

    ret = end = NULL;

    if (t)
	*mem = skip_space(t);
    if (!*mem)
	return NULL;

    ret = *mem;
    if (*ret == PARENTHESIS)
	end = strchr(++ret, PARENTHESIS);
    else
	end = strchr(ret, delim);

    if (end) {
	*end = ZCHAR;
	*mem = skip_space((end + 1));
    } else
	*mem = NULL;

    return ret;
}

int get_cloc(char c, char *str)
{
    char *tmp;

    tmp = strchr(str, c);

    return (tmp) ? tmp - str : -1;
}

/* undefined if called with '\t' or ' ' */
int get_cpos(char c, char *str)
{
    char *tmp;
    flag_t aft, bef;

    tmp = strchr(str, c);
    bef = aft = FALSE;

    if (tmp) {
	if (tmp > skip_space(str))
	    bef = TRUE;
	if (tmp < skip_space(tmp + 1))
	    aft = TRUE;

	if (aft && bef)
	    return AMP_MID;
	else if (aft)
	    return AMP_FRONT;
	else if (bef)
	    return AMP_END;
	else
	    return AMP_ALL;
    }

    return AMP_NONE;
}

char *safe_strcpy(char *dest, char *src, int len)
{
    dest[len - 1] = ZCHAR;
    return strncpy(dest, src, len - 1);
}

char *file_from_path(char *name)
{
    char *ret;

    if (name) {
	ret = strrchr(name, '/');

	if (ret)
	    return (ret + 1);
    }

    return name;
}

void build_string(va_list ap, const char *frmt, char *buff)
{
    char *t, *p, *v, ibuf[32];
    int i;

    t = (char *) frmt;
    p = buff;

    while (*t != ZCHAR) {
	if (*t == '%') {
	    if (*++t == '%')
		*p++ = *t;
	    else
		switch (*t) {
		case 's':
		    if (!(v = va_arg(ap, char *)))
			 v = "(NULL)";
		    while (*v != ZCHAR)
			*p++ = *v++;
		    break;
		case 'd':
		    v = ibuf;
		    i = va_arg(ap, int);
		    sprintf(ibuf, "%d", i);
		    while (*v != ZCHAR)
			*p++ = *v++;
		    break;
		}
	    t++;
	} else
	    *p++ = *t++;
    }

    *p = ZCHAR;
}

void build_time_string(time_t len, char *out)
{
    char tmp[64];
    int hrs, min, sec;

    hrs = len / 3600;
    len -= hrs * 3600;
    min = len / 60;
    len -= min * 60;
    sec = len;

    *out = ZCHAR;

    if (hrs) {
	sprintf(tmp, "%d hour(s) ", hrs);
	strcat(out, tmp);
    }

    if (min) {
	sprintf(tmp, "%d minute(s) ", min);
	strcat(out, tmp);
    }

    if (sec) {
	sprintf(tmp, "%d second(s)", sec);
	strcat(out, tmp);
    }
}

FILE *myfopen(char *name, const char *perm)
{
    struct stat sstat;
    FILE *fd;

    if (!stat(name, &sstat) && (sstat.st_mode & S_IFDIR)) {
	errno = EISDIR;
	return NULL;
    }

    fd = fopen(name, perm);

    return fd;
}

void mysetenv(char *name, char *val)
{
    if (name) {
	/* unsetenv(name); */
	if (val)
	    setenv(name, (const char *) val, /* FALSE */ TRUE);
	else
	    setenv(name, "\0", /* FALSE */ TRUE);
    }
}

int is_url(char *stream)
{
    int len;

    len = get_strlen(stream);

    if ((len >= 5 && strncasecmp(stream, "http:", 5) == 0) ||
	(len >= 4 && strncasecmp(stream, "ftp:", 4) == 0) ||
	(len >= 4 && strncasecmp(stream, "udp:", 4) == 0))
	return TRUE;
    else
	return FALSE;
}
