home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume33 / var / part02 < prev    next >
Encoding:
Text File  |  1992-11-28  |  34.0 KB  |  1,122 lines

  1. Newsgroups: comp.sources.misc
  2. From: tlhouns@srv.pacbell.com (Lee Hounshell)
  3. Subject:  v33i128:  var - C++ class library "var" (v1.1) and "VarMap" (associative arrays), Part02/02
  4. Message-ID: <1992Nov25.215658.19787@sparky.imd.sterling.com>
  5. X-Md4-Signature: 4d78e84a407547ae47405dd75ad48e64
  6. Date: Wed, 25 Nov 1992 21:56:58 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: tlhouns@srv.pacbell.com (Lee Hounshell)
  10. Posting-number: Volume 33, Issue 128
  11. Archive-name: var/part02
  12. Environment: C++, G++
  13. Supersedes: var: Volume 31, Issue 91-92
  14.  
  15. #! /bin/sh
  16. # This is a shell archive.  Remove anything before this line, then feed it
  17. # into a shell via "sh file" or similar.  To overwrite existing files,
  18. # type "sh file -c".
  19. # Contents:  LEGAL_NOTICE Makefile VarMap.3++ VarMap.C VarMap.H demo.C
  20. #   var.H
  21. # Wrapped by kent@sparky on Tue Nov 24 09:31:38 1992
  22. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  23. echo If this archive is complete, you will see the following message:
  24. echo '          "shar: End of archive 2 (of 2)."'
  25. if test -f 'LEGAL_NOTICE' -a "${1}" != "-c" ; then 
  26.   echo shar: Will not clobber existing file \"'LEGAL_NOTICE'\"
  27. else
  28.   echo shar: Extracting \"'LEGAL_NOTICE'\" \(1388 characters\)
  29.   sed "s/^X//" >'LEGAL_NOTICE' <<'END_OF_FILE'
  30. X
  31. X        Any use of this source code must include, in the user documentation
  32. X        and internal comments to the code, and notices to the end user as
  33. X        follows:
  34. X
  35. X    Copyright (c) 1992 Lee Hounshell
  36. X    LEE HOUNSHELL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF
  37. X        THIS SOURCE CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS"
  38. X        WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.  LEE HOUNSHELL
  39. X    SEVERALLY AND INDIVIDUALLY, DISCLAIM ALL WARRANTIES WITH REGARD
  40. X    TO THIS SOURCE CODE, INCLUDING ALL IMPLIED WARRANTIES OF
  41. X    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO
  42. X    EVENT SHALL LEE HOUNSHELL BE LIABLE FOR ANY SPECIAL, INDIRECT,
  43. X    INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
  44. X    RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  45. X    OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  46. X    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOURCE CODE.
  47. X
  48. X    Permission to use, copy, modify, and distribute this software
  49. X    and its documentation for any purpose and without fee is hereby
  50. X    granted, provided that the above copyright notice appear in all
  51. X    copies and that both that copyright notice and this permission
  52. X    notice appear in supporting documentation, and that the name of
  53. X    Lee Hounshell not be used in advertising in publicity pertaining
  54. X    to distribution of the software without specific, written prior
  55. X    permission.
  56. X
  57. END_OF_FILE
  58.   if test 1388 -ne `wc -c <'LEGAL_NOTICE'`; then
  59.     echo shar: \"'LEGAL_NOTICE'\" unpacked with wrong size!
  60.   fi
  61.   # end of 'LEGAL_NOTICE'
  62. fi
  63. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  64.   echo shar: Will not clobber existing file \"'Makefile'\"
  65. else
  66.   echo shar: Extracting \"'Makefile'\" \(1475 characters\)
  67.   sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  68. X############################################################################
  69. X#       make - compiles the C++ library (libvar.a) 
  70. X#       make clean - removes all .o files generated in these procedures
  71. X#       make clobber - removes all libraries, cleans up .o's
  72. X############################################################################
  73. X
  74. X
  75. X# from the two below, pick the operating system closest to yours:
  76. X
  77. X# If you're using g++, define CC and INCDIRS as appropriate for you
  78. XCC    = g++
  79. XINCDIRS = -I. -I/usr/local/lib/g++-include
  80. X
  81. X# If you're using Sun or AT&T C++ version 2.0 or 2.1 use these..
  82. X#CC    = CC
  83. X#INCDIRS = -I.
  84. X
  85. X#SYS    = HP
  86. XSYS    = SUN
  87. X
  88. X#-----don't change anything after this line-----
  89. X
  90. XCFLAGS    = -O $(INCDIRS)
  91. XARFLAGS    = rv
  92. X
  93. X
  94. Xis_bsd    = test $(SYS) = SUN
  95. X
  96. Xall:    libvar.a
  97. X
  98. XC++FILES    = var.C VarMap.C
  99. XINS_HFILES    = var.H VarMap.H
  100. XHFILES        = $(INS_HFILES)
  101. X
  102. XOBJECTS = ${C++FILES:.C=.o}
  103. X
  104. Xlibvar.a:    $(OBJECTS)
  105. X        ar $(ARFLAGS) $@ $?
  106. X        if $(is_bsd) ; then ranlib $@; fi
  107. X
  108. X$(OBJECTS):    $(HFILES)
  109. X
  110. Xdemo:        libvar.a demo.C demo2.C
  111. X        $(CC) $(INCDIRS) demo.C -L. -lvar -o demo
  112. X        $(CC) $(INCDIRS) demo2.C -L. -lvar -o demo2
  113. X
  114. Xclean:    
  115. X        rm -f *.o a.out core $(OBJECTS)
  116. X        cd demo; $(MAKE) clean
  117. X
  118. Xclobber:    clean
  119. X        rm -fr $(DOTAS)
  120. X        cd demo; $(MAKE) clobber
  121. X
  122. X#####
  123. X#####
  124. X#.SUFFIXES: .o .C .C~ .c .c~
  125. X.SUFFIXES: .o .C .C~
  126. X.C~.C:
  127. X    -cd $(<D); $(GET) $(GFLAGS) $(<F)
  128. X.C.c:
  129. X    $(CC) $(CFLAGS) -Fc $*.C > $*.c
  130. X.C.o:
  131. X    $(CC) $(CFLAGS) -c $*.C
  132. X.C~.o:
  133. X    -cd $(<D); $(GET) $(GFLAGS) $(<F)
  134. X    $(CC) $(CFLAGS) -c $*.C
  135. X
  136. END_OF_FILE
  137.   if test 1475 -ne `wc -c <'Makefile'`; then
  138.     echo shar: \"'Makefile'\" unpacked with wrong size!
  139.   fi
  140.   # end of 'Makefile'
  141. fi
  142. if test -f 'VarMap.3++' -a "${1}" != "-c" ; then 
  143.   echo shar: Will not clobber existing file \"'VarMap.3++'\"
  144. else
  145.   echo shar: Extracting \"'VarMap.3++'\" \(8400 characters\)
  146.   sed "s/^X//" >'VarMap.3++' <<'END_OF_FILE'
  147. X.po 6
  148. X.TH VARMAP 3++ "1/92" "Version 1.0" "VAR Associative Array Class Library"
  149. X
  150. X.SH CLASS
  151. XVARMAP    \- Variable Base Type Associative Array Class Library
  152. X.SH SYNOPSIS
  153. X.B #include     <VarMap.H>
  154. X.PP
  155. X.nf
  156. X.ta.5i 1.0i 4.0i
  157. X\fB
  158. Xclass VarMap {
  159. X
  160. X  public:
  161. X    // Standard Interface
  162. X    const var & type(void) const;        // who am i
  163. X
  164. X    // Constructors & Destructor
  165. X    VarMap(int type = VARMAP_FIFO);
  166. X    ~VarMap(void) {remove();}
  167. X
  168. X    // Operators
  169. X    VarMap & operator = (const VarMap &);    // assign VarMaps
  170. X    int operator == (VarMap &) const;        // equality
  171. X    int operator != (VarMap &) const;        // inequality
  172. X    var & operator [] (const var index);    // index/create entry
  173. X
  174. X    friend ostream & operator << (ostream &, const VarMap &); // output
  175. X    friend TraceStream & operator << (TraceStream &, const VarMap &);
  176. X
  177. X    // Standard Member Functions
  178. X    void print(ostream &) const;        // output
  179. X
  180. X    // Custom Interface - see VarMap man pages for use
  181. X    int    append(var newkey, var newvalue);    // append to map
  182. X    int    push(var newkey, var newvalue);        // push a var
  183. X    var    pop(void);                // pop a var
  184. X    int    empty(void) const;            // is map empty
  185. X    int    at_start(void) const;            // at head of map
  186. X    int    at_end(void) const;            // at end of map
  187. X    int    position(void);                // index within map
  188. X    int    size(void) const;            // length of map
  189. X    void remove(void);                // remove all items
  190. X    void delete_here(void);            // remove this item
  191. X    int    element(const var & index);        // test for element
  192. X    var & key(void);                // current key
  193. X    var & value(void);                // current value
  194. X    void display(ostream & out = cerr);        // print the map
  195. X    void first(void);                // point to first node
  196. X    void next(void);                // point to next node
  197. X    void last(void);                // point to last node
  198. X};
  199. X.fi
  200. X\fP
  201. X
  202. X.SH DESCRIPTION
  203. XClass \fBVarMap\fP represents an \fBassociative array\fP object class that allows dynamic
  204. Xarrays of \fBvar\fP objects to be indexed by another \fBvar\fP object.
  205. X\fBVarMap\fP, is in many ways a "super-associative-array" class.
  206. XThe VarMap class does a pretty good job of offering an associative array object with an
  207. Xindex of virtually \fBany base type\fP.  It also provides a base data "container" object
  208. Xthat can assume its "type" at run time, based on context.  VarMap can also function as a "stack"
  209. Xobject, with both FIFO (first-in-first-out) and LIFO (last-in-last-out) capabilities.
  210. X
  211. X.SH PUBLIC CONSTRUCTORS
  212. X.SS VarMap(int type = VARMAP_FIFO);
  213. XThis constructor is used to declare a VarMap object.  The newly created object will
  214. Xinitially contain NO data elements.  If the paramater \fBVARMAP_LIFO\fP is specified,
  215. Xthen this object will use LIFO (last-in-first-out) stack operations.  If either no
  216. Xparameter or \fBVARMAP_FIFO\fP is specified, then FIFO (first-in-first-out) stack
  217. Xoperations will occur on this object.  Type of the indexed object
  218. Xis determined at run-time, based on context of use.
  219. X
  220. X.SS ~VarMap(void);
  221. XThe destructor returns all memory allocated by the object back to the OS.
  222. X
  223. X.SH PUBLIC OPERATORS
  224. X.SS The Assignment Operator
  225. XThe assignment operator (=) will operate as expected.
  226. XAs no \fBcopy constructor\fP exists at present for \fBVarMap\fP, you will need
  227. Xto use this operator to initialize copies of other VarMaps.
  228. X
  229. X.SS Relational Operators
  230. XOnly the \fBequality\fP (==) and \fBinequality\fP (!=) operators are currently implemented.
  231. XThese operate as expected.  The objects must be \fBexactly\fP the same to match.
  232. X
  233. X.SS The Indexing Operator []
  234. XYou can index any \fBvar\fP object inside a \fBVarMap\fP object by using
  235. Xthe \fB"var & operator [] (const var);"\fP operator.  WARNING!! If you attempt
  236. Xto index an object that does not yet exist, then it will be created automatically.
  237. XTo test whether or not an object exists without creating it, use the \fBelement(const var)\fP
  238. Xmember function.  A reference to the indexed \fBvar\fP is returned, which can be assigned to.
  239. X
  240. X.SS Type Casting
  241. XThere are no cast operators for \fBVarMap\fP objects; however, you may cast the
  242. Xindexed \fBvar\fP object to any base type.
  243. X
  244. X.SS Output
  245. XYou can output the contents of an entire \fBVarMap\fP object using the io stream library.
  246. XObjects can be output using the ">>" operator.
  247. XFor formatted output, be sure to index and set each \fBvar\fP element's "format template"
  248. Xprior to calling the ">>" operator.  (see \fB"var & format(const char *);"\fP)
  249. X
  250. X.SH PUBLIC MEMBER FUNCTIONS
  251. X
  252. X.SS Appending a New Element and Key to a VarMap
  253. XThe \fB"int append(var newkey, var newvalue);\fP function will append a new element
  254. Xto the VarMap with the index \fBnewkey\fP and the value \fBnewvalue\fP.  The size of
  255. Xthe VarMap is increased by one.
  256. X
  257. X.SS Pushing a New Element (and Key) onto a VarMap
  258. XThe \fB"int push(var newkey, var newvalue);\fP function will push a new element
  259. Xto the \fBVarMap\fP stack with the index \fBnewkey\fP and the value \fBnewvalue\fP.
  260. XThe size of the \fBVarMap\fP is increased by one. The order that this element will be
  261. Xlater \fBpopped\fP is determined by the \fBVarmap\fP object's initial VARMAP_FIFO
  262. Xor VARMAP_LIFO constructor argument.
  263. X
  264. X.SS Poping an Element from a VarMap
  265. XThe \fB"int pop(void);\fP function will pop the next appropriate element
  266. Xfrom the \fBVarMap\fP stack.  The size of the \fBVarMap\fP is decreased by one.
  267. XThe order that each element is popped is determined by the \fBVarmap\fP object's
  268. Xinitial VARMAP_FIFO or VARMAP_LIFO constructor argument.
  269. X
  270. X.SS Testing a VarMap Index
  271. XThe \fB"int element(const var & index) const;"\fP function returns 1 if a \fBVarMap\fP entry
  272. Xexists for the passed index value, 0 otherwise.
  273. X
  274. X.SS Determining if a VarMap is Empty
  275. XThe \fB"int empty(void) const;"\fP function returns 1 if a \fBVarMap\fP is empty, 0 otherwise.
  276. X
  277. X.SS Determining VarMap's Size
  278. XThe \fB"int size(void) const;"\fP function returns the "size" of a \fBVarMap\fP.
  279. X
  280. X.SS Relative "Positions" Inside a VarMap Object
  281. XSix member functions exist to allow user to "walk" through a \fBVarMap's\fP elements and/or test
  282. Xwhere (relatively speaking) you are currently positioned within the map.  The first two are used
  283. Xto test if you are at the \fBhead\fP or \fBtail\fP of the \fBVarMap\fP.
  284. XThe \fB"int at_start(void) const;"\fP function returns 1 if you are at the head of a \fBVarMap\fP, 0 otherwise.
  285. XThe \fB"int at_end(void) const;"\fP function returns 1 if you are at the tail of a \fBVarMap\fP, 0 otherwise.
  286. XThe \fB"int position(void) const;"\fP function returns relative \fBnumerical position\fP of the current
  287. Xnode in a \fBVarMap\fP. Three memeber functions allow you to change the current position.
  288. XThe \fB"void first(void) const;"\fP function moves the current \fBVarMap\fP pointer to the head of the map.
  289. XThe \fB"void last(void) const;"\fP function moves the current \fBVarMap\fP pointer to the tail of the map.
  290. XThe \fB"void next(void) const;"\fP function moves the current \fBVarMap\fP pointer to the next entry of the map.
  291. X
  292. X.SS Deleting the current VarMap Entry
  293. XThe \fB"void delete_here(void) const;"\fP function will destroy the currently \fBpointed to\fP entry.
  294. XThis is often used in conjunction with the \fBfirst()\fP and \fBnext()\fP function to traverse a
  295. X\fBVarMap\fP and remove nodes at random.  After a \fBdelete_here()\fP call, the \fBVarMap\fP will be
  296. Xpointing to the \fBnext\fP node (just as if a \fBnext()\fP call had been made).
  297. X
  298. X.SS Deleting all VarMap Entries
  299. XThe \fB"void remove(void) const;"\fP function will destroy every entry in the \fBVarMap\fP.  After this
  300. Xcall, the \fBVarMap's\fP size will be zero.
  301. X
  302. X.SS Extracting the Current Entry's Key Value
  303. XThe \fB"var & key(void);"\fP function will return a reference to the current \fBVarMap\fP element's key.
  304. X
  305. X.SS Extracting the Current Entry's Data Value
  306. XThe \fB"var & value(void);"\fP function will return a reference to the current \fBVarMap\fP element.
  307. X
  308. X.SS Displaying a VarMap
  309. XThe \fB"void display(ostream & out = cerr);"\fP function will output all elements and keys of a \fBVarMap\fP
  310. Xto the passed ostream.  By default, output is sent to \fBcerr\fP.
  311. X
  312. X.SH AUTHOR
  313. XLee Hounshell - 1/92
  314. X
  315. X.SH EXAMPLE
  316. X.nf
  317. X#include    <VarMap.H>
  318. X
  319. Xmain ()
  320. X{
  321. X    VarMap aa;                // declare a VarMap
  322. X    var lee = "foobar";
  323. X    if (!aa.element(lee)) {
  324. X    aa[lee] = "hello world";    // create/assign using index "foobar"
  325. X    }
  326. X    aa[42] = "testing 123";        // create/assign using index "42"
  327. X    aa[5] = 123.45;            // create/assign using index "5"
  328. X    aa.display();            // display this map to stderr
  329. X}
  330. X.fi
  331. X
  332. X.SH SUPERCLASSES
  333. Xnone.
  334. X
  335. X.SH SUBCLASSES
  336. Xvar
  337. X
  338. X.SH BUGS
  339. X
  340. END_OF_FILE
  341.   if test 8400 -ne `wc -c <'VarMap.3++'`; then
  342.     echo shar: \"'VarMap.3++'\" unpacked with wrong size!
  343.   fi
  344.   # end of 'VarMap.3++'
  345. fi
  346. if test -f 'VarMap.C' -a "${1}" != "-c" ; then 
  347.   echo shar: Will not clobber existing file \"'VarMap.C'\"
  348. else
  349.   echo shar: Extracting \"'VarMap.C'\" \(6346 characters\)
  350.   sed "s/^X//" >'VarMap.C' <<'END_OF_FILE'
  351. X//
  352. X// NAME:    VarMap.C
  353. X//
  354. X// PURPOSE:    C++ "Map of Vars" Class Library Source File
  355. X// VERSION:     1.0
  356. X// AUTHOR:      Lee Hounshell
  357. X// LAST MOD:    Mon Nov 23 13:55:39 PST 1992
  358. X//
  359. X
  360. X#include <stream.h>
  361. X#include "VarMap.H"
  362. X
  363. X// -----------------------------------------------------------------------------
  364. X
  365. Xconst var & VarMapNode::type(void) const
  366. X{
  367. X    static const var objtype = "VarMapNode";
  368. X    return objtype;
  369. X}
  370. X
  371. X
  372. Xconst var & VarMap::type(void) const
  373. X{
  374. X    static const var objtype = "VarMap";
  375. X    return objtype;
  376. X}
  377. X
  378. X
  379. XVarMapNode::VarMapNode(var newkey, var newvalue)
  380. X{
  381. X    errno = 0;
  382. X    key = newkey;
  383. X    value = newvalue;
  384. X    prev = next = NULL;
  385. X}
  386. X
  387. X
  388. XVarMap::VarMap(int type)
  389. X{
  390. X    errno = 0;
  391. X    typeofmap = type;
  392. X    here = head = tail = NULL;
  393. X    _size = 0;
  394. X}
  395. X
  396. X// -----------------------------------------------------------------------------
  397. X
  398. XVarMap & VarMap::operator = (const VarMap & map)
  399. X{
  400. X    if (this != &map) {
  401. X    VarMapNode *vptr;
  402. X    remove();
  403. X    typeofmap = map.typeofmap;
  404. X    for (vptr = map.head; vptr; vptr = vptr->next) {
  405. X        append(vptr->key, vptr->value);
  406. X    }
  407. X    here = map.here;
  408. X    }
  409. X    return *this;
  410. X}
  411. X
  412. X
  413. Xint VarMap::operator == (VarMap & map) const
  414. X{
  415. X    if (size() != map.size()) {
  416. X    return 0;
  417. X    }
  418. X    if (empty() && map.empty()) {
  419. X    return 1;
  420. X    }
  421. X    int rc = 1;
  422. X    VarMapNode *tmp1 = head;
  423. X    VarMapNode *tmp2 = map.head;
  424. X    int done = 0;
  425. X    do {
  426. X    if (tmp1 == tail) {
  427. X        done = 1;
  428. X    }
  429. X    if (tmp1->key != tmp2->key) {
  430. X        rc = 0;
  431. X        break;
  432. X    }
  433. X    if (tmp1->value != tmp2->value) {
  434. X        rc = 0;
  435. X        break;
  436. X    }
  437. X    if ((tmp2 == map.tail) && (tmp1 != tail)) {
  438. X        rc = 0;
  439. X        break;
  440. X    }
  441. X    tmp1 = tmp1->next;
  442. X    tmp2 = tmp2->next;
  443. X    } while (!done);
  444. X    return rc;
  445. X}
  446. X
  447. X
  448. Xint VarMap::operator != (VarMap & map) const
  449. X{
  450. X    return (!(*this == map));
  451. X}
  452. X
  453. X
  454. Xvar & VarMap::operator [] (const var index)
  455. X{
  456. X    for (VarMapNode *vptr = head; vptr; vptr = vptr->next) {
  457. X    if (vptr->key == index) {
  458. X        here = vptr;
  459. X        return vptr->value;
  460. X    }
  461. X    }
  462. X    append(index, "");
  463. X    return here->value;
  464. X}
  465. X
  466. X// -----------------------------------------------------------------------------
  467. X
  468. Xint VarMap::append(var newkey, var newvalue)
  469. X{
  470. X    VarMapNode *newnode = new VarMapNode(newkey, newvalue);
  471. X    if (newnode == NULL) {
  472. X    cerr << "Out of memory.\n";
  473. X    return 0;
  474. X    }
  475. X    if (head == NULL) {
  476. X    // empty map
  477. X    head = newnode;
  478. X    tail = newnode;
  479. X    }
  480. X    else {
  481. X    tail->next = newnode;
  482. X    newnode->prev = tail;
  483. X    tail = newnode;
  484. X    }
  485. X    here = newnode;
  486. X    _size++;
  487. X    return 1;
  488. X}
  489. X
  490. X
  491. Xint VarMap::push(var newkey, var newvalue)
  492. X{
  493. X    if (typeofmap == VARMAP_FIFO) {
  494. X    return append(newkey, newvalue);
  495. X    }
  496. X    else if (typeofmap == VARMAP_LIFO) {
  497. X    VarMapNode *newnode = new VarMapNode(newkey, newvalue);
  498. X    if (newnode == NULL) {
  499. X        cerr << "Out of memory.\n";
  500. X        return 0;
  501. X    }
  502. X    if (head == NULL) {
  503. X        // empty map
  504. X        head = newnode;
  505. X        tail = newnode;
  506. X    }
  507. X    else {
  508. X        head->prev = newnode;
  509. X        newnode->next = head;
  510. X        newnode->prev = NULL;
  511. X        head = newnode;
  512. X    }
  513. X    here = newnode;
  514. X    }
  515. X    return 1;
  516. X}
  517. X
  518. X
  519. Xvar VarMap::pop(void)
  520. X{
  521. X    var        data;
  522. X    VarMapNode    *vptr;
  523. X    if (head == NULL) {
  524. X    // empty map
  525. X    return 0;
  526. X    }
  527. X    data = head->value;
  528. X    vptr = head;
  529. X    here = head = head->next;
  530. X    if (head) {
  531. X    head->prev = NULL;
  532. X    }
  533. X    else {
  534. X    tail = NULL;
  535. X    }
  536. X    delete vptr;
  537. X    _size--;
  538. X    return data;
  539. X}
  540. X
  541. X
  542. Xint VarMap::empty(void) const
  543. X{
  544. X    return head == NULL;
  545. X}
  546. X
  547. X
  548. Xint VarMap::at_start(void) const
  549. X{
  550. X    return here == head;
  551. X}
  552. X
  553. X
  554. Xint VarMap::at_end(void) const
  555. X{
  556. X    return here == tail;
  557. X}
  558. X
  559. X
  560. Xint VarMap::position(void)
  561. X{
  562. X    int index = 0;
  563. X    VarMapNode *vptr = head;
  564. X    while (vptr && vptr != here) {
  565. X    vptr = vptr->next;
  566. X    ++index;
  567. X    }
  568. X    return index;
  569. X}
  570. X
  571. X
  572. Xint VarMap::size(void) const
  573. X{
  574. X    return _size;
  575. X}
  576. X
  577. X
  578. Xvoid VarMap::remove(void)
  579. X{
  580. X    VarMapNode *vptr = head;
  581. X    while (vptr) {
  582. X    VarMapNode *tmp = vptr;
  583. X    vptr = vptr->next;
  584. X    delete tmp;
  585. X    }
  586. X    here = head = tail = NULL;
  587. X    _size = 0;
  588. X}
  589. X
  590. X
  591. Xvoid VarMap::delete_here(void)
  592. X{
  593. X    if (here) {
  594. X    VarMapNode *vptr = here;
  595. X    if (here->next) {
  596. X        here->next->prev = here->prev;
  597. X    }
  598. X    if (here->prev) {
  599. X        here->prev->next = here->next;
  600. X    }
  601. X    if (here == head && head == tail) {
  602. X        here = head = tail = NULL;
  603. X    }
  604. X    else if (here == head) {
  605. X        head = here->next;
  606. X        here = head;
  607. X    }
  608. X    else if (here == tail) {
  609. X        tail = here->prev;
  610. X        here = tail;
  611. X    }
  612. X    else {
  613. X        here = here->next;
  614. X    }
  615. X    delete vptr;
  616. X    --_size;
  617. X    }
  618. X}
  619. X
  620. X
  621. Xint VarMap::element(const var & index)
  622. X{
  623. X    for (VarMapNode *vptr = head; vptr; vptr = vptr->next) {
  624. X    if (vptr->key == index) {
  625. X        return 1;                // found it
  626. X    }
  627. X    }
  628. X    return 0;                    // this index doesn't exist
  629. X}
  630. X
  631. X
  632. Xvar & VarMap::key(void)
  633. X{
  634. X    static var vkey = "";
  635. X    if (!here) {
  636. X    return vkey;
  637. X    }
  638. X    return here->key;
  639. X}
  640. X
  641. X
  642. Xvar & VarMap::value(void)
  643. X{
  644. X    static var vvalue = "";
  645. X    if (!here) {
  646. X    return vvalue;
  647. X    }
  648. X    return here->value;
  649. X}
  650. X
  651. X
  652. Xvoid VarMap::display(ostream & out)
  653. X{
  654. X    out << "Display VarMap\n";
  655. X    for (VarMapNode *vptr = head; vptr; vptr = vptr->next) {
  656. X    out << "\tKey: " << vptr->key << " Value: " << vptr->value << "\n";
  657. X    }
  658. X}
  659. X
  660. X
  661. X// return value of first node - NULL if invalid
  662. Xvoid VarMap::first(void)
  663. X{
  664. X    here = head;
  665. X}
  666. X
  667. X
  668. X// return value of next node
  669. Xvoid VarMap::next(void)
  670. X{
  671. X    if (here == tail) {
  672. X    return;
  673. X    }
  674. X    here = here->next;
  675. X}
  676. X
  677. X
  678. X// return value of last node
  679. Xvoid VarMap::last(void)
  680. X{
  681. X    here = tail;
  682. X}
  683. X
  684. X// -----------------------------------------------------------------------------
  685. X
  686. Xostream & operator << (ostream &out, const VarMapNode &d)
  687. X{
  688. X    d.print(out);
  689. X    return out;
  690. X}
  691. X
  692. X
  693. Xostream & operator << (ostream &out, const VarMap &d)
  694. X{
  695. X    d.print(out);
  696. X    return out;
  697. X}
  698. X
  699. X
  700. Xvoid VarMapNode::print(ostream &out) const
  701. X{
  702. X    out << "VarMapNode OBJECT: {"
  703. X    << "\n\t<this>           = " << (void *) this
  704. X    << "\n\tvar key          = " << key
  705. X    << "\n\tvar value        = " << value
  706. X    << "\n\tVarMapNode *prev = " << prev
  707. X    << "\n\tVarMapNode *next = " << next
  708. X    << "\n}\n";
  709. X}
  710. X
  711. X
  712. Xvoid VarMap::print(ostream &out) const
  713. X{
  714. X    out << "VarMap OBJECT: {"
  715. X    << "\n\tint typeofmap    = " << typeofmap
  716. X    << "\n\tVarMapNode *head = " << head
  717. X    << "\n\tVarMapNode *tail = " << tail
  718. X    << "\n\tVarMapNode *here = " << here
  719. X    << "\n\tint _size        = '" << _size << "'";
  720. X    int i = 0;
  721. X    for (VarMapNode *ptr = head; ptr; ptr = ptr->next) {
  722. X    ++i;
  723. X    out << "\n\tVarMapNode #" << i << "    = " << *ptr;
  724. X    }
  725. X    out << "\n}\n";
  726. X}
  727. X
  728. END_OF_FILE
  729.   if test 6346 -ne `wc -c <'VarMap.C'`; then
  730.     echo shar: \"'VarMap.C'\" unpacked with wrong size!
  731.   fi
  732.   # end of 'VarMap.C'
  733. fi
  734. if test -f 'VarMap.H' -a "${1}" != "-c" ; then 
  735.   echo shar: Will not clobber existing file \"'VarMap.H'\"
  736. else
  737.   echo shar: Extracting \"'VarMap.H'\" \(2602 characters\)
  738.   sed "s/^X//" >'VarMap.H' <<'END_OF_FILE'
  739. X//
  740. X// NAME:        VarMap.H
  741. X//
  742. X// PURPOSE:     C++ "Map of Vars" Class Library Header File
  743. X// VERSION:     1.0
  744. X// AUTHOR:      Lee Hounshell
  745. X// LAST MOD:    Mon Nov 23 13:50:56 PST 1992
  746. X//
  747. X
  748. X#ifndef VARMAP_H
  749. X#define VARMAP_H
  750. X
  751. X#include    <stream.h>
  752. X#include    "var.H"
  753. X
  754. Xenum {VARMAP_FIFO, VARMAP_LIFO};
  755. X
  756. Xclass VarMapNode
  757. X{
  758. X   public:
  759. X        // Standard Interface
  760. X        const var & type(void) const;                   // who am i
  761. X
  762. X        // Operators
  763. X    friend ostream & operator << (ostream &, const VarMapNode &); // output
  764. X
  765. X    friend class VarMap;
  766. X
  767. X        // Standard Member Functions
  768. X        void print(ostream &) const;            // output
  769. X
  770. X        int             errno;                          // primitive error control
  771. X
  772. X   protected:
  773. X
  774. X   private:
  775. X    VarMapNode(var newkey, var newvalue);
  776. X    var        key;
  777. X    var        value;
  778. X    VarMapNode    *prev;
  779. X    VarMapNode    *next;
  780. X};
  781. X
  782. X
  783. Xclass VarMap
  784. X{
  785. X   public:
  786. X        // Standard Interface
  787. X        const var & type(void) const;                   // who am i
  788. X
  789. X        // Constructors & Destructor
  790. X    VarMap(int type = VARMAP_FIFO);
  791. X    ~VarMap(void)    {remove();}
  792. X
  793. X        // Operators
  794. X    VarMap &    operator = (const VarMap &);    // assign VarMaps
  795. X    int operator == (VarMap &) const;        // equality
  796. X    int operator != (VarMap &) const;        // inequality
  797. X    var &        operator [] (const var index);    // index/create entry
  798. X
  799. X    friend ostream & operator << (ostream &, const VarMap &); // output
  800. X
  801. X        // Standard Member Functions
  802. X        void print(ostream &) const;            // output
  803. X
  804. X        // Custom Interface - see VarMap man pages for use
  805. X    int         append(var newkey, var newvalue); // append to map
  806. X    int        push(var newkey, var newvalue);    // push a var
  807. X    var        pop(void);            // pop a var
  808. X    int        empty(void) const;        // is map empty
  809. X    int        at_start(void) const;        // at head of map
  810. X    int        at_end(void) const;        // at end of map
  811. X    int        position(void);            // index within map
  812. X    int        size(void) const;        // length of map
  813. X    void        remove(void);            // remove all items
  814. X    void        delete_here(void);        // remove this item
  815. X    int        element(const var & index);    // test for element
  816. X    var &        key(void);            // current key
  817. X    var &        value(void);            // current value
  818. X    void        display(ostream & out = cerr);    // print the map
  819. X    void        first(void);            // point to first node
  820. X    void        next(void);            // point to next node
  821. X    void        last(void);            // point to last node
  822. X
  823. X        int             errno;                          // primitive error control
  824. X
  825. X   protected:
  826. X
  827. X   private:
  828. X    int        typeofmap;            // VARMAP_FIFO or VARMAP_LIFO
  829. X    VarMapNode    *head;                // first node
  830. X    VarMapNode    *tail;                // last node
  831. X    VarMapNode    *here;                // current node
  832. X    int        _size;                // size of Map
  833. X};
  834. X
  835. X#endif
  836. X
  837. END_OF_FILE
  838.   if test 2602 -ne `wc -c <'VarMap.H'`; then
  839.     echo shar: \"'VarMap.H'\" unpacked with wrong size!
  840.   fi
  841.   # end of 'VarMap.H'
  842. fi
  843. if test -f 'demo.C' -a "${1}" != "-c" ; then 
  844.   echo shar: Will not clobber existing file \"'demo.C'\"
  845. else
  846.   echo shar: Extracting \"'demo.C'\" \(824 characters\)
  847.   sed "s/^X//" >'demo.C' <<'END_OF_FILE'
  848. X#include    <stdlib.h>
  849. X#include    <stream.h>
  850. X#include    <var.H>
  851. X
  852. Xmain ()
  853. X{
  854. X    var value;                // declare an "untyped" var
  855. X    value = "hello world";            // initialize it
  856. X    cout << value << "\n";            // output "hello world"
  857. X    value = value(3, value.length() - 6);    // substring example
  858. X    cout << value << "\n";            // output "lo wo"
  859. X    value = 34;                // assignment of int
  860. X    value++;                // increment
  861. X    value += 42.375;            // add 42.375 to present value
  862. X    cout << value << "\n";            // output "77.375"
  863. X    cout << value.format("formatted output example %05d of value\n");
  864. X    cout << value.format("multiple outputs #1=%d, #2=%9.2f of value\n");
  865. X    value.null(2);                // truncate string
  866. X    value += " sunset strip";        // concatenate a string
  867. X    cout << value.format("%s\n");        // output "77 sunset strip"
  868. X    cout << value[3] << value[4] << value[5] << "\n"; // output "sun"
  869. X}
  870. X
  871. END_OF_FILE
  872.   if test 824 -ne `wc -c <'demo.C'`; then
  873.     echo shar: \"'demo.C'\" unpacked with wrong size!
  874.   fi
  875.   # end of 'demo.C'
  876. fi
  877. if test -f 'var.H' -a "${1}" != "-c" ; then 
  878.   echo shar: Will not clobber existing file \"'var.H'\"
  879. else
  880.   echo shar: Extracting \"'var.H'\" \(8847 characters\)
  881.   sed "s/^X//" >'var.H' <<'END_OF_FILE'
  882. X//
  883. X// NAME:    var.H
  884. X//
  885. X// PURPOSE:    C++ Generic "Universal Variable" Class Library Header File
  886. X// VERSION:    1.1
  887. X// AUTHOR:    Lee Hounshell
  888. X// LAST MOD:    Mon Nov 23 08:56:24 PST 1992
  889. X//
  890. X
  891. X#ifndef VAR_H
  892. X#define VAR_H
  893. X
  894. X#include    <stream.h>
  895. X#include    "string.h"
  896. X
  897. X// -----------------------------------------------------------------------------
  898. X//
  899. Xclass varsize {
  900. X    // dummy class needed for var constructor of specific size that
  901. X    // doesn't conflict with the var integer type constructor
  902. X    public:
  903. X    // Constructors & Destructors
  904. X    varsize(int sz);                // constructor
  905. X    int size;                    // size of var
  906. X};
  907. X
  908. Xclass subvar;
  909. X
  910. X// -----------------------------------------------------------------------------
  911. X//
  912. Xclass var {
  913. X
  914. X    public:
  915. X    // Standard Interface
  916. X    const var & type(void) const;            // who am i
  917. X
  918. X    // Constructors & Destructors
  919. X    var(void);                    // constructor
  920. X    var(const varsize &);                // constructor
  921. X    var(const char *);                // constructor
  922. X    var(const char);                // constructor
  923. X    var(const short);                // constructor
  924. X    var(const unsigned short);            // constructor
  925. X    var(const int);                    // constructor
  926. X    var(const unsigned int);            // constructor
  927. X    var(const long);                // constructor
  928. X    var(const unsigned long);            // constructor
  929. X    var(const double);                // constructor
  930. X    var(const var &);                // copy constructor
  931. X    ~var(void);                    // destructor
  932. X
  933. X    // Operators
  934. X    var & operator = (const char *);        // assignment
  935. X        var & operator = (const var &);            // assignment
  936. X        char & operator [] (const int);            // indexing
  937. X        subvar & operator () (int, int) const;        // substring
  938. X        subvar & operator () (const int) const;        // substring
  939. X        friend ostream & operator << (ostream &, const var &); // output
  940. X    friend class subvar;                // substring
  941. X
  942. X    // More Operators (used for arithmetic type extension)
  943. X    var & operator = (const char);            // assignment
  944. X    var & operator = (const short);            // assignment
  945. X    var & operator = (const unsigned short);    // assignment
  946. X    var & operator = (const int);            // assignment
  947. X    var & operator = (const unsigned int);        // assignment
  948. X    var & operator = (const long);            // assignment
  949. X    var & operator = (const unsigned long);        // assignment
  950. X    var & operator = (const double);        // assignment
  951. X
  952. X    // Casting Operators
  953. X    operator char * (void) const;            // type conversion
  954. X    operator double (void) const;            // type conversion
  955. X
  956. X    // Assignment Operators
  957. X        var & operator ++ (void);            // increment (pre-index only)
  958. X        var & operator -- (void);            // decrement (pre-index only)
  959. X        var operator ! (void) const;            // not
  960. X
  961. X        var operator + (const var &) const;        // addition OR concatenation
  962. X        var operator - (const var &) const;        // subtraction
  963. X        var operator * (const var &) const;        // multiplication
  964. X        var operator / (const var &) const;        // division
  965. X        var operator % (const var &) const;        // remainder
  966. X
  967. X        var & operator += (const var &);        // addition OR concatenation
  968. X        var & operator -= (const var &);        // subtraction
  969. X        var & operator *= (const var &);        // multiplication
  970. X        var & operator /= (const var &);        // division
  971. X        var & operator %= (const var &);        // remainder
  972. X
  973. X        var & operator += (const char &);        // addition OR concatenation
  974. X        var & operator += (const char *);        // addition OR concatenation
  975. X        var & operator -= (const char *);        // subtraction
  976. X        var & operator *= (const char *);        // multiplication
  977. X        var & operator /= (const char *);        // division
  978. X        var & operator %= (const char *);        // remainder
  979. X
  980. X        var & operator += (const double);        // addition OR concatenation
  981. X        var & operator -= (const double);        // subtraction
  982. X        var & operator *= (const double);        // multiplication
  983. X        var & operator /= (const double);        // division
  984. X        var & operator %= (const double);        // remainder
  985. X
  986. X    friend var operator + (const char *, const var &); // addition OR concatenation
  987. X    friend var operator - (const char *, const var &); // subtraction
  988. X    friend var operator * (const char *, const var &); // multiplication
  989. X    friend var operator / (const char *, const var &); // division
  990. X    friend var operator % (const char *, const var &); // remainder
  991. X    friend var operator + (const var &, const char *); // addition OR concatenation
  992. X    friend var operator - (const var &, const char *); // subtraction
  993. X    friend var operator * (const var &, const char *); // multiplication
  994. X    friend var operator / (const var &, const char *); // division
  995. X    friend var operator % (const var &, const char *); // remainder
  996. X
  997. X    friend var operator + (const var &, const double); // addition OR concatenation
  998. X    friend var operator - (const var &, const double); // subtraction
  999. X    friend var operator * (const var &, const double); // multiplication
  1000. X    friend var operator / (const var &, const double); // division
  1001. X    friend var operator % (const var &, const double); // remainder
  1002. X    friend var operator + (const double, const var &); // addition OR concatenation
  1003. X    friend var operator - (const double, const var &); // subtraction
  1004. X    friend var operator * (const double, const var &); // multiplication
  1005. X    friend var operator / (const double, const var &); // division
  1006. X    friend var operator % (const double, const var &); // remainder
  1007. X
  1008. X    // Equality Operators
  1009. X        int operator == (const var &) const;        // equality
  1010. X        int operator != (const var &) const;        // inequality
  1011. X        int operator < (const var &) const;        // less than
  1012. X        int operator > (const var &) const;        // greater than
  1013. X        int operator <= (const var &) const;        // less than or equal
  1014. X        int operator >= (const var &) const;        // greater than or equal
  1015. X
  1016. X    friend int operator == (const var &, const char *); // equality
  1017. X    friend int operator != (const var &, const char *); // inequality
  1018. X    friend int operator < (const var &, const char *);  // less than
  1019. X    friend int operator > (const var &, const char *);  // greater than
  1020. X    friend int operator <= (const var &, const char *); // less than or equal
  1021. X    friend int operator >= (const var &, const char *); // greater than or equal
  1022. X    friend int operator == (const char *, const var &); // equality
  1023. X    friend int operator != (const char *, const var &); // inequality
  1024. X    friend int operator < (const char *, const var &);  // less than
  1025. X    friend int operator > (const char *, const var &);  // greater than
  1026. X    friend int operator <= (const char *, const var &); // less than or equal
  1027. X    friend int operator >= (const char *, const var &); // greater than or equal
  1028. X
  1029. X    friend int operator == (const var &, const double); // equality
  1030. X    friend int operator != (const var &, const double); // inequality
  1031. X    friend int operator < (const var &, const double);  // less than
  1032. X    friend int operator > (const var &, const double);  // greater than
  1033. X    friend int operator <= (const var &, const double); // less than or equal
  1034. X    friend int operator >= (const var &, const double); // greater than or equal
  1035. X    friend int operator == (const double, const var &); // equality
  1036. X    friend int operator != (const double, const var &); // inequality
  1037. X    friend int operator < (const double, const var &);  // less than
  1038. X    friend int operator > (const double, const var &);  // greater than
  1039. X    friend int operator <= (const double, const var &); // less than or equal
  1040. X    friend int operator >= (const double, const var &); // greater than or equal
  1041. X
  1042. X    // Custom Interface
  1043. X    void null(int);                    // "null" out string
  1044. X    void changesize(int);                // change allocated memory
  1045. X    int length(void) const;                // length of string
  1046. X    const char * vartype(void) const;        // name of this var type
  1047. X    void change_type(const char *);            // change var type
  1048. X    int is_string(void) const;            // test var type
  1049. X    int is_double(void) const;            // test var type
  1050. X    int is_long(void) const;            // test var type
  1051. X    int strchr(const char) const;            // strchr(char) index
  1052. X    int strrchr(const char) const;            // strrchr(char) index
  1053. X    var & concat(const var &);            // concatenation
  1054. X    var & format(const char *);            // set output format
  1055. X    void print(ostream &) const;            // output
  1056. X
  1057. X    protected:
  1058. X
  1059. X    private:
  1060. X    int    numcheck(const char *) const;        // determine is_numeric value
  1061. X
  1062. X    short    fixed;                    // 1=fixed data type
  1063. X    short    is_numeric;                // 0=string, 1=short/int/long, 2=float/double
  1064. X    int    data_str_len;                // length of allocated "string"
  1065. X    int    data_str_end;                // index to current "end" of string
  1066. X    char    *data_str;                // for "string" data
  1067. X    char    *format_str;                // the output format string
  1068. X
  1069. X};
  1070. X
  1071. X
  1072. Xclass subvar : public var {
  1073. X    public:
  1074. X        var & operator = (const var &);            // substring replacement
  1075. X    var        *varptr;
  1076. X    unsigned    offset;
  1077. X    unsigned    length;
  1078. X};
  1079. X
  1080. X
  1081. X// global conversion functions
  1082. Xchar *itoa(const int foo);
  1083. Xchar *uitoa(const unsigned int foo);
  1084. Xchar *dtoa(const double foo);
  1085. Xdouble atod(char * foo);
  1086. X
  1087. X#ifdef HP
  1088. X    char *ltoa(long foo);
  1089. X    char *ultoa(unsigned long foo);
  1090. X#else
  1091. X    char *ltoa(const long foo);
  1092. X    char *ultoa(const unsigned long foo);
  1093. X#endif
  1094. X
  1095. X
  1096. X#endif
  1097. X
  1098. END_OF_FILE
  1099.   if test 8847 -ne `wc -c <'var.H'`; then
  1100.     echo shar: \"'var.H'\" unpacked with wrong size!
  1101.   fi
  1102.   # end of 'var.H'
  1103. fi
  1104. echo shar: End of archive 2 \(of 2\).
  1105. cp /dev/null ark2isdone
  1106. MISSING=""
  1107. for I in 1 2 ; do
  1108.     if test ! -f ark${I}isdone ; then
  1109.     MISSING="${MISSING} ${I}"
  1110.     fi
  1111. done
  1112. if test "${MISSING}" = "" ; then
  1113.     echo You have unpacked both archives.
  1114.     rm -f ark[1-9]isdone
  1115. else
  1116.     echo You still must unpack the following archives:
  1117.     echo "        " ${MISSING}
  1118. fi
  1119. exit 0
  1120. exit 0 # Just in case...
  1121.