home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
pccts.zip
/
pccts
/
EXTEND.nonprinting.char
< prev
next >
Wrap
Text File
|
1994-03-31
|
5KB
|
223 lines
Printing non-printing chars for syntax errors
for ANTLR 1.10 (and hopefully 1.20)
by
tmoog@polhode.com (Tom Moog)
/******************************************************************************/
/*
zzexpand_text
For use with pccts
No copyright restrictions
No liability accepted
Copies a string from the source to the destination while replacing
recognized characters with a string from a table. Non-printable
characters NOT found in the table are expanded with a string
composed of their octal equivalent. If the destination string
is not long enough to contain the expanded string a "..." is
appended to the output.
Returns a pointer to the destination string.
Example using the default table:
"abc" -> "abc"
"abc\n" -> "abc<newline>"
"abc\n\fdef" -> "abc<newline>
<form feed>
def"
"abc\tdef" -> "abc<tab>def"
"abc\1\2\177" -> "abc<\001><\002><delete>"
Prototype:
char *zzexpand_text (char *source,
Zzexpand_text_entry *table,
char *dest,
size_t ldest)
If the table is coded as 0 then the default table is used
*/
/*
Initial Release: 9-Mar-94
*/
#include <string.h>
#include <ctype.h>
#include "expandtext.h"
#if __STDC__
#include <limits.h>
#define ZZCHAR_MASK UCHAR_MAX
#else
#define ZZCHAR_MASK 255
#endif
Zzexpand_text_entry
zzdefault_expand_text_table[]={
#ifdef __STDC__
{'\a' ,"<bell>"},
#else
{'\007' ,"<bell>"},
#endif
{'\b' ,"<back space>"},
{'\t' ,"<tab>"},
{'\n' ,"<newline>\n"},
{'\v' ,"<vtab>"},
{'\f' ,"\n<form feed>\n"},
{'\r' ,"<return>\n"},
{'\033' ,"<esc>"},
{'\177' ,"<del>"},
{0 ,0},
};
Zzexpand_text_entry (*zzexpand_text_table)[]=0;
/******************************************************************************/
#ifdef __STDC__
char * zzexpand_text (char *source,
Zzexpand_text_entry *table,
char *dest,
size_t ldest)
#else
char * zzexpand_text (source,
table,
dest,
ldest)
char *source;
Zzexpand_text_entry *table;
char *dest;
size_t ldest;
#endif
{
static Zzexpand_text_entry *current_table;
static char *d;
static Zzexpand_text_entry *next;
static int amount_left;
static char *replace;
static int extra_newline;
static char char_as_octal[12];
static int l;
static char dot_dot_dot[]=" ...";
extra_newline=0;
d=dest;
amount_left=ldest-sizeof(dot_dot_dot)-5;
/* Leave room for " ..." + null */
if (dest==0) return dest;
if (source==0) {
*dest=0;
return dest;
};
if (ldest <= 0) return dest;
if (amount_left <=0) {
strncpy(dest,dot_dot_dot,ldest);
dest[ldest-1]=0;
return dest;
};
if (table==0) {
current_table=zzdefault_expand_text_table;
} else {
current_table=table;
};
for ( ;*source!=0;source++) {
if (amount_left <= 0) {
amount_left=(-1);
break;
};
/* first search the table then check isprint so that user can
choose how to represent a space character or anyother printable
character */
next=current_table;
for ( ;(next->text!=0) &&
(next->match!=*source);
next++) { continue; };
/* not in table: */
/* if printable copy directly to the destination */
/* otherwise encode as octal character */
if (next->text==0) {
if (isprint(*source)) {
extra_newline=0; /* printable */
*d++=*source;
amount_left--;
} else { /* not printable */
sprintf (char_as_octal,"<\\%.3o>",(*source) & ZZCHAR_MASK);
l=strlen(char_as_octal);
amount_left-=l;
if (amount_left < 0) {
break;
};
strcpy(d,char_as_octal);
d+=l;
};
} else { /* in table */
replace=next->text;
l=strlen(replace);
amount_left-=l;
if (amount_left < 0) {
break;
};
/* check for two artificial newlines in a row */
if (*replace=='\n') { /* does expansion text start with \n */
if (d==dest) { /* don't insert \n at start */
replace++;
amount_left--;
l--;
};
if (extra_newline) { /* don't insert two \n */
d--;
amount_left++;
};
};
strcpy (d,replace);
d+=l;
if (replace[l-1]=='\n') {
extra_newline=1;
} else {
extra_newline=0;
};
};
};
if (extra_newline) {
d--;
};
/* Don't get alarmed - remember that some extra space was reserved */
if (amount_left < 0) {
strcpy (d,dot_dot_dot);
d+=strlen(dot_dot_dot);
};
*d=0;
return dest;
}