Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Total number of coded lines: 1384
- /*This source code is compatible with my Source Code Collector Program, which can automatically parse and separate this text file into all of the header/cpp files necessary to create and compile the project. The program can be found at:
- http://sourceforge.net/projects/selectivedataba/files/Source%20Code%20Text%20Collector.exe/download
- */
- /*Meant to be an alternative to the native Windows Image backup, the Selective Data Backup program was created because (as I descovered the hard way) windows image can not be used to restore a hard drive if that hard drive is not the same size as the hard drive the image was created by. So, say your 300Gb hard drive fails. You decide that 'hey, while we're at it, why not get a bigger drive?', so you get a 500Gb drive. The windows backup you created of your 300Gb hard drive will only work with another 300Gb hard drive, so now you have a 500Gb hard drive, with no data... This program allows you to save your all-too-important documents, programs, etc... without any limitation.*/
- \\backup_session.cpp begin==================================================================
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <boost/filesystem.hpp>
- #include "common.h"
- #include "fsys.h"
- #include "datastruct.h"
- #include <math.h>
- #include <windows.h>
- using namespace std;
- void clean_target(string target = "")
- {
- if(folder_empty(target) == true)
- {
- return;
- }
- boost::filesystem::path p;
- p = target;
- vector<boost::filesystem::directory_entry> directories;
- copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), back_inserter(directories));
- for(unsigned int x = 0; x < directories.size(); x++)
- {
- boost::filesystem::remove_all(directories[x].path());
- }
- p.clear();
- directories.clear();
- }
- boost::filesystem::path file_directory(string d = "")
- {
- while(d[(d.size() - 1)] != '\\')
- {
- d.resize(d.size() - 1);
- }
- if(d[(d.size() - 1)] == '\\')
- {
- d.resize(d.size() - 1);
- }
- boost::filesystem::path p;
- p.clear();
- p = d;
- return p;
- }
- void copy_special_file(boost::filesystem::path pfrom, boost::filesystem::path pto)
- {
- boost::filesystem::path ptodirectory;
- ptodirectory = (pto.string() + file_directory(pfrom.string()).relative_path().string());
- boost::filesystem::create_directories(ptodirectory);
- ptodirectory.clear();
- ptodirectory = (pto.string() + pfrom.relative_path().string());
- boost::filesystem::copy_file(pfrom, ptodirectory);
- }
- void copy_filesystem(boost::filesystem::path pfrom, boost::filesystem::path pto)
- {
- vector<boost::filesystem::directory_entry> directories;
- boost::filesystem::path ext_pto;
- if(is_folder(pfrom.string()) == true)
- {
- copy(boost::filesystem::recursive_directory_iterator(pfrom), boost::filesystem::recursive_directory_iterator(), back_inserter(directories));
- for(unsigned int x = 0; x < directories.size(); x++)
- {
- if(is_folder(directories[x].path().string()) == true)
- {
- ext_pto.clear();
- ext_pto = (pto.string() + directories[x].path().relative_path().string());
- boost::filesystem::create_directories(ext_pto);
- }
- }
- }
- }
- void copy_data(boost::filesystem::path pfrom, boost::filesystem::path pto, int perc = -1)
- {
- int pcomplete = -1;
- vector<boost::filesystem::directory_entry> directories;
- boost::filesystem::path file;
- copy(boost::filesystem::recursive_directory_iterator(pfrom), boost::filesystem::recursive_directory_iterator(), back_inserter(directories));
- for(unsigned int x = 0; x < directories.size(); x++)
- {
- if(is_file(directories[x].path().string()) == true)
- {
- file.clear();
- file = (file.string() + pto.string() + directories[x].path().relative_path().string());
- if(percent_complete(x, signed(directories.size())) != pcomplete)
- {
- pcomplete = percent_complete(x, signed(directories.size()));
- cls();
- cout<< "Backup is "<< perc<< "\% complete..."<< endl<< endl;
- cout<< "Backing up \""<< pfrom.string()<< "\""<< endl;
- cout<< pcomplete<< "\%"<< endl;
- }
- boost::filesystem::copy_file(directories[x].path(), file);
- }
- }
- directories.clear();
- }
- void copy_files(string target = "", vector<string> files = vector<string>())
- {
- boost::filesystem::path pfrom, pto;
- vector<boost::filesystem::path> filesp;
- for(unsigned int x = 0; x < files.size(); x++) //convert to paths
- {
- filesp.push_back(files[x]);
- }
- cls();
- for(unsigned int x = 0; x < filesp.size(); x++)
- {
- if(boost::filesystem::is_regular_file(filesp[x]) == true)
- {
- pto.clear();
- pfrom.clear();
- pto = (pto.string() + target + "\\");
- pfrom = files[x];
- copy_special_file(pfrom, pto);
- continue;
- }
- if(boost::filesystem::is_directory(filesp[x]) == true)
- {
- cls();
- cout<< "Backing up \""<< filesp[x]<< "\"..."<< endl;
- pto.clear();
- pto = (pto.string() + target + "\\" + filesp[x].relative_path().string()); //path: C:\Targetdir\relative path of our directory
- pfrom.clear();
- pfrom = filesp[x];
- boost::filesystem::create_directories(pto);
- pto.clear();
- pto = (pto.string() + target + "\\");
- copy_filesystem(pfrom, pto);
- copy_data(pfrom, pto, percent_complete(x, signed(filesp.size())));
- }
- }
- }
- bool are_you_sure()
- {
- char ch;
- int x = 0;
- while(x == 0)
- {
- cls();
- cout<< "Are you sure you want to perform a backup?"<< endl;
- cout<< "Y/N";
- ch = gkey();
- if(ch == 'y')
- {
- return true;
- }
- if(ch == 'n')
- {
- return false;
- }
- }
- return false;
- }
- void after_action(string action = "nothing")
- {
- if(action == "sleep")
- {
- system("rundll32 PowrProf.dll SetSuspendState 0 1 0");
- return;
- }
- if(action == "beep")
- {
- Beep(2000, 500);
- Beep(1500, 1000);
- Beep(1900, 500);
- return;
- }
- if(action == "nothing")
- {
- return;
- }
- }
- int start_backup()
- {
- if(are_you_sure() == false)
- {
- return 0;
- }
- check_subdirectory_inclusion();
- cls();
- cout<< "Checking data..."<< endl;
- if(!there_are_directories_to_back_up())
- {
- cls();
- cout<< "You have not listed any valid directories to back up."<< endl;
- wait();
- return 0;
- }
- vector<string> options;
- options = get_options();
- if(!is_folder(options[0]))
- {
- cls();
- cout<< "Invalid target directory!"<< endl;
- wait();
- return 0;
- }
- string target;
- target = options[0]; //for easy reading
- cout<< "Validating target directory..."<< endl;
- if(is_part_of_backup(target) == true)
- {
- cls();
- cout<< "FATAL ERROR: target == boost::filesystem::recursive_directory_iterator(p) \n Please check that your target is not part of your backup."<< endl;
- wait();
- return 0;
- }
- vector<string> files;
- vector<boost::filesystem::directory_entry> directories;
- files = get_file("dirbacklist.dat");
- cout<< "Cleaning target directory..."<< endl;
- if(is_folder(target) == true)
- {
- clean_target(target);
- if(!folder_empty(target)) //a folder with contents might cause conflictions
- {
- cls();
- cout<< "Error: ARBORTING BACKUP"<< endl;
- exit(1);
- }
- }
- cout<< "Starting backup..."<< endl;
- copy_files(target, files);
- after_action(options[1]);
- cls();
- cout<< "Backup to \""<< target<< "\" is complete."<< endl;
- wait();
- return 0;
- }
- \\backup_session.cpp END==================================================================
- \\common.cpp begin==================================================================
- #include <iostream>
- #include <conio.h>
- #include <windows.h>
- #include <string>
- #include <fstream>
- #include <math.h>
- #include "common.h"
- #include "fsys.h"
- using namespace std;
- void cls()
- {
- system("CLS");
- }
- void cl()
- {
- Sleep(10);
- while(_kbhit()) _getch();
- }
- void wait()
- {
- cout<< endl<< endl<< endl<< endl;
- cout<< "Press any key to continue..."<< endl;
- cl();
- while(!_kbhit())
- {
- continue;
- }
- cl();
- }
- char gkey()
- {
- char ch;
- cl();
- ch = _getch();
- return ch;
- }
- int conv_str(string s)
- {
- ifstream in;
- ofstream out;
- int x = 0;
- out.open("temp.dat", ios::out);
- while((is_file("temp.dat", true) == true) && (!out.is_open()))
- {
- continue;
- }
- out<< s;
- out.close();
- in.open("temp.dat", ios::in);
- while((is_file("temp.dat", true) == true) && (!in.is_open()))
- {
- continue;
- }
- in>> x;
- in.close();
- out.clear();
- in.clear();
- remove("temp.dat");
- return x;
- }
- string conv_int(int x)
- {
- ifstream in;
- ofstream out;
- string s;
- s.clear();
- out.open("temp.dat", ios::out);
- while((is_file("temp.dat", true) == true) && (!out.is_open()))
- {
- continue;
- }
- out<< x;
- out.close();
- in.open("temp.dat", ios::in);
- while((is_file("temp.dat", true) == true) && (!in.is_open()))
- {
- continue;
- }
- getline(in, s);
- in.close();
- remove("temp.dat");
- in.clear();
- out.clear();
- return s;
- }
- int percent_complete(int x, int max)
- {
- float p = 0;
- p = (float(x) / float(max));
- p = (p * 100);
- return floor(p);
- }
- \\common.cpp END==================================================================
- \\common.h begin==================================================================
- #ifndef COMMON_H_INCLUDED
- #define COMMON_H_INCLUDED
- using namespace std;
- void cls();
- void cl();
- void wait();
- char gkey();
- int conv_str(string s = "");
- string conv_int(int x = 0);
- int percent_complete(int x = 0, int max = 0);
- #endif // COMMON_H_INCLUDED
- \\common.h END==================================================================
- \\datastruct.cpp begin==================================================================
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include "datastruct.h"
- #include "common.h"
- #include "fsys.h"
- using namespace std;
- /**
- Data structure:
- directiories:
- file: dirbacklist.dat
- just listed. We can iterate all sub-directories upon the initiation of
- our backup. this will also allow the user some freedom in changing his/her filesystem
- without worrying about this program.
- */
- vector<string> get_file(string f)
- {
- vector<string> file;
- if(data_there(f) == false)
- {
- return file;
- }
- ifstream in;
- string line;
- in.open(f.c_str(), ios::in);
- while(in.good())
- {
- getline(in, line);
- file.push_back(line);
- }
- in.close();
- return file;
- }
- vector<string> get_options()
- {
- vector<string> options;
- if(data_there("optsback.dat") == false)
- {
- options.clear();
- options.push_back("");
- options.push_back("do nothing");
- return options;
- }
- ifstream in;
- string line;
- in.open("optsback.dat", ios::in);
- while(in.good())
- {
- getline(in, line);
- if(line == "x")
- {
- line = "";
- }
- options.push_back(line);
- }
- in.close();
- return options;
- }
- void save_directories(vector<string> directories)
- {
- ofstream out;
- out.open("dirbacklist.dat", ios::out);
- if(directories.size() > 0)
- {
- for(unsigned int x = 0; x < directories.size(); x++)
- {
- out<< directories[x];
- if(x < (directories.size() - 1))
- {
- out<< endl;
- }
- }
- }
- out.close();
- }
- void save_options(vector<string> options)
- {
- ofstream out;
- out.open("optsback.dat", ios::out);
- for(unsigned int x = 0; x < options.size(); x++)
- {
- out<< options[x];
- if(x < (options.size() - 1))
- {
- out<< endl;
- }
- }
- out.close();
- }
- //checks if the target directory is part of the list of directories it will back up
- bool target_isnt_part_of_backup(string target)
- {
- vector<string> directories;
- directories = get_file("dirbacklist.dat");
- for(unsigned int x = 0; x < directories.size(); x++)
- {
- if(target == directories[x])
- {
- directories.clear();
- return false;
- }
- }
- directories.clear();
- return true;
- }
- bool there_are_directories_to_back_up()
- {
- remove_extinct_directories();
- vector<string> f;
- f = get_file("dirbacklist.dat");
- if(f.size() > 0)
- {
- f.clear();
- return true;
- }
- return false;
- }
- \\datastruct.cpp END==================================================================
- \\datastruct.h begin==================================================================
- #ifndef DATASTRUCT_H_INCLUDED
- #define DATASTRUCT_H_INCLUDED
- #include <string>
- #include <vector>
- using namespace std;
- vector<string> get_file(string f = "");
- vector<string> get_options();
- void save_directories(vector<string> directories = vector<string>());
- void save_options(vector<string> options = vector<string>());
- bool target_isnt_part_of_backup(string target = "");
- bool there_are_directories_to_back_up();
- #endif // DATASTRUCT_H_INCLUDED
- \\datastruct.h END==================================================================
- \\dataunits.cpp begin==================================================================
- #include <iostream>
- #include <string>
- #include <math.h>
- #include "dataunits.h"
- #include "common.h"
- using namespace std;
- /**First off, this file is special:
- it conatins ONLY functions for converting
- units of data storage. lets keep it that way.*/
- /*
- Detirmines what unit to use to measure the data size.
- return values:
- none = no data type, no data
- byte, megabyte, kilobyte, and gigabyte*/
- double round_to_tenths(double x)
- {
- float newf = 0;
- newf = (x * 100);
- newf = int(newf);
- newf = (newf / 100);
- return newf;
- }
- string choose_unit(double s)
- {
- if(s == 0)
- {
- return "none";
- }
- if(s < 1024)
- {
- return "b";
- }
- if((s >= 1024) && (s < 1048576))
- {
- return "Kb";
- }
- if((s >= 1048576) && (s < 1073741824))
- {
- return "MB";
- }
- if(s >= 1073741824)
- {
- return "Gb";
- }
- return "unknown";
- }
- double convert_size(double size, string& unit)
- {
- if(size == 0)
- {
- unit = "b";
- return 0;
- }
- float news = 0;
- unit = choose_unit(size);
- if(unit == "unknown")
- {
- news = float(size);
- return news;
- }
- if(unit == "b")
- {
- news = float(size);
- return round_to_tenths(news);
- }
- if(unit == "Kb")
- {
- news = (float(size) / 1024);
- return round_to_tenths(news);
- }
- if(unit == "MB")
- {
- news = (float(size) / 1048576);
- return round_to_tenths(news);
- }
- if(unit == "Gb")
- {
- news = (float(size) / 1073741824);
- return round_to_tenths(news);
- }
- unit = "b";
- return 0;
- }
- \\dataunits.cpp END==================================================================
- \\dataunits.h begin==================================================================
- #ifndef DATAUNITS_H_INCLUDED
- #define DATAUNITS_H_INCLUDED
- using namespace std;
- static string default_s2 = string();
- string choose_unit(double s = 0);
- double convert_size(double size = 0, string& unit = default_s2);
- double round_to_tenths(double x = 0);
- #endif // DATAUNITS_H_INCLUDED
- \\dataunits.h END==================================================================
- \\fsys.cpp begin==================================================================
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <boost/filesystem.hpp>
- #include "fsys.h"
- #include "datastruct.h"
- #include "dataunits.h"
- #include "common.h"
- using namespace std;
- /*Here we have all of our filesystem functions. That
- is to say most of the functions in this program that
- perform fileoperations are included in this file.*/
- /**This also include file/folder confirmation
- so that our buackup session doesnt try to copy
- non-existent files/folders.*/
- //does that file exist?
- //this_directory can be specified so that we dont have to use entire paths
- bool is_file(string f, bool this_directory)
- {
- boost::filesystem::path p;
- p = f;
- if(this_directory == false)
- {
- if(boost::filesystem::is_regular_file(p) == true)
- {
- p.clear();
- return true;
- }
- return false;
- }
- if(this_directory == true)
- {
- p = (boost::filesystem::current_path().string() + "\\" + p.string());
- if(boost::filesystem::is_regular_file(p) == true)
- {
- return true;
- }
- return false;
- }
- return false;
- }
- //does that folder exist?
- bool is_folder(string d)
- {
- boost::filesystem::path p;
- p = d;
- if(boost::filesystem::is_directory(p) == true)
- {
- p.clear();
- return true;
- }
- p.clear();
- return false;
- }
- bool data_there(string f)
- {
- string line;
- ifstream in;
- int number_of_lines = 0;
- if(is_file(f) == true)
- {
- in.open(f.c_str(), ios::in);
- while(in.good())
- {
- getline(in, line);
- if(line.size() > 0)
- {
- number_of_lines++;
- }
- }
- in.close();
- line.clear();
- in.clear();
- if(number_of_lines > 0)
- {
- return true;
- }
- }
- return false;
- }
- //lists the contents of a folder. all subdiurectories, files, and sub-sub-directories...
- //d = directory of folder, size = size of its contents in bytes
- void list_folder_contents(string d, vector<string>& contents, double& s)
- {
- contents.clear();
- s = 0;
- if(!is_folder(d))
- {
- return;
- }
- vector<boost::filesystem::directory_entry> directories;
- boost::filesystem::path p;
- p = d;
- //we get a list of all the files/folders
- copy(boost::filesystem::recursive_directory_iterator(p), boost::filesystem::recursive_directory_iterator(), back_inserter(directories));
- //now we will calculate the size of this directory...
- s = 0;
- for(unsigned int x = 0; x < directories.size(); x++)
- {
- if(is_file(directories[x].path().string()) == true)
- {
- s = (s + file_size(directories[x].path()));
- }
- contents.push_back(directories[x].path().string());
- }
- directories.clear();
- p.clear();
- }
- //converts a plain text file to binary. this appears as single-lined in notepad.exe
- void conv_tobin(string f)
- {
- ifstream in;
- ofstream out;
- vector<string> file;
- string line;
- if(is_file(f) == true)
- {
- in.open(f.c_str(), ios::in);
- while(in.good())
- {
- getline(in, line);
- file.push_back(line);
- }
- in.close();
- out.open(f.c_str(), ios::binary);
- for(unsigned int x = 0; x < file.size(); x++)
- {
- out<< file[x];
- if(x < (file.size() - 1))
- {
- out<< endl;
- }
- }
- out.close();
- file.clear();
- in.clear();
- out.clear();
- line.clear();
- }
- }
- //converts a file to text format. this appears in lines in notepad.exe
- void conv_totext(string f)
- {
- ofstream out;
- ifstream in;
- string line;
- vector<string> file;
- if(is_file(f) == true)
- {
- in.open(f.c_str(), ios::binary);
- while(in.good())
- {
- getline(in, line);
- file.push_back(line);
- }
- in.close();
- out.open(f.c_str(), ios::out);
- for(unsigned int x = 0; x < file.size(); x++)
- {
- out<< file[x];
- if(x < (file.size() - 1))
- {
- out<< endl;
- }
- }
- out.close();
- file.clear();
- in.clear();
- out.clear();
- line.clear();
- }
- }
- /*This function makes sure all of the
- directories in the directories folder exist.
- It will remove any directories that dont
- exist.*/
- void remove_extinct_directories()
- {
- if((is_file("dirbacklist.dat", true) == false) || (data_there("dirbacklist.dat") == false))
- {
- return;
- }
- ifstream in;
- vector<string> directories, existing;
- ofstream out;
- string line;
- in.open("dirbacklist.dat", ios::in);
- while(in.good())
- {
- getline(in, line);
- directories.push_back(line);
- }
- in.close();
- for(unsigned int x = 0; x < directories.size(); x++) //go through the list of directories and filter out non-existing ones
- {
- if((is_file(directories[x]) == true) || (is_folder(directories[x]) == true))
- {
- existing.push_back(directories[x]);
- }
- }
- //write it back to the file.
- out.open("dirbacklist.dat", ios::out);
- for(unsigned int x = 0; x < existing.size(); x++)
- {
- out<< existing[x];
- if(x < (existing.size() - 1))
- {
- out<< endl;
- }
- }
- out.close();
- existing.clear();
- directories.clear();
- in.clear();
- out.clear();
- line.clear();
- }
- double backup_size(string& unit_type)
- {
- cls();
- cout<< "Please wait..."<< endl;
- vector<string> directories, contents;
- directories = get_file("dirbacklist.dat");
- boost::filesystem::path p;
- double s = 0, total = 0, per = 0, total_final = 0;
- for(unsigned int x = 0; x < directories.size(); x++)
- {
- if(per != percent_complete(x, directories.size()))
- {
- per = percent_complete(x, signed(directories.size()));
- cls();
- cout<< per<< "\%"<< endl;
- }
- if(is_folder(directories[x]) == true)
- {
- list_folder_contents(directories[x], contents, s);
- total = (total + s);
- continue;
- }
- if(is_file(directories[x]) == true)
- {
- p = directories[x];
- s = boost::filesystem::file_size(p);
- total = (total + s);
- continue;
- }
- }
- total_final = float(total);
- total_final = convert_size(total, unit_type);
- return total_final;
- }
- /*Checks a directory's contents. Returns true if
- it is empty, and false if it doesnt exist, or contains
- data.*/
- bool folder_empty(string f)
- {
- boost::filesystem::path p;
- p = f;
- vector<boost::filesystem::directory_entry> directories;
- if(boost::filesystem::is_regular_file(p) == true)
- {
- return false;
- }
- if(boost::filesystem::is_directory(p) == false)
- {
- return false;
- }
- copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), back_inserter(directories));
- if(directories.size() > 0)
- {
- directories.clear();
- return false;
- }
- return true;
- }
- //this is necessary to avoid a fatal runtime error.....
- bool is_part_of_backup(string directory)
- {
- vector<boost::filesystem::directory_entry> dirs;
- vector<string> files;
- int matches = 0;
- boost::filesystem::path p;
- files = get_file("dirbacklist.dat");
- for(unsigned int x = 0; x < files.size(); x++)
- {
- p = files[x];
- if(boost::filesystem::is_regular_file(p) == true)
- {
- continue;
- }
- copy(boost::filesystem::recursive_directory_iterator(p), boost::filesystem::recursive_directory_iterator(), back_inserter(dirs));
- for(unsigned int y = 0; y < dirs.size(); y++)
- {
- if(dirs[x].path().string() == directory)
- {
- matches++;
- }
- }
- }
- if(matches > 0)
- {
- return true;
- }
- return false;
- }
- bool sd_included_in_backup(boost::filesystem::path p, vector<string> file)
- {
- for(unsigned int x = 0; x < file.size(); x++)
- {
- if(p.string() == file[x])
- {
- return true;
- }
- }
- return false;
- }
- void remove_directory_from_list(boost::filesystem::path p)
- {
- vector<string> file, newfile;
- file = get_file("dirbacklist.dat");
- ofstream out;
- newfile.clear();
- for(unsigned int x = 0; x < file.size(); x++)
- {
- if(file[x] != p.string())
- {
- newfile.push_back(file[x]);
- }
- }
- out.open("dirbacklist.dat", ios::out);
- for(unsigned int x = 0; x < newfile.size(); x++)
- {
- out<< newfile[x];
- if(x < (newfile.size() - 1))
- {
- out<< endl;
- }
- }
- out.close();
- newfile.clear();
- file.clear();
- }
- /*Checks to see if the user included subdirectories
- of those directories he did include.*/
- void check_subdirectory_inclusion()
- {
- vector<boost::filesystem::directory_entry> directories;
- vector<string> file;
- file = get_file("dirbacklist.dat");
- boost::filesystem::path p;
- for(unsigned int x = 0; x < file.size(); x++)
- {
- cls();
- cout<< "I am removeing from your list directories that are subdirectories of directories you already included."<< endl<< endl;
- cout<< "Removing sub-directory inclusions..."<< endl;
- cout<< percent_complete(x, signed(file.size()))<< " \%"<< endl;
- p.clear();
- p = file[x];
- if(is_folder(p.string()) == true)
- {
- copy(boost::filesystem::recursive_directory_iterator(p), boost::filesystem::recursive_directory_iterator(), back_inserter(directories));
- for(unsigned int y = 0; y < directories.size(); y++)
- {
- if(sd_included_in_backup(directories[y].path(), file) == true)
- {
- remove_directory_from_list(directories[y].path());
- }
- }
- }
- /*we dont need to do this for any of the files, because we go
- through the entire list in sd_included_in_backup(), so if it
- finds a file, it will be removed.*/
- }
- directories.clear();
- file.clear();
- }
- \\fsys.cpp END==================================================================
- \\fsys.h begin==================================================================
- #ifndef FSYS_H_INCLUDED
- #define FSYS_H_INCLUDED
- #include <string>
- #include <vector>
- #include <boost/filesystem.hpp>
- using namespace std;
- static vector<string> default_vs;
- static string default_s = string();
- static double default_d = double();
- bool is_file(string f = "", bool this_directory = false);
- bool is_folder(string d = "");
- void list_folder_contents(string d = "", vector<string>& contents = default_vs, double& s = default_d);
- void conv_tobin(string f = "");
- void conv_totext(string f = "");
- bool data_there(string f = "");
- void remove_extinct_directories();
- double backup_size(string& unit_type = default_s);
- bool folder_empty(string f = "");
- bool is_part_of_backup(string directory = "");
- bool sd_included_in_backup(boost::filesystem::path p, vector<string> file = vector<string>());
- void check_subdirectory_inclusion();
- #endif // FSYS_H_INCLUDED
- \\fsys.h END==================================================================
- \\link.h begin==================================================================
- #ifndef LINK_H_INCLUDED
- #define LINK_H_INCLUDED
- void options_menu();
- int start_backup();
- #endif // LINK_H_INCLUDED
- \\link.h END==================================================================
- \\main.cpp begin==================================================================
- #include <iostream>
- #include "datastruct.h"
- #include "common.h"
- #include "link.h"
- #include "fsys.h"
- using namespace std;
- /**
- In-Dev notes:
- Menu layout:
- Main menu options-
- start backup (must add directories)
- -y/n prompt
- -displays amount of data being backed up and a percent complete
- Options-
- -Add/remove directories
- - Directory options:
- - edit directory
- - remove directory from list
- - add/edit target directory
- - Do somthing when backup is complete:
- - put computer to sleep
- - beep
- - nothing
- */
- int main()
- {
- int x = 0;
- char ch;
- while(x == 0)
- {
- remove_extinct_directories(); //making sure we do not have non-existent file/folders listed
- cls();
- cout<< " Main menu"<< endl<< endl<< endl;
- cout<< "1 - Start Backup"<< endl;
- cout<< "2 - Options"<< endl;
- cout<< "3/[ESCAPE] - Exit Program"<< endl;
- ch = gkey();
- if(ch == '1')
- {
- start_backup();
- continue;
- }
- if(ch == '2')
- {
- options_menu();
- continue;
- }
- if((ch == '3') || (ch == 0x1b))
- {
- return 0;
- }
- }
- return 0;
- }
- \\main.cpp END==================================================================
- \\options_menu.cpp begin==================================================================
- #include <iostream>
- #include <string>
- #include <vector>
- #include "fsys.h"
- #include "common.h"
- #include "datastruct.h"
- using namespace std;
- static vector<string> default_vs2;
- /*Options menu
- file: optsback.dat
- directories file: dirbacklist.dat
- data structure:
- 0 target directory
- 1 what to do after backup completed
- */
- void remove_directory(vector<string>& directories = default_vs2, int line = -1)
- {
- if(line < 0)
- {
- return;
- }
- vector<string> temp;
- //copy to temp
- for(unsigned int x = 0; x < directories.size(); x++)
- {
- temp.push_back(directories[x]);
- }
- directories.clear();
- //copy all but that line back to the origional vector
- for(int x = 0; unsigned(x) < temp.size(); x++)
- {
- if(x != line)
- {
- directories.push_back(temp[x]);
- }
- }
- //clear the temp vector
- temp.clear();
- }
- bool is_duplicate_directory(vector<string> directories = vector<string>(), string dir = "")
- {
- int dups = 0;
- for(unsigned int x = 0; x < directories.size(); x++)
- {
- if(directories[x] == dir)
- {
- dups++;
- }
- }
- if(dups > 0)
- {
- return true;
- }
- return false;
- }
- void modify_directory(vector<string>& directories = default_vs2, int dir = 0)
- {
- int x = 0;
- string renam = "";
- char ch;
- while(x == 0)
- {
- cls();
- cout<< " Modify \""<< directories[dir]<< "\":"<< endl<< endl<< endl;
- cout<< " 1 - Re-Name"<< endl;
- cout<< " 2 - Delete"<< endl;
- cout<< " [BACK] - Back to directory list"<< endl;
- ch = gkey();
- if(ch == '1')
- {
- renam = "";
- while(renam == "")
- {
- cls();
- cout<< "Rename \""<< directories[dir]<< "\":";
- getline(cin, renam);
- if(renam.size() == 1)
- {
- if(tolower(renam[0]) == 'x')
- {
- renam = directories[dir];
- break;
- }
- }
- if((is_duplicate_directory(directories, renam) == true) && (renam != directories[dir]))
- {
- cls();
- cout<< "I already have that directory listed. You do noty need to add it again."<< endl;
- wait();
- renam.clear();
- renam = "";
- continue;
- }
- if((is_file(renam) == false) && (is_folder(renam) == false))
- {
- cls();
- cout<< "That is not a valid path!"<< endl;
- wait();
- renam = "";
- }
- }
- directories[dir] = renam;
- save_directories(directories);
- continue;
- }
- if(ch == '2')
- {
- remove_directory(directories, dir);
- save_directories(directories);
- return;
- }
- if(ch == 0x08)
- {
- save_directories(directories);
- return;
- }
- }
- }
- void add_directory(vector<string>& directories = default_vs2)
- {
- string directory_name = "";
- while(directory_name == "")
- {
- cls();
- cout<< "Enter 'x' to cancel."<< endl<< endl;
- cout<< "Enter a path: ";
- getline(cin, directory_name);
- if(directory_name.size() == 1)
- {
- if(tolower(directory_name[0]) == 'x')
- {
- return;
- }
- }
- if((is_file(directory_name) == false) && (is_folder(directory_name) == false))
- {
- cls();
- cout<< "That path is invalid!"<< endl;
- wait();
- directory_name.clear();
- directory_name = "";
- }
- if(is_duplicate_directory(directories, directory_name) == true)
- {
- cls();
- cout<< "I already have that directory listed. You do noty need to add it again."<< endl;
- wait();
- directory_name.clear();
- directory_name = "";
- }
- }
- directories.push_back(directory_name);
- save_directories(directories);
- }
- void edit_directory_list()
- {
- vector<string> directories;
- int x = 0, choice;
- string choice_s;
- while(x == 0)
- {
- directories.clear();
- directories = get_file("dirbacklist.dat");
- cls();
- cout<< " Directories:"<< endl<< endl<< endl;
- if(directories.size() > 0)
- {
- for(unsigned int x = 0; x < directories.size(); x++)
- {
- cout<< (x + 1)<< " - "<< directories[x]<< endl;
- }
- }
- cout<< endl<< endl;
- cout<< "*************************************"<< endl;
- cout<< "'a' - Add a new directory"<< endl;
- cout<< "'q' - Back"<< endl;
- cout<< endl<< "*************************************"<< endl;
- choice_s.clear();
- choice = 0;
- while((choice < 1) || (unsigned(choice) > directories.size()))
- {
- choice_s.clear();
- cin.sync();
- getline(cin, choice_s);
- if(choice_s.size() > 0)
- {
- if(tolower(choice_s[0]) == 'a')
- {
- choice = -1;
- break;
- }
- if(tolower(choice_s[0] == 'q'))
- {
- save_directories(directories);
- return;
- }
- }
- choice = conv_str(choice_s);
- }
- if(choice == -1)
- {
- add_directory(directories);
- continue;
- }
- choice--;
- modify_directory(directories, choice);
- }
- }
- void edit_target_directory(vector<string>& options = default_vs2)
- {
- vector<string> directories;
- directories = get_file("dirbacklist.dat");
- string edit = "";
- while(edit == "")
- {
- cls();
- cout<< "Press 'x' to cancel."<< endl<< endl;
- if(options[0].size() > 0)
- {
- cout<< "Current target: \""<< options[0]<< "\""<< endl;
- }
- cout<< endl<< endl<< endl<< endl;
- cout<< "Enter the new target directory: ";
- getline(cin, edit);
- if(edit.size() == 1)
- {
- if(tolower(edit[0]) == 'x')
- {
- return;
- }
- }
- if(folder_empty(edit) == false)
- {
- if(is_folder(edit) == true)
- {
- cls();
- cout<< "It is recommended that you name an empty folder, as your target's contents will be completely erased."<< endl;
- edit = "";
- wait();
- continue;
- }
- }
- if(target_isnt_part_of_backup(edit) == false)
- {
- cls();
- cout<< "You can not copy a folder to itself!! This path is part of your backup, and cannot be the target directory."<< endl;
- wait();
- edit = "";
- continue;
- }
- if(is_duplicate_directory(directories, edit) == false)
- {
- if(is_folder(edit) == true)
- {
- options[0] = edit;
- save_options(options);
- return;
- }
- cls();
- cout<< "You can not name a file as your target!"<< endl;
- wait();
- edit = "";
- continue;
- }
- edit = "";
- }
- }
- void toggle_end(vector<string>& options = default_vs2)
- {
- vector<string> toggles;
- toggles.push_back("sleep");
- toggles.push_back("beep");
- toggles.push_back("nothing");
- int id = 2;
- for(unsigned int x = 0; x < toggles.size(); x++)
- {
- if(options[1] == toggles[x])
- {
- id = x;
- break;
- }
- }
- id++;
- if(id > 2)
- {
- id = (id - 3);
- }
- options[1] = toggles[id];
- save_options(options);
- toggles.clear();
- }
- void options_menu()
- {
- vector<string> options;
- int x = 0;
- string unit = "";
- double backup = 0;
- char ch;
- while(x == 0)
- {
- remove_extinct_directories();
- options.clear();
- options = get_options();
- cls();
- cout<< " Options"<< endl;
- cout<< endl<< endl<< endl;
- cout<< " 1 - Add/Remove directories"<< endl;
- cout<< " 2 - Add/Edit the Target Directory"<< endl;
- cout<< " 3 - End of backup option: "<< options[1]<< endl;
- cout<< " 4 - Calculate the Size of the backup"<< endl;
- cout<< " 5 - Remove sub-directory inclusions"<< endl;
- cout<< " [BACK] - Back to main menu"<< endl;
- ch = gkey();
- if(ch == '1')
- {
- edit_directory_list();
- continue;
- }
- if(ch == '2')
- {
- edit_target_directory(options);
- continue;
- }
- if(ch == '3')
- {
- toggle_end(options);
- continue;
- }
- if(ch == '4')
- {
- cls();
- backup = backup_size(unit);
- cls();
- cout<< "Size of backup: "<< backup<< " "<< unit<< "."<< endl;
- wait();
- cls();
- continue;
- }
- if(ch == '5')
- {
- check_subdirectory_inclusion();
- continue;
- }
- if(ch == 0x08)
- {
- save_options(options);
- return;
- }
- }
- }
- \\options_menu.cpp END==================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement