Advertisement
weeez

bead2 - dunci

Dec 5th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.85 KB | None | 0 0
  1. András
  2.  
  3.  
  4. Search Drive
  5.  
  6. Drive
  7. .
  8. Folder Path
  9. My Drive
  10. Egyetem
  11. NEW
  12. Folders and views
  13. My Drive
  14. Shared with me
  15. Google Photos
  16. Recent
  17. Starred
  18. Bin
  19. 530 MB of 15 GB used
  20. Upgrade storage
  21. .
  22. Java
  23. .
  24.  
  25. Google Sheets
  26. BA Órarend
  27.  
  28. Text
  29. bead.txt
  30.  
  31. Google Docs
  32. Korábbi pluszminusz
  33.  
  34. Google Sheets
  35. Követelmények
  36.  
  37. Google Docs
  38. pluszminusz
  39.  
  40. Google Sheets
  41. Tárgyaim megléte
  42. Text
  43. bead.txt
  44. Details
  45. Activity
  46. YESTERDAY
  47.  
  48. Youuploaded an item
  49. Fri 13:03
  50. Text
  51. bead.txt
  52. No recorded activity before 4 December 2015
  53. Get Drive for PC
  54.  
  55. #include <stdio.h>
  56. #include <string.h>
  57. #include <stdlib.h>
  58. #include <time.h>
  59. #include <errno.h>
  60. #include <unistd.h>
  61. #include <sys/types.h>
  62. #include <sys/stat.h>
  63. #include <fcntl.h>
  64. #include <wait.h>
  65. #include <sys/ipc.h>
  66. #include <sys/msg.h>
  67.  
  68. #define CHAR_SIZE 512
  69.  
  70. typedef enum { false, true } bool;
  71.  
  72. typedef struct {
  73.     char date[CHAR_SIZE];
  74.     char name[CHAR_SIZE];
  75.     char city[CHAR_SIZE];
  76.     char address[CHAR_SIZE];
  77.     char gift_name[CHAR_SIZE];
  78. } request_data;
  79.  
  80. struct message {
  81.      long mtype; //ez egy szabadon hasznalhato ertek, pl uzenetek osztalyozasara
  82.      char mtext[CHAR_SIZE];
  83. };
  84.  
  85. void write_list(request_data* requests, int number_of_request);
  86. int write_list_with_filter_city(char* filter, request_data* requests, int number_of_request, char* msg[], bool b);
  87. int write_list_with_filter_gift(char* filter, request_data* requests, int number_of_request, char* msg[], bool b);
  88. void create_new_request(request_data* requests, int* number_of_request);
  89. void write_list_to_file(request_data* requests, int number_of_request, const char* file_name);
  90. bool check_gift_type(const char* gift_type);
  91. void read_from_file(const char* file_name, request_data* requests, int* number_of_request);
  92. int check_request(request_data* requests, int number_of_request, request_data request);
  93. int send_message(int msg_id, char* msg_string);
  94. void deliver_gifts(char* filter, char* name, request_data* requests, int number_of_request, char* msg[], const char* argv[]);
  95. void write_list_to_file_mikulas(const char* file_name, char* msg);
  96.  
  97. int main(int argc, const char * argv[]) {
  98.  
  99.     while(true) {
  100.         char* msg[CHAR_SIZE];
  101.         char tmp[CHAR_SIZE];
  102.         int number_of_request;
  103.         request_data requests[512];
  104.         read_from_file("data.txt", requests, &number_of_request);
  105.         printf("\nKerem valasszon az alabbi lehetosegek kozul:\n");
  106.         printf("- Uj keres felvetele(ujkeres)\n");
  107.         printf("- Lista kiirasa(kiiras)\n");
  108.         printf("- Listaba kereses(keres)\n");
  109.         printf("- Ajandekok kiszallitasa(szallit)\n");
  110.         printf("- Kilepes (exit)\n\n");
  111.         fgets(tmp, sizeof(tmp), stdin);
  112.         if(strncmp(tmp, "kiiras", 6) == 0) {
  113.             write_list(requests, number_of_request);
  114.         }
  115.         if(strncmp(tmp, "keres", 5) == 0) {
  116.             printf("- Mi szerint szeretne keresni?(varos/ajandek)\n");
  117.             fgets(tmp, sizeof(tmp), stdin);
  118.             if(strncmp(tmp, "varos", 5) == 0) {
  119.                 printf("- Kerem a varos nevet\n");
  120.                 fgets(tmp, sizeof(tmp), stdin);
  121.                 write_list_with_filter_city(tmp, requests, number_of_request, msg, true);
  122.             } else if(strncmp(tmp, "ajandek", 7) == 0) {
  123.                 printf("- Kerem az ajandek nevet\n");
  124.                 fgets(tmp, sizeof(tmp), stdin);
  125.                 write_list_with_filter_gift(tmp, requests, number_of_request, msg, true);
  126.             }
  127.         }
  128.        
  129.         if(strncmp(tmp, "szallit", 7) == 0) {
  130.             printf("- Mi szerint szeretne szallitani?(varos/ajandek)\n");
  131.             fgets(tmp, sizeof(tmp), stdin);
  132.             if(strncmp(tmp, "varos", 5) == 0) {
  133.                 printf("- Kerem a varos nevet\n");
  134.                 fgets(tmp, sizeof(tmp), stdin);
  135.                 deliver_gifts("varos", tmp, requests, number_of_request, msg, argv);
  136.             } else if(strncmp(tmp, "ajandek", 7) == 0) {
  137.                 printf("- Kerem az ajandek nevet\n");
  138.                 fgets(tmp, sizeof(tmp), stdin);
  139.                 deliver_gifts("ajandek", tmp, requests, number_of_request, msg, argv);
  140.             }
  141.         }
  142.  
  143.         if(strncmp(tmp, "ujkeres", 7) == 0) {
  144.             create_new_request(requests, &number_of_request);
  145.             write_list_to_file(requests, number_of_request, "data.txt");
  146.         }
  147.  
  148.         if(strncmp(tmp, "exit", 4) == 0) {
  149.             break;
  150.         }      
  151.     }
  152.     return 0;
  153. }
  154.  
  155. void create_new_request(request_data* requests, int* number_of_request) {
  156.     bool end = false;
  157.     char tmp[CHAR_SIZE];
  158.     while(!end) {
  159.         request_data request;
  160.         int i = 0;
  161.         while(i < 5) {
  162.             if(i == 0) {
  163.                 time_t timer;
  164.                 char buffer[CHAR_SIZE];
  165.                 struct tm* tm_info;
  166.                 time(&timer);
  167.                 tm_info = localtime(&timer);
  168.                 strftime(buffer, 26, "%Y.%m.%d. %H:%M:%S\n", tm_info);
  169.                 strcpy(request.date, buffer);
  170.             } else if(i == 1) {
  171.                 printf("\nKerem a gyermek nevet!:(Jozsika)\n");
  172.                 fgets(tmp, sizeof(tmp), stdin);
  173.                 strcpy(request.name, tmp);
  174.             } else if(i == 2) {
  175.                 printf("Kerem a ajandek nevet!:(Baba Konyv)\n");
  176.                 fgets(tmp, sizeof(tmp), stdin);
  177.                 bool gift_type = check_gift_type(tmp);
  178.                 while(gift_type == false) {
  179.                     printf("Kerem a ajandek nevet!:(Baba Konyv)\n");
  180.                     fgets(tmp, sizeof(tmp), stdin);
  181.                     gift_type = check_gift_type(tmp);
  182.                 }
  183.                 strcpy(request.gift_name, tmp);
  184.             } else if(i == 3) {
  185.                 printf("Kerem a varos nevet!:(Budapest)\n");
  186.                 fgets(tmp, sizeof(tmp), stdin);
  187.                 strcpy(request.city, tmp);
  188.             } else if(i == 4) {
  189.                 printf("Kerem a pontos cimet!:(Almafa utca 1)\n");
  190.                 fgets(tmp, sizeof(tmp), stdin);
  191.                 strcpy(request.address, tmp);
  192.  
  193.                 int index = check_request(requests, *number_of_request, request);
  194.  
  195.                 if(index == -1) {
  196.                     requests[*number_of_request] = request;
  197.                     ++(*number_of_request);
  198.                 } else {
  199.                     strcpy(requests[index].name, request.name);
  200.                     strcpy(requests[index].address, request.address);
  201.                     strcpy(requests[index].gift_name, request.gift_name);
  202.                     strcpy(requests[index].city, request.city);
  203.                     strcpy(requests[index].date, request.date);
  204.                 }
  205.             }
  206.             i++;
  207.         }
  208.         printf("Szeretne meg kerest rogziteni?(y/n)\n");
  209.         fgets(tmp, sizeof(tmp), stdin);
  210.         const char* yes = "y";
  211.         if(strncmp(tmp, yes, 1) != 0) {
  212.             end = true;
  213.         }
  214.     }
  215. }
  216.  
  217. void deliver_gifts(char* filter, char* name, request_data* requests, int number_of_request, char* msg[], const char* argv[]) {
  218.     int pid, fd;
  219.     int fid = mkfifo ("pipe", 0666);
  220.     int uzenetsor, status;
  221.     key_t key;
  222.     key = ftok(argv[0], 1);
  223.     uzenetsor = msgget(key, 0600 | IPC_CREAT);
  224.    
  225.     if (fid == -1) {
  226.         printf("Error number: %i",errno);
  227.         exit(EXIT_FAILURE);
  228.     }
  229.  
  230.     pid = fork();
  231.     if (pid > 0) {
  232.         int wait_status;
  233.         fd = open("pipe", O_WRONLY);
  234.         write(fd, filter, sizeof(filter));
  235.         close(fd);
  236.        
  237.         waitpid(pid, &wait_status, -1);
  238.        
  239.         //üzenet
  240.         struct message uz;
  241.         status = msgrcv(uzenetsor, &uz, CHAR_SIZE, 5, 0 );
  242.         if(status >= 0) {
  243.             printf("A mikulas megkapta a manoktol a listat es kiirta egy fajlba!\nBoldog karacsonyt gyerekek!\n");
  244.             if(strncmp(filter, "varos", 5) == 0) {
  245.                 write_list_to_file_mikulas("varos.txt", uz.mtext);
  246.             } else {
  247.                 write_list_to_file_mikulas("ajandek.txt", uz.mtext);
  248.             }
  249.            
  250.         } else {
  251.             perror("hiba");
  252.         }
  253.        
  254.     } else {
  255.         char msg_from_santa[CHAR_SIZE];
  256.         int amount;
  257.         fd = open("pipe", O_RDONLY);
  258.         read(fd, msg_from_santa, sizeof(msg_from_santa));
  259.         if (strncmp("varos", msg_from_santa, 5) == 0) {
  260.             amount = write_list_with_filter_city(name, requests, number_of_request, msg, false);
  261.         } else if (strncmp("ajandek", msg_from_santa, 7) == 0) {
  262.             amount = write_list_with_filter_gift(name, requests, number_of_request, msg, false);
  263.         }
  264.  
  265.         char tmp[CHAR_SIZE];
  266.         int i;
  267.         for(i = 0; i < amount; i++) {
  268.             if(i == 0) {
  269.                 strcpy(tmp, msg[i]);
  270.             } else {
  271.                 strcat(tmp, msg[i]);
  272.             }  
  273.         }
  274.  
  275.         send_message(uzenetsor, tmp);
  276.         wait( NULL );
  277.         status = msgctl( uzenetsor, IPC_RMID, NULL );
  278.         if ( status < 0 ) {
  279.             perror("msgctl");
  280.         }
  281.         close(fd);
  282.         unlink("pipe");
  283.         exit(0);
  284.     }  
  285. }
  286.  
  287. int send_message(int msg_id, char* msg_string) {
  288.     struct message msg;
  289.     strcpy(msg.mtext, msg_string);
  290.     msg.mtype = 5;
  291.     int status;
  292.     status = msgsnd(msg_id, &msg, strlen (msg.mtext) + 1 , 0);
  293.     if ( status < 0 )
  294.         perror("msgsnd");
  295.     return 0;
  296. }
  297.  
  298. void read_from_file(const char* file_name, request_data* requests, int* number_of_request) {
  299.     FILE *file;
  300.     *number_of_request = 0;
  301.     char tmp[512];
  302.     file = fopen(file_name, "r");
  303.     int rows = 0;
  304.     request_data request;
  305.     while (fgets(tmp, 100, file) != NULL) {
  306.         if(strcmp(tmp, "\n") != 0) {
  307.             if(rows == 0) {
  308.             strcpy(request.date, tmp);
  309.             } else if(rows == 1) {
  310.                 strcpy(request.name, tmp);
  311.             } else if(rows == 2) {
  312.                 strcpy(request.gift_name, tmp);
  313.             } else if(rows == 3) {
  314.                 strcpy(request.city, tmp);
  315.             } else if(rows == 4) {
  316.                 rows = -1;
  317.                 strcpy(request.address, tmp);
  318.                 requests[*number_of_request] = request;
  319.                 ++(*number_of_request);
  320.             }
  321.             rows++;
  322.         }
  323.     }
  324.     fclose(file);
  325. }
  326.  
  327. void write_list(request_data* requests, int number_of_request) {
  328.     int i = 0;
  329.     for(i = 0; i < number_of_request; i++) {
  330.         printf("%s%s%s%s%s\n",
  331.         requests[i].date, requests[i].name,
  332.         requests[i].gift_name, requests[i].city, requests[i].address);
  333.     }
  334.     printf("A manok %d kisgyermek kivansagat rogzitettek!\n", i);
  335. }
  336.  
  337. int write_list_with_filter_city(char* filter, request_data* requests, int number_of_request, char* msg[], bool b) {
  338.     int i;
  339.     int amount = 0;
  340.     for(i = 0; i < number_of_request; i++) {
  341.         if(strcmp(filter, requests[i].city) == 0) {
  342.             char tmp[CHAR_SIZE];
  343.             strcpy(tmp, requests[i].date);
  344.             strcat(tmp, requests[i].name);
  345.             strcat(tmp, requests[i].gift_name);
  346.             strcat(tmp, requests[i].city);
  347.             strcat(tmp, requests[i].address);
  348.             strcat(tmp, "\n");
  349.             msg[amount] = tmp;
  350.             if(b) {
  351.                 printf("%s%s%s%s%s\n",
  352.                 requests[i].date, requests[i].name,
  353.                 requests[i].gift_name, requests[i].city, requests[i].address);  
  354.             }
  355.             amount++;
  356.         }
  357.     }
  358.     if(b) printf("%d kisgyermek kivansaga erkezett a varosbol!\n", amount);
  359.     return amount;
  360. }
  361.  
  362. int write_list_with_filter_gift(char* filter, request_data* requests, int number_of_request, char* msg[], bool b){
  363.     int i;
  364.     int amount = 0;
  365.     for(i = 0; i < number_of_request; i++) {
  366.         if(strcmp(filter, requests[i].gift_name) == 0) {
  367.             char tmp[CHAR_SIZE];
  368.             strcpy(tmp, requests[i].date);
  369.             strcat(tmp, requests[i].name);
  370.             strcat(tmp, requests[i].gift_name);
  371.             strcat(tmp, requests[i].city);
  372.             strcat(tmp, requests[i].address);
  373.             strcat(tmp, "\n");
  374.             msg[amount] = tmp;
  375.             if(b) {
  376.                 printf("%s%s%s%s%s\n",
  377.                 requests[i].date, requests[i].name,
  378.                 requests[i].gift_name, requests[i].city, requests[i].address);  
  379.             }
  380.             amount++;
  381.         }
  382.     }
  383.     if(b) printf("%d darab ajandek van a listan!\n", amount);
  384.     return amount;
  385. }
  386.  
  387. bool check_gift_type(const char* gift_type) {
  388.     if(strncmp("Baba", gift_type, 4) == 0) { return true; }
  389.     if(strncmp("Auto", gift_type, 4) == 0) { return true; }
  390.     if(strncmp("Labda", gift_type, 5) == 0) { return true; }
  391.     if(strncmp("Kirako", gift_type, 6) == 0) { return true; }
  392.     if(strncmp("Szinezo", gift_type, 7) == 0) { return true; }
  393.     if(strncmp("Konyv", gift_type, 5) == 0) { return true; }
  394.     return false;
  395. }
  396.  
  397. void write_list_to_file(request_data* requests, int number_of_request, const char* file_name) {
  398.     FILE *file;
  399.     file = fopen(file_name, "w");
  400.     int i;
  401.     for(i = 0; i < number_of_request; i++) {
  402.         fprintf(file, "%s%s%s%s%s\n",
  403.             requests[i].date, requests[i].name,
  404.             requests[i].gift_name, requests[i].city, requests[i].address);
  405.     }
  406.     fclose(file);
  407. }
  408.  
  409. void write_list_to_file_mikulas(const char* file_name, char* msg) {
  410.     FILE *file;
  411.     file = fopen(file_name, "w");
  412.     fprintf(file, "%s", msg);
  413.     fclose(file);
  414. }
  415.  
  416. int check_request(request_data* requests, int number_of_request, request_data request) {
  417.     int i;
  418.     for(i = 0; i < number_of_request; i++) {
  419.         if(strcmp(requests[i].name, request.name) == 0) {
  420.             return i;
  421.         }
  422.     }
  423.     return -1;
  424. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement