Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h> // for open()
- #include <errno.h> // for perror()
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/wait.h> //waitpid
- #include <string.h> //for strcpy()
- #include <unistd.h> //for file write() / fork() /pipe()
- #include <time.h>
- struct Kivansag {
- char date[100];
- char name[100];
- char city[100];
- char address[100];
- char gift[100];
- };
- const char *FILE_NAME = "kivansagok.scv";
- FILE *fileContents;
- int list_size = 0;
- struct Kivansag gifts[1000];
- struct Kivansag rowToKivansag(char row[1000]);
- char *kivansagToRow(struct Kivansag kivansag);
- char *swapSpace(char *row);
- int isCorrect(char answer[10]);
- void menu_print();
- void cutEnter(char string[100]);
- struct Kivansag getEmptyKivansag();
- int isEmptyKivansag(struct Kivansag kivansag);
- int main(){
- char msg[100];
- char msg2[100];
- int status;
- if((fileContents= fopen(FILE_NAME, "rb")) == NULL){
- if(errno == ENOENT){//if "kivansagok" file doesn't exists
- printf("A Kivansagok lista fajlja nem letezik! \tKilepes...\n");
- exit(1);
- }
- }else{
- //file read start
- fscanf(fileContents,"%d\n",&list_size); //init list_size
- if(list_size > 0){
- int i;
- for(i = 0; i < list_size; i++){ // init gifts
- struct Kivansag kivansag;
- char row[1000];
- fscanf(fileContents,"%s",row);
- kivansag = rowToKivansag(row);
- gifts[i] = kivansag;
- }
- printf("TESZT*****************\n");
- for(i = 0; i < list_size; i++){ // init gifts
- printf("Datum: %s Nev: %s Varos: %s Pontos cim: %s Ajandek: %s\n",gifts[i].date,gifts[i].name,gifts[i].city,gifts[i].address,gifts[i].gift);
- }
- }
- fclose(fileContents);
- //file read ending
- int notacommonvalue=1;
- printf("The value is %i before forking \n",notacommonvalue);
- int pipe_santa_to_child[2];
- int pipe_child_to_santa[2];
- int pipeee[2];
- if (pipe(pipe_santa_to_child) == -1 || pipe(pipe_child_to_santa) == -1 || pipe(pipeee) == -1) {
- perror("Hiba a pipe nyitaskor!");
- exit(EXIT_FAILURE);
- }
- pid_t child;
- child = fork();
- if (child<0){
- perror("The fork calling was not succesful\n");
- exit(1);
- }
- else if(child > 0){ //santa session
- printf("Mikulas folyamat indul...\n");
- close(pipe_santa_to_child[0]);
- char answer[10];
- int check = 0;
- do{
- menu_print();
- fgets(answer,10, stdin);
- check = isCorrect(answer);
- if(check < 1 || check > 5){
- printf("\nHibas adatbevitel!\n");
- }else{
- if(check == 1){
- write(pipe_santa_to_child[1], "varosos szallitas",18);
- check = 3;
- }
- if(check == 2){
- write(pipe_santa_to_child[1], "222222",7);
- //check = 4;
- }
- }
- }
- while(check != 3);
- close(pipe_santa_to_child[1]);
- fflush(NULL);
- waitpid(child, &status, 0);
- close(pipe_child_to_santa[1]);
- read(pipe_child_to_santa[0], msg2, sizeof(msg2));
- printf("Ezt kuldtek a manok: %s \n", msg2 );
- close(pipe_child_to_santa[0]);
- printf("Mikulas folyamat befejezodik...\n");
- }
- else if(child == 0){ // elf session
- printf("Mano folyamat indul...\n");
- close(pipe_santa_to_child[1]);
- read(pipe_santa_to_child[0], msg, sizeof(msg));
- printf("Ezt kuldte a mikulas: %s \n", msg);
- close(pipe_santa_to_child[0]);
- char to_child[200];
- to_child[0] = '\0';
- //char arr[MAX_NUMBER_STRINGS][MAX_STRING_SIZE];
- char sublogs[1000][1000];
- int log_counter = 0;
- sublogs[log_counter][1000] = '\0';
- if(strcmp(msg,"varosos szallitas") == 0){
- char city[100];
- city[0] = '\0';
- char solution[100];
- int solInt = -1;
- char subgift[100];
- char *oldRows;
- int sub_size = list_size;
- /*printf("Adja meg a varost nevet, ahova az ajandekokat szallitjuk!\n");
- fgets(city,100,stdin);
- cutEnter(city);*/
- int city_counter = 0;
- char citys[1000][1000];
- citys[city_counter][1000] = '\0';
- int j = -1;
- do{
- printf("\n");
- printf("Valaszon az alabbi varosok kozul: \n");
- for(j = 0; j < list_size; j++){
- if(city_counter > 0){
- int k;
- int hadBeen = 0;
- for(k = 0; k < city_counter; ++k){
- if(strcmp(citys[k],gifts[j].city) == 0)
- {
- hadBeen = 1;
- }
- }
- if(hadBeen == 0){
- strcat(citys[city_counter],gifts[j].city);
- ++city_counter;
- }
- }else{
- strcat(citys[city_counter],gifts[j].city);
- ++city_counter;
- }
- }
- for(j = 0; j < city_counter; ++j){
- printf(" (%d) - %s \n", j, citys[j]);
- }
- printf(" (%d) - Kilepes \n", j);
- fgets(solution,100,stdin);
- cutEnter(solution);
- solInt = isCorrect(solution);
- if(solInt < j+1 && solInt >= 0){
- strcat(city,citys[solInt]);
- solInt = j;
- }else{
- printf("\n Hibas parameter! \n");
- }
- }while(solInt != j);
- printf("\n\n Tesztelek : %s\n\n", city);
- int i;
- for(i = 0; i < list_size; i++){
- if(strcmp(city, gifts[i].city)==0){
- subgift[0] = '\0';
- strcat(subgift,gifts[i].gift);
- strcat(sublogs[log_counter],subgift);
- ++log_counter;
- gifts[i] = getEmptyKivansag();
- --sub_size;
- }
- }
- list_size = sub_size;
- fileContents = fopen(FILE_NAME,"wb");
- fprintf(fileContents, "%d\n",list_size);
- for(i = 0; i < list_size; i++){
- if(isEmptyKivansag(gifts[i]) == 0){
- oldRows = kivansagToRow(gifts[i]);
- oldRows = swapSpace(oldRows);
- fprintf(fileContents, "%s",oldRows);
- }
- }
- fclose(fileContents);
- to_child[0] = '\0';
- if(log_counter == 1){
- strcat(to_child,"\n");
- strcat(to_child,city);
- strcat(to_child,",");
- strcat(to_child, sublogs[log_counter-1]);
- strcat(to_child,"\n");
- }else if(log_counter > 1){
- int i;
- for(i = 0; i < log_counter; ++i){
- strcat(to_child,"\n");
- strcat(to_child,city);
- strcat(to_child,",");
- strcat(to_child,sublogs[i]);
- }
- strcat(to_child,"\n");
- }else if(log_counter < 1){
- strcat(to_child,"Nem volt ilyen varos");
- strcat(to_child,"\n");
- }
- }
- log_counter = 0;
- msg[0] = '\0';
- close(pipe_child_to_santa[0]);
- write(pipe_child_to_santa[1], to_child,sizeof(to_child));
- close(pipe_child_to_santa[1]);
- printf("\nMano folyamat befejezodik...\n");
- }
- }
- return 0;
- }
- struct Kivansag getEmptyKivansag(){
- struct Kivansag kivansag;
- strcat(kivansag.gift,"");
- return kivansag;
- }
- int isEmptyKivansag(struct Kivansag kivansag){
- return (strcmp(kivansag.gift,"") == 0);
- }
- char *swapSpace(char *row){
- int count = 0;
- while(*row != '\0'){
- ++count;
- if(*row == ' '){
- *row = '_';
- }
- ++row;
- }
- int i;
- for(i = 0; i < count; i++){
- --row;
- }
- return row;
- }
- char *kivansagToRow(struct Kivansag kivansag){
- char *row;
- row = kivansag.date;
- strcat(row,",");
- strcat(row,kivansag.name);
- strcat(row,",");
- strcat(row,kivansag.city);
- strcat(row,",");
- strcat(row,kivansag.address);
- strcat(row,",");
- strcat(row,kivansag.gift);
- strcat(row,"\n");
- return row;
- }
- void cutEnter(char string[100]){
- char *pos;
- if ((pos=strchr(string, '\n')) != NULL)
- *pos = '\0';
- }
- void menu_print(){
- printf("\n*********************************************************\n");
- printf("Udv a Mikulas kezelofeluleten! Az alabbiak kozul valaszthat:\n"
- " (1) Ajandek kuldese gyujtohelyrol, egy varosba, sok ajandekot\n"
- " (2) Ajandek kuldese uzembol, sok varosba, egy fele ajandekot\n"
- " (3) Kilepes\n");
- }
- int isCorrect(char answer[10]){
- return *answer-48;
- }
- struct Kivansag rowToKivansag(char row[1000]){
- struct Kivansag kivansag;
- int i = 0;
- int j = 0;
- int count = 0;
- int lastPos = 0;
- while(row[i] != '\0'){
- if(row[i] == ','){
- ++count;
- lastPos = j;
- j = 0;
- }else{
- if(count == 0){
- if(row[i] == '_'){
- kivansag.date[j] = ' ';
- }
- else{
- kivansag.date[j] = row[i];
- }
- ++j;
- }
- if(count == 1){
- kivansag.date[lastPos] = '\0';
- if(row[i] == '_'){
- kivansag.name[j] = ' ';
- }
- else{
- kivansag.name[j] = row[i];
- }
- ++j;
- }
- if(count == 2){
- kivansag.name[lastPos] = '\0';
- if(row[i] == '_'){
- kivansag.city[j] = ' ';
- }
- else{
- kivansag.city[j] = row[i];
- }
- ++j;
- }
- if(count == 3){
- kivansag.city[lastPos] = '\0';
- if(row[i] == '_'){
- kivansag.address[j] = ' ';
- }
- else{
- kivansag.address[j] = row[i];
- }
- ++j;
- }
- if(count == 4){
- kivansag.address[lastPos] = '\0';
- if(row[i] == '_'){
- kivansag.gift[j] = ' ';
- }
- else{
- kivansag.gift[j] = row[i];
- }
- ++j;
- }
- }
- ++i;
- }
- kivansag.gift[j] = '\0';
- return kivansag;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement