Advertisement
TwITe

Untitled

Sep 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. //It's database library
  2. //Use the function "set_filesize" to set a default size of the data file
  3. //Use the function "set_path" to set path for saving data files
  4. #ifndef DATABASE_LIBRARY_H
  5. #define DATABASE_LIBRARY_H
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <cassert>
  10. #include <bitset>
  11. #include <map>
  12. #include <unordered_map>
  13. #include <vector>
  14. #include <set>
  15.  
  16. using namespace std;
  17. string filename = "data0";
  18. string path;
  19. int num = 0;
  20. int default_filesize = 1;
  21. unordered_map <int, <vector <string> >> users;
  22.  
  23. void set_path(const string &saving_path) {
  24.     path = saving_path;
  25. }
  26.  
  27. void check_path() {
  28.     if (path.empty()) {
  29.         throw std::runtime_error("path is invalid");
  30.     }
  31. }
  32.  
  33. void set_filesize(int user_size) {
  34.     default_filesize = user_size;
  35. }
  36.  
  37. bool is_datafile_full(int i) {
  38.     if (i == default_filesize) {
  39.         num++;
  40.         filename = "data" + to_string(num);
  41.         return true;
  42.     }
  43.     return false;
  44. }
  45.  
  46. template <typename T>
  47. void store(int id, T* data) {
  48.     check_path();
  49.     users[id].push_back(filename);
  50.     bitset <sizeof(T)> bytes (data);
  51.     ofstream datafile;
  52.     datafile.open(path + filename + ".txt");
  53.     users[id].push_back(filename);
  54.     for (int i= 0; i < sizeof(T); i++) {
  55.         datafile << bytes[i];
  56.         if ((is_datafile_full(i))) {
  57.             datafile.close();
  58.             users[id].push_back(filename);
  59.             datafile.open(path + filename + ".txt");
  60.         }
  61.     }
  62.     datafile.close();
  63. }
  64.  
  65. template <typename T>
  66. T load(int id) {
  67.     bitset <sizeof(T)> return_value;
  68.     ofstream datafile;
  69.     for (auto filename: users[id]) {
  70.         datafile.open(path + filename + ".txt");
  71.        
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement