Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parser and pform use hierarchical names as hname_t
objects instead of encoded strings.
- Loading branch information
steve
committed
Dec 3, 2001
1 parent
33c7c47
commit ab6c8cb
Showing
34 changed files
with
963 additions
and
561 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
/* | ||
* 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 | ||
*/ | ||
#if !defined(WINNT) | ||
#ident "$Id: HName.cc,v 1.1 2001/12/03 04:47:14 steve Exp $" | ||
#endif | ||
|
||
# include "HName.h" | ||
# include <iostream> | ||
# include <string.h> | ||
# include <malloc.h> | ||
|
||
hname_t::hname_t() | ||
{ | ||
item_ = 0; | ||
count_ = 0; | ||
} | ||
|
||
hname_t::hname_t(const char*text) | ||
{ | ||
item_ = strdup(text); | ||
count_ = 1; | ||
} | ||
|
||
hname_t::hname_t(const hname_t&that) | ||
{ | ||
count_ = that.count_; | ||
switch (count_) { | ||
case 0: | ||
item_ = 0; | ||
break; | ||
case 1: | ||
item_ = strdup(that.item_); | ||
break; | ||
default: | ||
array_ = new char*[count_]; | ||
for (unsigned idx = 0 ; idx < count_ ; idx += 1) | ||
array_[idx] = strdup(that.array_[idx]); | ||
break; | ||
} | ||
} | ||
|
||
hname_t::~hname_t() | ||
{ | ||
switch (count_) { | ||
case 0: | ||
break; | ||
case 1: | ||
free(item_); | ||
break; | ||
default: | ||
for (unsigned idx = 0 ; idx < count_ ; idx += 1) | ||
free(array_[idx]); | ||
delete[]array_; | ||
break; | ||
} | ||
} | ||
|
||
void hname_t::append(const char*text) | ||
{ | ||
char**tmp; | ||
|
||
switch (count_) { | ||
case 0: | ||
count_ = 1; | ||
item_ = strdup(text); | ||
break; | ||
case 1: | ||
count_ = 2; | ||
tmp = new char*[2]; | ||
tmp[0] = item_; | ||
tmp[1] = strdup(text); | ||
array_ = tmp; | ||
break; | ||
default: | ||
tmp = new char*[count_+1]; | ||
for (unsigned idx = 0 ; idx < count_ ; idx += 1) | ||
tmp[idx] = array_[idx]; | ||
delete[]array_; | ||
array_ = tmp; | ||
array_[count_] = strdup(text); | ||
count_ += 1; | ||
} | ||
} | ||
|
||
void hname_t::prepend(const char*text) | ||
{ | ||
char**tmp; | ||
|
||
switch (count_) { | ||
case 0: | ||
count_ = 1; | ||
item_ = strdup(text); | ||
break; | ||
case 1: | ||
count_ = 2; | ||
tmp = new char*[2]; | ||
tmp[0] = strdup(text); | ||
tmp[1] = item_; | ||
array_ = tmp; | ||
break; | ||
default: | ||
tmp = new char*[count_+1]; | ||
tmp[0] = strdup(text); | ||
for (unsigned idx = 0 ; idx < count_ ; idx += 1) | ||
tmp[idx+1] = array_[idx]; | ||
delete[]array_; | ||
array_ = tmp; | ||
count_ += 1; | ||
} | ||
} | ||
|
||
char* hname_t::remove_tail_name() | ||
{ | ||
if (count_ == 0) | ||
return 0; | ||
|
||
if (count_ == 1) { | ||
char*tmp = item_; | ||
count_ = 0; | ||
item_ = 0; | ||
return tmp; | ||
} | ||
|
||
if (count_ == 2) { | ||
char*tmp1 = array_[0]; | ||
char*tmp2 = array_[1]; | ||
delete[]array_; | ||
count_ = 1; | ||
item_ = tmp1; | ||
return tmp2; | ||
} | ||
|
||
char*tmpo = array_[count_-1]; | ||
char**tmpa = new char*[count_-1]; | ||
for (unsigned idx = 0 ; idx < count_-1 ; idx += 1) | ||
tmpa[idx] = array_[idx]; | ||
|
||
delete[]array_; | ||
array_ = tmpa; | ||
count_ -= 1; | ||
return tmpo; | ||
} | ||
|
||
const char* hname_t::peek_name(unsigned idx) const | ||
{ | ||
if (idx >= count_) | ||
return 0; | ||
|
||
if (count_ == 1) | ||
return item_; | ||
|
||
return array_[idx]; | ||
} | ||
|
||
const char* hname_t::peek_tail_name() const | ||
{ | ||
switch (count_) { | ||
case 0: | ||
return 0; | ||
case 1: | ||
return item_; | ||
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 $ | ||
* Revision 1.1 2001/12/03 04:47:14 steve | ||
* Parser and pform use hierarchical names as hname_t | ||
* objects instead of encoded strings. | ||
* | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#ifndef __HName_H | ||
#define __HName_H | ||
/* | ||
* 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 | ||
*/ | ||
#if !defined(WINNT) | ||
#ident "$Id: HName.h,v 1.1 2001/12/03 04:47:14 steve Exp $" | ||
#endif | ||
|
||
#ifdef HAVE_IOSFWD | ||
# include <iosfwd> | ||
#else | ||
class ostream; | ||
#endif | ||
|
||
/* | ||
* This class represents a Verilog hierarchical name. A hierarchical | ||
* name is an ordered list of simple names. | ||
*/ | ||
|
||
class hname_t { | ||
|
||
public: | ||
hname_t (); | ||
explicit hname_t (const char*text); | ||
hname_t (const hname_t&that); | ||
~hname_t(); | ||
|
||
// This method adds a name to the end of the hierarchical | ||
// path. This becomes a new base name. | ||
void append(const char*text); | ||
|
||
// This method adds a name to the *front* of the hierarchical | ||
// path. The base name remains the same, unless this is the | ||
// only component. | ||
void prepend(const char*text); | ||
|
||
// This method removes the tail name from the hierarchy, and | ||
// returns a pointer to that tail name. That tail name now | ||
// must be removed by the caller. | ||
char* remove_tail_name(); | ||
|
||
// Return the given component in the hierarchical name. If the | ||
// idx is too large, return 0. | ||
const char*peek_name(unsigned idx) const; | ||
const char*peek_tail_name() const; | ||
|
||
friend ostream& operator<< (ostream&, const hname_t&); | ||
|
||
private: | ||
union { | ||
char**array_; | ||
char* item_; | ||
}; | ||
unsigned count_; | ||
|
||
private: // not implemented | ||
hname_t& operator= (const hname_t&); | ||
}; | ||
|
||
extern bool operator < (const hname_t&, const hname_t&); | ||
extern bool operator == (const hname_t&, const hname_t&); | ||
|
||
/* | ||
* $Log: HName.h,v $ | ||
* Revision 1.1 2001/12/03 04:47:14 steve | ||
* Parser and pform use hierarchical names as hname_t | ||
* objects instead of encoded strings. | ||
* | ||
*/ | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.