File: op_fnzbitfind.c

package info (click to toggle)
fis-gtm 6.3-007-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,284 kB
  • sloc: ansic: 328,861; asm: 5,182; csh: 5,102; sh: 1,918; awk: 291; makefile: 69; sed: 13
file content (145 lines) | stat: -rwxr-xr-x 2,839 bytes parent folder | download | duplicates (4)
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
/****************************************************************
 *								*
 *	Copyright 2001 Sanchez Computer Associates, Inc.	*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/

#include "mdef.h"
#include "mvalconv.h"
#include "op.h"

void op_fnzbitfind(mval *dst, mval *bitstr, int truthval, int pos)
{
	int str_len, find_bit;
	unsigned char *byte_1, *byte_n, byte_0;
	int m, n, mp, np, i, i1, j;
	static const unsigned char mask[8] = {0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1};
	error_def(ERR_INVBITSTR);

	MV_FORCE_STR(bitstr);

	if (!bitstr->str.len)
		rts_error(VARLSTCNT(1) ERR_INVBITSTR);

	byte_1 = (unsigned char *)bitstr->str.addr;
	str_len = (bitstr->str.len -1) * 8;
	if (*byte_1 > 7)
	{
		rts_error(VARLSTCNT(1) ERR_INVBITSTR);
	}
	if (pos < 1)
	{
		pos = 1; /* It is the way it works in DATA TREE */
	}
	else if (pos > str_len - *byte_1)
	{
		find_bit = 0;
		MV_FORCE_MVAL(dst, find_bit);
		return; /* It is the way it works in DATA TREE */
	}
	np = (pos + 7)/8;
	mp = pos % 8;
	if (mp == 0)
		mp = 8;
	n = (str_len - *byte_1 + 7)/8 - 1;
	m = (str_len - *byte_1) % 8;
	if (m == 0)
		m = 8;
	byte_n = byte_1 + np;
	find_bit = 0;
	if (truthval)
	{
		if (np == n + 1)
			i1 = m;
		else
			i1 = 8;
		for (i = mp - 1; i < i1; i++)
		{
			if (byte_0 = *byte_n & mask[i])
			{
				find_bit = i + 2 + (np - 1)*8;
				MV_FORCE_MVAL(dst, find_bit);
				return;
			}
		}
		if (np == n + 1)
		{
			MV_FORCE_MVAL(dst, find_bit);
			return;
		}
		for(j = 1; j <= n - np; j++)
		{
			byte_n++;
			for (i = 0; i < 8; i++)
			{
				if (byte_0 = *byte_n & mask[i])
				{
					find_bit = i + 2 + (np + j - 1)*8;
					MV_FORCE_MVAL(dst, find_bit);
					return;
				}
			}
		}
		byte_n++;
		for (i = 0; i < m; i++)
		{
			if (byte_0 = *byte_n & mask[i])
			{
				find_bit = i + 2 + n*8;
				MV_FORCE_MVAL(dst, find_bit);
				return;
			}
		}
	}
	else
	{
		if (np == n + 1)
			i1 = m;
		else
			i1 = 8;
		for (i = mp - 1; i < i1; i++)
		{
			if (!(byte_0 = *byte_n & mask[i]))
			{
				find_bit = i + 2 + (np - 1)*8;
				MV_FORCE_MVAL(dst, find_bit);
				return;
			}
		}
		if (np == n + 1)
		{
			MV_FORCE_MVAL(dst, find_bit);
			return;
		}
		for(j = 1; j <= n - np; j++)
		{
			byte_n++;
			for (i = 0; i < 8; i++)
			{
				if (!(byte_0 = *byte_n & mask[i]))
				{
					find_bit = i + 2 + (np + j - 1)*8;
					MV_FORCE_MVAL(dst, find_bit);
					return;
				}
			}
		}
		byte_n++;
		for (i = 0; i < m; i++)
		{
			if (!(byte_0 = *byte_n & mask[i]))
			{
				find_bit = i + 2 + n*8;
				MV_FORCE_MVAL(dst, find_bit);
				return;
			}
		}
	}
	MV_FORCE_MVAL(dst, find_bit);
	return;
}