Advertisement
Guest User

program

a guest
Aug 6th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. #define BUFSIZE 1000
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. int init_files(FILE **src, FILE **tmp, char *file_path);
  8. void print_file(FILE *src);
  9. int write(FILE *src, FILE **dest, char* file_path, int *lines);
  10. int memberOf(int val, int *array);
  11.  
  12. int parse_input(int argc, char *argv[]);
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16.     /* init */
  17.  
  18.     FILE *fptr_src;
  19.     FILE *fptr_tmp;
  20.  
  21.     char *file_path = "./test.txt";
  22.     char *buf = malloc(sizeof(char)*BUFSIZE);
  23.  
  24.     int *lines = malloc(sizeof(int)*100);
  25.     int i;
  26.     for (i = 0; i < 100; ++i) {
  27.         lines[i] = -1;
  28.     }
  29.  
  30.     if (init_files(&fptr_src, &fptr_tmp, file_path)) {
  31.         exit(1);
  32.     }
  33.  
  34.  
  35.     if (argc > 1) {
  36.         switch(parse_input(argc, argv)) {
  37.         case 0:
  38.             if (argv[2])
  39.                 fprintf(fptr_tmp, "%s\n", argv[2]);
  40.             else
  41.                 printf("error: must supply arguemnt for add\n");
  42.             break;
  43.         case 1:
  44.             print_file(fptr_tmp);
  45.             break;
  46.         case 2: ;
  47.             int j;
  48.             i = 2, j = 0;
  49.             while(argv[i]) {
  50.                 lines[j++] = atoi(argv[i++]);
  51.             }
  52.             break;
  53.         default:
  54.             printf("command not regocognized\n");
  55.             break;
  56.         }
  57.     } else {
  58.         printf("no arguments given\n");
  59.     }
  60.  
  61.     write(fptr_tmp, &fptr_src, file_path, lines);
  62.  
  63.     fclose(fptr_src);
  64.     fclose(fptr_tmp);
  65.  
  66.     return 0;
  67. }
  68.  
  69. /* input parsing */
  70.  
  71. int parse_input(int argc, char *argv[])
  72. {
  73.     const char *commands[] = { "add", "ls", "rm" , NULL };
  74.     int i = 0;
  75.  
  76.     while (commands[i]) {
  77.         if (!strcmp(commands[i++], argv[1])) {
  78.             return i-1;
  79.         }
  80.     }
  81.     return -1;
  82. }
  83.  
  84. /* file operations */
  85.  
  86. int init_files(FILE **src, FILE **tmp, char *file_path)
  87. {
  88.     char *buf = malloc(sizeof(char)*BUFSIZE);
  89.  
  90.     *src = fopen(file_path, "r");
  91.     *tmp = fopen("./.test.tmp", "w+");
  92.     if (*src == NULL || *tmp == NULL) {
  93.         perror("Error:");
  94.         return 1;
  95.     }
  96.  
  97.     rewind(*src);
  98.  
  99.     /* else, copy data into tmp */
  100.     int i = 0;
  101.     while(fgets(buf, BUFSIZE, *src)) {
  102.         fprintf(*tmp, "%s", buf);
  103.     }
  104.     return 0;
  105. }
  106.  
  107. void print_file(FILE *src)
  108. {
  109.     char *buf = malloc(sizeof(char)*BUFSIZE);
  110.     int i = 0;
  111.  
  112.     rewind(src);
  113.  
  114.     while(fgets(buf, BUFSIZE, src)) {
  115.         printf("%d - %s", i++, buf);
  116.     }
  117. }
  118.  
  119. int write(FILE *src, FILE **dest, char *file_path, int *lines) {
  120.     char *buf = malloc(sizeof(char)*BUFSIZE);
  121.    
  122.     fclose(*dest);
  123.    
  124.     *dest = fopen(file_path, "w+");
  125.  
  126.     if (*dest == NULL) {
  127.         perror("Error: ");
  128.         return 1;
  129.     }
  130.  
  131.     rewind(src);
  132.     int i = 0;
  133.  
  134.     while (fgets(buf, BUFSIZE, src)) {
  135.         if (!memberOf(i++, lines)) {
  136.             fprintf(*dest, "%s", buf);
  137.         }
  138.     }
  139.     return 0;
  140. }
  141.  
  142. int memberOf(int val, int *arr)
  143. {
  144.     int i;
  145.     for (i = 0; i < 100; ++i) {
  146.         if (arr[i] == val) {
  147.             return 1;
  148.         }
  149.     }
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement