Advertisement
TwITe

Untitled

Oct 9th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. void write_data_to_file(void* data, FILE * data_file, int last_written_byte_position, int bytes_number_to_write, const string& current_file, int id) {
  2.     long int start_position = ftell(data_file);
  3.     fwrite(((void*)((char*)data + last_written_byte_position)), 1, bytes_number_to_write, data_file);
  4.     long int end_position = ftell(data_file);
  5.     save_file_name_and_reading_positions(id, current_file, start_position, end_position);
  6. }
  7.  
  8. bool write_data(int id, void* data, int current_data_size) {
  9.     FILE * data_file = NULL;
  10.     string current_file;
  11.     int last_written_byte_position = 0;
  12.     while (current_data_size != 0) {
  13.         if (data_file == NULL) {
  14.             current_file = get_new_file_path();
  15.         }
  16.         data_file = fopen(current_file.c_str(), "a+b");
  17.         unsigned int file_free_space = get_file_free_space(current_file);
  18.         if (file_free_space == 0) {
  19.             current_file = get_new_file_path();
  20.             data_file = fopen(current_file.c_str(), "a+b");
  21.         }
  22.         int bytes_number_to_write;
  23.         if (current_data_size > file_free_space) {
  24.             bytes_number_to_write = file_free_space;
  25.             write_data_to_file(data, data_file, last_written_byte_position, bytes_number_to_write, current_file, id);
  26.             last_written_byte_position += bytes_number_to_write;
  27.         }
  28.         else {
  29.             bytes_number_to_write = current_data_size;
  30.             write_data_to_file(data, data_file, last_written_byte_position, bytes_number_to_write, current_file, id);
  31.         }
  32.         current_data_size -= bytes_number_to_write;
  33.         is_data_size_invalid(current_data_size);
  34.         fclose(data_file);
  35.         data_file = NULL;
  36.     }
  37.     return true;
  38. }
  39.  
  40. #define CATCH_CONFIG_MAIN
  41. #include "/Users/TwITe/Documents/Visual Studio 2017/Projects/Database/database_lib.h"
  42. #include "/Users/TwITe/Documents/Visual Studio 2017/Projects/catch.hpp"
  43.  
  44. TEST_CASE("Data was succesfully writed to files", "[data_store]") {
  45.     int arr[5]{ 0, 1, 2, 3, 4 };
  46.     REQUIRE((write_data(1, arr, 20)) == true);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement