Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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:
- moguće je proslijediti stdout izlaz iz nekog programa u njega i vršiti filtriranje na takvom tekstu
- moguće učitati tekstualnu datoteku s diska i vršiti filtriranje na takvom tekstu
- 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.
- 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:
- -i predstavljat će ignore case, tj. ispisat će sve linije koje sadrže tekst neovisno o tome koriste li velika ili mala slova
- -v predstavljat će invert match, tj. ispisat će sve one linije koje ne sadrže traženi tekst
- -c predstavljat će count, tj. neće ispisivati linije već će jednostavno ispisati broj linija koje odgovaraju pretrazi (broj linija koje bi ispisao)
- -n predstavljat će number prefix, tj. ispisat će ispred svake filtrirane linije koja je to linija po redu u originalnom fileu.
- 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.
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<ctype.h>
- void checkforoptions(char* expression, int *ignore, int *invert, int *count, int *number_prefix);
- char *getFilename(char *expression);
- char *getKeyword(char *expression);
- char *strtok_r(char *str, const char *delim, char **nextp);
- void printLine(char *keyword, char *filename, int ignore, int invert, int count, int number_prefix);
- char* stristr(char* str, const char* subStr);
- int main()
- {
- char expression[200];
- int ignore = 0;
- int invert = 0;
- int count = 0;
- int number_prefix = 0;
- char *filename;
- char *keyword;
- FILE *source;
- scanf("%[^\n]%*c", expression);
- checkforoptions(expression, &ignore, &invert, &count, &number_prefix);
- filename = getFilename(expression);
- keyword = getKeyword(expression);
- source = fopen(filename, "r");
- if (source == NULL)
- {
- perror("Error.");
- return (-1);
- }
- printLine(keyword, filename, ignore, invert, count, number_prefix);
- printf("\n");
- fclose(source);
- return 0;
- }
- //FUNKCIJE
- //provjera unesenih opcija
- void checkforoptions(char* expression, int *ignore, int *invert, int *count, int *number_prefix)
- {
- if (strstr(expression, "-i") != NULL) *ignore = 1;
- if (strstr(expression, "-v") != NULL) *invert = 1;
- if (strstr(expression, "-c") != NULL) *count = 1;
- if (strstr(expression, "-n") != NULL) *number_prefix = 1;
- }
- //dohvaćanje imena datoteka, tj. zadnje riječi u naredbi/izrazu kojeg unosimo
- char *getFilename(char *expression)
- {
- char *filename = strrchr(expression, ' ');
- if (filename && *(filename + 1))
- return (filename + 1);
- }
- //dohvaćanje traženog izraza koji se nalazi unutar navodnika
- char *getKeyword(char *expression)
- {
- char *last_pointer;
- char *keyword;
- keyword = strtok_r(expression, "\"", &last_pointer);
- keyword = strtok_r(NULL, "\"", &last_pointer);
- return keyword;
- }
- /* pomoćna funkcija u funkciji getKeyword
- * public domain strtok_r() by Charlie Gordon
- *
- * from comp.lang.c 9/14/2007
- *
- * http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684
- *
- * (Declaration that it's public domain):
- * http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c
- */
- char* strtok_r(char *str, const char *delim, char **nextp)
- {
- char *ret;
- if (str == NULL)
- {
- str = *nextp;
- }
- str += strspn(str, delim);
- if (*str == '\0')
- {
- return NULL;
- }
- ret = str;
- str += strcspn(str, delim);
- if (*str)
- {
- *str++ = '\0';
- }
- *nextp = str;
- return ret;
- }
- void printLine(char *keyword, char *filename, int ignore, int invert, int count, int number_prefix)
- {
- FILE *f1;
- char *substring;
- int linenumber = 0;
- f1 = fopen(filename, "r");
- if (f1 == NULL)
- {
- perror("Error.");
- exit(0);
- }
- char line[256];
- // bez opcija
- if(!ignore && !invert && !count && !number_prefix){
- while (fgets(line, sizeof line, f1) != NULL)
- {
- substring = strstr(line, keyword);
- if (substring!= NULL) {
- printf("%s", line);
- }
- }
- }
- //-n opcija
- else if (number_prefix && !ignore && !invert && !count)
- {
- linenumber = 0;
- while (fgets(line, sizeof line, f1) != NULL)
- {
- ++linenumber;
- substring = strstr(line, keyword);
- if (substring!= NULL) {
- printf("%d:%s", linenumber, line);
- }
- }
- }
- //-n -c opcije i -c opcija
- else if ((number_prefix && count && !ignore && !invert) || (!number_prefix && count && !ignore && !invert))
- {
- int countlines = 0;
- while (fgets(line, sizeof line, f1) != NULL)
- {
- substring = strstr(line, keyword);
- if (substring!= NULL) {
- ++countlines;
- }
- }
- printf("%d\n", countlines);
- }
- //-n -v opcije
- else if (number_prefix && !count && !ignore && invert)
- {
- int linenumber = 0;
- while (fgets(line, sizeof line, f1) != NULL)
- {
- ++linenumber;
- substring = strstr(line, keyword);
- if (substring == NULL) {
- printf("%d:%s", linenumber, line);
- }
- }
- }
- //-n -i opcije
- else if (number_prefix && !count && ignore && !invert)
- {
- int linenumber = 0;
- while (fgets(line, sizeof line, f1) != NULL)
- {
- ++linenumber;
- substring = stristr(line, keyword);
- if (substring!= NULL) {
- printf("%d:%s", linenumber, line);
- }
- }
- }
- //-n -i -c opcije / -i -c opcije
- else if ((number_prefix && count && ignore && !invert) || (!number_prefix && count && ignore && !invert))
- {
- int countlines = 0;
- while (fgets(line, sizeof line, f1) != NULL)
- {
- substring = stristr(line, keyword);
- if (substring!= NULL) {
- ++countlines;
- }
- }
- printf("%d\n", countlines);
- }
- //-n -v -c opcije / -v -c opcije
- else if ((number_prefix && count && !ignore && invert) || (!number_prefix && count && !ignore && invert))
- {
- int countlines = 0;
- while (fgets(line, sizeof line, f1) != NULL)
- {
- substring = stristr(line, keyword);
- if (substring == NULL) {
- ++countlines;
- }
- }
- printf("%d", countlines);
- }
- //-n -v -i opcije
- else if (number_prefix && !count && ignore && invert)
- {
- int linenumber = 0;
- while (fgets(line, sizeof line, f1) != NULL)
- {
- ++linenumber;
- substring = stristr(line, keyword);
- if (substring== NULL) {
- printf("%d:%s", linenumber, line);
- }
- }
- }
- //-n -v -i -c opcije / -v -i -c opcije
- else if ((number_prefix && count && ignore && invert) || (!number_prefix && count && ignore && invert))
- {
- int countlines = 0;
- while (fgets(line, sizeof line, f1) != NULL)
- {
- substring = stristr(line, keyword);
- if (substring == NULL) {
- ++countlines;
- }
- }
- printf("%d\n", countlines);
- }
- //-i opcija
- else if (!number_prefix && !count && ignore && !invert)
- {
- int linenumber = 0;
- while (fgets(line, sizeof line, f1) != NULL)
- {
- ++linenumber;
- substring = stristr(line, keyword);
- if (substring!= NULL) {
- printf("%s", line);
- }
- }
- }
- //-v opcija
- else if (!number_prefix && !count && !ignore && invert)
- {
- int linenumber = 0;
- while (fgets(line, sizeof line, f1) != NULL)
- {
- ++linenumber;
- substring = strstr(line, keyword);
- if (substring == NULL) {
- printf("%s", line);
- }
- }
- }
- //-v -i opcije
- else if (!number_prefix && !count && ignore && invert)
- {
- int linenumber = 0;
- while (fgets(line, sizeof line, f1) != NULL)
- {
- ++linenumber;
- substring = stristr(line, keyword);
- if (substring== NULL) {
- printf("%s", line);
- }
- }
- }
- }
- //case insensitive provjera podstringa unutar stringa
- char* stristr(char* str, const char* subStr)
- {
- char* p1 = str;
- const char* p2 = subStr;
- char* r = *p2 == 0 ? str : 0;
- while (*p1 != 0 && *p2 != 0)
- {
- if (tolower(*p1) == tolower(*p2))
- {
- if (r == 0)
- {
- r = p1;
- }
- p2++;
- }
- else
- {
- p2 = subStr;
- if (tolower(*p1) == tolower(*p2))
- {
- r = p1;
- p2++;
- }
- else
- {
- r = 0;
- }
- }
- p1++;
- }
- return *p2 == 0 ? r : 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement