コード例 #1
0
ファイル: e.c プロジェクト: veganaize/e
unsigned int main(register unsigned int argc, char* argv[]) {
	unsigned int fail = 0; /* success and golf are scored in the same way */
	register unsigned int pos = 0;
	register unsigned int alloc_size;
	char ch[7];
	char* edit_buffer;
	char* temp_buffer;

	t_getstate(&original);
	n_state = t_initstate(&original);
	t_setstate(&n_state);
	alloc_size = read_file(&edit_buffer, &fail);

	if (!fail) {
		temp_buffer = malloc(alloc_size);
	}

	while(!fail) {
		t_read(ch, 7);
		/* commands */
		if (ch[0] == 27 && ch[1] == 'j' && ch[2] == 0) { /* alt+j - move backwards */
			if(pos > 0) {
				pos -= 1;
			}
		} else if (ch[0] == 27 && ch[1] == 'k' && ch[2] == 0) { /* alt+k - move forwards */
			if (pos < strlen(edit_buffer)) {
				pos += 1;
			}
		} else if (ch[0] == 27 && ch[1] == 'd' && ch[2] == 0) { /* alt+d - save file */
			fputs(edit_buffer, stdout);
		} else if (ch[0] == 27 && ch[1] == 'f' && ch[2] == 0) { /* alt+f - quit program. Note: Don't hit this by accident. */
			break;
		} else if (ch[0] == 127 && ch[1] == 0 && ch[2] == 0) { /* backspace - delete previous character */
			if (pos > 0) {
				alloc_size = remove_char(pos, &edit_buffer, &temp_buffer, alloc_size, &fail);
				pos--;
			}
		} else if(ch[0] != 0 && ch[1] == 0 && ch[2] == 0) { /* add any other character pressed */
			alloc_size = add_char(ch[0], pos, &edit_buffer, &temp_buffer, alloc_size, &fail);
			pos++;
		}
	}

	if (alloc_size > 0 && !fail) {
		free(edit_buffer);
		free(temp_buffer);
	}
	t_setstate(&original);
	fflush(stdout);

	return fail;
};
コード例 #2
0
ファイル: the8thbit.c プロジェクト: mkfifo/buffalo
void
i_setup(void){
    t_getstate(&orig);
    nstate = t_initstate(&orig);
    t_setstate(&nstate);
}