Source code for /engineering/bos/rewrite/LABEL.CPPOriginal file LABEL.CPP
   1 /*
   2 	BattleOS : Engage battling programs.
   3 
   4 	Copyright (C) 1993 Erich P Gatejen    ALL RIGHTS RESERVED
   5 
   6 
   7 	File     : LABEL.CPP
   8 	Purpose  : Program code for the BattleOS Assembler
   9 
  10 	!! Program code for the label list class members !!
  11 	!! Must be linked to the BOA				    !!
  12 
  13 */
  14 
  15 
  16 // --------------------------------------------------------------------------
  17 // ---- INCLUDE files -------------------------------------------------------
  18 #include<stdlib.h>
  19 #include<alloc.h>
  20 #include<ctype.h>
  21 #include<string.h>
  22 #include "defines.h"
  23 #include "label.h"
  24 
  25 
  26 // --------------------------------------------------------------------------
  27 // ---- Global Variables ----------------------------------------------------
  28 // NONE
  29 
  30 
  31 // --------------------------------------------------------------------------
  32 // |||| Member function code ||||||||||||||||||||||||||||||||||||||||||||||||
  33 // --------------------------------------------------------------------------
  34 
  35 // <<-- CLASS  : ListLabel ------------------------------------------------>>
  36 
  37 // ---- MEMBER : constructor ----
  38    LabelList::LabelList( void ) {
  39 
  40    // Point to nothing!
  41    Head = NULL;
  42    Tail = NULL;
  43 
  44 }; // end constructor
  45 
  46 
  47 // ---- MEMBER : destructor ----
  48    LabelList::~LabelList( void ) {
  49 
  50    // Release all the nodes back to the heap
  51    while ( Head != NULL ) {
  52 
  53 	Tail = Head; 		// Save this node to delete
  54 	Head = Head->Next;  // Point to the next node
  55 
  56 	delete Tail;		// Delete the node
  57 
  58    }; // end while
  59 
  60 }; // end destructor
  61 
  62 
  63 // ---- MEMBER : AddLabel ----
  64 LabelResult  LabelList::AddLabel ( char**  StringPointer, int  Number ) {
  65 
  66    // Local var
  67    char*	String   = *StringPointer;	// Our string pointer
  68    char	Copy[32];					// Our copy of the text
  69    char	ValueP[7];				// Our copy of the text
  70    int	Step;					// General iterator
  71    int	Value;					// Value to place
  72    int	Sign;					// Sign Value
  73 
  74    Label* Node;					// Working node pointer
  75 
  76    // Load the local copy of the string
  77    Step = 0;
  78    while ( !isspace(*String) ) {             // Loop until white space
  79 	 if ( Step < 31 ) Copy[Step] = *String; // Only copy is room left
  80 	 ++String;
  81 	 ++Step;
  82    }; // end while
  83    Copy[Step] = NULL;	// Null terminate the string
  84 
  85    // Point String to anything but white space
  86    while ( isspace(*String) ) { ++String; };
  87 
  88    // Is this a explicit label set ( '=' char )
  89    if ( *String == '=' ) {
  90 
  91     // --- Yes, then pick the value
  92 	 ++String;
  93 	 // Find sign value or number; if none, then error
  94 	 while( isspace(*String) ) { ++String; };
  95 	 if (*String != '-') {          // minus sign
  96 	    Sign = -1;
  97 	    ++String;
  98 	 } else if (*String != '+') {   // positive sign
  99 	    Sign = 1;
 100 	    ++String;
 101 	 } else if (!isdigit(*String))  // digit, assume positive
 102 	    Sign = 1;
 103 	 else
 104 	    return BADLABELSET;	       // !!Last case!! ERROR
 105 
 106 	 // Get the number text
 107 	 Step = 0;
 108 	 while ( !isspace(*String) ) {               // Loop until white space
 109 	    if ( Step == 5 ) return VALUEOUTOFRANGE; // Too large ?
 110 	    ValueP[Step] = *String;
 111 	    ++String;
 112 	    ++Step;
 113 	 }; // end while
 114 	 ValueP[Step] = NULL;	// Null terminate the string
 115 
 116 	 Value = atoi( ValueP );
 117 	 Value = Value * Sign;
 118 
 119 	 // Make sure the value is in range.  If not, return error
 120 	 if ( (Value < INTLOW) || (Value > INTHI) ) return VALUEOUTOFRANGE;
 121 
 122    } else {
 123 
 124 	 // Value is the number passed into the function
 125 	 Value = Number;
 126 
 127    }; // end if
 128 
 129    // Return string pointer
 130    *StringPointer = String;
 131 
 132    // Is it the first label?
 133    if ( Head == NULL ) {
 134 
 135     // -- Yes!
 136 
 137 	 // Add it to the list as the first node
 138 	 Node = new Label;
 139 	 Tail = Node;
 140 	 Head = Node;
 141 
 142    } else {
 143 
 144     // -- No!
 145 
 146 	 // Traverse the list and see if it is a duplicate label
 147 	 Node = Head;      // Point to first node
 148 	 do {
 149 	   // check for duplication
 150 	   if ( strcmp(Copy, Node->Text) == 0 ) {
 151 		 return DUPLABEL;					// Dup found!, return
 152 	   }; // end if
 153 
 154 	   // Move to next node
 155 	   Node = Node->Next;
 156 
 157 	 } while ( Node != NULL ); // end while whil no more nodes
 158 
 159 	 // No duplicate.  So link in this new label
 160 	 Node 	  = new Label;
 161 	 Node->Next = NULL;
 162 
 163 	 Tail->Next = Node;	// Link last to new
 164 	 Tail	  = Node; // Point tail to new
 165 
 166    }; // end if first
 167 
 168    // Ok, stuff the data
 169    Node->Value = Value;
 170    strcpy(Node->Text, Copy );
 171 
 172    // DONE!! return no error
 173    return NOERROR;
 174 
 175 
 176 }; // end AddLabel
 177 
 178 
 179 // ---- MEMBER : ExtractLabel ----
 180 LabelResult  LabelList::ExtractLabel( char**  StringPointer, int&   Value  ) {
 181 
 182    // Local var
 183    Label*	Node;
 184    char		Copy[32];
 185    char*	String = *StringPointer;
 186    int		Step;
 187 
 188    // Load the local copy of the string
 189    Step = 0;
 190    while ( !isspace(*String) ) {             // Loop until white space
 191 	 if ( Step < 31 ) Copy[Step] = *String; // Only copy is room left
 192 	 ++String;
 193 	 ++Step;
 194    }; // end while
 195    Copy[Step] = NULL;	// Null terminate the string
 196 
 197    // Point String to anything but white space
 198    while ( isspace(*String) ) { ++String; };
 199 
 200    // Traverse the list and try to find the label.
 201    Node = Head;
 202    while ( Node ) {
 203 
 204 	 // check for duplication
 205 	 if ( strcmp(Copy, Node->Text) == 0 ) {
 206 	    // We found it
 207 
 208 	    // Get the data
 209 	    Value = Node->Value;
 210 
 211 	    // Return good
 212 	    return NOERROR;
 213 
 214 	 }; // end if
 215 
 216 	 // move to next node
 217 	 Node = Node->Next;    // Point to next
 218 
 219    }; // end while
 220 
 221    // If we got here, then the label wasn't found.  ERROR
 222    return LABELNOTFOUND;
 223 
 224 }; // end ExtractLabel
 225 
 226 
 227 
 228