/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "Licence").
 * You may not use this file except in compliance with the Licence.
 *
 * You can obtain a copy of the licence at RISC OS path @.^.LICENCE
 * or  http://www.riscosdev.com/lanman98/LICENCE.CDDL
 * See the Licence for the specific language governing permissions
 * and limitations under the Licence.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the Licence file. If applicable, add the
 * following below this CDDL HEADER, with the fields enclosed by
 * brackets "[]" replaced with your own identifying information:
 * Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
 
/*
 *   Copyright 1996 Warm Silence Software Ltd.  All rights reserved.
 *   Use is subject to license terms.
 */

/*   PHBG 14/12/97: Initial version
 */

#include <string.h>
#include "LanMan98BaseLib/strext.h"
#include "LanMan98BaseLib/memory.h"
#include "LanMan98BaseLib/error.h"
#include "fsys_err.h"
#include "fsys.h"

#include "fspsup_e.h"

path_t FSPName2Path(char *name, fsys_image_t img, int im)
{
    path_t path;
    char *t, *tokbuf, **tok;
    int ntok;

    path = Malloc(sizeof(*path));
    tok = path->tok;

    if(!im)
    {
        name = strchr(name, '$');
        if(name == NULL)
            ErrorNum(ERR_BadName);
        name++;
        if(name[0] != 0)
        {
            if(name[0] != '.')
                ErrorNum(ERR_BadName);
            name++;
        }
    }
    tokbuf = strdup(name ? name : "");
    ntok = 0;
    tok[ntok++] = NULL; /* The name of the root, effectively. */
    for(t = strtok(tokbuf, "."); t; t = strtok(NULL, "."))
    {
        if(ntok >= MAX_DEPTH)
            ErrorNum(ERR_BadName);
        tok[ntok++] = FsysNameMap(t, img);
    }

    path->ntok = ntok;
    path->tokbuf = tokbuf;
    return path;
}

void FSPPathDestruct(path_t path)
{
    Free(path->tokbuf);
    Free(path);
}

int FSPSubPath(path_t path1, path_t path2)
{
    int i;

    if(path1->ntok >= path2->ntok)
        return 0;
    for(i = 1; i < path1->ntok; i++)
        if(ci_strcmp(path1->tok[i], path2->tok[i]) != 0)
            return 0;
    return 1;
}

int FSPSamePath(path_t path1, path_t path2)
{
    int i;

    if(path1->ntok != path2->ntok)
        return 0;
    for(i = 1; i < path1->ntok; i++)
        if(ci_strcmp(path1->tok[i], path2->tok[i]) != 0)
            return 0;
    return 1;
}

int FSPSameDirPath(path_t path1, path_t path2)
{
    int i;

    if(path1->ntok != path2->ntok)
        return 0;
    for(i = 1; i < path1->ntok-1; i++)
        if(ci_strcmp(path1->tok[i], path2->tok[i]) != 0)
            return 0;
    return 1;
}
