Advertisement
Faguss

Verifying my GameFront OFP Backup

May 1st, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.66 KB | None | 0 0
  1. // Program for verifying GameFront OFP backup
  2. // 1. Check if files listed in MainList.txt really exist in the sub-directories
  3. // 2. Check if files listed in MainList.txt are also listed in _Info.txt
  4.  
  5.  
  6.  
  7. // Headers:
  8. #include "stdafx.h"     // VS
  9. #include <vector>       // vectors
  10. #include <windows.h>    // winapi
  11. #include <filesystem>   // directory iterator
  12.  
  13. // Namespaces:
  14. using namespace std;
  15. using namespace std::tr2::sys;
  16.  
  17. // Global variables:
  18. ofstream logfile;
  19.  
  20.  
  21.  
  22.  
  23.  
  24. // Functions:
  25. //-------------------------------------------------------------------------------
  26. // Open main list, extract titles & names and put them to vectors
  27. void ListToVector(string filename, vector<string>& titles, vector<string>& names)
  28. {
  29.     // Open file
  30.     fstream listfile;
  31.     listfile.open(filename, ios::in);
  32.  
  33.     if (!listfile.is_open())
  34.         return;
  35.  
  36.     // Copy text to a string
  37.     string content((istreambuf_iterator<char>(listfile)), istreambuf_iterator<char>());
  38.  
  39.     // Parse
  40.     size_t uploaded = content.find("Uploaded:");
  41.  
  42.     while (uploaded != string::npos)
  43.     {
  44.         // Line after "uploaded" contains title
  45.         size_t TitleStart = content.find("\n", uploaded);
  46.         size_t TitleEnd   = content.find("\n", TitleStart+1);
  47.  
  48.         // Extract title
  49.         string Title      = content.substr(TitleStart+1, TitleEnd-TitleStart-1);
  50.  
  51.         // Two lines after title there is a filename
  52.         size_t FileStart  = content.find_first_not_of(" \t\f\v\n\r", TitleEnd);
  53.         size_t FileEnd    = content.find(" |", FileStart);
  54.  
  55.         // Extract filename
  56.         string FileName   = content.substr(FileStart, FileEnd-FileStart);
  57.  
  58.         // Copy extracted data to vectors
  59.         titles.push_back(Title);
  60.         names .push_back(FileName);
  61.  
  62.         // Find next occurence
  63.         uploaded = content.find("Uploaded:", uploaded+1);
  64.     };
  65.  
  66.     listfile.close();
  67. };
  68. //-------------------------------------------------------------------------------
  69.  
  70.  
  71.  
  72.  
  73.  
  74. //-------------------------------------------------------------------------------
  75. // Open info list, extract title and put it to a vector
  76. void InfoToVector(string filename, vector<string>& titles)
  77. {
  78.     //logfile << "\tfile:" << filename << endl;
  79.  
  80.     // Open file
  81.     fstream info;
  82.     info.open(filename, ios::in);
  83.  
  84.     if (!info.is_open())
  85.         return;
  86.  
  87.     // Variables for parsing
  88.     string line;                    // holds current line
  89.     bool nextIsTitle     = true;    // copy line to vector
  90.     bool nextIsSeparator = false;   // switch nextIsTitle
  91.  
  92.     // For each line
  93.     while (getline(info, line))
  94.     {
  95.         //logfile << line << endl;
  96.  
  97.         // If it's a title
  98.         if (nextIsTitle && !isspace(line[0]))
  99.         {
  100.             //logfile << line << endl;
  101.  
  102.             // Copy to vector
  103.             titles.push_back(line);
  104.  
  105.             // Condition to quit particular file to prevent crashing
  106.             if (filename.compare(".\\Modifications\\Add Ons\\Weapons\\_Info.txt") == 0  &&  line == "[PSOL] DesertEagle.zip")
  107.                 break;
  108.  
  109.             nextIsTitle = false;
  110.         };
  111.  
  112.         // "Popularity" precedes separator
  113.         if (line.compare(0, 11, "Popularity:") == 0)
  114.             nextIsSeparator = true;
  115.  
  116.         // If it's a item separator
  117.         if (nextIsSeparator  &&  line.compare(0, 3, "===") == 0)
  118.             nextIsSeparator = false,
  119.             nextIsTitle     = true;
  120.     };
  121.  
  122.     info.close();
  123. };
  124. //-------------------------------------------------------------------------------
  125.  
  126.  
  127.  
  128.  
  129.  
  130. //-------------------------------------------------------------------------------
  131. // Log vector contents
  132. void VectorToLog(vector<string>& var)
  133. {
  134.     for (vector<string>::iterator q = var.begin(); q != var.end(); q++)
  135.         if (*q != "null")
  136.             logfile << "\t" << *q << endl;
  137. };
  138. //-------------------------------------------------------------------------------
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. //-------------------------------------------------------------------------------
  146. // Our program
  147. int _tmain(int argc, _TCHAR* argv[])
  148. {
  149.     // Start logging
  150.     logfile.open("logfile.txt", ios::out | ios::trunc);
  151.  
  152.  
  153.     // Create vectors
  154.     vector<string> FileTitle;       // for verifying files
  155.     vector<string> FileName;
  156.     vector<string> FileTitle2;      // for verifying info lists
  157.     vector<string> InfoTitle;
  158.  
  159.     // Fill vectors
  160.     ListToVector("MainList.txt", FileTitle, FileName);
  161.  
  162.  
  163.     // Find out if this is the "Operation Flashpoint" folder
  164.     char buffer[MAX_PATH];
  165.     GetModuleFileNameA(NULL, buffer, MAX_PATH);
  166.     string exepath = string(buffer);
  167.  
  168.     // If it is then we need to remove files from the other games
  169.     if (exepath.find("\\Operation Flashpoint\\") != string::npos)
  170.     {
  171.         // list of other directories
  172.         vector<string> dirs
  173.         {
  174.             "Operation Flashpoint 2 Dragon Rising",
  175.             "Operation Flashpoint Dragon Rising Red River",
  176.             "Operation Flashpoint Elite",
  177.             "Operation Flashpoint Red River",
  178.             "Operation Flashpoint Resistance"
  179.         };
  180.  
  181.         // temporary vectors
  182.         vector<string> tmp_titles;
  183.         vector<string> tmp_names;
  184.  
  185.         // iterate through the dir list
  186.         for (vector<string>::iterator q = dirs.begin(); q != dirs.end(); q++)
  187.         {
  188.             tmp_titles.clear();
  189.             tmp_names.clear();
  190.  
  191.             // Get lists from the other folders to a temporary vector
  192.             string filename = "..\\" + *q + "\\MainList.txt";
  193.             ListToVector(filename, tmp_titles, tmp_names);
  194.  
  195.             // for each entry in a temporary vector
  196.             int i = 0;
  197.             for (vector<string>::iterator r = tmp_titles.begin(); r != tmp_titles.end(); r++, i++)
  198.             {
  199.                 // search for it in the main list
  200.                 int j = 0;
  201.                 for (vector<string>::iterator p = FileTitle.begin(); p != FileTitle.end(); p++, j++)
  202.  
  203.                     // if both name and title match then remove it from the main list
  204.                     if (*r == *p  &&  tmp_names[i] == FileName[j])
  205.                     {
  206.                         //logfile << "REMOVE: " << *r << " - " << *p << "\t\t\t" << tmp_names[i] << " - " << FileName[j] << endl;
  207.                         FileTitle[j] = "null";
  208.                         FileName[j]  = "null";
  209.                         break;
  210.                     };
  211.             }
  212.         }
  213.     }
  214.  
  215.     // Make a copy for verifying infolists
  216.     FileTitle2 = FileTitle;
  217.  
  218.  
  219.  
  220.    
  221.     // Browse sub-directories
  222.     for (recursive_directory_iterator i("."), end;  i!=end;  ++i)
  223.     {
  224.         string filename  = i->path().filename();
  225.         string extension = filename.substr(filename.length()-3, 3);
  226.        
  227.         // If current file is an infolist
  228.         if (filename.compare("_Info.txt") == 0)
  229.             InfoToVector(i->path(), InfoTitle);
  230.  
  231.         // If current file is not a directory and not a text
  232.         if (!is_directory(i->path())  &&  extension!="txt")
  233.         {
  234.             // then search for it in a vector
  235.             int j = 0;
  236.             for (vector<string>::iterator q = FileName.begin();   q != FileName.end();   q++, j++)
  237.  
  238.                 // if found then remove
  239.                 if (*q == filename)
  240.                 {
  241.                     FileTitle[j] = "null";
  242.                     FileName[j]  = "null";
  243.                     continue;
  244.                 };
  245.         };
  246.     };
  247.  
  248.    
  249.     // Save remaining vector contents to the log file
  250.     logfile << "FILES THAT WEREN'T FOUND:\n\n";
  251.  
  252.     int j = 0;
  253.     for (vector<string>::iterator q=FileName.begin();   q!=FileName.end();   q++, j++)
  254.         if (*q != "null")
  255.             logfile << "\t" << *q << "  -  " << FileTitle[j] << endl;
  256.  
  257.  
  258.  
  259.  
  260.  
  261.     // Finish with info lists
  262.     // Subtract InfoTitle from FileTitle2
  263.  
  264.     // For each entry in FileTitle2
  265.     int I = 0;
  266.     for (vector<string>::iterator q = FileTitle2.begin(); q != FileTitle2.end(); q++, I++)
  267.     {
  268.         //logfile << "item to find: " << *q << endl;
  269.        
  270.         // search for it in the InfoList
  271.         int J = 0;
  272.         for (vector<string>::iterator r = InfoTitle.begin(); r != InfoTitle.end(); r++, J++)
  273.         {
  274.             //logfile << "\titerating: " << *r << endl;
  275.  
  276.             string qq = *q;
  277.             string rr = *r;
  278.  
  279.             // if found then remove
  280.             if (rr.compare(0, rr.length(), qq) == 0)
  281.             {
  282.                 FileTitle2[I] = "null";
  283.                 InfoTitle[J]  = "null";
  284.                 break;
  285.             };
  286.         };
  287.     };
  288.  
  289.     // Output result
  290.     logfile << "\n\n\n\nINFO THAT WASN'T FOUND:\n\n" << "FileTitle2:\n\n";
  291.     VectorToLog(FileTitle2);
  292.  
  293.     logfile << "\n\nInfoTitle:\n\n";
  294.     VectorToLog(InfoTitle);
  295.  
  296.  
  297.     logfile.close();
  298.     return 0;
  299. }
  300. //-------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement