Implemented charmap stack

New commands:
 .PUSHCHARMAP: will push the current charmap state into an internal stack
 .POPCHARMAP: will restore the current charmap to the last pushed charmap

Details:
  The push and pop facilities are implemented directly inside the tgttrans.h,
  to facilitate its reuse on the C compiler.
This commit is contained in:
Marco Aurelio da Costa
2021-04-16 14:42:16 -03:00
committed by Oliver Schmidt
parent ffc30c0c6e
commit c915b5d7f3
5 changed files with 101 additions and 0 deletions

View File

@@ -39,6 +39,8 @@
#include "check.h"
#include "target.h"
#include "tgttrans.h"
#include "coll.h"
#include "xmalloc.h"
@@ -68,6 +70,9 @@ static unsigned char Tab[256] = {
0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF,
};
#define MAX_CHARMAP_STACK 16
static Collection CharmapStack = STATIC_COLLECTION_INITIALIZER;
/*****************************************************************************/
@@ -76,6 +81,8 @@ static unsigned char Tab[256] = {
void TgtTranslateInit (void)
/* Initialize the translation tables */
{
@@ -127,3 +134,52 @@ void TgtTranslateSet (unsigned Index, unsigned char C)
CHECK (Index < sizeof (Tab));
Tab[Index] = C;
}
int TgtTranslatePush (void)
/* Pushes the current translation table to the internal stack
** Returns 1 on success, 0 on stack full
*/
{
unsigned char* TempTab;
if (CollCount (&CharmapStack) >= MAX_CHARMAP_STACK) {
return 0;
}
TempTab = xmalloc (sizeof (Tab));
memcpy (TempTab, Tab, sizeof (Tab));
CollAppend (&CharmapStack, TempTab);
return 1;
}
int TgtTranslatePop (void)
/* Pops a translation table from the internal stack into the current table
** Returns 1 on success, 0 on stack empty
*/
{
unsigned char* TempTab;
if (CollCount (&CharmapStack) == 0) {
return 0;
}
TempTab = CollPop (&CharmapStack);
memcpy (Tab, TempTab, sizeof (Tab));
xfree (TempTab);
return 1;
}
int TgtTranslateStackIsEmpty (void)
/* Returns 1 if the internal stack is empty */
{
return CollCount (&CharmapStack) == 0;
}

View File

@@ -70,6 +70,19 @@ void TgtTranslateStrBuf (StrBuf* Buf);
void TgtTranslateSet (unsigned Index, unsigned char C);
/* Set the translation code for the given character */
int TgtTranslatePush (void);
/* Pushes the current translation table to the internal stack
** Returns 1 on success, 0 on stack full
*/
int TgtTranslatePop (void);
/* Pops a translation table from the internal stack into the current table
** Returns 1 on success, 0 on stack empty
*/
int TgtTranslateStackIsEmpty (void);
/* Returns 1 if the internal stack is empty */
/* End of tgttrans.h */