Personal tools
You are here: Home Projects C++ Cfront releases Release 3.0.3 source libSC Path path.c
Document Actions

path.c

by Michael L Powell last modified 2007-01-26 03:24

Click here to get the file

Size 2.3 kB - File type text/plain

File contents

/*ident	"@(#)Path:path.c	3.1" */
/******************************************************************************
*
* C++ Standard Components, Release 3.0.
*
* Copyright (c) 1991, 1992 AT&T and Unix System Laboratories, Inc.
* Copyright (c) 1988, 1989, 1990 AT&T.  All Rights Reserved.
*
* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T and Unix System
* Laboratories, Inc.  The copyright notice above does not evidence
* any actual or intended publication of such source code.
*
******************************************************************************/

#include "Pathlib.h"

// The algorithms to find the login directory are so complicated 
// (see /home/tools/ksh/src/cmd/ksh/shlib/tilde.c),
// that I think I will be cutting my throat if I try to
// incorporate them in this library.  Better just to
// make the user do something reasonable herself.
//Path Path::tilde(const char * login)
//{
//	String s = "~";
//	if (login)
//		s += login;
//	return Path(s);
//}

Path::Path() : rep(".")
{
}

Path::Path(const char * f) : rep(f)
{
	canonicalize();
}

Path::Path(const String & s) : rep(s)
{
	canonicalize();
}

Path::Path(const Path & f) : rep(f.rep)
{
}

void Path::build_from(const char * dir, const char * base, const char * ext)
{
	rep = dir;
	char c;
	if (rep.lastX(c) && c != '/')
		rep += '/';
	if (base)
		rep += base;
	if (ext)
		rep += ext;
	canonicalize();
}

Path::Path(const char * dir, const char * base, const char * ext)
{
	build_from(dir, base, ext);
}


int Path::length() const
{
	return rep.length();
}

int Path::ncomponents() const
{
	const char *p = rep;
	if (p[1] == '\0')
	{
		if (*p == '.')
			return 0;
		else if (*p == '/')
			return 1;
	}
	int n = 1;
	while (*p != '\0')
	{
		if (*p++ == '/')
			n++;
	}
	return n;
}

int Path::is_absolute() const
{
	return (rep.char_at(0) == '/');
}
	
int Path::is_relative() const
{
	return !is_absolute();
}

Path::operator const char *() const
{
	return rep;
}

Path::operator String() const
{
	return rep;
}

Path & Path::operator=(const Path & t)
{
	rep = t.rep;
	return *this;
}

int operator<(const Path & p1, const Path & p2)
{
	return String(p1) < String(p2);
}

int operator==(const Path & p1, const Path & p2)
{
	return (String)p1 == (String)p2;
}

int operator!=(const Path & p1, const Path & p2)
{
	return (String)p1 != (String)p2;
}

« November 2014 »
Su Mo Tu We Th Fr Sa
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
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: