Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Friday, March 29, 2024

File: [Platon] / libco / libco / co_string.c (download)

Revision 1.2, Tue Feb 18 10:29:28 2003 UTC (21 years, 1 month ago) by yenar


Changes since 1.1: +14 -7 lines

fix formatting

/***{{{*******************************************************************
 * This file is part of libco - object library for C                     *
 * Copyright (c) 2002                                                    *
 *     Peter Rockai (yenar) <yenar@host.sk>                              *
 *                                                                       *
 * This library is free software; you can redistribute it and/or         *
 * modify it under the terms of the GNU Lesser General Public            *
 * License as published by the Free Software Foundation; either          *
 * version 2 of the License, or (at your option) any later version.      *
 *                                                                       *
 * This library 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     *
 * Lesser General Public License for more details.                       *
 *                                                                       *
 * You should have received a copy of the GNU Lesser General Public      *
 * License along with this library; if not, write to the Free Software   *
 * Foundation, Inc., 59 Temple Place, Suite 330,                         *
 * Boston, MA 02111-1307  USA                                            *
 *******************************************************************}}}***/
/* {{{ file description */
/**
 * @file co_string.c
 * @brief String manipulation functions.
 **/
/* }}} */

/* {{{ includes */
#include <stdarg.h>
#include <stdio.h>
#include "co_string.h"
#include "co_util.h"
/* }}} */

/* {{{ */
char *vsaprintf (const char *fmt, va_list ap)
    /// Allocate buffer of needed size and dump formatted string into it.
{
    int n = 0, size = 64; char *buf;
    va_list wap;
    buf = new_ar_p (char, size);
    while (1) {
        __va_copy (wap, ap);
        n = vsnprintf (buf, size, fmt, wap);
        if (n <= size  && n > -1) {
            return buf;
        }
        if (n > -1) {
            size = n + 1;
        } else {
            size *= 2;
        }
        buf = renew_ar (buf, char, size);
    }
}
/* }}} */
/* {{{ */
char *saprintf (const char *fmt, ...)
    /// Allocate buffer of needed size and dump formatted string into it.
{
    char *ret; va_list ap;
    va_start (ap, fmt);
    ret = vsaprintf (fmt, ap);
    va_end (ap);
    return ret;
}
/* }}} */
/* {{{ */
char *strimplode (const char *delim, ...)
{
    va_list ap1, ap2;
    const char *cur;
    char *buf;
    int len = 0;
    int nstr = 0, i;
    int dl = strlen (delim);
    va_start (ap1, delim);
    __va_copy (ap2, ap1);
    while ((cur = va_arg (ap1, const char *))) {
        len += dl;
        len += strlen (cur);
        nstr ++;
    }
    len += 1 - dl;
    buf = new_ar (char, len);
    buf [0] = 0;
    for (i = 0; i < nstr; i ++) {
        cur = va_arg (ap2, const char *);
        strcat (buf, cur);
        if (i < nstr - 1)
            strcat (buf, delim);
    }
    return buf;
}
/* }}} */
/* {{{ */
char *str_env_subst (const char *in)
{
    int i, j, len, nstr = 0;
    char *work = strdup (in);
    char *tmp = work;
    //char *tmp1;
    char *subs [256];
    char *ret;
    for (i = 0; in [i]; i ++) {
        if ((in [i] == '$') && (i == 0 || in [i - 1] != '\\')
                && in [i + 1] == '{') {
            subs [nstr ++] = tmp;
            work [i] = 0;
            for (j = 0; in [j] && in [j] != '}'; j ++);
            work [j] = 0;
            subs [nstr ++] = getenv (work + i + 2);
            tmp = work + i + 2 + strlen (work + i + 2) + 1;
            CO_DEBUG (3, "left: %s", tmp);
        }
    }
    subs [nstr ++] = tmp;
    for (i = len = 0; i < nstr; i ++) {
        len += subs [i] ? strlen (subs [i]) : 0;
    }
    ret = new_ar (char, len + 1);
    ret [0] = 0;
    for (i = 0; i < nstr; i ++) {
        if (subs [i])
            strcat (ret, subs [i]);
    }
    return ret;
}
/* }}} */

Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top