aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-path.c
blob: d1da9abd38b93fd88ee675eb91c5c27f944b0e4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*- 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;
}