aboutsummaryrefslogblamecommitdiffstats
path: root/e-util/e-path.c
blob: 0728c94013b0c8cba145763e1365d928d4d5a647 (plain) (tree)
























                                                                           
 



























                                                                      
                             































































                                                                                 
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-path.c
 *
 * Copyright (C) 2001 Ximian, Inc.
 *
 * 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 <config.h>

#include <string.h>
#include <glib.h>

#include "e-path.h"

#define SUBFOLDER_DIR_NAME     "subfolders"
#define SUBFOLDER_DIR_NAME_LEN 10

/**
 * e_path_to_physical:
 * @prefix: a prefix to prepend to the path, or %NULL
 * @path: the virtual path to convert to a filesystem path.
 *
 * This converts the "virtual" path @path into an expanded form that
 * allows a given name to refer to both a file and a directory. The
 * expanded path will have a "subfolders" directory inserted between
 * each path component.
 *
 * If @prefix is non-%NULL, it will be prepended to the returned path.
 *
 * Return value: the expanded path
 **/
char *
e_path_to_physical (const char *prefix, const char *vpath)
{
    const char *p, *newp;
    char *dp;
    char *ppath;
    int ppath_len;
    int prefix_len;

    while (*vpath == '/')
        vpath++;
    if (!prefix)
        prefix = "";

    /* Calculate the length of the real path. */
    ppath_len = strlen (vpath);
    ppath_len++;    /* For the ending zero.  */

    prefix_len = strlen (prefix);
    ppath_len += prefix_len;
    ppath_len++;    /* For the separating slash.  */

    /* Take account of the fact that we need to translate every
     * separator into `subfolders/'.
     */
    p = vpath;
    while (1) {
        newp = strchr (p, '/');
        if (newp == NULL)
            break;

        ppath_len += SUBFOLDER_DIR_NAME_LEN;
        ppath_len++; /* For the separating slash.  */

        /* Skip consecutive slashes.  */
        while (*newp == '/')
            newp++;

        p = newp;
    };

    ppath = g_malloc (ppath_len);
    dp = ppath;

    memcpy (dp, prefix, prefix_len);
    dp += prefix_len;
    *(dp++) = '/';

    /* Copy the mangled path.  */
    p = vpath;
    while (1) {
        newp = strchr (p, '/');
        if (newp == NULL) {
            strcpy (dp, p);
            break;
        }

        memcpy (dp, p, newp - p + 1); /* `+ 1' to copy the slash too.  */
        dp += newp - p + 1;

        memcpy (dp, SUBFOLDER_DIR_NAME, SUBFOLDER_DIR_NAME_LEN);
        dp += SUBFOLDER_DIR_NAME_LEN;

        *(dp++) = '/';

        /* Skip consecutive slashes.  */
        while (*newp == '/')
            newp++;

        p = newp;
    }

    return ppath;
}