Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct list_item
- {
- char *value;
- struct list_item *next;
- }list_item;
- void print_help()
- {
- printf("Program sluzy do sprawdzania wystapien wrostka w pliku tekstowym.\n");
- printf("Jako argumenty nalezy podac nazwe pliku o rozszerzeniu .txt i tekst wrostka.\n");
- printf("Wrostek i slowa w pliku tekstowym musza skladac sie w pelni z wartosci alfabetycznych.\n");
- printf("Podanie nieprawidlowych parametrow skutkuje wyswietleniem tej wiadomosci i zamknieciem programu.\n\n");
- }
- int is_alphabetical(char* string)
- {
- int i;
- for (i = 0; i<strlen(string); i++)
- {
- if ((string[i] < 'A') || (string[i] > 'Z' && string[i] < 'a') || (string[i] > 'z'))
- {
- return 0;
- }
- }
- return 1;
- }
- list_item* create_item(char* string)
- {
- list_item* new_item = malloc(sizeof(list_item));
- new_item->value = string;
- new_item->next = 0;
- return new_item;
- }
- void add_to_list(list_item** item, char* string)
- {
- list_item *ptr, *new_item = create_item(string);
- //Jesli lista nie jest pusta
- if (*item)
- {
- ptr = *item;
- //Przejdz do konca listy
- while (ptr->next)
- {
- ptr = ptr->next;
- }
- //Dodaj element na koncu listy
- ptr->next = new_item;
- }
- else
- {
- //Dodaj pierwszy element listy
- *item = new_item;
- }
- }
- char* cut_string(char *input)
- {
- char* output = (char*)malloc((strlen(input) + 1)*sizeof(char));
- strcpy(output, input);
- output += 1;
- output[strlen(output) - 1] = '\0';
- return output;
- }
- int* count_substrings(char* base, char* substring)
- {
- //Pierwsza pozycja zawiera ilosc wrostkow, kolejne ich pozycje
- int* results = (int*)malloc(sizeof(int));
- const char *tmp_base = base;
- int count = 0;
- while (tmp_base == strstr(tmp_base, substring))
- {
- count++;
- void *tmp = realloc(results, (count + sizeof(int)));
- if (!tmp)
- {
- return 0;
- }
- else
- {
- results = tmp;
- }
- results[count] = tmp_base - base;
- tmp_base += strlen(substring);
- }
- results[0] = count;
- return results;
- }
- char* cut_string_length(char* input, int pos, int length)
- {
- int i;
- char* output = (char*)malloc((length+1)*sizeof(char));
- for(i = 0; i < length; i++)
- {
- output[i] = input[pos];
- pos++;
- }
- output[length] = 0;
- return output;
- }
- char* load_string_from_file(char* filename)
- {
- FILE *file = fopen(filename, "rb");
- fseek(file, 0, SEEK_END);
- long fsize = ftell(file);
- rewind(file);
- char *response = malloc(fsize + 1);
- fread(response, fsize, 1, file);
- fclose(file);
- response[fsize] = 0;
- return response;
- }
- list_item* load_list_from_string(char* string)
- {
- list_item* item;
- char* word;
- int count = strlen(string);
- int word_length = 0, word_count = 0, i;
- for(i = 0; i < count; i++)
- {
- if ((string[i] <= 'z' && string[i] >= 'a') || (string[i] <= 'Z' && string[i] >= 'A'))
- {
- word_length++;
- }
- else if (word_length > 0)
- {
- if(word_count == 0)
- {
- item = malloc(sizeof(list_item));
- }
- else
- {
- word = cut_string_length(string, i, word_length);
- add_to_list(&item, word);
- printf("%s", word);
- }
- word_count++;
- word_length = 0;
- }
- }
- return item;
- }
- int main(int argc, char* argv[])
- {
- int flag_help = 0, flag_error = 0, arg_file = 0, arg_text = 0, arg_help = 0, i = 0;
- //Znajdz parametry - moga byc podane w dowolnej kolejnosci
- //Pomin pierwszy parametr - nazwe pliku
- for (i = 1; i < argc; i++)
- {
- if (strstr(argv[i], ".txt")) arg_file = i;
- if (strstr(argv[i], "-h"))
- {
- arg_help = i;
- flag_help = 1;
- }
- if (arg_text == arg_file || arg_text == arg_help) arg_text++;
- }
- if ((arg_help + arg_file) == 0) flag_error = 1;
- while (arg_text < argc)
- {
- if (!is_alphabetical(argv[arg_text]))
- {
- arg_text++;
- flag_error = 1;
- }
- else
- {
- flag_error = 0;
- break;
- }
- }
- if (flag_error)
- {
- print_help();
- return 0;
- }
- if (flag_help) print_help();
- printf("Nazwa pliku : %s\n", argv[arg_file]);
- printf("Poszukiwany wrostek : %s\n\n", argv[arg_text]);
- printf("Nacisnij Enter, aby potwierdzic.\n");
- if (getchar() == 10)
- {
- //Wybrano enter, wykonaj operacje
- //list_item *strings_to_scan = load_list_from_file(argv[arg_file]);
- char* text = load_string_from_file(argv[arg_file]);
- if (text)
- {
- list_item* item = load_list_from_string(text);
- free(text);
- }
- printf("\n\nNacisnij dowolny przycisk, aby zakonczyc dzialanie programu.");
- }
- else
- {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement