Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "document.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sysexits.h>
- #include <limits.h>
- #define MAX_LINE 1024
- int run_if_Valid(char* cmd_name, char* arg1, char* arg2, char* arg3, char* arg4, char* line, int num_cmd_parts, Document* doc);
- char* get_str_after_star(char* str);
- int get_num_occurances(char *s,char c);
- int main(int argc, char* argv[]) {
- int arg_num = 2, num_cmd_parts = 0, is_std_io = 1;
- char line[MAX_LINE+10+1] = "", cmd_name[MAX_LINE+1] = "", arg1[MAX_LINE+1] = "",
- arg2[MAX_LINE+1] = "", arg3[MAX_LINE+1] = "", arg4[MAX_LINE+1] = "";
- FILE* input;
- Document doc;
- init_document(&doc, "main_document");
- /*add_paragraph_after(&doc, 0);
- append_line(&doc, 1, "1");
- append_line(&doc, 1, "2");
- add_paragraph_after(&doc, 0);
- add_paragraph_after(&doc, 0);
- append_line(&doc, 1, "3");
- append_line(&doc, 1, "4");
- append_line(&doc, 2, "5");
- append_line(&doc, 2, "6");*/
- if (argc > 2) {
- fprintf(stderr, "Usage: user_interface\nUsage: user_interface <filename>\n");
- return EX_USAGE;
- } else if(argc == 2) {
- if((input = fopen(argv[1], "r")) != NULL) {
- is_std_io = 0;
- } else {
- fprintf(stderr, "%s cannot be opened.\n", argv[1]);
- return EX_OSERR;
- }
- } else {
- input = stdin;
- }
- while (fgets(line, MAX_LINE + 10, input)) {
- if(is_std_io) printf("> ");
- if ( (num_cmd_parts = sscanf(line, " %s %s %s %s %s ", cmd_name, arg1, arg2, arg3, arg4)) == -1) {
- continue;
- }
- if(run_if_Valid(cmd_name, arg1, arg2, arg3, arg4, line, num_cmd_parts, &doc) == -1) {
- break;
- }
- strcpy(cmd_name, "");
- strcpy(arg1, "");
- strcpy(arg2, "");
- strcpy(arg3, "");
- strcpy(arg4, "");
- }
- fclose(input);
- }
- int run_if_Valid(char* cmd_name, char* arg1, char* arg2, char* arg3, char* arg4, char* line, int num_cmd_parts, Document* doc) {
- char* str_after_star, str_in_quote[MAX_STR_SIZE], *quote1_ptr = NULL, *quote2_ptr = NULL;
- int start = 0, end = 0;
- /* printf("cmd_name: %s arg1: %s arg2: %s arg3: %s arg4: %s num_cmd_parts: %d\n", cmd_name, arg1, arg2, arg3, arg4, num_cmd_parts);*/
- if(is_comment(cmd_name)) return SUCCESS;
- if(strlen(line) > MAX_LINE) {
- printf("Invalid Command\n");
- return SUCCESS;
- }
- if(strchr(line, '*') && num_cmd_parts >= 3) {
- str_after_star = get_str_after_star(line);
- if(!strcmp(cmd_name, "append_line")) {
- /*printf("APPENDED LINE %d String: %s\n", atoi(arg1), str_after_star);*/
- if ( (atoi(arg1) > 0 || !strcmp(arg1, "0")) ) {
- run(append_line(doc, atoi(arg1), str_after_star), "append_line");
- } else {
- printf("Invalid Command\n");
- }
- } else if(!strcmp(cmd_name, "add_line_after")) {
- /*printf("ADDED LINE %d %d String: %s\n", atoi(arg1), atoi(arg2), str_after_star);*/
- if( (atoi(arg1) > 0 || !strcmp(arg1, "0")) && (atoi(arg2) > 0 || !strcmp(arg2, "0")) ) {
- run(add_line_after(doc, atoi(arg1), atoi(arg2), str_after_star), "add_line_after");
- } else {
- printf("Invalid Command\n");
- }
- } else {
- printf("Invalid Command\n");
- }
- } else if ( !strcmp(cmd_name, "highlight_text") || !strcmp(cmd_name, "remove_text") || !strcmp(cmd_name, "replace_text") ) {
- if (!strcmp(cmd_name, "highlight_text")) {
- if(get_num_occurances(line, '\"') < 2) {
- printf("Invalid Command\n");
- return SUCCESS;
- }
- quote1_ptr = strchr(line, '\"') + 1;
- quote1_ptr = strchr(quote1_ptr, '\"');
- *quote1_ptr = '\0';
- quote1_ptr = strchr(line, '\"') + 1;
- highlight_text(doc, quote1_ptr);
- } else if (!strcmp(cmd_name, "remove_text")) {
- if(get_num_occurances(line, '\"') < 2) {
- printf("Invalid Command\n");
- return SUCCESS;
- }
- quote1_ptr = strchr(line, '\"') + 1;
- quote1_ptr = strchr(quote1_ptr, '\"');
- *quote1_ptr = '\0';
- quote1_ptr = strchr(line, '\"') + 1;
- remove_text(doc, quote1_ptr);
- } else if (!strcmp(cmd_name, "replace_text")) {
- if(get_num_occurances(line, '\"') < 4) {
- printf("Invalid Command\n");
- return SUCCESS;
- }
- quote1_ptr = strchr(line, '\"') + 1;
- quote1_ptr = strchr(quote1_ptr, '\"');
- *quote1_ptr = '\0';
- quote2_ptr = quote1_ptr + 1;
- quote1_ptr = strchr(line, '\"') + 1;
- quote2_ptr = strchr(quote2_ptr, '\"') + 1;
- quote2_ptr = strchr(quote2_ptr, '\"');
- *quote2_ptr = '\0';
- quote2_ptr = strchr(quote1_ptr + strlen(quote1_ptr) + 1, '\"') + 1;
- if (quote1_ptr && quote2_ptr && *quote1_ptr && *quote2_ptr) {
- run(replace_text(doc, quote1_ptr, quote2_ptr), "replace_text");
- }
- return SUCCESS;
- } else {
- printf("Invalid Command\n");
- }
- } else if (num_cmd_parts == 0) {
- return SUCCESS;
- } else if (num_cmd_parts == 1) {
- /*print quit exit resetdocument*/
- /*printf("case 1\n");*/
- if(!strcmp(cmd_name, "print_document")) {
- /*printf("PRINTED DOC\n");*/
- print_document(doc);
- } else if(!strcmp(cmd_name, "reset_document")) {
- reset_document(doc);
- } else if(!strcmp(cmd_name, "quit") || !strcmp(cmd_name, "exit")) {
- exit(EXIT_SUCCESS);
- return -1;
- } else {
- printf("Invalid Command\n");
- }
- } else if (num_cmd_parts == 2) {
- /*add_pafter load file highlight save*/
- /*printf("case 2\n");*/
- if(!strcmp(cmd_name, "add_paragraph_after")) {
- /*printf("ADDED P AFTER %d\n", atoi(arg1));*/
- if(atoi(arg1) > 0 || !strcmp(arg1, "0")) {
- run(add_paragraph_after(doc, atoi(arg1)), "add_paragraph_after");
- } else {
- printf("Invalid Command\n");
- }
- } else if (!strcmp(cmd_name, "load_file")) {
- run(load_file(doc, arg1), "load_file");
- } else if (!strcmp(cmd_name, "save_document")) {
- run(save_document(doc, arg1), "save_document");
- } else {
- printf("Invalid Command\n");
- }
- } else if (num_cmd_parts == 3) {
- /*remove */
- if(!strcmp(cmd_name, "remove_line")) {
- /*printf("ADDED P AFTER %d\n", atoi(arg1));*/
- if ( (atoi(arg1) > 0 || !strcmp(arg1, "0")) && (atoi(arg2) > 0 || !strcmp(arg2, "0")) ) {
- run(remove_line(doc, atoi(arg1), atoi(arg2)), "remove_line");
- } else {
- printf("Invalid Command\n");
- }
- } else {
- printf("Invalid Command\n");
- }
- } else {
- printf("Invalid Command\n");
- return 0;
- }
- }
- char* get_str_after_star(char* str) {
- str = strstr(str, "*") + 1;
- str[strcspn(str, "\n")] = 0;
- return str;
- }
- int run(int exit_condition, char* str) {
- if(exit_condition == FAILURE) {
- printf("%s failed\n", str);
- }
- return 0;
- }
- /*returns true if there are arugments after the nth command line argument (inclusive)*/
- int are_args_after(int n) {
- /* for */
- }
- int is_comment(char * str) {
- char* result = strchr(str, '#');
- int index = 0;
- if(result == NULL) return 0;
- for(index; index < str - result; index++) {
- if(!isspace(str + index)) return 0;
- }
- return 1;
- }
- int get_num_occurances(char *s,char c) {
- int i, count=0;
- for(i=0; s[i]; i++) {
- if(s[i]==c) {
- count++;
- }
- }
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement