2001-12-03 05:47:14 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2001 Stephen Williams (steve@icarus.com)
|
|
|
|
|
*
|
|
|
|
|
* This source code is free software; you can redistribute it
|
|
|
|
|
* and/or modify it in source code form 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
|
|
|
|
|
*/
|
2002-08-12 03:34:58 +02:00
|
|
|
#ifdef HAVE_CVS_IDENT
|
2007-04-26 05:06:21 +02:00
|
|
|
#ident "$Id: HName.cc,v 1.6 2007/04/26 03:06:21 steve Exp $"
|
2001-12-03 05:47:14 +01:00
|
|
|
#endif
|
|
|
|
|
|
2001-12-18 05:52:45 +01:00
|
|
|
# include "config.h"
|
2001-12-03 05:47:14 +01:00
|
|
|
# include "HName.h"
|
|
|
|
|
# include <iostream>
|
|
|
|
|
# include <string.h>
|
2002-01-05 05:36:06 +01:00
|
|
|
# include <stdlib.h>
|
|
|
|
|
#ifdef HAVE_MALLOC_H
|
2001-12-03 05:47:14 +01:00
|
|
|
# include <malloc.h>
|
2002-01-05 05:36:06 +01:00
|
|
|
#endif
|
2001-12-03 05:47:14 +01:00
|
|
|
|
|
|
|
|
hname_t::hname_t()
|
|
|
|
|
{
|
|
|
|
|
count_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-26 05:06:21 +02:00
|
|
|
hname_t::hname_t(perm_string text)
|
2001-12-03 05:47:14 +01:00
|
|
|
{
|
2007-04-26 05:06:21 +02:00
|
|
|
new (item_) perm_string(text);
|
2001-12-03 05:47:14 +01:00
|
|
|
count_ = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hname_t::hname_t(const hname_t&that)
|
|
|
|
|
{
|
|
|
|
|
count_ = that.count_;
|
|
|
|
|
switch (count_) {
|
|
|
|
|
case 0:
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
2007-04-26 05:06:21 +02:00
|
|
|
new(item_) perm_string (that.item_ref1_());
|
2001-12-03 05:47:14 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
2007-04-26 05:06:21 +02:00
|
|
|
array_ = new perm_string[count_];
|
2001-12-03 05:47:14 +01:00
|
|
|
for (unsigned idx = 0 ; idx < count_ ; idx += 1)
|
2007-04-26 05:06:21 +02:00
|
|
|
array_[idx] = that.array_[idx];
|
2001-12-03 05:47:14 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hname_t::~hname_t()
|
|
|
|
|
{
|
|
|
|
|
switch (count_) {
|
|
|
|
|
case 0:
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
2007-04-26 05:06:21 +02:00
|
|
|
item_ref1_().~perm_string();
|
2001-12-03 05:47:14 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
delete[]array_;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-02 04:27:51 +01:00
|
|
|
unsigned hname_t::component_count() const
|
|
|
|
|
{
|
|
|
|
|
return count_;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-26 05:06:21 +02:00
|
|
|
void hname_t::append(perm_string text)
|
2001-12-03 05:47:14 +01:00
|
|
|
{
|
2007-04-26 05:06:21 +02:00
|
|
|
perm_string*tmp;
|
2001-12-03 05:47:14 +01:00
|
|
|
|
|
|
|
|
switch (count_) {
|
|
|
|
|
case 0:
|
|
|
|
|
count_ = 1;
|
2007-04-26 05:06:21 +02:00
|
|
|
new (item_) perm_string(text);
|
2001-12-03 05:47:14 +01:00
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
count_ = 2;
|
2007-04-26 05:06:21 +02:00
|
|
|
tmp = new perm_string[2];
|
|
|
|
|
tmp[0] = item_ref1_();
|
|
|
|
|
tmp[1] = text;
|
|
|
|
|
item_ref1_().~perm_string();
|
2001-12-03 05:47:14 +01:00
|
|
|
array_ = tmp;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2007-04-26 05:06:21 +02:00
|
|
|
tmp = new perm_string[count_+1];
|
2001-12-03 05:47:14 +01:00
|
|
|
for (unsigned idx = 0 ; idx < count_ ; idx += 1)
|
|
|
|
|
tmp[idx] = array_[idx];
|
|
|
|
|
delete[]array_;
|
|
|
|
|
array_ = tmp;
|
2007-04-26 05:06:21 +02:00
|
|
|
array_[count_] = text;
|
2001-12-03 05:47:14 +01:00
|
|
|
count_ += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-26 05:06:21 +02:00
|
|
|
void hname_t::prepend(perm_string text)
|
2001-12-03 05:47:14 +01:00
|
|
|
{
|
2007-04-26 05:06:21 +02:00
|
|
|
perm_string*tmp;
|
2001-12-03 05:47:14 +01:00
|
|
|
|
|
|
|
|
switch (count_) {
|
|
|
|
|
case 0:
|
|
|
|
|
count_ = 1;
|
2007-04-26 05:06:21 +02:00
|
|
|
new (item_) perm_string(text);
|
2001-12-03 05:47:14 +01:00
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
count_ = 2;
|
2007-04-26 05:06:21 +02:00
|
|
|
tmp = new perm_string[2];
|
|
|
|
|
tmp[0] = text;
|
|
|
|
|
tmp[1] = item_ref1_();
|
|
|
|
|
item_ref1_().~perm_string();
|
2001-12-03 05:47:14 +01:00
|
|
|
array_ = tmp;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2007-04-26 05:06:21 +02:00
|
|
|
tmp = new perm_string[count_+1];
|
|
|
|
|
tmp[0] = text;
|
2001-12-03 05:47:14 +01:00
|
|
|
for (unsigned idx = 0 ; idx < count_ ; idx += 1)
|
|
|
|
|
tmp[idx+1] = array_[idx];
|
|
|
|
|
delete[]array_;
|
|
|
|
|
array_ = tmp;
|
|
|
|
|
count_ += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-26 05:06:21 +02:00
|
|
|
perm_string hname_t::remove_tail_name()
|
2001-12-03 05:47:14 +01:00
|
|
|
{
|
|
|
|
|
if (count_ == 0)
|
2007-04-26 05:06:21 +02:00
|
|
|
return perm_string();
|
2001-12-03 05:47:14 +01:00
|
|
|
|
|
|
|
|
if (count_ == 1) {
|
2007-04-26 05:06:21 +02:00
|
|
|
perm_string tmp = item_ref1_();
|
2001-12-03 05:47:14 +01:00
|
|
|
count_ = 0;
|
2007-04-26 05:06:21 +02:00
|
|
|
item_ref1_().~perm_string();
|
2001-12-03 05:47:14 +01:00
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count_ == 2) {
|
2007-04-26 05:06:21 +02:00
|
|
|
perm_string tmp1 = array_[0];
|
|
|
|
|
perm_string tmp2 = array_[1];
|
2001-12-03 05:47:14 +01:00
|
|
|
delete[]array_;
|
|
|
|
|
count_ = 1;
|
2007-04-26 05:06:21 +02:00
|
|
|
new (item_) perm_string(tmp1);
|
2001-12-03 05:47:14 +01:00
|
|
|
return tmp2;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-26 05:06:21 +02:00
|
|
|
perm_string tmpo = array_[count_-1];
|
|
|
|
|
perm_string*tmpa = new perm_string[count_-1];
|
2001-12-03 05:47:14 +01:00
|
|
|
for (unsigned idx = 0 ; idx < count_-1 ; idx += 1)
|
|
|
|
|
tmpa[idx] = array_[idx];
|
|
|
|
|
|
|
|
|
|
delete[]array_;
|
|
|
|
|
array_ = tmpa;
|
|
|
|
|
count_ -= 1;
|
|
|
|
|
return tmpo;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-26 05:06:21 +02:00
|
|
|
perm_string hname_t::peek_name(unsigned idx) const
|
2001-12-03 05:47:14 +01:00
|
|
|
{
|
|
|
|
|
if (idx >= count_)
|
2007-04-26 05:06:21 +02:00
|
|
|
return perm_string();
|
2001-12-03 05:47:14 +01:00
|
|
|
|
|
|
|
|
if (count_ == 1)
|
2007-04-26 05:06:21 +02:00
|
|
|
return item_ref1_();
|
2001-12-03 05:47:14 +01:00
|
|
|
|
|
|
|
|
return array_[idx];
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-26 05:06:21 +02:00
|
|
|
perm_string hname_t::peek_tail_name() const
|
2001-12-03 05:47:14 +01:00
|
|
|
{
|
|
|
|
|
switch (count_) {
|
|
|
|
|
case 0:
|
2007-04-26 05:06:21 +02:00
|
|
|
return perm_string();
|
2001-12-03 05:47:14 +01:00
|
|
|
case 1:
|
2007-04-26 05:06:21 +02:00
|
|
|
return item_ref1_();
|
2001-12-03 05:47:14 +01:00
|
|
|
default:
|
|
|
|
|
return array_[count_-1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator < (const hname_t&l, const hname_t&r)
|
|
|
|
|
{
|
|
|
|
|
unsigned idx = 0;
|
|
|
|
|
const char*lc = l.peek_name(idx);
|
|
|
|
|
const char*rc = r.peek_name(idx);
|
|
|
|
|
|
|
|
|
|
while (lc && rc) {
|
|
|
|
|
int cmp = strcmp(lc, rc);
|
|
|
|
|
if (cmp < 0)
|
|
|
|
|
return true;
|
|
|
|
|
if (cmp > 0)
|
|
|
|
|
return false;
|
|
|
|
|
idx += 1;
|
|
|
|
|
lc = l.peek_name(idx);
|
|
|
|
|
rc = r.peek_name(idx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lc && !rc)
|
|
|
|
|
return false;
|
|
|
|
|
if (rc && !lc)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// Must be ==
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator == (const hname_t&l, const hname_t&r)
|
|
|
|
|
{
|
|
|
|
|
unsigned idx = 0;
|
|
|
|
|
const char*lc = l.peek_name(idx);
|
|
|
|
|
const char*rc = r.peek_name(idx);
|
|
|
|
|
|
|
|
|
|
while (lc && rc) {
|
|
|
|
|
int cmp = strcmp(lc, rc);
|
|
|
|
|
if (cmp != 0)
|
|
|
|
|
return false;
|
|
|
|
|
idx += 1;
|
|
|
|
|
lc = l.peek_name(idx);
|
|
|
|
|
rc = r.peek_name(idx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lc || rc)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Must be ==
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ostream& operator<< (ostream&out, const hname_t&that)
|
|
|
|
|
{
|
|
|
|
|
switch (that.count_) {
|
|
|
|
|
case 0:
|
|
|
|
|
out << "";
|
|
|
|
|
return out;
|
|
|
|
|
case 1:
|
|
|
|
|
out << that.item_;
|
|
|
|
|
return out;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
out << that.array_[0];
|
|
|
|
|
for (unsigned idx = 1 ; idx < that.count_ ; idx += 1)
|
|
|
|
|
out << "." << that.array_[idx];
|
|
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* $Log: HName.cc,v $
|
2007-04-26 05:06:21 +02:00
|
|
|
* Revision 1.6 2007/04/26 03:06:21 steve
|
|
|
|
|
* Rework hname_t to use perm_strings.
|
|
|
|
|
*
|
2002-11-02 04:27:51 +01:00
|
|
|
* Revision 1.5 2002/11/02 03:27:52 steve
|
|
|
|
|
* Allow named events to be referenced by
|
|
|
|
|
* hierarchical names.
|
|
|
|
|
*
|
2002-08-12 03:34:58 +02:00
|
|
|
* Revision 1.4 2002/08/12 01:34:58 steve
|
|
|
|
|
* conditional ident string using autoconfig.
|
|
|
|
|
*
|
2002-01-05 05:36:06 +01:00
|
|
|
* Revision 1.3 2002/01/05 04:36:06 steve
|
|
|
|
|
* include malloc.h only when available.
|
|
|
|
|
*
|
2001-12-18 05:52:45 +01:00
|
|
|
* Revision 1.2 2001/12/18 04:52:45 steve
|
|
|
|
|
* Include config.h for namespace declaration.
|
|
|
|
|
*
|
2001-12-03 05:47:14 +01:00
|
|
|
* Revision 1.1 2001/12/03 04:47:14 steve
|
|
|
|
|
* Parser and pform use hierarchical names as hname_t
|
|
|
|
|
* objects instead of encoded strings.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|