Advertisement
m4ly

Lab4 | Struktura | malloc | realloc

Apr 20th, 2013
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. /*
  2. Author: Dawid Mocek
  3. Zadanie 8
  4.  
  5. Napisz program, który umożliwia automatyczne generowanie poczty elektronicznej. Program dostaje jako wywołanie dwa parametry:
  6.  
  7.     Plik z adresatami
  8.     Plik z treścią wiadomości
  9.  
  10. W pliku z adresatami każda linia ma następujący format:
  11.  
  12. Imie Nazwisko adres@email
  13.  
  14. Przykładowy plik z wiadomością:
  15.  
  16. Drogi [KTO]
  17.  
  18. Właśnie wygrałeś 100 000 EURO. Odpisz proszę, a ustalimy sposób doręczenie pieniędzy.
  19.  
  20. Całuje,
  21.  
  22. Ka.
  23.  
  24. Zadaniem programu, jest stworzenie osobnego pliku z kopią wiadomości dla każdego adresata. Program powinien podmienić każde wystąpienie [KTO] na imię i nazwisko z pliku z adresatami i zapisać nową wiadomość w pliku o nazwie odpowiadającej emailowi.
  25. */
  26.  
  27. #include "stdio.h"
  28. #include "stdlib.h"
  29. #include "string.h"
  30. #include "conio.h"
  31.  
  32. const int NAME_LEN = 255;
  33. const int SURNAME_LEN = 255;
  34. // RFC 5321 max email len.
  35. const int EMAIL_LEN = 254;
  36.  
  37.  
  38. struct desc
  39. {
  40.     char name[NAME_LEN];
  41.     char surname[SURNAME_LEN];
  42.     char email[EMAIL_LEN];
  43. };
  44.  
  45. typedef FILE* _file;
  46.  
  47. /* Replace function */
  48. char *replace(char *mesg, char *from, char *to)
  49. {
  50.     static char buff[4096];
  51.     char *ch;
  52. //strstr - returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
  53.     if (!(ch = strstr(mesg, from)))
  54.         return mesg;
  55.  
  56.     strncpy(buff, mesg, ch - mesg);  
  57.     buff[ch - mesg] = 0;
  58.     sprintf(buff + (ch - mesg), "%s%s", to, ch + strlen(from));
  59.  
  60.     return buff;
  61. }
  62.  
  63. int main(int argc, char **argv)
  64. {
  65.     char emails_file_path[255];
  66.     char mesg_file_path[255];
  67.     char *result_dir_path = "D:\\emails\\";
  68.     char line[254+255+255];
  69.     char *mesg_source = NULL;
  70.  
  71.     if(argc-1 != 2)
  72.     {
  73.         printf("Need 2 params\n");
  74.         getchar();
  75.         exit(1);
  76.     }
  77.  
  78.     strcpy(emails_file_path, argv[1]);
  79.     strcpy(mesg_file_path, argv[2]);
  80.  
  81.     // file with emails
  82.     _file p_emails_file = fopen(emails_file_path, "r");
  83.  
  84.     // file with mesg
  85.     _file p_mesg_file =  fopen(mesg_file_path, "r");
  86.  
  87.     if(p_emails_file == NULL)
  88.     {
  89.         printf("Error opening Email DB file");
  90.         exit(1);
  91.     }
  92.  
  93.     if(p_mesg_file == NULL)
  94.     {
  95.         printf("Error opening mesg DB file");
  96.         exit(1);
  97.     }
  98.  
  99.     /* Read entire mesg content */
  100.     char *tmp = NULL;
  101.     size_t bufSiz = 0;
  102.     char inputBuf[256];
  103.     while (fgets(inputBuf, sizeof inputBuf, p_mesg_file) != NULL)
  104.     {
  105.         tmp = (char*)realloc(mesg_source, bufSiz + strlen(inputBuf) + 1);
  106.         if (tmp)
  107.         {
  108.             mesg_source = tmp;
  109.             mesg_source[bufSiz] = 0;
  110.             strcat(mesg_source, inputBuf);
  111.             bufSiz += strlen(inputBuf) + 1;
  112.         }
  113.         else
  114.         {
  115.             printf("Error: allocated memory\n");
  116.             free(mesg_source);
  117.             mesg_source = NULL;
  118.             break;
  119.         }
  120.     }
  121.  
  122.     /* Analyze Email File */
  123.     while(fgets(line, 254, p_emails_file) != NULL)
  124.     {
  125.         // trim \r\n
  126.         char* line_trim = strtok(line, "\r\n");
  127.  
  128.         // split string
  129.         char* token[255];
  130.         int cnt = 0;
  131.  
  132.         token[0] = strtok(line_trim, " ");
  133.  
  134.         // Tricky
  135.         while (token[cnt] != NULL)
  136.         {
  137.             cnt++;
  138.             token[cnt] = strtok (NULL, " ");
  139.         }
  140.  
  141.         desc* field = (desc*)malloc(sizeof(desc));
  142.         for (int j = 0; j <= cnt -1 ; j++) 
  143.         {  
  144.             //Create basic structure
  145.             strcpy(field->name, token[0]);
  146.             strcpy(field->surname, token[1]);  
  147.             strcpy(field->email, token[2]);
  148.         }
  149.  
  150.         // Create filename: path + email + .txt(4)
  151.         char *filename = (char*)malloc(strlen(result_dir_path) + strlen(field->email) + 4);
  152.         strcpy(filename, result_dir_path);
  153.         strcat(filename, field->email);
  154.         strcat(filename, ".txt");
  155.  
  156.         // Open result file for given mail a+
  157.         _file p_res = fopen(filename, "a+");
  158.         if(p_res == NULL)
  159.         {
  160.             printf("Cannot open file: %s\n", filename);
  161.             exit(1);
  162.         }
  163.  
  164.         //  1 = whitespace
  165.         char *name_and_surname = (char*)malloc(strlen(field->name) + 2 + strlen(field->surname));
  166.         strcpy(name_and_surname, field->name);
  167.         strcat(name_and_surname, " ");
  168.         strcat(name_and_surname, field->surname);
  169.  
  170.         //replace [KTO]
  171.         char* new_mesg = replace(mesg_source, "[KTO]", name_and_surname);
  172.  
  173.         //write
  174.         //fprintf(p_res, mesg_source);
  175.         fprintf(p_res, new_mesg);
  176.  
  177.         //Close
  178.         fclose(p_res);
  179.  
  180.         free(name_and_surname);
  181.         free(field);
  182.     }  
  183.  
  184.     fclose(p_emails_file);
  185.     fclose(p_mesg_file);
  186.     free(mesg_source);
  187.  
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement