file data writing is more reliable
This commit is contained in:
parent
8acc0b89e1
commit
5c89187c71
1 changed files with 46 additions and 6 deletions
52
main.cpp
52
main.cpp
|
@ -142,7 +142,36 @@ std::string get_filename(std::string data)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void write_file(const std::string& filename, char *start_data, char *end_data)
|
||||||
|
{
|
||||||
|
FILE *fd = fopen(filename.c_str(), "wb");
|
||||||
|
while (start_data != end_data)
|
||||||
|
{
|
||||||
|
fwrite(start_data, sizeof(char), 1, fd);
|
||||||
|
start_data++;
|
||||||
|
}
|
||||||
|
fclose(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *search_substring(char *start_data, const char *substring, size_t size)
|
||||||
|
{
|
||||||
|
size_t index = 0;
|
||||||
|
size_t index_sub = 0;
|
||||||
|
|
||||||
|
while (index < size)
|
||||||
|
{
|
||||||
|
if (start_data[index] == substring[index_sub])
|
||||||
|
{
|
||||||
|
index_sub++;
|
||||||
|
if (index_sub >= strlen(substring))
|
||||||
|
return start_data + (index - index_sub);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
index_sub = 0;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -225,15 +254,26 @@ int main()
|
||||||
auto h = parse_header(line_str);
|
auto h = parse_header(line_str);
|
||||||
std::vector<std::string> file_ = split(h.begin()->second, "; ");
|
std::vector<std::string> file_ = split(h.begin()->second, "; ");
|
||||||
std::string filename = get_filename(file_.back());
|
std::string filename = get_filename(file_.back());
|
||||||
size_t size = file_size - (line_str.size() + std::string(line2).size() + std::string(line3).size() + (line_str2.size() * 2) + 4);
|
char *file_data = server.receive_data_ensured(user, file_size);
|
||||||
char *file_data = server.receive_data_ensured(user, size);
|
if (!file_data)
|
||||||
std::ofstream a(filename, std::ios::binary);
|
throw std::runtime_error("Getting file data failed");
|
||||||
a.write(file_data, size);
|
std::string final_boundary = std::format("\r\n--{}--", boundary);
|
||||||
a.close();
|
char *end = search_substring(file_data, final_boundary.c_str(), file_size);
|
||||||
|
if (!end)
|
||||||
|
{
|
||||||
|
std::println("Couldn't find ending for the file");
|
||||||
|
free(line);
|
||||||
|
free(line1);
|
||||||
|
free(line2);
|
||||||
|
free(line3);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
write_file(filename, file_data, end);
|
||||||
print_map(h);
|
print_map(h);
|
||||||
free(file_data);
|
free(file_data);
|
||||||
std::println("File {} received with {}B size", filename, size);
|
std::println("File {} received", filename);
|
||||||
}
|
}
|
||||||
|
free(line);
|
||||||
free(line1);
|
free(line1);
|
||||||
free(line2);
|
free(line2);
|
||||||
free(line3);
|
free(line3);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue