File: mapm_set.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (357 lines) | stat: -rw-r--r-- 6,987 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357

/*
 *  M_APM  -  mapm_set.c
 *
 *  Copyright (C) 1999 - 2007   Michael C. Ring
 *
 *  Permission to use, copy, and distribute this software and its
 *  documentation for any purpose with or without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and
 *  that both that copyright notice and this permission notice appear
 *  in supporting documentation.
 *
 *  Permission to modify the software is granted. Permission to distribute
 *  the modified code is granted. Modifications are to be distributed by
 *  using the file 'license.txt' as a template to modify the file header.
 *  'license.txt' is available in the official MAPM distribution.
 *
 *  This software is provided "as is" without express or implied warranty.
 */

/*
 *
 *      This file contains the functions necessary to get C 'longs' and
 *	'strings' into the MAPM number system. It also contains the function
 *	to get a string from a MAPM number.
 *
 */

#include "pgAdmin3.h"
#include "pgscript/utilities/mapm-lib/m_apm_lc.h"

static	char *M_buf  = NULL;
static  int   M_lbuf = 0;
static  const char *M_set_string_error_msg = "\'m_apm_set_string\', Out of memory";

/****************************************************************************/
void	M_free_all_set()
{
	if (M_lbuf != 0)
	{
		MAPM_FREE(M_buf);
		M_buf  = NULL;
		M_lbuf = 0;
	}
}
/****************************************************************************/
void	m_apm_set_long(M_APM atmp, long mm)
{
	int     len, ii, nbytes;
	char	*p, *buf, ch, buf2[64];

	/* if zero, return right away */

	if (mm == 0)
	{
		M_set_to_zero(atmp);
		return;
	}

	M_long_2_ascii(buf2, mm);     /* convert long -> ascii in base 10 */
	buf = buf2;

	if (mm < 0)
	{
		atmp->m_apm_sign = -1;
		buf++;                     /* get past '-' sign */
	}
	else
	{
		atmp->m_apm_sign = 1;
	}

	len = strlen(buf);
	atmp->m_apm_exponent = len;

	/* least significant nibble of ODD data-length must be 0 */

	if ((len & 1) != 0)
	{
		buf[len] = '0';
	}

	/* remove any trailing '0' ... */

	while (TRUE)
	{
		if (buf[--len] != '0')
			break;
	}

	atmp->m_apm_datalength = ++len;

	nbytes = (len + 1) >> 1;
	p = buf;

	for (ii = 0; ii < nbytes; ii++)
	{
		ch = *p++ - '0';
		atmp->m_apm_data[ii] = 10 * ch + *p++ - '0';
	}
}
/****************************************************************************/
void	m_apm_set_string(M_APM ctmp, const char *s_in)
{
	char	ch, *cp, *s, *p;
	void	*vp;
	int	i, j, zflag, exponent, sign;

	if (M_lbuf == 0)
	{
		M_lbuf = 256;
		if ((M_buf = (char *)MAPM_MALLOC(256)) == NULL)
		{
			/* fatal, this does not return */

			M_apm_log_error_msg(M_APM_FATAL, M_set_string_error_msg);
		}
	}

	if ((i = strlen(s_in)) > (M_lbuf - 4))
	{
		M_lbuf = i + 32;
		if ((vp = MAPM_REALLOC(M_buf, M_lbuf)) == NULL)
		{
			/* fatal, this does not return */

			M_apm_log_error_msg(M_APM_FATAL, M_set_string_error_msg);
		}

		M_buf = (char *)vp;
	}

	s = M_buf;
	strcpy(s, s_in);

	/* default == zero ... */

	M_set_to_zero(ctmp);

	p = s;

	while (TRUE)
	{
		if (*p == ' ' || *p == '\t')
			p++;
		else
			break;
	}

	if (*p == '\0')
		return;

	sign = 1;             /* assume number is positive */

	if (*p == '+')        /* scan by optional '+' sign */
		p++;
	else
	{
		if (*p == '-')     /* check if number negative */
		{
			sign = -1;
			p++;
		}
	}

	M_lowercase(p);       /* convert string to lowercase */
	exponent = 0;         /* default */

	if ((cp = strstr(p, "e")) != NULL)
	{
		exponent = atoi(cp + sizeof(char));
		*cp = '\0';          /* erase the exponent now */
	}

	j = M_strposition(p, (char *) ".");        /* is there a decimal point ?? */
	if (j == -1)
	{
		strcat(p, ".");               /* if not, append one */
		j = M_strposition(p, (char *) ".");     /* now find it ... */
	}

	if (j > 0)                       /* normalize number and adjust exponent */
	{
		exponent += j;
		memmove((p + 1), p, (j * sizeof(char)));
	}

	p++;        /* scan past implied decimal point now in column 1 (index 0) */

	i = strlen(p);
	ctmp->m_apm_datalength = i;

	if ((i & 1) != 0)   /* if odd number of digits, append a '0' to make it even */
		strcat(p, "0");

	j = strlen(p) >> 1;  /* number of bytes in encoded M_APM number */

	/* do we need more memory to hold this number */

	if (j > ctmp->m_apm_malloclength)
	{
		if ((vp = MAPM_REALLOC(ctmp->m_apm_data, (j + 32))) == NULL)
		{
			/* fatal, this does not return */

			M_apm_log_error_msg(M_APM_FATAL, M_set_string_error_msg);
		}

		ctmp->m_apm_malloclength = j + 28;
		ctmp->m_apm_data = (UCHAR *)vp;
	}

	zflag = TRUE;

	for (i = 0; i < j; i++)
	{
		ch = *p++ - '0';
		if ((ch = (10 * ch + *p++ - '0')) != 0)
			zflag = FALSE;

		if (((int)ch & 0xFF) >= 100)
		{
			M_apm_log_error_msg(M_APM_RETURN,
			                    "\'m_apm_set_string\', Non-digit char found in parse");

			M_apm_log_error_msg(M_APM_RETURN, "Text =");
			M_apm_log_error_msg(M_APM_RETURN, s_in);

			M_set_to_zero(ctmp);
			return;
		}

		ctmp->m_apm_data[i]   = ch;
		ctmp->m_apm_data[i + 1] = 0;
	}

	ctmp->m_apm_exponent = exponent;
	ctmp->m_apm_sign     = sign;

	if (zflag)
	{
		ctmp->m_apm_exponent   = 0;
		ctmp->m_apm_sign       = 0;
		ctmp->m_apm_datalength = 1;
	}
	else
	{
		M_apm_normalize(ctmp);
	}

	/*
	 *  if our local temp string is getting too big,
	 *  release it's memory and start over next time.
	 *  (this 1000 byte threshold is quite arbitrary,
	 *  it may be more efficient in your app to make
	 *  this number bigger).
	 */

	if (M_lbuf > 1000)
	{
		MAPM_FREE(M_buf);
		M_buf  = NULL;
		M_lbuf = 0;
	}
}
/****************************************************************************/
void	m_apm_to_string(char *s, int places, M_APM mtmp)
{
	M_APM   ctmp;
	char	*cp;
	int	i, index, first, max_i, num_digits, dec_places;
	UCHAR	numdiv, numrem;

	ctmp = M_get_stack_var();
	dec_places = places;

	if (dec_places < 0)
		m_apm_copy(ctmp, mtmp);
	else
		m_apm_round(ctmp, dec_places, mtmp);

	if (ctmp->m_apm_sign == 0)
	{
		if (dec_places < 0)
			strcpy(s, "0.0E+0");
		else
		{
			strcpy(s, "0");

			if (dec_places > 0)
				strcat(s, ".");

			for (i = 0; i < dec_places; i++)
				strcat(s, "0");

			strcat(s, "E+0");
		}

		M_restore_stack(1);
		return;
	}

	max_i = (ctmp->m_apm_datalength + 1) >> 1;

	if (dec_places < 0)
		num_digits = ctmp->m_apm_datalength;
	else
		num_digits = dec_places + 1;

	cp = s;

	if (ctmp->m_apm_sign == -1)
		*cp++ = '-';

	first = TRUE;

	i = 0;
	index = 0;

	while (TRUE)
	{
		if (index >= max_i)
		{
			numdiv = 0;
			numrem = 0;
		}
		else
			M_get_div_rem_10((int)ctmp->m_apm_data[index], &numdiv, &numrem);

		index++;

		*cp++ = numdiv + '0';

		if (++i == num_digits)
			break;

		if (first)
		{
			first = FALSE;
			*cp++ = '.';
		}

		*cp++ = numrem + '0';

		if (++i == num_digits)
			break;
	}

	i = ctmp->m_apm_exponent - 1;
	if (i >= 0)
		sprintf(cp, "E+%d", i);
	else
		sprintf(cp, "E%d", i);

	M_restore_stack(1);
}
/****************************************************************************/