Advertisement
rocky1oo

user_interface.c

Mar 29th, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.85 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. int get_num_occurances(char *s,char c);
  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 ( !strcmp(cmd_name, "highlight_text") || !strcmp(cmd_name, "remove_text") || !strcmp(cmd_name, "replace_text") ) {
  99.             if (!strcmp(cmd_name, "highlight_text")) {
  100.                 if(get_num_occurances(line, '\"') < 2) {
  101.                     printf("Invalid Command\n");
  102.                     return SUCCESS;
  103.                 }
  104.  
  105.                 quote1_ptr = strchr(line, '\"') + 1;
  106.                 quote1_ptr = strchr(quote1_ptr, '\"');
  107.                 *quote1_ptr = '\0';
  108.                 quote1_ptr = strchr(line, '\"') + 1;
  109.  
  110.                 highlight_text(doc, quote1_ptr);
  111.             } else if (!strcmp(cmd_name, "remove_text")) {
  112.                 if(get_num_occurances(line, '\"') < 2) {
  113.                     printf("Invalid Command\n");
  114.                     return SUCCESS;
  115.                 }
  116.  
  117.                 quote1_ptr = strchr(line, '\"') + 1;
  118.                 quote1_ptr = strchr(quote1_ptr, '\"');
  119.                 *quote1_ptr = '\0';
  120.                 quote1_ptr = strchr(line, '\"') + 1;
  121.  
  122.                 remove_text(doc, quote1_ptr);
  123.             } else if (!strcmp(cmd_name, "replace_text")) {
  124.                 if(get_num_occurances(line, '\"') < 4) {
  125.                     printf("Invalid Command\n");
  126.                     return SUCCESS;
  127.                 }
  128.  
  129.                 quote1_ptr = strchr(line, '\"') + 1;
  130.                 quote1_ptr = strchr(quote1_ptr, '\"');
  131.                 *quote1_ptr = '\0';
  132.                 quote2_ptr = quote1_ptr + 1;
  133.                 quote1_ptr = strchr(line, '\"') + 1;
  134.  
  135.                 quote2_ptr = strchr(quote2_ptr, '\"') + 1;
  136.                 quote2_ptr = strchr(quote2_ptr, '\"');
  137.                 *quote2_ptr = '\0';
  138.                 quote2_ptr = strchr(quote1_ptr + strlen(quote1_ptr) + 1, '\"') + 1;
  139.  
  140.                 if (quote1_ptr && quote2_ptr && *quote1_ptr && *quote2_ptr) {
  141.                     run(replace_text(doc, quote1_ptr, quote2_ptr), "replace_text");
  142.                 }
  143.  
  144.                 return SUCCESS;
  145.             } else {
  146.                 printf("Invalid Command\n");
  147.             }
  148.     } else if (num_cmd_parts == 0) {
  149.         return SUCCESS;
  150.     } else if (num_cmd_parts == 1) {
  151.         /*print quit exit resetdocument*/
  152.         /*printf("case 1\n");*/
  153.         if(!strcmp(cmd_name, "print_document")) {
  154.             /*printf("PRINTED DOC\n");*/
  155.             print_document(doc);
  156.         } else if(!strcmp(cmd_name, "reset_document")) {
  157.             reset_document(doc);
  158.         } else if(!strcmp(cmd_name, "quit") || !strcmp(cmd_name, "exit")) {
  159.             exit(EXIT_SUCCESS);
  160.             return -1;
  161.         } else {
  162.             printf("Invalid Command\n");
  163.         }
  164.     } else if (num_cmd_parts == 2) {
  165.         /*add_pafter load file highlight save*/
  166.         /*printf("case 2\n");*/
  167.         if(!strcmp(cmd_name, "add_paragraph_after")) {
  168.             /*printf("ADDED P AFTER %d\n", atoi(arg1));*/
  169.             if(atoi(arg1) > 0 || !strcmp(arg1, "0")) {
  170.                 run(add_paragraph_after(doc, atoi(arg1)), "add_paragraph_after");
  171.             } else {
  172.                 printf("Invalid Command\n");
  173.             }
  174.         } else if (!strcmp(cmd_name, "load_file")) {
  175.             run(load_file(doc, arg1), "load_file");
  176.         } else if (!strcmp(cmd_name, "save_document")) {
  177.             run(save_document(doc, arg1), "save_document");
  178.         } else {
  179.             printf("Invalid Command\n");
  180.         }
  181.     } else if (num_cmd_parts == 3) {
  182.         /*remove */
  183.         if(!strcmp(cmd_name, "remove_line")) {
  184.             /*printf("ADDED P AFTER %d\n", atoi(arg1));*/
  185.             if ( (atoi(arg1) > 0 || !strcmp(arg1, "0")) && (atoi(arg2) > 0 || !strcmp(arg2, "0")) ) {
  186.                 run(remove_line(doc, atoi(arg1), atoi(arg2)), "remove_line");
  187.             } else {
  188.                 printf("Invalid Command\n");
  189.             }
  190.         } else {
  191.             printf("Invalid Command\n");
  192.         }
  193.     } else {
  194.         printf("Invalid Command\n");
  195.         return 0;
  196.     }
  197. }
  198.  
  199. char* get_str_after_star(char* str) {
  200.     str = strstr(str, "*") + 1;
  201.     str[strcspn(str, "\n")] = 0;
  202.  
  203.     return str;
  204. }
  205.    
  206. int run(int exit_condition, char* str) {
  207.     if(exit_condition == FAILURE) {
  208.         printf("%s failed\n", str);
  209.     }
  210.  
  211.     return 0;
  212. }
  213.  
  214. /*returns true if there are arugments after the nth command line argument (inclusive)*/
  215. int are_args_after(int n) {
  216. /*  for */
  217. }
  218.  
  219. int is_comment(char * str) {
  220.     char* result = strchr(str, '#');
  221.     int index = 0;
  222.     if(result == NULL) return 0;
  223.  
  224.     for(index; index < str - result; index++) {
  225.         if(!isspace(str + index)) return 0;
  226.     }
  227.  
  228.     return 1;
  229. }
  230.  
  231. int get_num_occurances(char *s,char c) {
  232.     int i, count=0;
  233.     for(i=0; s[i]; i++) {
  234.         if(s[i]==c) {
  235.             count++;
  236.         }
  237.     }
  238.     return count;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement