示例#1
0
/* mode 0 new file
 * mode 1 edit file
 */
int wama_file(int mode)
{
	char path[128];
	clean_screen();
	printf("Ruta del archivo: ");
	gets_s(path, 128);
	clean_screen();
	if (path[0] != '/') {
		char dev_path[128] = "/dev/";
		strcat(dev_path, path);
		strcpy(path, dev_path);
	}
	/* creamos el archivo 'path' */
	if ((touch(path, S_IRUSR | S_IWUSR)) < 0 && mode == NEW_FILE_MODE) {
		return -1;
	}
	int line_count = 1;
	char line[128];
	int fd;
	if ((fd = open(path, O_WRONLY)) < 0) {
		return -1;
	}
	if (mode == EDIT_FILE_MODE) {
		char *ch = read_file(path);
		write(fd, ch, strlen(ch));
		line_count = linecounter(ch);
		line_count++;
	}
	printf("\n\n\n %d ", line_count);
	char *data;
	while ( 1 ) {
		setwindow();
		subwindow(line_count);
		gets_s(line, 128);
		switch (checkWamaCommand(line)) {
			case 0:
				write(fd, line, strlen(line));
				write(fd, "\n", 1);
				break;
			case 1:
				close(fd);
				if (goto_wama_command(path, line_count) < 0) {
					return -1;
				}
				data = read_file(path);
				if ((fd = open(path, O_WRONLY)) < 0) {
					return -1;
				}
				write(fd, data, strlen(data));
				line_count--;
				break;
			case 2:
				close(fd);
				return 0;
				break;
		}
		line_count++;
		printf(" %d ", line_count);
	}
}
示例#2
0
int line_navigator(char *path, char *doc_page)
{
	int ret = 0;
	char *data = read_file(path);
	int n_lines = linecounter(data);
	int act_line = 0;
	char **lines;
	lines = new char *[n_lines];
	lines[0] = &data[0];
	int data_len = strlen(data);
	if (data_len > 0) {
		for (int i = 1, x = 0; data[x] != '\0'; x++) {
			if (data[x] == '\n') {
				data[x] = '\0';
				if (i < n_lines) {
					lines[i] = &data[x+1];
				}
				i++;
			}
		}
		if (n_lines > 0) {
			ret = 1;
			char char_read[2];
			do {
				clean_screen();
				int start_line = act_line;
				if (start_line > (n_lines-22) && n_lines >= 22) {
					start_line = (n_lines-22);
				}
				if (n_lines < 22) {
					start_line = 0;
				}
				for (int i = start_line; i < (start_line+22) && i < n_lines; i++) {
					printf("%s\n", lines[i]);
				}
				printf("\n\nManual page for %s line %d "
				"(press 'h' for help or 'q' to exit)", doc_page, act_line+1);
				read(0, char_read, 1);
				if (char_read[0] == 'e' && act_line < n_lines) {
					act_line++;
				}
				if (char_read[0] == 'y' && act_line > 0) {
					act_line--;
				}
				if (char_read[0] == 'm') {
					char line[8];
					clean_screen();
					printf("Move to line: ");
					gets_s(line, 8);
					int line_to_move = atoi(line);
					if (line_to_move > 0 && line_to_move < n_lines) {
						act_line = (line_to_move-1);
					}
				}
				if (char_read[0] == 'h') {
					clean_screen();
					printf("\e[9;32HSUMMARY OF LESS COMMANDS\n");
					printf("\e[10;30Hh\t\tDisplay this help\n");
					printf("\e[11;30Hq\t\tExit\n");
					printf("\e[12;40HMOVING\n");
					printf("\e[13;30He\t\tForward one line\n");
					printf("\e[14;30Hy\t\tBackward one line\n");
					printf("\e[15;30Hm\t\tMove to 'n' line\n");
					printf("\e[17;35HPress 'q' when done\e[1;1H");
					while (getchar() != 'q');
				}
			} while(char_read[0] != 'q');
			clean_screen();
		}
	}