Advertisement
RollTime

FileVar.cpp

Jan 23rd, 2021 (edited)
1,414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <filesystem>
  3. #include <cstdlib>
  4. #include <fstream>
  5. #include <vector>
  6. #include <algorithm>
  7. #include "FileVar.h"
  8. using namespace std;
  9. namespace fs = std::filesystem;
  10.  
  11. FileVar::FileVar(){
  12.     cout << "Type Needed!" << endl;
  13. }
  14.  
  15. FileVar::FileVar(int i, fs::path in_path){
  16.     type = "int";
  17.     root_dir = in_path;
  18.     fs::path good_path = get_good_path();
  19.    
  20.     my_path = good_path += ".int";
  21.        
  22.     ofstream file(my_path.string());
  23.    
  24.     file << to_string(i) << endl;
  25.    
  26.     file.close();
  27. }
  28.  
  29. FileVar::FileVar(string s, fs::path in_path){
  30.     type = "string";
  31.     root_dir = in_path;
  32.     fs::path good_path = get_good_path();
  33.    
  34.     my_path = good_path += ".string";
  35.        
  36.     ofstream file(my_path.string());
  37.    
  38.     file << s << endl;
  39.    
  40.     file.close();
  41. }
  42.  
  43. FileVar::FileVar(fs::path in_path){
  44.     string my_ext = in_path.extension();
  45.     if(my_ext != ".int" && my_ext != ".string"){
  46.         cout << "Invalid File!" << endl;
  47.         return;
  48.     }
  49.     if(!fs::exists(in_path)){
  50.         cout << "File Does Not Exist!" << endl;
  51.         return;
  52.     }
  53.    
  54.     my_path = in_path;
  55.     root_dir = in_path.parent_path();
  56.     type = my_ext.substr(1, string::npos);
  57. }
  58.  
  59. fs::path FileVar::get_good_path(){
  60.     vector<fs::path> paths;
  61.     for(auto& p: fs::directory_iterator(root_dir)){
  62.         paths.push_back(p.path().stem());
  63.     }
  64.    
  65.     sort(paths.begin(), paths.end());
  66.        
  67.     string last;
  68.    
  69.     if(paths.empty()){
  70.         last = "-1";
  71.     }
  72.     else{
  73.         last = paths.back().stem();
  74.     }
  75.        
  76.     return root_dir /= to_string(stoi(last) + 1);
  77. }
  78.  
  79. string FileVar::get_string(){
  80.     ifstream file(my_path);
  81.    
  82.     string out;
  83.     getline(file, out);
  84.     file.close();
  85.    
  86.     return out;
  87. }
  88.  
  89. int FileVar::get_int(){
  90.     if(type != "int"){
  91.         cout << "Can't output this as int, returned 0" << endl;
  92.         return 0;
  93.     }
  94.    
  95.     ifstream file(my_path);
  96.    
  97.     string out;
  98.     getline(file, out);
  99.     file.close();
  100.  
  101.     return stoi(out);
  102. }
  103.  
  104. fs::path FileVar::get_path(){
  105.     return my_path;
  106.     }
  107.    
  108. FileVar::~FileVar(){}
  109.  
  110. bool FileVar::delete_var(){
  111.     return fs::remove(my_path);
  112. }
  113.  
  114. ostream& operator<< (ostream& out, FileVar& fv){
  115.     out << fv.get_string();
  116.     return out;
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement