Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <assert.h>
- #include <memory.h>
- char* parse(char* input) {
- input++;
- while (*input != '\0') {
- if (*(input - 1) == ':' && *input == ' ') {
- return ++input;
- }
- input++;
- }
- return input;
- }
- bool starts_with(const char* const input, const char* const pattern) {
- char* input_mutable = input;
- char* patter_mutable = pattern;
- while (*patter_mutable != '\0') {
- if (*patter_mutable != *input_mutable) {
- return false;
- }
- input_mutable++, patter_mutable++;
- }
- return true;
- }
- char* eat_spaces(char* input){
- while (*input == ' '){
- input++;
- }
- return input;
- }
- char* add_str(const char* a, const char* b){
- size_t a_size = strlen(a);
- a_size -= (a[a_size - 1] == '\n');
- size_t b_size = strlen(b);
- b_size -= (b[b_size - 1] == '\n');
- size_t size = a_size + b_size;
- char* result = (char*)malloc((size + 1) * sizeof(char));
- for (int i = 0; i < a_size; ++i) {
- result[i] = a[i];
- }
- for (int i = 0; i < b_size; ++i) {
- result[a_size + i] = b[i];
- }
- result[size] = '\0';
- return result;
- }
- char* parse_boundary(const char* const input){
- size_t input_size = strlen(input);
- int abc = 0;
- for(int i = 0; i < input_size; i++){
- char* temp = (input + i);
- if(starts_with(temp, "boundary=")){
- temp += strlen("boundary=\"");
- size_t temp_size = strlen(temp);
- char* boundary = malloc((temp_size - 2) * sizeof(char));
- for (int j = 0; j < temp_size - 2; ++j) {
- boundary[j] = temp[j];
- }
- return boundary;
- }
- }
- }
- char* str_without_newline(const char* input){
- size_t input_size = strlen(input);
- char* copied_str = malloc(input_size * sizeof(char));
- if(input[input_size - 1] == '\n' || input[input_size - 1] == '\r' || input[input_size - 1] == '\r\n' || input[input_size - 1] == '\n\r'){
- input_size--;
- }
- for (int i = 0; i < input_size; ++i) {
- copied_str[i] = input[i];
- }
- return copied_str;
- }
- int main() {
- FILE * file = fopen("input", "r+");
- assert(file);
- const size_t buffer_size = 256;
- char buffer[buffer_size];
- char* headers_to_find[] = {"From:", "To:", "Date", "Content-Type"};
- char* boundary = NULL;
- int boundaries_count = 0;
- bool body_started = false;
- char* results[3];
- while (fgets(buffer, buffer_size, file)) {
- if(boundary || body_started){
- if(starts_with(buffer, boundary)){
- boundaries_count++;
- body_started = true;
- }
- if(body_started){
- continue;
- }
- }
- for (size_t i = 0; i < 4; i++) {
- if (starts_with(buffer, headers_to_find[i])) {
- char* parsed_str = parse(buffer);
- char sub_buffer[buffer_size];
- if(fgets(sub_buffer, buffer_size, file)) {
- if (starts_with(sub_buffer, " ")) {
- char * sub_parsed_str = eat_spaces(sub_buffer);
- parsed_str = add_str(parsed_str, " ");
- parsed_str = add_str(parsed_str, sub_parsed_str);
- } else {
- fseek(file, -strlen(sub_buffer), SEEK_CUR);
- }
- }
- if(i == 3){//content-type
- boundary = add_str("--", parse_boundary(parsed_str));
- int abc = 0;
- }else{
- results[i] = str_without_newline(parsed_str);
- int abc = 0;
- }
- }
- }
- }
- if(boundaries_count > 0){
- boundaries_count--;
- }
- if(!boundary){
- boundaries_count = 1;
- }
- printf("|%s|%s|%s|%d\n", results[0], results[1], results[2], boundaries_count);
- free(boundary);
- for (int i = 0; i < 3; ++i) {
- free(results[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement