Initial import of the CDE 2.1.30 sources from the Open Group.

This commit is contained in:
Peter Howkins
2012-03-10 18:21:40 +00:00
commit 83b6996daa
18978 changed files with 3945623 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
/* $XConsortium: DirIterator.C /main/2 1995/07/17 14:09:48 drk $ */
/*******************************************************************
** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
** All rights are reserved. Copying or other reproduction of this
** program except for archival purposes is prohibited without prior
** written consent of Hewlett-Packard Company.
********************************************************************
****************************<+>*************************************/
#include "DirIterator.h"
#include <errno.h>
DirectoryIterator::DirectoryIterator
(
const CString & dir
) : state(good_)
{
theDir = opendir(dir.data());
if (theDir == 0)
state = bad_;
}
DirectoryIterator::~DirectoryIterator()
{
closedir(theDir);
}
struct dirent * DirectoryIterator::operator()()
{
struct dirent * direntry = readdir(theDir);
if (direntry == 0)
state = (errno ? bad_ : done_);
return direntry;
}

View File

@@ -0,0 +1,31 @@
/* $XConsortium: DirIterator.h /main/3 1995/11/03 12:33:19 rswiston $ */
/*******************************************************************
** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
** All rights are reserved. Copying or other reproduction of this
** program except for archival purposes is prohibited without prior
** written consent of Hewlett-Packard Company.
********************************************************************
****************************<+>*************************************/
#include "cstring.h"
#include <dirent.h>
class DirectoryIterator {
public:
DirectoryIterator(const CString &);
~DirectoryIterator();
enum directoryState { good_ = 0, bad_ = 1, done_ = 2 };
struct dirent * operator()();
int bad() { return state & bad_; }
int good() { return state & good_; }
int done() { return state & done_; }
private:
DirectoryIterator() {}
DIR * theDir;
int state;
};

View File

@@ -0,0 +1,147 @@
/* $XConsortium: Environ.C /main/4 1996/04/30 18:12:49 barstow $ */
/*******************************************************************
** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
** All rights are reserved. Copying or other reproduction of this
** program except for archival purposes is prohibited without prior
** written consent of Hewlett-Packard Company.
********************************************************************
****************************<+>*************************************/
#include "Environ.h"
OSEnvironment::OSEnvironment()
{
}
CDEEnvironment::CDEEnvironment
(
CString * homedir,
OSEnvironment * os_
) : dtspSysApp(0),
dtspUserApp(0),
dtspSysIcon(0),
dtspUserIcon(0),
dtspSysHelp(0),
dtspUserHelp(0),
dtspSysInfoLib(0),
dtspUserInfoLib(0),
dtspSysDB(0),
dtspUserDB(0),
dtManPath(0),
userHostDir(""),
os(os_)
{
CString envVar;
sysAdmConfig = "/etc/dt/appconfig";
factoryInstall = "/usr/dt/appconfig";
factoryManPath = "/usr/dt/man";
envVar = os->getEnvironmentVariable("DTSPSYSAPPHOSTS");
if (!envVar.isNull())
dtspSysApp = new CString(envVar);
envVar = os->getEnvironmentVariable("DTSPUSERAPPHOSTS");
if (!envVar.isNull())
dtspUserApp = new CString(envVar);
envVar = os->getEnvironmentVariable("DTSPSYSICON");
if (!envVar.isNull())
dtspSysIcon = new CString(envVar);
envVar = os->getEnvironmentVariable("DTSPUSERICON");
if (!envVar.isNull())
dtspUserIcon = new CString(envVar);
envVar = os->getEnvironmentVariable("DTSPSYSHELP");
if (!envVar.isNull())
dtspSysHelp = new CString(envVar);
envVar = os->getEnvironmentVariable("DTSPUSERHELP");
if (!envVar.isNull())
dtspUserHelp = new CString(envVar);
envVar = os->getEnvironmentVariable("DTSPSYSINFOLIB");
if (!envVar.isNull())
dtspSysInfoLib = new CString(envVar);
envVar = os->getEnvironmentVariable("DTSPUSERINFOLIB");
if (!envVar.isNull())
dtspUserInfoLib = new CString(envVar);
envVar = os->getEnvironmentVariable("DTSPSYSDATABASEHOSTS");
if (!envVar.isNull())
dtspSysDB = new CString(envVar);
envVar = os->getEnvironmentVariable("DTSPUSERDATABASEHOSTS");
if (!envVar.isNull())
dtspUserDB = new CString(envVar);
envVar = os->getEnvironmentVariable("DTMANPATH");
if (!envVar.isNull())
dtManPath = new CString(envVar);
envVar = os->getEnvironmentVariable("DTUSERSESSION");
if (!envVar.isNull())
userHostDir = envVar;
if (homedir == 0) {
envVar = os->getEnvironmentVariable("HOME");
theHome = envVar;
}
else
theHome = *homedir;
theHome += "/.dt";
defaultSearchPath = theHome + "," + sysAdmConfig + "," + factoryInstall;
}
CDEEnvironment::~CDEEnvironment()
{
delete dtspSysApp;
delete dtspUserApp;
delete dtspSysIcon;
delete dtspUserIcon;
delete dtspSysHelp;
delete dtspUserHelp;
delete dtspSysInfoLib;
delete dtspUserInfoLib;
delete dtspSysDB;
delete dtspUserDB;
delete dtManPath;
delete os;
}
void CDEEnvironment::setDTAPPSP
(
const CString & sp
)
{
delete dtspSysApp;
dtspSysApp = new CString(sp);
}
void CDEEnvironment::CreateHomeAppconfigDir()
{
if (!os->FileExists (HOME()))
os->MakeDirectory (HOME(), 0755);
if (!os->FileExists (HOME() + "/types"))
os->MakeDirectory (HOME() + "/types", 0775);
if (!os->FileExists (HOME() + "/icons"))
os->MakeDirectory (HOME() + "/icons", 0775);
if (!os->FileExists (HOME() + "/appmanager"))
os->MakeDirectory (HOME() + "/appmanager", 0775);
if (!os->FileExists (HOME() + "/help"))
os->MakeDirectory (HOME() + "/help", 0775);
}

View File

@@ -0,0 +1,214 @@
/* $TOG: Environ.h /main/8 1998/04/06 13:32:59 mgreess $ */
/*******************************************************************
** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
** All rights are reserved. Copying or other reproduction of this
** program except for archival purposes is prohibited without prior
** written consent of Hewlett-Packard Company.
********************************************************************
****************************<+>*************************************/
#ifndef _ENVIRON_H_
#define _ENVIRON_H_
#include "cstring.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
class Shell {
public:
// class constructor, destructor
Shell() {}
~Shell() {}
// pure virtual function
virtual void putToEnv( const CString &, const char * ) = 0;
};
class KShell : public Shell {
public:
KShell() {}
~KShell() {}
void putToEnv ( const CString & envvar,
const char * value )
{
CString toPut(envvar);
toPut += "=";
toPut += value;
#ifdef IOSTREAMSWORKS
cout << toPut << ";" << endl;
cout << "export " << envvar << ";" << endl;
#else
printf("%s;\n", toPut.data());
printf("export %s;\n", envvar.data());
#endif
}
};
class CShell : public Shell {
public:
CShell() {}
~CShell() {}
void putToEnv ( const CString & envvar,
const char * value )
{
CString toPut("setenv ");
toPut += envvar;
toPut += " ";
toPut += value;
#ifdef IOSTREAMSWORKS
cout << toPut << ";" << endl;
#else
printf("%s;\n", toPut.data());
#endif
}
};
class OSEnvironment {
public:
OSEnvironment();
virtual ~OSEnvironment() {}
virtual int FileExists (const CString &) const = 0;
virtual void MakeDirectory (const CString &, mode_t) = 0;
virtual CString getEnvironmentVariable (const char *) = 0;
virtual int isDirectory (const CString &) = 0;
virtual int isFile (const CString &) = 0;
virtual int isLink (const CString &) = 0;
virtual void changePermissions (const CString &, mode_t) = 0;
virtual void changeOwnerGroup (const CString &, const char *,
const char * = 0) = 0;
virtual void removeDirectory (const CString &) = 0;
virtual void removeFiles (const CString &, const CString &) = 0;
virtual void removeFile (const CString &) = 0;
virtual void removeDeadLinks (const CString &) = 0;
virtual void symbolicLink (const CString &, const CString &) = 0;
virtual void setUserId (const char * = 0) = 0;
CString MountPoint() const { return dtMountPoint; }
CString LocalHost() const { return localHost; }
CString LANG() const { return lang; }
CString MANPATH() const { return manpath; }
int NonDefaultLang() { return lang != "C"; }
Shell *shell () { return shell_ ; }
protected:
CString dtMountPoint;
CString localHost;
CString lang;
CString manpath;
Shell *shell_;
};
class UnixEnvironment : public OSEnvironment {
public:
UnixEnvironment();
virtual ~UnixEnvironment();
virtual int FileExists (const CString &) const;
virtual void MakeDirectory (const CString &, mode_t);
virtual CString getEnvironmentVariable (const char *);
virtual int isDirectory (const CString &);
virtual int isFile (const CString &);
virtual int isLink (const CString &);
virtual void changePermissions (const CString &, mode_t);
virtual void changeOwnerGroup (const CString &, const char *,
const char * = 0);
virtual void removeDirectory (const CString &);
virtual void removeFiles (const CString &, const CString &);
virtual void removeFile (const CString &);
virtual void removeDeadLinks (const CString &);
virtual void symbolicLink (const CString &, const CString &);
virtual void setUserId (const char * = 0);
int cshFormat;
static CString kshString() {
return CString("ksh");
}
static CString cshString() {
return CString("csh");
}
static int isCsh(const CString &shStr) {
CString cshStr(cshString());
return shStr.contains(cshStr);
}
static int isKsh(const CString &shStr) {
CString kshStr(kshString());
return shStr.contains(kshStr);
}
private:
uid_t uid_;
gid_t gid_;
};
class CDEEnvironment {
public:
CDEEnvironment () {}
CDEEnvironment (CString *, OSEnvironment *);
~CDEEnvironment();
void CreateHomeAppconfigDir();
CString * DTAPPSP() const { return dtspSysApp; }
CString * DTUSERAPPSP() const { return dtspUserApp; }
CString * DTICONSP() const { return dtspSysIcon; }
CString * DTUSERICONSP() const { return dtspUserIcon; }
CString * DTHELPSP() const { return dtspSysHelp; }
CString * DTUSERHELPSP() const { return dtspUserHelp; }
CString * DTINFOLIBSP() const { return dtspSysInfoLib; }
CString * DTUSERINFOLIBSP() const { return dtspUserInfoLib; }
CString * DTDBSP() const { return dtspSysDB; }
CString * DTUSERDBSP() const { return dtspUserDB; }
CString * DTMANPATH() const { return dtManPath; }
void setDTAPPSP (const CString & sp);
CString HOME() const { return theHome; }
CString SysAdmConfig() const { return sysAdmConfig; }
CString FactoryInstall() const { return factoryInstall; }
CString FactoryManPath() const { return factoryManPath; }
CString DefaultSearchPath() const { return defaultSearchPath; }
CString UserHostDir() const { return userHostDir; }
OSEnvironment * OS() const { return os; }
// These vars are public because their defaults will be
// changed for DTINFOLIBSEARCHPATH.
CString sysAdmConfig;
CString factoryInstall;
private:
CString theHome;
CString factoryManPath;
CString defaultSearchPath;
CString userHostDir;
CString * dtspUserApp;
CString * dtspSysApp;
CString * dtspSysIcon;
CString * dtspUserIcon;
CString * dtspSysHelp;
CString * dtspUserHelp;
CString * dtspSysInfoLib;
CString * dtspUserInfoLib;
CString * dtspSysDB;
CString * dtspUserDB;
CString * dtManPath;
OSEnvironment * os;
};
#endif

View File

@@ -0,0 +1,33 @@
XCOMM $TOG: Imakefile /main/5 1998/08/05 13:22:12 mgreess $
#define DoNormalLib YES
#define DoSharedLib NO
#define DoDebugLib NO
#define DoProfileLib NO
#define LibName CliSrv
#define LibHeaders NO
#define LibInstall NO
#define CplusplusSource YES
DEPEND_DEFINES = $(CXXDEPENDINCLUDES)
INCLUDES = -I.
#ifdef SunArchitecture
SYS_LIBRARIES = -ldl -lintl
LAST_LOAD_FLAGS = -Bstatic -lC -Bdynamic -lm -lc -ladm -Bstatic
EXTRA_DEFINES = -DIOSTREAMSWORKS
#endif
#ifdef RsArchitecture
EXTRA_DEFINES = -DIOSTREAMSWORKS
#endif
SRCS = DirIterator.C Environ.C TTFile.C \
UnixEnv.C cstring.C
OBJS = DirIterator.o Environ.o TTFile.o \
UnixEnv.o cstring.o
#include <Library.tmpl>
DependTarget()

View File

@@ -0,0 +1,87 @@
/* $XConsortium: TTFile.C /main/2 1995/07/17 14:10:04 drk $ */
/*******************************************************************
** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
** All rights are reserved. Copying or other reproduction of this
** program except for archival purposes is prohibited without prior
** written consent of Hewlett-Packard Company.
********************************************************************
****************************<+>*************************************/
#include "TTFile.h"
#include <Tt/tt_c.h>
#include <stdio.h>
TTFile::TTFile
(
const CString & host,
const CString & path
) : CString(), status(TT_OK)
{
char * temp = tt_host_file_netfile(host.data(), path.data());
if ((status = tt_ptr_error(temp)) != TT_OK)
Throw (TT_Exception(temp));
contents = tt_netfile_file(temp);
tt_free(temp);
if ((status = tt_ptr_error(contents)) != TT_OK)
Throw (TT_Exception(contents));
}
TTFile::TTFile
(
const TTFile & file
)
{
contents = new char [file.length() + 1];
strcpy(contents,file.data());
status = file.status;
}
TTFile::~TTFile()
{
if (contents)
tt_free(contents);
}
#ifndef HAS_EXCEPTIONS
void TTFile::TT_Exception
(
char * str
)
{
#ifdef IOSTREAMSWORKS
cerr << tt_status_message(tt_pointer_error(str)) << endl;
#else
fprintf(stderr, "%s\n", tt_status_message(tt_pointer_error(str)));
#endif
}
#endif
TTFile & TTFile::operator=
(
const TTFile & file
)
{
if (file != *this) {
delete [] contents;
contents = new char [file.length() + 1];
strcpy(contents,file.data());
status = file.status;
}
return *this;
}
ostream & operator<<
(
ostream & os,
TTFile & file
)
{
if (file.ttFileOpFailed())
return os << "Error in filename mapping; status = "
<< file.getStatus() << endl;
else
return os << file.data() << endl;
}

View File

@@ -0,0 +1,55 @@
/* $XConsortium: TTFile.h /main/3 1995/11/03 12:33:57 rswiston $ */
/*******************************************************************
** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
** All rights are reserved. Copying or other reproduction of this
** program except for archival purposes is prohibited without prior
** written consent of Hewlett-Packard Company.
********************************************************************
****************************<+>*************************************/
#ifndef _TTFILE_H_
#define _TTFILE_H_
#include "cstring.h"
#include "Tt/tt_c.h"
#ifdef HAS_EXCEPTIONS
#define Throw(ex) throw ex
#define Try try
#define Catch(type,file) catch(type ex)
#else
#define Throw(ex) { ex; return; }
#define Try
#define Catch(type,file) if (file->ttFileOpFailed())
#endif
class TTFile : public CString {
public:
TTFile () : status(TT_OK) {}
TTFile (const CString &, const CString &);
TTFile (const TTFile &);
~TTFile();
TTFile & operator=(const TTFile &);
#ifdef HAS_EXCEPTIONS
class TT_Exception {
public:
TT_Exception (char * str)
{ cerr << tt_status_message(tt_pointer_error(str)); }
~TT_Exception() {}
friend ostream & operator<< (ostream &, TTFile &);
};
#else
void TT_Exception (char *);
friend ostream & operator<< (ostream &, TTFile &);
#endif
int ttFileOpFailed () { return status != TT_OK; }
Tt_status getStatus() { return status; }
private:
Tt_status status;
};
#endif

View File

@@ -0,0 +1,327 @@
/* $TOG: UnixEnv.C /main/11 1998/12/14 17:06:02 mgreess $ */
/*******************************************************************
** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
** All rights are reserved. Copying or other reproduction of this
** program except for archival purposes is prohibited without prior
** written consent of Hewlett-Packard Company.
********************************************************************
****************************<+>*************************************/
#include "Environ.h"
#include "DirIterator.h"
#include <sys/stat.h>
#if defined(USL) || defined(__uxp__)
#define S_ISLNK(mode) ((mode & S_IFMT) == S_IFLNK)
#endif
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
#include <stdio.h>
#include <unistd.h>
#include <grp.h>
#include <pwd.h>
#if defined(sun) || defined(USL) || defined(__uxp__)
#include <regexpr.h>
#else
#include <regex.h>
#endif
#include <errno.h>
#if defined(sun) || defined(_AIX) || defined(__osf__) || defined(USL) || defined(__uxp__) || (linux)
#define UID_NO_CHANGE ((uid_t) -1)
#define GID_NO_CHANGE ((gid_t) -1)
#endif
UnixEnvironment::UnixEnvironment()
{
dtMountPoint = getEnvironmentVariable("DTMOUNTPOINT");
if (dtMountPoint.isNull())
#if defined(sun) || defined(USL) || defined(__uxp__)
dtMountPoint = "/net/";
#else
dtMountPoint = "/nfs/";
#endif
CString temp = getEnvironmentVariable("MANPATH");
if (temp.isNull())
#if defined(sun) || defined(USL) || defined(__uxp__)
manpath = "/usr/share/man";
#elif defined(_AIX)
manpath = "/usr/share/man:/usr/lpp/info";
#elif defined(hpux)
manpath = "/usr/man:/usr/contrib/man:/usr/local/man";
#elif defined(__osf__)
manpath = "/usr/share/%L/man:/usr/share/man:/usr/local/man";
#elif defined(linux)
manpath = "/usr/share/man/%L:/usr/share/man:/usr/contrib/man/%L:/usr/contrib/man:/usr/local/man/%L:/usr/local/man";
#endif
else
manpath = temp;
lang = getEnvironmentVariable("LANG");
if (lang.isNull())
lang = "C";
localHost = "localhost";
CString sh(getEnvironmentVariable("SHELL"));
cshFormat = 0;
if (isCsh(sh)) {
shell_ = new CShell();
cshFormat = 1;
}
else
shell_ = new KShell();
uid_ = getuid();
gid_ = getgid();
}
UnixEnvironment::~UnixEnvironment()
{
delete shell_;
}
int UnixEnvironment::FileExists
(
const CString & filename
) const
{
struct stat file;
if (stat((const char *)filename.data(),&file) == 0)
return 1;
return 0;
}
void UnixEnvironment::MakeDirectory
(
const CString & dirname,
mode_t permissions
)
{
if (mkdir (dirname.data(), permissions) == -1) {
CString errorStr("MakeDirectory: " + dirname);
perror(errorStr.data());
}
}
CString UnixEnvironment::getEnvironmentVariable
(
const char * envvar
)
{
char * value = getenv(envvar);
if (value == 0)
return CString("");
return CString(value);
}
int UnixEnvironment::isDirectory
(
const CString & directory
)
{
struct stat file;
if (stat((const char *)directory.data(),&file) == 0)
if (S_ISDIR(file.st_mode))
return 1;
return 0;
}
int UnixEnvironment::isFile
(
const CString & filespec
)
{
struct stat file;
if (stat((const char *)filespec.data(),&file) == 0)
if (S_ISREG(file.st_mode))
return 1;
return 0;
}
int UnixEnvironment::isLink
(
const CString & filespec
)
{
struct stat file;
if (lstat((const char *)filespec.data(),&file) == 0)
if (S_ISLNK(file.st_mode))
return 1;
return 0;
}
void UnixEnvironment::symbolicLink
(
const CString & linkto,
const CString & linkee
)
{
if (symlink (linkto.data(), linkee.data()) == -1) {
CString errorStr("symbolicLink: " + linkee + " -> " + linkto + "| ");
perror(errorStr.data());
}
}
void UnixEnvironment::setUserId
(
const char * name
)
{
uid_t uid = UID_NO_CHANGE;
if (name && name[0]) {
struct passwd * pwent = getpwnam(name);
uid = pwent->pw_uid;
}
else if (name == 0)
uid = uid_;
if (setuid(uid) == -1) {
CString errorStr("setUserId: ");
errorStr += name;
errorStr += "| ";
perror(errorStr.data());
}
}
void UnixEnvironment::changePermissions
(
const CString & filespec,
mode_t mode
)
{
if (chmod (filespec.data(), mode) == -1) {
CString errorStr("changePermissions: " + filespec + "| ");
perror(errorStr.data());
}
}
void UnixEnvironment::changeOwnerGroup
(
const CString & filespec,
const char * owner,
const char * group
)
{
uid_t uid = UID_NO_CHANGE;
gid_t gid = GID_NO_CHANGE;
if (owner) {
if (owner[0]) {
struct passwd * pwent = getpwnam(owner);
uid = pwent->pw_uid;
}
else
uid = getuid();
}
if (group) {
if (group[0]) {
struct group * grent = getgrnam(group);
gid = grent->gr_gid;
}
else
gid = getgid();
}
if (chown(filespec.data(),uid,gid) == -1) {
CString errorStr("changeOwnerGroup: " + filespec + "| ");
perror(errorStr.data());
}
}
void UnixEnvironment::removeDirectory
(
const CString & dirspec
)
{
#ifdef sun
removeFiles(dirspec, ".~*");
removeFiles(dirspec, "*");
#else
removeFiles(dirspec, "[.]~*");
removeFiles(dirspec, "[.]*");
#endif
if (rmdir (dirspec.data()) == -1) {
CString errorStr("removeDirectory: " + dirspec + "| ");
perror(errorStr.data());
}
}
void UnixEnvironment::removeFiles
(
const CString & dirspec,
const CString & filespec
)
{
#if defined(sun) || defined(__uxp__)
char buffer[100];
sprintf(buffer,"rm -f %s/%s", dirspec.data(),filespec.data());
system(buffer);
#else
DirectoryIterator dir(dirspec);
struct dirent * direntry;
while (direntry = dir()) {
/*# ifdef should_be_sun_but_this_dont_work*/
#if defined(USL)
char * re = NULL;
re = compile (filespec.data(), NULL, NULL);
if (step (direntry->d_name,re)) {
# else
regex_t re;
regcomp (&re, filespec.data(), 0);
if (regexec (&re, direntry->d_name, 0, NULL, 0) == 0) {
# endif
if (strcmp(direntry->d_name,".") == 0 ||
strcmp(direntry->d_name,"..") == 0)
continue;
removeFile(dirspec + "/" + direntry->d_name);
}
}
#endif
}
void UnixEnvironment::removeFile
(
const CString & filespec
)
{
if (isDirectory(filespec) && !isLink(filespec))
removeDirectory(filespec);
else {
if (unlink (filespec.data()) == -1) {
CString errorStr("removeFile(unlink): " + filespec + "| ");
perror(errorStr.data());
}
}
}
void UnixEnvironment::removeDeadLinks
(
const CString & dirspec
)
{
DIR * dir = opendir(dirspec.data());
struct dirent * direntry;
while (direntry = readdir(dir)) {
if (isLink(dirspec + "/" + direntry->d_name))
if (!isFile(dirspec + "/" + direntry->d_name) &&
!isDirectory(dirspec + "/" + direntry->d_name))
removeFile(dirspec + "/" + direntry->d_name);
}
closedir(dir);
}

View File

@@ -0,0 +1,555 @@
/* $XConsortium: cstring.C /main/2 1995/07/17 14:10:22 drk $ */
/*******************************************************************
** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
** All rights are reserved. Copying or other reproduction of this
** program except for archival purposes is prohibited without prior
** written consent of Hewlett-Packard Company.
********************************************************************
****************************<+>*************************************/
#include "cstring.h"
#include <ctype.h>
CString::CString()
{
contents = 0;
skipWhiteSpace = 1;
}
CString::CString
(
const char * s,
unsigned char ws
) : skipWhiteSpace(ws)
{
if (s != 0) {
contents = new char [strlen(s) + 1];
strcpy(contents,s);
}
else {
contents = new char [1];
strcpy(contents,"");
}
}
CString::CString
(
const char c,
unsigned char ws
) : skipWhiteSpace(ws)
{
contents = new char [2];
contents[0] = c;
contents[1] = 0;
}
CString::~CString()
{
delete [] contents;
}
CString::CString
(
const CString & s
)
{
contents = new char [s.length() + 1];
strcpy(contents,s.data());
skipWhiteSpace = s.skipWhiteSpace;
}
char & CString::operator[]
(
int index
) const
{
if (index < 0)
return contents[0];
if (index > strlen(contents))
return contents[strlen(contents)-1];
return contents[index];
}
int CString::operator!=
(
const CString & s
) const
{
return !(*this == s);
}
int CString::operator==
(
const CString & s
) const
{
if (isNull() && s.isNull())
return 1;
if (isNull() || s.isNull())
return 0;
if (strcmp(contents,s.data()) == 0)
return 1;
return 0;
}
CString & CString::operator=
(
const CString & s
)
{
if (s != *this) {
delete [] contents;
contents = new char [s.length() + 1];
strcpy(contents,s.data());
}
return *this;
}
CString & CString::operator=
(
const char * s
)
{
delete [] contents;
contents = new char [strlen(s) + 1];
strcpy(contents,s);
return *this;
}
CString & CString::operator+=
(
const char * cs
)
{
char * temp;
if (contents) {
if (cs)
temp = new char [strlen(contents) + strlen(cs) + 1];
else
return *this;
}
else {
if (cs)
temp = new char [strlen(cs) + 1];
else
return *this;
}
*temp = 0;
if (contents)
strcat(temp,contents);
if (cs)
strcat(temp,cs);
delete [] contents;
contents = temp;
return *this;
}
CString & CString::operator+=
(
const CString & s
)
{
char * temp;
if (contents) {
if (s.contents)
temp = new char [strlen(contents) + s.length() + 1];
else
return *this;
}
else {
if (s.contents)
temp = new char [s.length() + 1];
else
return *this;
}
*temp = 0;
if (contents)
strcat(temp,contents);
if (s.contents)
strcat(temp,s.data());
delete [] contents;
contents = temp;
return *this;
}
CString operator+
(
const CString & s1,
const CString & s2
)
{
CString result(s1);
result += s2;
return result;
}
CString operator+
(
const CString & s,
const char * cs
)
{
CString result(s);
result += cs;
return result;
}
CString operator+
(
const char * cs,
const CString & s
)
{
CString result(cs);
result += s;
return result;
}
CString CString::copy
(
unsigned int start,
const char * delim
)
{
if (isNull())
return *this;
for (int i = 0; i < strlen(delim); i++) {
char * q;
if (q = strchr(contents,delim[i])) {
char remember = *q;
*q = 0;
CString result(&contents[start]);
*q = remember;
return result;
}
}
return *this;
}
CString CString::copy
(
const char * delim1,
const char * delim2
)
{
if (isNull())
return *this;
for (int i = 0; i < strlen(delim1); i++) {
char * q;
if (q = strchr(contents,delim1[i])) {
for (int j = 0; j < strlen(delim2); j++) {
char * p;
if (p = strchr(q+1,delim2[j])) {
char remember = *p;
*p = 0;
CString result(q+1);
*p = remember;
return result;
}
}
return CString(q);
}
}
return *this;
}
CString CString::find
(
const char * cs
)
{
char * q;
if (isNull())
return *this;
if (q = strstr(contents,cs))
return CString(q);
return *this;
}
int CString::contains
(
const CString & s,
const char * leader,
const char * trailer
) const
{
return contains(s.data(), leader, trailer);
}
int CString::contains
(
const char * cs,
const char * leader,
const char * trailer
) const
{
// first see if there is anything in contents
if (isNull())
return 0;
// or if the strings are exactly the same
if (strcmp(cs,data()) == 0)
return 1;
// now check to see if it is embedded in the string
CString search(leader);
search += cs;
search += trailer;
if (strstr (contents, search.data()))
return 1;
// if skipWhiteSpace is on then check for spaces on one or
// both sides of the target in the source.
if (skipWhiteSpace) {
// first, a space in front, delimiter on the end
search = " ";
search += cs;
search += trailer;
if (strstr (contents, search.data()))
return 1;
// second, a delimiter on the front, space on the end
search = leader;
search += cs;
search += " ";
if (strstr (contents, search.data()))
return 1;
// third, spaces before and after
search = " ";
search += cs;
search += " ";
if (strstr (contents, search.data()))
return 1;
}
// now check to see if it is the start of the string
search = cs;
search += trailer;
if (strncmp (contents, search.data(), search.length()) == 0)
return 1;
// now check to see if it is the start of the string followed
// by a space
search = cs;
search += " ";
if (strncmp (contents, search.data(), search.length()) == 0)
return 1;
// last, check to see if it is on the end of the string using
// the delimiter and a space
search = leader;
while (1) {
search += cs;
char *q, *r = 0;
char *p = contents;
while (q = strstr(p, search.data())) {
r = q + strlen(leader);
p = q + 1;
}
if (r && strcmp(r,cs) == 0)
return 1;
if (search[0] == ' ')
break;
search = " ";
}
return 0;
}
void CString::replace
(
const CString & to_be_replaced,
const CString & replacee
)
{
char * q;
if (isNull())
return;
if (q = strstr(contents,to_be_replaced.data())) {
*q = 0;
char * prefix = new char [strlen(contents) + 1];
strcpy(prefix,contents);
q += to_be_replaced.length();
char * suffix = new char [strlen(q) + 1];
strcpy(suffix,q);
delete [] contents;
contents = new char [strlen(prefix) +
replacee.length() +
strlen(suffix) + 1];
strcpy(contents,prefix);
strcat(contents,replacee.data());
strcat(contents,suffix);
delete [] prefix;
delete [] suffix;
}
}
int CString::isNull() const
{
if (contents == 0)
return 1;
if (strlen(contents) == 0)
return 1;
return 0;
}
CTokenizedString::CTokenizedString()
: CString(""), finished(0), cursor(0), delimiter(0)
{}
CTokenizedString::CTokenizedString
(
const CTokenizedString & s
)
{
if (s != *this) {
cursor = new char [strlen(s.cursor) + 1];
strcpy(cursor,s.cursor);
delimiter = new char [strlen(s.delimiter) + 1];
strcpy(delimiter,s.delimiter);
finished = s.finished;
skipWhiteSpace = s.skipWhiteSpace;
contents = new char [strlen(s.contents) + 1];
strcpy(contents,s.contents);
}
}
CTokenizedString::CTokenizedString
(
const CString & s,
char * d,
unsigned char ws
) : CString(s), finished(0)
{
cursor = new char [s.length() + 1];
strcpy(cursor,s.data());
delimiter = new char [strlen(d) + 1];
strcpy(delimiter,d);
skipWhiteSpace = ws;
}
CTokenizedString::~CTokenizedString()
{
delete [] cursor;
delete [] delimiter;
}
CString CTokenizedString::next()
{
char * q = 0;
if (cursor) {
if (strlen(delimiter) == 1)
q = strchr(cursor,delimiter[0]);
else {
for (int i = 0; i < strlen(cursor); i++)
if (strchr(delimiter,cursor[i])) {
q = &cursor[i];
break;
}
}
}
if (q) {
// a delimiter has been found. Create the return token,
// adjust the cursor
*q = 0;
// eliminate trailing white space
if (skipWhiteSpace) {
for (char *p = q; isspace(*(p-1)); p--);
*p = 0;
}
CString result(cursor);
// eliminate leading white space
if (skipWhiteSpace)
for (; isspace(*(q+1)); q++);
char * temp = new char [strlen(q+1) + 1];
strcpy(temp,q+1);
delete [] cursor;
cursor = temp;
return result;
}
else if (cursor) {
// no delimiter found. Return what is left of the string
CString result(cursor);
delete [] cursor;
cursor = 0;
return result;
}
else {
finished = 1;
return CString("");
}
}
ostream & operator<<
(
ostream & os,
const CString & s
)
{
if (s.isNull())
return os << "(null)";
return os << s.data();
}

View File

@@ -0,0 +1,75 @@
/* $XConsortium: cstring.h /main/3 1995/11/03 12:34:22 rswiston $ */
/*******************************************************************
** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
** All rights are reserved. Copying or other reproduction of this
** program except for archival purposes is prohibited without prior
** written consent of Hewlett-Packard Company.
********************************************************************
****************************<+>*************************************/
#ifndef _CSTRING_H_
#define _CSTRING_H_
#include <iostream.h>
#include <string.h>
class CString {
public:
CString();
CString(const char * s, unsigned char = 1);
CString(const char, unsigned char = 1);
CString(const CString &);
~CString();
int length() const { return strlen(contents); }
char * data() const { return contents; }
CString & operator= (const CString &);
CString & operator= (const char *);
CString & operator+= (const CString &);
CString & operator+= (const char *);
int operator!= (const CString &) const;
int operator== (const CString &) const;
char & operator[](int) const;
friend CString operator+ (const CString & s1, const CString & s2);
friend CString operator+ (const CString & s, const char * cs);
friend CString operator+ (const char * cs, const CString & s);
CString copy (unsigned int, const char *);
CString copy (const char *, const char *);
CString find (const char *);
int contains (const char *, const char * = "", const char * = "") const;
int contains (const CString &, const char * = "", const char * = "") const;
int isNull() const;
void replace (const CString &, const CString &);
friend ostream & operator<< (ostream &, const CString &);
protected:
char * contents;
unsigned char skipWhiteSpace;
};
class CTokenizedString : public CString {
public:
CTokenizedString();
CTokenizedString(const CTokenizedString &);
CTokenizedString(const CString &, char *, unsigned char = 1);
~CTokenizedString();
int Finished() { return finished == 1; }
CString next();
private:
char * cursor;
char * delimiter;
int finished;
};
#endif