www.pudn.com > HEC-linux.zip > strtbl.cpp


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
+                                                                   + 
+ strtbl.cpp - the string table ( init, add elements, print )       + 
+                                                                   + 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
+ Macros                                                            +                                                                   + 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 
 
#define STR_TBL_INIT  2*1024	/*initial size of string table*/ 
#define STR_TBL_INC	  1024		/*amount increment when expand*/ 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
+ declaration                                                       + 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 
 
class StringTable 
{ 
	U8 nStr;	/*current capacity*/ 
	 
	public: 
	U1 *text; 
	U8 iStr;	/*next free space*/ 
 
	StringTable(); 
	~StringTable(); 
	void addStrTbl(char *str); 
	void printStrTbl(); 
}; 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
+ definitions                                                       + 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 
 
StringTable::StringTable() 
{ 
	text = (U1*)malloc(STR_TBL_INIT); 
	if(text==NULL) 
	{ 
		ERROR0("StringTable::StringTable(): out of memory\n"); 
		FATAL_ERROR(); 
	} 
	iStr = 0; 
	nStr = STR_TBL_INIT; 
	return; 
 
}/*end constructor*/ 
 
/*-----------------------------------------------------------------*/ 
 
StringTable::~StringTable() 
{ 
	free(text); 
	return; 
 
}/*end destructor*/ 
 
/*-----------------------------------------------------------------*/ 
 
/* 
add string to strTbl ( re-size if necessary ) 
*/ 
 
void StringTable::addStrTbl(char *str) 
{ 
	U8 len; 
	U8 i; 
	U8 final_index; 
	U1 *ptr; 
 
	len = strlen(str);	/*note: null not included*/ 
	len++;				/*count null*/ 
	final_index = iStr + len - 1; 
 
	/* capacity n will have indices 0,...,n-1 */ 
 
	if(final_index >= nStr) 
	{ 
		ptr = (U1*)malloc((size_t)(nStr+STR_TBL_INC)); 
		if(ptr==NULL) 
		{ 
			ERROR0("StringTable::addStrTbl(): no more memory\n"); 
			FATAL_ERROR(); 
		} 
		else 
		{ 
			for(i=0;i