Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

snapshot_class.cpp

By: iwishiknew on Jun 22nd, 2013  |  syntax: C++  |  size: 3.62 KB  |  views: 55  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using namespace std;
  2.  
  3.  
  4. /* Take Snapshots efficiently*/
  5. #ifndef EFFICIENT_ON
  6. #define EFFICIENT_ON
  7. #endif
  8.  
  9.  
  10. #ifdef EFFICIENT_ON
  11. #define CPU_CYCLE_SKIPS 0
  12. #endif
  13.  
  14. template<class type1, class type2>
  15. type2 conv(const type1& t1)
  16. {
  17.     type2 t2 = type2();
  18.     stringstream ss;
  19.     ss<< t1;
  20.     ss>> t2;
  21.     return t2;
  22. }
  23.  
  24.  
  25.  
  26.  
  27. /* Prototype function declarations*/
  28. bool is_snapshot_file(const string&);
  29. string fname(const string&);
  30. void get_snap_info(basic_snap_data&);
  31. map<unsigned int, basic_snap_data> load_file_list();
  32. ULL lines_in_file(const string&);
  33. void sort_by_date(map<unsigned int, basic_snap_data>&);
  34. ID_T take_snapshot();
  35. string construct_snapshot_path(const ID_T&);
  36. bool is_valid_snapshot(const string&);
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. /* ***********************************
  44.  * Constructors for snapshot_class  *
  45.  */
  46.  
  47. /* tasks for all constructors go here*/
  48. void snapshot_class::constructor_tasks(const string& s)
  49. {
  50.     snapshot_class::clear();
  51.     snapshot_class::filename = s;
  52.     snapshot_class::load_snapshot(s);
  53. }
  54.  
  55. snapshot_class::snapshot_class(const char* s)
  56. {
  57.     snapshot_class::constructor_tasks(s);
  58. }
  59.  
  60. snapshot_class::snapshot_class(const string& s)
  61. {
  62.     snapshot_class::constructor_tasks(s);
  63. }
  64.  
  65. snapshot_class::snapshot_class()
  66. {
  67.     snapshot_class::clear();
  68.     snapshot_class::id = take_snapshot();
  69.     if(snapshot_class::id == 0)
  70.     {
  71.         snapshot_class::clear();
  72.     }
  73.     if(snapshot_class::id != 0)
  74.     {
  75.         snapshot_class::load_snapshot(construct_snapshot_path(snapshot_class::id));
  76.     }
  77. }
  78.  
  79. snapshot_class::snapshot_class(const char& c)
  80. {
  81. }
  82.  
  83. snapshot_class::~snapshot_class()
  84. {
  85.     snapshot_class::snapshot.reset();
  86. }
  87.  
  88.  
  89.  
  90. /* ***********************************
  91.  * Constructors for snapshot_class   *
  92.  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  93.  
  94.  
  95.  
  96. /* Snapshot_class Member functions:
  97.  */
  98.  
  99. /* Clears all the variables of the class*/
  100. void snapshot_class::clear()
  101. {
  102.     snapshot_class::snapshot.reset();
  103.     snapshot_class::filename.clear();
  104.     snapshot_class::date.clear();
  105.     snapshot_class::id = 0;
  106. }
  107.  
  108. /* Loads a snapshot from a full path.*/
  109. void snapshot_class::load_snapshot(const string& s)
  110. {
  111.     cls();
  112.     cout<< endl<< endl<< endl;
  113.     cout<< "                    Please wait... Loading information..."<< endl<< endl;
  114.     bool valid = is_valid_snapshot(s);
  115.     if(valid == false)
  116.     {
  117.         return;
  118.     }
  119.     ifstream in;
  120.     string temps = "";
  121.    
  122.     snapshot_class::filename = s;
  123.    
  124.    
  125.     in.open(snapshot_class::filename.c_str(), ios::INFILE);
  126.    
  127.     getline(in, temps);
  128.     snapshot_class::id = conv<string, ID_T>(temps);
  129.    
  130.     getline(in, temps);
  131.     snapshot_class::date = temps;
  132.    
  133.     snapshot_class::snapshot = make_shared<map<unsigned int, string>>();
  134.     if(!in.good())
  135.     {
  136.         in.close();
  137.         return;
  138.     }
  139.     while(in.good())
  140.     {
  141.         if(snapshot_class::snapshot->find(snapshot_class::snapshot->size()) == snapshot_class::snapshot->end())
  142.         {
  143.             (*snapshot_class::snapshot)[snapshot_class::snapshot->size()] = "";
  144.         }
  145.         getline(in, snapshot_class::snapshot->at((snapshot_class::snapshot->size() - 1)));
  146.     }
  147.     in.close();
  148.    
  149.     cls();
  150. }
  151.  
  152. void snapshot_class::del()
  153. {
  154.     remove(snapshot_class::filename.c_str());
  155.     snapshot_class::clear();
  156. }
  157.  
  158. ID_T snapshot_class::gid()
  159. {
  160.     return snapshot_class::id;
  161. }
  162.  
  163. string snapshot_class::gdate()
  164. {
  165.     return snapshot_class::date;
  166. }
  167.  
  168. map<unsigned int, string> snapshot_class::gsnap()
  169. {
  170.     return *snapshot_class::snapshot;
  171. }
  172.  
  173. /* Snapshot_class Member functions:
  174.  */
  175. //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^