Advertisement
weeez

bead2 - félkész

Nov 22nd, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h> // for open()
  4. #include <errno.h> // for perror()
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/wait.h> //waitpid
  8. #include <string.h> //for strcpy()
  9. #include <unistd.h> //for file write() / fork() /pipe()
  10. #include <time.h>
  11.  
  12. struct Kivansag {
  13.   char date[100];
  14.   char name[100];
  15.   char city[100];
  16.   char address[100];
  17.   char gift[100];
  18. };
  19.  
  20. const char *FILE_NAME = "kivansagok.scv";
  21. FILE *fileContents;
  22. int list_size = 0;
  23. struct Kivansag gifts[1000];  
  24.  
  25. struct Kivansag rowToKivansag(char row[1000]);
  26. char *kivansagToRow(struct Kivansag kivansag);
  27. char *swapSpace(char *row);
  28. int isCorrect(char answer[10]);
  29. void menu_print();
  30. void cutEnter(char string[100]);
  31. struct Kivansag getEmptyKivansag();
  32. int isEmptyKivansag(struct Kivansag kivansag);
  33.  
  34. int main(){
  35.        
  36.   char msg[100];
  37.   char msg2[100];
  38.   int status;
  39.  
  40.  
  41.     if((fileContents= fopen(FILE_NAME, "rb")) == NULL){
  42.         if(errno == ENOENT){//if "kivansagok" file doesn't exists
  43.             printf("A Kivansagok lista fajlja nem letezik! \tKilepes...\n");
  44.             exit(1);
  45.         }
  46.     }else{
  47.         //file read start
  48.         fscanf(fileContents,"%d\n",&list_size); //init list_size
  49.        
  50.         if(list_size > 0){
  51.             int i; 
  52.                
  53.             for(i = 0; i < list_size; i++){ // init gifts
  54.                 struct Kivansag kivansag;
  55.                 char row[1000];
  56.                 fscanf(fileContents,"%s",row);
  57.                 kivansag = rowToKivansag(row);
  58.                 gifts[i] = kivansag;
  59.             }
  60.  
  61.             printf("TESZT*****************\n");
  62.             for(i = 0; i < list_size; i++){ // init gifts
  63.                 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);               
  64.             }
  65.  
  66.         }
  67.         fclose(fileContents);  
  68.         //file read ending
  69.                
  70.         int notacommonvalue=1;
  71.         printf("The value is %i before forking \n",notacommonvalue);       
  72.        
  73.         int pipe_santa_to_child[2];
  74.         int pipe_child_to_santa[2];
  75.         int pipeee[2];
  76.        
  77.         if (pipe(pipe_santa_to_child) == -1 || pipe(pipe_child_to_santa) == -1 || pipe(pipeee) == -1) {
  78.             perror("Hiba a pipe nyitaskor!");
  79.             exit(EXIT_FAILURE);
  80.         }
  81.        
  82.         pid_t child;
  83.         child = fork();
  84.        
  85.                
  86.         if (child<0){
  87.             perror("The fork calling was not succesful\n");
  88.             exit(1);
  89.         }
  90.         else if(child > 0){ //santa session
  91.             printf("Mikulas folyamat indul...\n");
  92.            
  93.             close(pipe_santa_to_child[0]);
  94.            
  95.             char answer[10];
  96.             int check = 0;
  97.             do{  
  98.                 menu_print();
  99.                 fgets(answer,10, stdin);
  100.  
  101.                 check = isCorrect(answer);
  102.  
  103.                 if(check < 1 || check > 5){
  104.                     printf("\nHibas adatbevitel!\n");
  105.                 }else{   
  106.                     if(check == 1){                    
  107.                         write(pipe_santa_to_child[1], "varosos szallitas",18);                     
  108.                         check = 3;
  109.                     }
  110.                     if(check == 2){
  111.                         write(pipe_santa_to_child[1], "222222",7);
  112.                         //check = 4;
  113.                     }
  114.  
  115.                 }
  116.             }
  117.             while(check != 3);                                   
  118.                                
  119.             close(pipe_santa_to_child[1]);
  120.                  
  121.             fflush(NULL);
  122.             waitpid(child, &status, 0);
  123.            
  124.             close(pipe_child_to_santa[1]);
  125.             read(pipe_child_to_santa[0], msg2, sizeof(msg2));
  126.             printf("Ezt kuldtek a manok: %s \n", msg2 );
  127.             close(pipe_child_to_santa[0]);
  128.                                
  129.             printf("Mikulas folyamat befejezodik...\n");
  130.        
  131.         }
  132.         else if(child == 0){ // elf session                
  133.             printf("Mano folyamat indul...\n");
  134.            
  135.             close(pipe_santa_to_child[1]);
  136.             read(pipe_santa_to_child[0], msg, sizeof(msg));
  137.             printf("Ezt kuldte a mikulas: %s \n", msg);
  138.             close(pipe_santa_to_child[0]);
  139.            
  140.             char to_child[200];
  141.             to_child[0] = '\0';
  142.             //char arr[MAX_NUMBER_STRINGS][MAX_STRING_SIZE];           
  143.             char sublogs[1000][1000];
  144.             int log_counter = 0;           
  145.             sublogs[log_counter][1000] = '\0';
  146.            
  147.            
  148.             if(strcmp(msg,"varosos szallitas") == 0){
  149.                 char city[100];
  150.                 city[0] = '\0';
  151.                 char solution[100];
  152.                 int solInt = -1;
  153.                 char subgift[100];             
  154.                 char *oldRows;                     
  155.                 int sub_size = list_size;
  156.                
  157.                 /*printf("Adja meg a varost nevet, ahova az ajandekokat szallitjuk!\n");
  158.                 fgets(city,100,stdin);
  159.                 cutEnter(city);*/
  160.                
  161.                 int city_counter = 0;
  162.                 char citys[1000][1000];
  163.                 citys[city_counter][1000] = '\0';
  164.                 int j = -1;
  165.                 do{
  166.                     printf("\n");
  167.                     printf("Valaszon az alabbi varosok kozul: \n");                
  168.                     for(j = 0; j < list_size; j++){
  169.                         if(city_counter > 0){
  170.                             int k;
  171.                             int hadBeen = 0;
  172.                             for(k = 0; k < city_counter; ++k){
  173.                                 if(strcmp(citys[k],gifts[j].city) == 0)
  174.                                 {
  175.                                     hadBeen = 1;
  176.                                 }                          
  177.                             }
  178.                             if(hadBeen == 0){
  179.                                 strcat(citys[city_counter],gifts[j].city);
  180.                                 ++city_counter;
  181.                             }
  182.                         }else{
  183.                             strcat(citys[city_counter],gifts[j].city);
  184.                             ++city_counter;
  185.                         }
  186.                     }              
  187.                     for(j = 0; j < city_counter; ++j){
  188.                         printf("    (%d) - %s \n", j, citys[j]);
  189.                     }
  190.                     printf("    (%d) - Kilepes \n", j);
  191.                     fgets(solution,100,stdin);
  192.                     cutEnter(solution);
  193.                     solInt = isCorrect(solution);                  
  194.                     if(solInt < j+1 && solInt >= 0){
  195.                         strcat(city,citys[solInt]);
  196.                         solInt = j;
  197.                     }else{
  198.                         printf("\n Hibas parameter! \n");
  199.                     }
  200.                 }while(solInt != j);
  201.                
  202.                 printf("\n\n Tesztelek : %s\n\n", city);
  203.                
  204.                
  205.                 int i;
  206.                 for(i = 0; i < list_size; i++){
  207.                     if(strcmp(city, gifts[i].city)==0){                            
  208.                         subgift[0] = '\0';
  209.                         strcat(subgift,gifts[i].gift); 
  210.                         strcat(sublogs[log_counter],subgift);                      
  211.                         ++log_counter;
  212.                         gifts[i] = getEmptyKivansag();                             
  213.                         --sub_size;
  214.                     }
  215.                 }
  216.                
  217.                 list_size = sub_size;
  218.                
  219.                 fileContents = fopen(FILE_NAME,"wb");      
  220.                 fprintf(fileContents, "%d\n",list_size);
  221.                 for(i = 0; i < list_size; i++){
  222.                     if(isEmptyKivansag(gifts[i]) == 0){
  223.                         oldRows = kivansagToRow(gifts[i]);
  224.                         oldRows = swapSpace(oldRows);
  225.                         fprintf(fileContents, "%s",oldRows);   
  226.                     }
  227.                 }
  228.                 fclose(fileContents);    
  229.                
  230.                 to_child[0] = '\0';
  231.                 if(log_counter == 1){
  232.                     strcat(to_child,"\n"); 
  233.                     strcat(to_child,city);
  234.                     strcat(to_child,",");
  235.                     strcat(to_child, sublogs[log_counter-1]);              
  236.                     strcat(to_child,"\n"); 
  237.                    
  238.                 }else if(log_counter > 1){
  239.                     int i;
  240.                     for(i = 0; i < log_counter; ++i){
  241.                         strcat(to_child,"\n");
  242.                         strcat(to_child,city);
  243.                         strcat(to_child,",");
  244.                         strcat(to_child,sublogs[i]);                                       
  245.                     }
  246.                     strcat(to_child,"\n");
  247.                 }else if(log_counter < 1){                                 
  248.                     strcat(to_child,"Nem volt ilyen varos");                   
  249.                     strcat(to_child,"\n");     
  250.                 }              
  251.                
  252.             }
  253.            
  254.             log_counter = 0;
  255.             msg[0] = '\0';
  256.            
  257.             close(pipe_child_to_santa[0]);
  258.             write(pipe_child_to_santa[1], to_child,sizeof(to_child));
  259.             close(pipe_child_to_santa[1]);
  260.  
  261.             printf("\nMano folyamat befejezodik...\n");
  262.         }
  263.        
  264.  
  265.     }      
  266.    
  267.     return 0;
  268. }
  269.  
  270. struct Kivansag getEmptyKivansag(){
  271.     struct Kivansag kivansag;
  272.     strcat(kivansag.gift,"");
  273.     return kivansag;
  274. }
  275.  
  276. int isEmptyKivansag(struct Kivansag kivansag){
  277.     return (strcmp(kivansag.gift,"") == 0);
  278. }
  279.  
  280. char *swapSpace(char *row){
  281.     int count = 0;
  282.     while(*row != '\0'){
  283.         ++count;
  284.         if(*row == ' '){
  285.             *row = '_';
  286.         }
  287.         ++row;
  288.     }
  289.     int i;
  290.     for(i = 0; i < count; i++){
  291.         --row;
  292.     }
  293.     return row;
  294. }
  295.  
  296. char *kivansagToRow(struct Kivansag kivansag){
  297.     char *row;
  298.     row = kivansag.date;   
  299.     strcat(row,",");
  300.     strcat(row,kivansag.name); 
  301.     strcat(row,",");
  302.     strcat(row,kivansag.city); 
  303.     strcat(row,",");
  304.     strcat(row,kivansag.address);
  305.     strcat(row,",");
  306.     strcat(row,kivansag.gift); 
  307.     strcat(row,"\n");
  308.     return row;
  309. }
  310.  
  311. void cutEnter(char string[100]){
  312.     char *pos;
  313.     if ((pos=strchr(string, '\n')) != NULL)
  314.         *pos = '\0';
  315. }
  316.  
  317. void menu_print(){
  318.     printf("\n*********************************************************\n");
  319.                 printf("Udv a Mikulas kezelofeluleten! Az alabbiak kozul valaszthat:\n"
  320.                 "  (1) Ajandek kuldese gyujtohelyrol, egy varosba, sok ajandekot\n"
  321.                 "  (2) Ajandek kuldese uzembol, sok varosba, egy fele ajandekot\n"                         
  322.                 "  (3) Kilepes\n");
  323. }
  324.  
  325. int isCorrect(char answer[10]){
  326.    return *answer-48;
  327. }
  328.  
  329. struct Kivansag rowToKivansag(char row[1000]){
  330.     struct Kivansag kivansag;
  331.     int i = 0;
  332.     int j = 0;
  333.     int count = 0;
  334.     int lastPos = 0;
  335.     while(row[i] != '\0'){
  336.         if(row[i] == ','){
  337.             ++count;
  338.             lastPos = j;
  339.             j = 0;
  340.         }else{
  341.             if(count == 0){        
  342.                 if(row[i] == '_'){
  343.                     kivansag.date[j] = ' ';
  344.                 }
  345.                 else{
  346.                     kivansag.date[j] = row[i];
  347.                 }
  348.                 ++j;
  349.             }
  350.             if(count == 1){
  351.                 kivansag.date[lastPos] = '\0';
  352.                 if(row[i] == '_'){
  353.                     kivansag.name[j] = ' ';
  354.                 }
  355.                 else{
  356.                     kivansag.name[j] = row[i];
  357.                 }
  358.                 ++j;
  359.             }
  360.             if(count == 2){
  361.                 kivansag.name[lastPos] = '\0';
  362.                 if(row[i] == '_'){
  363.                     kivansag.city[j] = ' ';
  364.                 }
  365.                 else{
  366.                     kivansag.city[j] = row[i];
  367.                 }
  368.                 ++j;
  369.             }
  370.             if(count == 3){
  371.                 kivansag.city[lastPos] = '\0';
  372.                 if(row[i] == '_'){
  373.                     kivansag.address[j] = ' ';
  374.                 }
  375.                 else{
  376.                     kivansag.address[j] = row[i];
  377.                 }
  378.                 ++j;
  379.             }
  380.             if(count == 4){
  381.                 kivansag.address[lastPos] = '\0';
  382.                 if(row[i] == '_'){
  383.                     kivansag.gift[j] = ' ';
  384.                 }
  385.                 else{
  386.                     kivansag.gift[j] = row[i];
  387.                 }
  388.                 ++j;
  389.             }      
  390.         }
  391.         ++i;
  392.     }          
  393.     kivansag.gift[j] = '\0';   
  394.     return kivansag;
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement