Advertisement
filip710

Z4.1: grep

Jul 9th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.34 KB | None | 0 0
  1. Grep je jednostavan alat na UNIX operativnim sustavima koji omogućuje filtriranje tekstualnih datoteka. On jednostavno ispisuje svaku liniju ulaznog teksta koja u sebi sadrži traženi tekst. Ima dva načina korištenja:
  2. moguće je proslijediti stdout izlaz iz nekog programa u njega i vršiti filtriranje na takvom tekstu
  3. moguće učitati tekstualnu datoteku s diska i vršiti filtriranje na takvom tekstu
  4. U ovom zadatku implementirat ćete funkcionalnost pod 2, gdje ćete ispisati sve one linije nekog tekstualnog filea na disku koje sadrže neki tekst. Implementirat ćete i neke od opcija koje standardni grep sadrži.
  5. U vašoj implementaciji vaš program će tražiti unos jedne naredbe iz koje će zaključiti što treba raditi. Ta naredba će biti oblika grep [opcije] "trazeni tekst" naziv_filea.txt. Opcije koje će vaš grep podržavati bit će:
  6.  
  7. -i predstavljat će ignore case, tj. ispisat će sve linije koje sadrže tekst neovisno o tome koriste li velika ili mala slova
  8. -v predstavljat će invert match, tj. ispisat će sve one linije koje ne sadrže traženi tekst
  9. -c predstavljat će count, tj. neće ispisivati linije već će jednostavno ispisati broj linija koje odgovaraju pretrazi (broj linija koje bi ispisao)
  10. -n predstavljat će number prefix, tj. ispisat će ispred svake filtrirane linije koja je to linija po redu u originalnom fileu.
  11. Oni koji žele znati više mogu pogledati dokumentaciju na: http://www.gnu.org/software/grep/manual/grep.html, ali za rješavanje same zadaće vam nije potrebna.
  12.  
  13. #include<stdio.h>
  14. #include<string.h>
  15. #include<stdlib.h>
  16. #include<ctype.h>
  17.  
  18. void checkforoptions(char* expression, int *ignore, int *invert, int *count, int *number_prefix);
  19. char *getFilename(char *expression);
  20. char *getKeyword(char *expression);
  21. char *strtok_r(char *str, const char *delim, char **nextp);
  22. void printLine(char *keyword, char *filename, int ignore, int invert, int count, int number_prefix);
  23. char* stristr(char* str, const char* subStr);
  24.  
  25. int main()
  26. {
  27.     char expression[200];
  28.     int ignore = 0;
  29.     int invert = 0;
  30.     int count = 0;
  31.     int number_prefix = 0;
  32.     char *filename;
  33.     char *keyword;
  34.  
  35.     FILE *source;
  36.  
  37.     scanf("%[^\n]%*c", expression);
  38.  
  39.     checkforoptions(expression, &ignore, &invert, &count, &number_prefix);
  40.     filename = getFilename(expression);
  41.     keyword = getKeyword(expression);
  42.  
  43.     source = fopen(filename, "r");
  44.     if (source == NULL)
  45.     {
  46.         perror("Error.");
  47.         return (-1);
  48.     }
  49.  
  50.     printLine(keyword, filename, ignore, invert, count, number_prefix);
  51.     printf("\n");
  52.     fclose(source);
  53.  
  54.     return 0;
  55. }
  56.  
  57. //FUNKCIJE
  58. //provjera unesenih opcija
  59. void checkforoptions(char* expression, int *ignore, int *invert, int *count, int *number_prefix)
  60. {
  61.     if (strstr(expression, "-i") != NULL) *ignore = 1;
  62.     if (strstr(expression, "-v") != NULL) *invert = 1; 
  63.     if (strstr(expression, "-c") != NULL) *count = 1;
  64.     if (strstr(expression, "-n") != NULL) *number_prefix = 1;
  65. }
  66.  
  67. //dohvaćanje imena datoteka, tj. zadnje riječi u naredbi/izrazu kojeg unosimo
  68. char *getFilename(char *expression)
  69. {
  70.     char *filename = strrchr(expression, ' ');
  71.     if (filename && *(filename + 1))
  72.         return (filename + 1);
  73. }
  74.  
  75. //dohvaćanje traženog izraza koji se nalazi unutar navodnika
  76. char *getKeyword(char *expression)
  77. {
  78.     char *last_pointer;
  79.     char *keyword;
  80.  
  81.     keyword = strtok_r(expression, "\"", &last_pointer);
  82.     keyword = strtok_r(NULL, "\"", &last_pointer);
  83.  
  84.     return keyword;
  85. }
  86.  
  87. /* pomoćna funkcija u funkciji getKeyword
  88.  * public domain strtok_r() by Charlie Gordon
  89.  *
  90.  * from comp.lang.c  9/14/2007
  91.  *
  92.  * http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684
  93.  *
  94.  * (Declaration that it's public domain):
  95.  * http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c
  96.  */
  97. char* strtok_r(char *str, const char *delim, char **nextp)
  98. {
  99.     char *ret;
  100.     if (str == NULL)
  101.     {
  102.         str = *nextp;
  103.     }
  104.  
  105.     str += strspn(str, delim);
  106.  
  107.     if (*str == '\0')
  108.     {
  109.         return NULL;
  110.     }
  111.  
  112.     ret = str;
  113.     str += strcspn(str, delim);
  114.  
  115.     if (*str)
  116.     {
  117.         *str++ = '\0';
  118.     }
  119.  
  120.     *nextp = str;
  121.  
  122.     return ret;
  123. }
  124.  
  125. void printLine(char *keyword, char *filename, int ignore, int invert, int count, int number_prefix)
  126. {
  127.     FILE *f1;
  128.     char *substring;
  129.     int linenumber = 0;
  130.     f1 = fopen(filename, "r");
  131.     if (f1 == NULL)
  132.     {
  133.         perror("Error.");
  134.         exit(0);
  135.     }
  136.     char line[256];
  137.  
  138.     // bez opcija
  139.     if(!ignore && !invert && !count && !number_prefix){
  140.         while (fgets(line, sizeof line, f1) != NULL)
  141.         {
  142.             substring = strstr(line, keyword);
  143.             if (substring!= NULL) {
  144.                 printf("%s", line);
  145.                 }
  146.         }
  147.        
  148.     }
  149.  
  150.     //-n opcija
  151.     else if (number_prefix && !ignore && !invert && !count)
  152.     {
  153.         linenumber = 0;
  154.         while (fgets(line, sizeof line, f1) != NULL)
  155.         {
  156.             ++linenumber;
  157.             substring = strstr(line, keyword);
  158.             if (substring!= NULL) {
  159.                 printf("%d:%s", linenumber, line);
  160.             }
  161.         }
  162.     }
  163.     //-n -c opcije i -c opcija
  164.     else if ((number_prefix && count && !ignore && !invert) || (!number_prefix && count && !ignore && !invert))
  165.     {
  166.         int countlines = 0;
  167.         while (fgets(line, sizeof line, f1) != NULL)
  168.         {
  169.             substring = strstr(line, keyword);
  170.             if (substring!= NULL) {
  171.                 ++countlines;
  172.             }
  173.         }
  174.         printf("%d\n", countlines);
  175.     }
  176.     //-n -v opcije
  177.     else if (number_prefix && !count && !ignore && invert)
  178.     {
  179.         int linenumber = 0;
  180.         while (fgets(line, sizeof line, f1) != NULL)
  181.         {
  182.             ++linenumber;
  183.             substring = strstr(line, keyword);
  184.             if (substring == NULL) {
  185.                 printf("%d:%s", linenumber, line);
  186.             }
  187.         }
  188.     }
  189.     //-n -i opcije
  190.     else if (number_prefix && !count && ignore && !invert)
  191.     {
  192.         int linenumber = 0;
  193.         while (fgets(line, sizeof line, f1) != NULL)
  194.         {
  195.             ++linenumber;
  196.             substring = stristr(line, keyword);
  197.             if (substring!= NULL) {
  198.                 printf("%d:%s", linenumber, line);
  199.             }
  200.         }
  201.     }
  202.     //-n -i -c opcije / -i -c opcije
  203.     else if ((number_prefix && count && ignore && !invert) || (!number_prefix && count && ignore && !invert))
  204.     {
  205.         int countlines = 0;
  206.         while (fgets(line, sizeof line, f1) != NULL)
  207.         {
  208.             substring = stristr(line, keyword);
  209.             if (substring!= NULL) {
  210.                 ++countlines;
  211.             }
  212.         }
  213.         printf("%d\n", countlines);
  214.     }
  215.     //-n -v -c opcije / -v -c opcije
  216.     else if ((number_prefix && count && !ignore && invert) || (!number_prefix && count && !ignore && invert))
  217.     {
  218.         int countlines = 0;
  219.         while (fgets(line, sizeof line, f1) != NULL)
  220.         {
  221.             substring = stristr(line, keyword);
  222.             if (substring == NULL) {
  223.                 ++countlines;
  224.             }
  225.         }
  226.         printf("%d", countlines);
  227.     }
  228.     //-n -v -i opcije
  229.     else if (number_prefix && !count && ignore && invert)
  230.     {
  231.         int linenumber = 0;
  232.         while (fgets(line, sizeof line, f1) != NULL)
  233.         {
  234.             ++linenumber;
  235.             substring = stristr(line, keyword);
  236.             if (substring== NULL) {
  237.                 printf("%d:%s", linenumber, line);
  238.             }
  239.         }
  240.     }
  241.     //-n -v -i -c opcije / -v -i -c opcije
  242.     else if ((number_prefix && count && ignore && invert) || (!number_prefix && count && ignore && invert))
  243.     {
  244.         int countlines = 0;
  245.         while (fgets(line, sizeof line, f1) != NULL)
  246.         {
  247.             substring = stristr(line, keyword);
  248.             if (substring == NULL) {
  249.                 ++countlines;
  250.             }
  251.         }
  252.         printf("%d\n", countlines);
  253.     }
  254.     //-i opcija
  255.     else if (!number_prefix && !count && ignore && !invert)
  256.     {
  257.         int linenumber = 0;
  258.         while (fgets(line, sizeof line, f1) != NULL)
  259.         {
  260.             ++linenumber;
  261.             substring = stristr(line, keyword);
  262.             if (substring!= NULL) {
  263.                 printf("%s", line);
  264.             }
  265.         }
  266.     }
  267.     //-v opcija
  268.     else if (!number_prefix && !count && !ignore && invert)
  269.     {
  270.         int linenumber = 0;
  271.         while (fgets(line, sizeof line, f1) != NULL)
  272.         {
  273.             ++linenumber;
  274.             substring = strstr(line, keyword);
  275.             if (substring == NULL) {
  276.                 printf("%s", line);
  277.             }
  278.         }
  279.     }
  280.     //-v -i opcije
  281.     else if (!number_prefix && !count && ignore && invert)
  282.     {
  283.         int linenumber = 0;
  284.         while (fgets(line, sizeof line, f1) != NULL)
  285.         {
  286.             ++linenumber;
  287.             substring = stristr(line, keyword);
  288.             if (substring== NULL) {
  289.                 printf("%s", line);
  290.             }
  291.         }
  292.     }
  293.  
  294. }
  295.  
  296. //case insensitive provjera podstringa unutar stringa
  297. char* stristr(char* str, const char* subStr)
  298. {
  299.     char* p1 = str;
  300.     const char* p2 = subStr;
  301.     char* r = *p2 == 0 ? str : 0;
  302.  
  303.     while (*p1 != 0 && *p2 != 0)
  304.     {
  305.         if (tolower(*p1) == tolower(*p2))
  306.         {
  307.             if (r == 0)
  308.             {
  309.                 r = p1;
  310.             }
  311.  
  312.             p2++;
  313.         }
  314.         else
  315.         {
  316.             p2 = subStr;
  317.             if (tolower(*p1) == tolower(*p2))
  318.             {
  319.                 r = p1;
  320.                 p2++;
  321.             }
  322.             else
  323.             {
  324.                 r = 0;
  325.             }
  326.         }
  327.         p1++;
  328.     }
  329.     return *p2 == 0 ? r : 0;
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement