add: work
This commit is contained in:
		 Submodule lib/IronGolem updated: a39e4a780d...ee80b05d10
									
								
							| @ -1,26 +1,39 @@ | |||||||
| cmd_add(text_ptr, line_pos) | cmd_add(text) | ||||||
| { | { | ||||||
| 	local line; | 	local line; | ||||||
|  | 	local i, j; | ||||||
| 	local tmp; | 	local tmp; | ||||||
| 	local out; |  | ||||||
| 	local len; |  | ||||||
| 	local cursor_pos; |  | ||||||
|  |  | ||||||
| 	tmp = NULL; |  | ||||||
| 	loop | 	loop | ||||||
| 	{ | 	{ | ||||||
| 		line = geadline("(add)"); | 		line = geadline("(add)"); | ||||||
| 		if (line == NULL | strcmp(line, ".\n") == 0) | 		if (line == NULL) | ||||||
| 			break; | 			return 1; | ||||||
| 		tmp = realloc(tmp, strlen(line) + strlen(tmp) + 1); | 		if (strcmp(line, ".\n") == 0) | ||||||
|  | 		{ | ||||||
|  | 			[text + LOCATION_CURRENT_LINE] = [text + LOCATION_CURRENT_LINE] - 1; | ||||||
|  | 			return 0; | ||||||
|  | 		} | ||||||
|  | 		[text + LOCATION_LENGTH] = [text + LOCATION_LENGTH] + 1; | ||||||
|  | 		tmp = galloc([text + LOCATION_LENGTH]); | ||||||
| 		if (tmp == NULL) | 		if (tmp == NULL) | ||||||
| 			return 1; | 			return 1; | ||||||
| 		strcat(tmp, line); | 		i = 0; | ||||||
|  | 		j = 0; | ||||||
|  | 		loop | ||||||
|  | 		{ | ||||||
|  | 			if (i == [text + LOCATION_CURRENT_LINE]) | ||||||
|  | 			{ | ||||||
|  | 				[tmp + i] = line; | ||||||
|  | 				j++; | ||||||
|  | 			} | ||||||
|  | 			if (i + j == [text + LOCATION_LENGTH]) | ||||||
|  | 				break; | ||||||
|  | 			[tmp + i + j] = [[text + LOCATION_ARRAY] + i]; | ||||||
|  | 			i++; | ||||||
|  | 		} | ||||||
|  | 		free([[text + LOCATION_ARRAY]]); | ||||||
|  | 		[text + LOCATION_ARRAY] = tmp; | ||||||
|  | 		[text + LOCATION_CURRENT_LINE] = [text + LOCATION_CURRENT_LINE] + 1; | ||||||
| 	} | 	} | ||||||
| 	cursor_pos = line_pos_to_cursor_pos([text_ptr], line_pos); |  | ||||||
| 	dbg cursor_pos; |  | ||||||
| 	out = replace([text_ptr], tmp, cursor_pos, cursor_pos); |  | ||||||
| 	free([text_ptr]); |  | ||||||
| 	[text_ptr] = out; |  | ||||||
| 	return 0; |  | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										4
									
								
								src/commands/print.🗿
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/commands/print.🗿
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,4 @@ | |||||||
|  | cmd_print(text) | ||||||
|  | { | ||||||
|  | 	putstr([[text + LOCATION_ARRAY] + [text + LOCATION_CURRENT_LINE]]); | ||||||
|  | } | ||||||
							
								
								
									
										77
									
								
								src/main.🗿
									
									
									
									
									
								
							
							
						
						
									
										77
									
								
								src/main.🗿
									
									
									
									
									
								
							| @ -1,24 +1,77 @@ | |||||||
| define BUFFER_SIZE=1000; | define LOCATION_LENGTH = 0; | ||||||
|  | define LOCATION_CURRENT_LINE = 1; | ||||||
|  | define LOCATION_ARRAY = 2; | ||||||
|  |  | ||||||
|  | parsing(text, cmd_ptr) | ||||||
|  | { | ||||||
|  | 	local input; | ||||||
|  | 	local tmp; | ||||||
|  | 	local line_application; | ||||||
|  |  | ||||||
|  | 	input = geadline("(default)"); | ||||||
|  | 	if (input == NULL) | ||||||
|  | 		return 1; | ||||||
|  | 	[input + strlen(input) - 1] = 0; | ||||||
|  | 	if (isdigit([input]) == 1) | ||||||
|  | 	{ | ||||||
|  | 		line_application = aton(input); | ||||||
|  | 		if (line_application >= [text + LOCATION_LENGTH]) | ||||||
|  | 		{ | ||||||
|  | 			[cmd_ptr] = NULL; | ||||||
|  | 			putstr("invalid line\n"); | ||||||
|  | 			free(input); | ||||||
|  | 			return 0; | ||||||
|  | 		} | ||||||
|  | 		[text + LOCATION_CURRENT_LINE] = line_application; | ||||||
|  | 	} | ||||||
|  | 	tmp = input; | ||||||
|  | 	loop | ||||||
|  | 	{ | ||||||
|  | 		if (isdigit([tmp]) == 0) | ||||||
|  | 			break; | ||||||
|  | 		tmp++; | ||||||
|  | 	} | ||||||
|  | 	if (strcmp(tmp, "") == 0) | ||||||
|  | 		[cmd_ptr] = strdup("p"); | ||||||
|  | 	else | ||||||
|  | 		[cmd_ptr] = strdup(input); | ||||||
|  | 	free(input); | ||||||
|  | 	if ([cmd_ptr] == NULL) | ||||||
|  | 		return 1; | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
| main() | main() | ||||||
| { | { | ||||||
| 	local text; | 	local text; | ||||||
| 	local line_pos; | 	local array; | ||||||
| 	local line; |  | ||||||
| 	local cmd; | 	local cmd; | ||||||
|  |  | ||||||
| 	line_pos = 0; | 	text = galloc(3); | ||||||
| 	text = strdup(""); |  | ||||||
| 	if (text == NULL) | 	if (text == NULL) | ||||||
| 		return 1; | 		return 1; | ||||||
| 	cmd = 0; | 	[text + LOCATION_CURRENT_LINE] = 0; | ||||||
|  | 	[text + LOCATION_LENGTH] = 0; | ||||||
|  | 	array = [text + LOCATION_ARRAY]; | ||||||
|  | 	[array] = galloc(1); | ||||||
|  | 	if ([array] == NULL) | ||||||
|  | 	{ | ||||||
|  | 		free(text); | ||||||
|  | 		return 1; | ||||||
|  | 	} | ||||||
|  | 	[[array]] = NULL; | ||||||
| 	loop | 	loop | ||||||
| 	{ | 	{ | ||||||
| 		line = geadline("(default)"); | 		if (parsing(text, &cmd)) | ||||||
| 		if (line == NULL) | 			return 1; | ||||||
| 			return; | 		if (cmd != NULL) | ||||||
| 		if (strcmp(line, "a\n") == 0) | 		{ | ||||||
| 			cmd_add(&text, line_pos); | 			dbg [text + LOCATION_CURRENT_LINE]; | ||||||
| 		putstr(text); | 			if (strcmp(cmd, "a") == 0) | ||||||
|  | 				cmd_add(text); | ||||||
|  | 			else if (strcmp(cmd, "p") == 0) | ||||||
|  | 				cmd_print(text); | ||||||
|  | 			free(cmd); | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,13 +1,13 @@ | |||||||
| line_pos_to_cursor_pos(text, line_pos) | line_pos_to_cursor(text, line_pos) | ||||||
| { | { | ||||||
| 	local ptr = text; | 	local ptr = text; | ||||||
| 	loop | 	loop | ||||||
| 	{ | 	{ | ||||||
| 		if (line_pos == 0) | 		if (line_pos == 0) | ||||||
| 			return ptr - text; | 			return ptr; | ||||||
| 		ptr = strchr(ptr, '\n'); | 		ptr = strchr(ptr, '\n'); | ||||||
| 		if (ptr == NULL) | 		if (ptr == NULL) | ||||||
| 			return 0; | 			return NULL; | ||||||
| 		line_pos--; | 		line_pos--; | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user