Advertisement
Guest User

user_interface.c

a guest
Mar 29th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.28 KB | None | 0 0
  1. #include "document.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sysexits.h>
  6. #include <limits.h>
  7.  
  8. #define MAX_LINE 1024
  9.  
  10. int run_if_Valid(char* cmd_name, char* arg1, char* arg2, char* arg3, char* arg4, char* line, int num_cmd_parts, Document* doc);
  11. char* get_str_after_star(char* str);
  12.  
  13.  
  14. int main(int argc, char* argv[]) {
  15.     int arg_num = 2, num_cmd_parts = 0, is_std_io = 1;
  16.     char line[MAX_LINE+10+1] = "", cmd_name[MAX_LINE+1] = "", arg1[MAX_LINE+1] = "",
  17.         arg2[MAX_LINE+1] = "", arg3[MAX_LINE+1] = "", arg4[MAX_LINE+1] = "";
  18.     FILE* input;
  19.     Document doc;
  20.     init_document(&doc, "main_document");
  21.  
  22.     /*add_paragraph_after(&doc, 0);
  23.     append_line(&doc, 1, "1");
  24.     append_line(&doc, 1, "2");
  25.     add_paragraph_after(&doc, 0);
  26.     add_paragraph_after(&doc, 0);
  27.     append_line(&doc, 1, "3");
  28.     append_line(&doc, 1, "4");
  29.     append_line(&doc, 2, "5");
  30.     append_line(&doc, 2, "6");*/
  31.  
  32.     if (argc > 2) {
  33.         fprintf(stderr, "Usage: user_interface\nUsage: user_interface <filename>\n");
  34.         return EX_USAGE;
  35.     } else if(argc == 2) {
  36.         if((input = fopen(argv[1], "r")) != NULL) {
  37.             is_std_io = 0;
  38.         } else {
  39.             fprintf(stderr, "%s cannot be opened.\n", argv[1]);
  40.             return EX_OSERR;
  41.         }
  42.     } else {
  43.         input = stdin;
  44.     }
  45.  
  46.     while (fgets(line, MAX_LINE + 10, input)) {
  47.         if(is_std_io) printf("> ");
  48.         if ( (num_cmd_parts = sscanf(line, " %s %s %s %s %s ", cmd_name, arg1, arg2, arg3, arg4)) == -1) {
  49.             continue;
  50.         }
  51.  
  52.  
  53.         if(run_if_Valid(cmd_name, arg1, arg2, arg3, arg4, line, num_cmd_parts, &doc) == -1) {
  54.             break;
  55.         }
  56.  
  57.         strcpy(cmd_name, "");
  58.         strcpy(arg1, "");
  59.         strcpy(arg2, "");
  60.         strcpy(arg3, "");
  61.         strcpy(arg4, "");
  62.     }
  63.  
  64.     fclose(input);
  65. }
  66.  
  67.  
  68. int run_if_Valid(char* cmd_name, char* arg1, char* arg2, char* arg3, char* arg4, char* line, int num_cmd_parts, Document* doc) {
  69.     char* str_after_star, str_in_quote[MAX_STR_SIZE], *quote1_ptr = NULL, *quote2_ptr = NULL;
  70.     int start = 0, end = 0;
  71. /*  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);*/
  72.     if(is_comment(cmd_name)) return SUCCESS;
  73.     if(strlen(line) > MAX_LINE) {
  74.         printf("Invalid Command\n");
  75.         return SUCCESS;
  76.     }
  77.  
  78.     if(strchr(line, '*') && num_cmd_parts >= 3) {
  79.         str_after_star = get_str_after_star(line);
  80.  
  81.         if(!strcmp(cmd_name, "append_line")) {
  82.             /*printf("APPENDED LINE %d String: %s\n", atoi(arg1), str_after_star);*/
  83.             if ( (atoi(arg1) > 0 || !strcmp(arg1, "0")) ) {
  84.                 run(append_line(doc, atoi(arg1),  str_after_star), "append_line");
  85.             } else {
  86.                 printf("Invalid Command\n");
  87.             }
  88.         } else if(!strcmp(cmd_name, "add_line_after")) {
  89.             /*printf("ADDED LINE %d %d String: %s\n", atoi(arg1), atoi(arg2), str_after_star);*/
  90.             if( (atoi(arg1) > 0 || !strcmp(arg1, "0")) && (atoi(arg2) > 0 || !strcmp(arg2, "0")) ) {
  91.                 run(add_line_after(doc, atoi(arg1), atoi(arg2), str_after_star), "add_line_after");
  92.             } else {
  93.                 printf("Invalid Command\n");
  94.             }
  95.         } else {
  96.             printf("Invalid Command\n");
  97.         }
  98.     } else if (strstr(line, "\"") && num_cmd_parts > 1) {
  99.         if (!strcmp(cmd_name, "highlight_text")) {
  100.             quote1_ptr = strchr(line, '\"') + 1;
  101.             quote1_ptr = strchr(quote1_ptr, '\"');
  102.             *quote1_ptr = '\0';
  103.             quote1_ptr = strchr(line, '\"') + 1;
  104.  
  105.             highlight_text(doc, quote1_ptr);
  106.         } else if (!strcmp(cmd_name, "remove_text")) {
  107.             quote1_ptr = strchr(line, '\"') + 1;
  108.             quote1_ptr = strchr(quote1_ptr, '\"');
  109.             *quote1_ptr = '\0';
  110.             quote1_ptr = strchr(line, '\"') + 1;
  111.  
  112.             remove_text(doc, quote1_ptr);
  113.         } else if (!strcmp(cmd_name, "replace_text")) {
  114.             quote1_ptr = strchr(line, '\"') + 1;
  115.             quote1_ptr = strchr(quote1_ptr, '\"');
  116.             *quote1_ptr = '\0';
  117.             quote2_ptr = quote1_ptr + 1;
  118.             quote1_ptr = strchr(line, '\"') + 1;
  119.  
  120.             quote2_ptr = strchr(quote2_ptr, '\"') + 1;
  121.             quote2_ptr = strchr(quote2_ptr, '\"');
  122.             *quote2_ptr = '\0';
  123.             quote2_ptr = strchr(quote1_ptr + strlen(quote1_ptr) + 1, '\"') + 1;
  124.  
  125.             if (quote1_ptr && quote2_ptr && *quote1_ptr && *quote2_ptr) {
  126.                 run(replace_text(doc, quote1_ptr, quote2_ptr), "replace_text");
  127.             } else {
  128.                 printf("Invalid Command\n");
  129.             }
  130.         } else {
  131.             printf("Invalid Command\n");
  132.         }
  133.     } else if (num_cmd_parts == 0) {
  134.         return SUCCESS;
  135.     } else if (num_cmd_parts == 1) {
  136.         /*print quit exit resetdocument*/
  137.         /*printf("case 1\n");*/
  138.         if(!strcmp(cmd_name, "print_document")) {
  139.             /*printf("PRINTED DOC\n");*/
  140.             print_document(doc);
  141.         } else if(!strcmp(cmd_name, "reset_document")) {
  142.             reset_document(doc);
  143.         } else if(!strcmp(cmd_name, "quit") || !strcmp(cmd_name, "exit")) {
  144.             exit(EXIT_SUCCESS);
  145.             return -1;
  146.         } else {
  147.             printf("Invalid Command\n");
  148.         }
  149.     } else if (num_cmd_parts == 2) {
  150.         /*add_pafter load file highlight save*/
  151.         /*printf("case 2\n");*/
  152.         if(!strcmp(cmd_name, "add_paragraph_after")) {
  153.             /*printf("ADDED P AFTER %d\n", atoi(arg1));*/
  154.             if(atoi(arg1) > 0 || !strcmp(arg1, "0")) {
  155.                 run(add_paragraph_after(doc, atoi(arg1)), "add_paragraph_after");
  156.             } else {
  157.                 printf("Invalid Command\n");
  158.             }
  159.         } else if (!strcmp(cmd_name, "load_file")) {
  160.             run(load_file(doc, arg1), "load_file");
  161.         } else if (!strcmp(cmd_name, "save_document")) {
  162.             run(save_document(doc, arg1), "save_document");
  163.         } else {
  164.             printf("Invalid Command\n");
  165.         }
  166.     } else if (num_cmd_parts == 3) {
  167.         /*remove */
  168.         if(!strcmp(cmd_name, "remove_line")) {
  169.             /*printf("ADDED P AFTER %d\n", atoi(arg1));*/
  170.             if ( (atoi(arg1) > 0 || !strcmp(arg1, "0")) && (atoi(arg2) > 0 || !strcmp(arg2, "0")) ) {
  171.                 run(remove_line(doc, atoi(arg1), atoi(arg2)), "remove_line");
  172.             } else {
  173.                 printf("Invalid Command\n");
  174.             }
  175.         } else {
  176.             printf("Invalid Command\n");
  177.         }
  178.     } else {
  179.         printf("Invalid Command\n");
  180.         return 0;
  181.     }
  182. }
  183.  
  184. char* get_str_after_star(char* str) {
  185.     str = strstr(str, "*") + 1;
  186.     str[strcspn(str, "\n")] = 0;
  187.  
  188.     return str;
  189. }
  190.    
  191. int run(int exit_condition, char* str) {
  192.     if(exit_condition == FAILURE) {
  193.         printf("%s failed\n", str);
  194.     }
  195.  
  196.     return 0;
  197. }
  198.  
  199. /*returns true if there are arugments after the nth command line argument (inclusive)*/
  200. int are_args_after(int n) {
  201. /*  for */
  202. }
  203.  
  204. int is_comment(char * str) {
  205.     char* result = strchr(str, '#');
  206.     int index = 0;
  207.     if(result == NULL) return 0;
  208.  
  209.     for(index; index < str - result; index++) {
  210.         if(!isspace(str + index)) return 0;
  211.     }
  212.  
  213.     return 1;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement