File: pf_text.c

package info (click to toggle)
pforth 21-11
  • links: PTS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 820 kB
  • ctags: 873
  • sloc: ansic: 5,050; makefile: 102
file content (297 lines) | stat: -rw-r--r-- 7,418 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* @(#) pf_text.c 98/01/26 1.3 */
/***************************************************************
** Text Strings for Error Messages
** Various Text tools.
**
** For PForth based on 'C'
**
** Author: Phil Burk
** Copyright 1994 3DO, Phil Burk, Larry Polansky, Devid Rosenboom
**
** The pForth software code is dedicated to the public domain,
** and any third party may reproduce, distribute and modify
** the pForth software code or any derivative works thereof
** without any compensation or license.  The pForth software
** code is provided on an "as is" basis without any warranty
** of any kind, including, without limitation, the implied
** warranties of merchantability and fitness for a particular
** purpose and their equivalents under the laws of any jurisdiction.
**
****************************************************************
** 19970702 PLB Fixed ConvertNumberToText for unsigned numbers.
** 19980522 PLB Expand PAD for ConvertNumberToText so "-1 binary .s" doesn't crash.
***************************************************************/

#include "pf_all.h"

#define PF_ENGLISH

/*
** Define array of error messages.
** These are defined in one place to make it easier to translate them.
*/
#ifdef PF_ENGLISH
/***************************************************************/
void pfReportError( const char *FunctionName, Err ErrCode )
{
	const char *s;
	
	MSG("Error in ");
	MSG(FunctionName);
	MSG(" - ");
	
	switch(ErrCode & 0xFF)
	{
	case PF_ERR_NO_MEM & 0xFF:
		s = "insufficient memory"; break;
	case PF_ERR_BAD_ADDR & 0xFF:
		s = "address misaligned"; break;
	case PF_ERR_TOO_BIG & 0xFF:
		s = "data chunk too large"; break;
	case PF_ERR_NUM_PARAMS & 0xFF:
		s = "incorrect number of parameters"; break;
	case PF_ERR_OPEN_FILE & 0xFF:
		s = "could not open file"; break;
	case PF_ERR_WRONG_FILE & 0xFF:
		s = "wrong type of file format"; break;
	case PF_ERR_BAD_FILE & 0xFF:
		s = "badly formatted file"; break;
	case PF_ERR_READ_FILE & 0xFF:
		s = "file read failed"; break;
	case PF_ERR_WRITE_FILE & 0xFF:
		s = "file write failed"; break;
	case PF_ERR_CORRUPT_DIC & 0xFF:
		s = "corrupted dictionary"; break;
	case PF_ERR_NOT_SUPPORTED & 0xFF:
		s = "not supported in this version"; break;
	case PF_ERR_VERSION_FUTURE & 0xFF:
		s = "version from future"; break;
	case PF_ERR_VERSION_PAST & 0xFF:
		s = "version is obsolete. Rebuild new one."; break;
	case PF_ERR_COLON_STACK & 0xFF:
		s = "stack depth changed between : and ; . Probably unbalanced conditional"; break;
	case PF_ERR_HEADER_ROOM & 0xFF:
		s = "no room left in header space"; break;
	case PF_ERR_CODE_ROOM & 0xFF:
		s = "no room left in code space"; break;
	case PF_ERR_NO_SHELL & 0xFF:
		s = "attempt to use names in forth compiled with PF_NO_SHELL"; break;
	case PF_ERR_NO_NAMES & 0xFF:
		s = "dictionary has no names";  break;
	case PF_ERR_OUT_OF_RANGE & 0xFF:
		s = "parameter out of range";  break;
	case PF_ERR_ENDIAN_CONFLICT & 0xFF:
		s = "endian-ness of dictionary does not match code";  break;
	case PF_ERR_FLOAT_CONFLICT & 0xFF:
		s = "float support mismatch between .dic file and code";  break;
	default:
		s = "unrecognized error code!"; break;
	}
	MSG(s);
	EMIT_CR;
}
#endif

/**************************************************************
** Copy a Forth String to a 'C' string.
*/

char *ForthStringToC( char *dst, const char *FString )
{
	int32 Len;

	Len = (int32) *FString;
	pfCopyMemory( dst, FString+1, Len );
	dst[Len] = '\0';

	return dst;
}

/**************************************************************
** Copy a NUL terminated string to a Forth counted string.
*/
char *CStringToForth( char *dst, const char *CString )
{
	char *s;
	int32 i;

	s = dst+1;
	for( i=0; *CString; i++ )
	{
		*s++ = *CString++;
	}
	*dst = (char ) i;
	return dst;
}

/**************************************************************
** Compare two test strings, case sensitive.
** Return TRUE if they match.
*/
int32 ffCompareText( const char *s1, const char *s2, int32 len )
{
	int32 i, Result;
	
	Result = TRUE;
	for( i=0; i<len; i++ )
	{
DBUGX(("ffCompareText: *s1 = 0x%x, *s2 = 0x%x\n", *s1, *s2 ));
		if( *s1++ != *s2++ )
		{
			Result = FALSE;
			break;
		}
	}
DBUGX(("ffCompareText: return 0x%x\n", Result ));
	return Result;
}

/**************************************************************
** Compare two test strings, case INsensitive.
** Return TRUE if they match.
*/
int32 ffCompareTextCaseN( const char *s1, const char *s2, int32 len )
{
	int32 i, Result;
	char  c1,c2;
	
	Result = TRUE;
	for( i=0; i<len; i++ )
	{
		c1 = pfCharToLower(*s1++);
		c2 = pfCharToLower(*s2++);
DBUGX(("ffCompareText: c1 = 0x%x, c2 = 0x%x\n", c1, c2 ));
		if( c1 != c2 )
		{
			Result = FALSE;
			break;
		}
	}
DBUGX(("ffCompareText: return 0x%x\n", Result ));
	return Result;
}

/**************************************************************
** Compare two strings, case sensitive.
** Return zero if they match, -1 if s1<s2, +1 is s1>s2;
*/
int32 ffCompare( const char *s1, int32 len1, const char *s2, int32 len2 )
{
	int32 i, result, n, diff;
	
	result = 0;
	n = MIN(len1,len2);
	for( i=0; i<n; i++ )
	{
		if( (diff = (*s2++ - *s1++)) != 0 )
		{
			result = (diff > 0) ? -1 : 1 ;
			break;
		}
	}
	if( result == 0 )  /* Match up to MIN(len1,len2) */
	{
		if( len1 < len2 )
		{
			result = -1;
		}
		else if ( len1 > len2 )
		{
			result = 1;
		}
	}
	return result;
}

/***************************************************************
** Convert number to text.
*/
#define CNTT_PAD_SIZE ((sizeof(int32)*8)+2)  /* PLB 19980522 - Expand PAD so "-1 binary .s" doesn't crash. */
static char cnttPad[CNTT_PAD_SIZE];

char *ConvertNumberToText( int32 Num, int32 Base, int32 IfSigned, int32 MinChars )
{
	int32 IfNegative = 0;
	char *p,c;
	uint32 NewNum, Rem, uNum;
	int32 i = 0;
	
	uNum = Num;
	if( IfSigned )
	{
/* Convert to positive and keep sign. */
		if( Num < 0 )
		{
			IfNegative = TRUE;
			uNum = -Num;
		}
	}
	
/* Point past end of Pad */
	p = cnttPad + CNTT_PAD_SIZE;
	*(--p) = (char) 0; /* NUL terminate */
	
	while( (i++<MinChars) || (uNum != 0) )
	{
		NewNum = uNum / Base;
		Rem = uNum - (NewNum * Base);
		c = (char) (( Rem < 10 ) ? (Rem + '0') : (Rem - 10 + 'A'));
		*(--p) = c;
		uNum = NewNum;
	}
	
	if( IfSigned )
	{
		if( IfNegative ) *(--p) = '-';
	}
	return p;
}

/***************************************************************
** Diagnostic routine that prints memory in table format.
*/
void DumpMemory( void *addr, int32 cnt)
{
	int32 ln, cn, nlines;
	unsigned char *ptr, *cptr, c;

	nlines = (cnt + 15) / 16;

	ptr = (unsigned char *) addr;

	EMIT_CR;
	
	for (ln=0; ln<nlines; ln++)
	{
		MSG( ConvertNumberToText( (int32) ptr, 16, FALSE, 8 ) );
		MSG(": ");
		cptr = ptr;
		for (cn=0; cn<16; cn++)
		{
			MSG( ConvertNumberToText( (int32) *cptr++, 16, FALSE, 2 ) );
			EMIT(' ');
		}
		EMIT(' ');
		for (cn=0; cn<16; cn++)
		{
			c = *ptr++;
			if ((c < ' ') || (c > '}')) c = '.';
			EMIT(c);
		}
		EMIT_CR;
	}
}


/* Print name, mask off any dictionary bits. */
void TypeName( const char *Name )
{
	const char *FirstChar;
	int32 Len;
	
	FirstChar = Name+1;
	Len = *Name & 0x1F;
	
	ioType( FirstChar, Len );
}