summaryrefslogtreecommitdiff
path: root/programs/sub-suite/shift_line.c
blob: 255d21bc379cd804f7d9e63ce91d66d9e0f559d5 (plain)
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
#include <stdint.h>

const uint8_t bits[8] = {
	0x80,
	0x40,
	0x20,
	0x10,
	0x08,
	0x04,
	0x02,
	0x01
};

int maxcol = 80;
int scr_str = 0;
int scr_row = 0;
int scr_col = 0;
uint8_t bitabl[16];

uint8_t bitpos(unsigned int row, uint8_t *mask) {
	uint8_t bit = row & 7;
	*mask = bits[bit];
	return row >> 3;

}

void setbit(unsigned int row) {
	uint8_t mask;
	uint8_t byte = bitpos(row, &mask);
	bitabl[byte] |= mask;
}

void clrbit(unsigned int row) {
	uint8_t mask;
	uint8_t byte = bitpos(row, &mask);
	bitabl[byte] &= ~mask;
}

int find_end(char *str, int start) {
	int i;
	for (i = start; str[i]; i++);
	return i;
}

void shift_line(char *str, int cursor, int left) {
	/*int cursor = ((scr_row+scr_str)*maxcol)+scr_col;*/
	int end = find_end(str, cursor);
	if (left) {
		int i = end-1;
		int j = end;
		for (; i > cursor; i--, j--) {
			if (i < 0) {
				i = 0;
				str[i] = 0;
				break;
			}
			str[j] = str[i];
			str[i] = 0;

		}
		/*str[i+1] = (!str[i+1]) ? ' ' : str[i+1];*/
		end = find_end(str, i+2);
		/*str[i+1] = (str[i+1] == ' ') ? 0 : str[i+1];*/
		if ((end/maxcol) > scr_row) {
			setbit(end/maxcol);
		}
	} else {
		int i = cursor;
		int j = cursor-1;
		for (; str[i]; i++, j++) {
			str[j] = str[i];
			str[i] = 0;
		}
		end = find_end(str, i);
		if ((end % maxcol) == 0 && (end/maxcol) > scr_row) {
			clrbit(end/maxcol);
		}
	}
}