Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <assert.h>
  5. #include <memory.h>
  6.  
  7. char* parse(char* input) {
  8.     input++;
  9.     while (*input != '\0') {
  10.         if (*(input - 1) == ':' && *input == ' ') {
  11.             return ++input;
  12.         }
  13.         input++;
  14.     }
  15.     return input;
  16. }
  17.  
  18. bool starts_with(const char* const input, const char* const pattern) {
  19.     char* input_mutable = input;
  20.     char* patter_mutable = pattern;
  21.     while (*patter_mutable != '\0') {
  22.         if (*patter_mutable != *input_mutable) {
  23.             return false;
  24.         }
  25.         input_mutable++, patter_mutable++;
  26.     }
  27.     return true;
  28. }
  29.  
  30. char* eat_spaces(char* input){
  31.     while (*input == ' '){
  32.         input++;
  33.     }
  34.     return input;
  35. }
  36.  
  37. char* add_str(const char* a, const char* b){
  38.     size_t a_size = strlen(a);
  39.     a_size -= (a[a_size - 1] == '\n');
  40.     size_t b_size = strlen(b);
  41.     b_size -= (b[b_size - 1] == '\n');
  42.     size_t size = a_size + b_size;
  43.     char* result = (char*)malloc((size + 1) * sizeof(char));
  44.     for (int i = 0; i < a_size; ++i) {
  45.         result[i] = a[i];
  46.     }
  47.     for (int i = 0; i < b_size; ++i) {
  48.         result[a_size + i] = b[i];
  49.     }
  50.     result[size] = '\0';
  51.     return result;
  52. }
  53.  
  54. char* parse_boundary(const char* const input){
  55.     size_t input_size = strlen(input);
  56.     int abc = 0;
  57.     for(int i = 0; i < input_size; i++){
  58.         char* temp = (input + i);
  59.         if(starts_with(temp, "boundary=")){
  60.             temp += strlen("boundary=\"");
  61.             size_t temp_size = strlen(temp);
  62.             char* boundary = malloc((temp_size - 2) * sizeof(char));
  63.             for (int j = 0; j < temp_size - 2; ++j) {
  64.                 boundary[j] = temp[j];
  65.             }
  66.             return boundary;
  67.         }
  68.     }
  69. }
  70.  
  71. char* str_without_newline(const char* input){
  72.     size_t input_size = strlen(input);
  73.     char* copied_str = malloc(input_size * sizeof(char));
  74.     if(input[input_size - 1] == '\n' || input[input_size - 1] == '\r' || input[input_size - 1] == '\r\n' || input[input_size - 1] == '\n\r'){
  75.         input_size--;
  76.     }
  77.     for (int i = 0; i < input_size; ++i) {
  78.         copied_str[i] = input[i];
  79.     }
  80.     return copied_str;
  81. }
  82.  
  83. int main() {
  84.     FILE * file = fopen("input", "r+");
  85.     assert(file);
  86.     const size_t buffer_size = 256;
  87.     char buffer[buffer_size];
  88.  
  89.     char* headers_to_find[] = {"From:", "To:", "Date", "Content-Type"};
  90.  
  91.     char* boundary = NULL;
  92.     int boundaries_count = 0;
  93.     bool body_started = false;
  94.  
  95.     char* results[3];
  96.  
  97.     while (fgets(buffer, buffer_size, file)) {
  98.  
  99.         if(boundary || body_started){
  100.             if(starts_with(buffer, boundary)){
  101.                 boundaries_count++;
  102.                 body_started = true;
  103.             }
  104.             if(body_started){
  105.                 continue;
  106.             }
  107.         }
  108.         for (size_t i = 0; i < 4; i++) {
  109.             if (starts_with(buffer, headers_to_find[i])) {
  110.                 char* parsed_str = parse(buffer);
  111.                 char sub_buffer[buffer_size];
  112.                 if(fgets(sub_buffer, buffer_size, file)) {
  113.                     if (starts_with(sub_buffer, " ")) {
  114.                         char * sub_parsed_str = eat_spaces(sub_buffer);
  115.                         parsed_str = add_str(parsed_str, " ");
  116.                         parsed_str = add_str(parsed_str, sub_parsed_str);
  117.                     } else {
  118.                         fseek(file, -strlen(sub_buffer), SEEK_CUR);
  119.                     }
  120.                 }
  121.  
  122.                 if(i == 3){//content-type
  123.                    boundary = add_str("--", parse_boundary(parsed_str));
  124.                     int abc = 0;
  125.                 }else{
  126.                     results[i] = str_without_newline(parsed_str);
  127.                     int abc = 0;
  128.                 }
  129.             }
  130.         }
  131.     }
  132.  
  133.     if(boundaries_count > 0){
  134.         boundaries_count--;
  135.     }
  136.  
  137.     if(!boundary){
  138.         boundaries_count = 1;
  139.     }
  140.     printf("|%s|%s|%s|%d\n", results[0], results[1], results[2], boundaries_count);
  141.  
  142.     free(boundary);
  143.     for (int i = 0; i < 3; ++i) {
  144.         free(results[i]);
  145.     }
  146.  
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement