Advertisement
iwishiknew

Source Code Collector

Jan 11th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 36.69 KB | None | 0 0
  1. Total number of coded lines: 1349
  2. /*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:
  3. http://sourceforge.net/projects/selectivedataba/files/Source%20Code%20Text%20Collector.exe/download
  4. */
  5.  
  6. \\common.cpp begin==================================================================
  7.  
  8. #include <iostream>
  9. #include <string>
  10. #include <sstream>
  11. #include <conio.h>
  12. #include <windows.h>
  13. #include "common.h"
  14.  
  15. using namespace std;
  16.  
  17. void cls()
  18. {
  19.     system("CLS");
  20. }
  21.  
  22. void cl()
  23. {
  24.     Sleep(5);
  25.     while(_kbhit()) _getch();
  26. }
  27.  
  28. void wait()
  29. {
  30.     cout<< endl<< endl<< endl<< endl;
  31.     cout<< "Press any key to continue..."<< endl;
  32.     cl();
  33.     if(_getch())
  34.     {
  35.     }
  36.     cl();
  37. }
  38.  
  39. char gkey()
  40. {
  41.     char ch;
  42.     cl();
  43.     ch = _getch();
  44.     cl();
  45.     if(is_letter(ch) == true)
  46.     {
  47.         if(isupper(ch) == true)
  48.         {
  49.             ch = tolower(ch);
  50.         }
  51.     }
  52.     return ch;
  53. }
  54.  
  55. string letters()
  56. {
  57.     return "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
  58. }
  59.  
  60. string specials()
  61. {
  62.     return "`~!@#$%^&*()-_=+[{]}\\|;:\"\',<.>/? ";
  63. }
  64.  
  65. string numbers()
  66. {
  67.     return "1234567890";
  68. }
  69.  
  70. bool char_match(char ch, string s = "")
  71. {
  72.     for(unsigned int x = 0; x < s.size(); x++)
  73.     {
  74.         if(s[x] == ch)
  75.         {
  76.             s.clear();
  77.             return true;
  78.         }
  79.     }
  80.     s.clear();
  81.     return false;
  82. }
  83.  
  84. bool is_special(char ch)
  85. {
  86.     return char_match(ch, specials());
  87. }
  88.  
  89. bool is_letter(char ch)
  90. {
  91.     return char_match(ch, letters());
  92. }
  93.  
  94. bool is_number(char ch)
  95. {
  96.     return char_match(ch, numbers());
  97. }
  98.  
  99. bool is_sure(string message)
  100. {
  101.     int x = 0;
  102.     char ch;
  103.     while(x == 0)
  104.     {
  105.         cls();
  106.         cout<< message<< endl<< endl;
  107.         cout<< "                Y/N"<< endl;
  108.         ch = gkey();
  109.         if(ch == 'y')
  110.         {
  111.             return true;
  112.         }
  113.         if(ch == 'n')
  114.         {
  115.             return false;
  116.         }
  117.     }
  118.     return false;
  119. }
  120.  
  121. bool string_is_number(string s)
  122. {
  123.     if(s.size() == 0)
  124.     {
  125.         return false;
  126.     }
  127.     for(unsigned int x = 0; x < s.size(); x++)
  128.     {
  129.         if(is_number(s[x]) == false)
  130.         {
  131.             s.clear();
  132.             return false;
  133.         }
  134.     }
  135.     s.clear();
  136.     return true;
  137. }
  138.  
  139. int conv_str(string s)
  140. {
  141.     stringstream ss;
  142.     int x = 0;
  143.     ss.clear();
  144.     ss<< s;
  145.     ss>> x;
  146.     return x;
  147. }
  148.  
  149. string conv_int(int x)
  150. {
  151.     stringstream ss;
  152.     ss.clear();
  153.     ss<< x;
  154.     return ss.str();
  155. }
  156.  
  157. bool str_match(string s1, string s2)
  158. {
  159.     string temps = "";
  160.     for(unsigned int x = 0; x < s2.size(); x++)
  161.     {
  162.         temps.clear();
  163.         for(unsigned int y = x; y < (x + s1.size()); y++)
  164.         {
  165.             if(y < s2.size())
  166.             {
  167.                 temps = (temps + s2[y]);
  168.             }
  169.         }
  170.         if(temps == s1)
  171.         {
  172.             temps.clear();
  173.             s1.clear();
  174.             return true;
  175.         }
  176.     }
  177.     return false;
  178. }
  179.  
  180. bool is_header_or_cpp(string name = "")
  181. {
  182.     if(name == "")
  183.     {
  184.         return false;
  185.     }
  186.     vector<string> extensions = {
  187.         ".h",
  188.         ".cpp"
  189.     };
  190.     bool dotmet = false;
  191.     string ext, fname;
  192.     ext.clear();
  193.     fname.clear();
  194.     for(unsigned int x = 0; x < name.size(); x++)
  195.     {
  196.         if(name[x] == '.')
  197.         {
  198.             dotmet = true;
  199.         }
  200.         if(dotmet == false)
  201.         {
  202.             fname = (fname + name[x]);
  203.         }
  204.         if(dotmet == true)
  205.         {
  206.             ext = (ext + name[x]);
  207.         }
  208.     }
  209.     if(dotmet == false)
  210.     {
  211.         return false;
  212.     }
  213.     for(unsigned int x = 0; x < fname.size(); x++)
  214.     {
  215.         if(fname[x] == '\\')
  216.         {
  217.             return false;
  218.         }
  219.     }
  220.     for(unsigned int x = 0; x < ext.size(); x++)
  221.     {
  222.         if(ext[x] == '\\')
  223.         {
  224.             return false;
  225.         }
  226.     }
  227.     dotmet = false;
  228.     for(unsigned int x = 0; x < extensions.size(); x++)
  229.     {
  230.         if(ext == extensions[x])
  231.         {
  232.             dotmet = true;
  233.         }
  234.     }
  235.     if(dotmet == true)
  236.     {
  237.         return true;
  238.     }
  239.     return false;
  240. }
  241.  
  242. //checks if it qualifies as a headerfooter
  243. bool line_is_headerfooter(string line)
  244. {
  245.     if(line.size() == 0)
  246.     {
  247.         return false;
  248.     }
  249.     string name = "";
  250.     bool dotmet = false;
  251.     if(line.size() > 2)
  252.     {
  253.         if((line[0] == '\\') && (line[1] == '\\'))
  254.         {
  255.             if(line.size() > 2)
  256.             {
  257.                 for(unsigned int x = 2; x < line.size(); x++)
  258.                 {
  259.                     if(line[x] == '.')
  260.                     {
  261.                         dotmet = true;
  262.                     }
  263.                     if((line[x] == ' ') && (dotmet == true))
  264.                     {
  265.                         break;
  266.                     }
  267.                     if((is_letter(line[x]) == true) || (line[x] == '.'))
  268.                     {
  269.                         name.push_back(line[x]);
  270.                     }
  271.                 }
  272.                 dotmet = false;
  273.                 if(is_header_or_cpp(name) == false)
  274.                 {
  275.                     return false;
  276.                 }
  277.                 if((str_match(" begin==================================================================", line) == false) && (str_match(" END==================================================================", line) == false))
  278.                 {
  279.                     return false;
  280.                 }
  281.                 return true;
  282.             }
  283.         }
  284.     }
  285.     return false;
  286. }
  287.  
  288. //returns the filename associated with the headerfooter
  289. string get_headerfooter_name(string line)
  290. {
  291.     if(line_is_headerfooter(line) == false)
  292.     {
  293.         return "ERROR";
  294.     }
  295.     bool dotmet = false;
  296.     string name = "";
  297.     for(unsigned int x = 2; x < line.size(); x++)
  298.     {
  299.         if((line[x] == ' ') && (dotmet == true))
  300.         {
  301.             break;
  302.         }
  303.         if(line[x] == '.')
  304.         {
  305.             dotmet = true;
  306.         }
  307.         name = (name + line[x]);
  308.     }
  309.     return name;
  310. }
  311.  
  312.  
  313.  
  314. \\common.cpp END==================================================================
  315.  
  316. \\common.h begin==================================================================
  317.  
  318. #ifndef COMMON_H_INCLUDED
  319. #define COMMON_H_INCLUDED
  320. #include <string>
  321. #include <vector>
  322. using namespace std;
  323.  
  324. void cls();
  325. void cl();
  326. void wait();
  327. char gkey();
  328. bool is_special(char ch = char());
  329. bool is_letter(char ch = char());
  330. bool is_number(char ch = char());
  331. bool is_sure(string message = "Are you sure?");
  332. bool string_is_number(string s = "");
  333. int conv_str(string s = "");
  334. string conv_int(int x = 0);
  335. bool line_is_headerfooter(string line = "");
  336. bool str_match(string s1 = "", string s2 = "");
  337. string get_headerfooter_name(string line = "");
  338.  
  339.  
  340. #endif // COMMON_H_INCLUDED
  341.  
  342.  
  343. \\common.h END==================================================================
  344.  
  345. \\directory_mod.cpp begin==================================================================
  346.  
  347. #include <iostream>
  348. #include "fsys.h"
  349. #include "common.h"
  350. #include "pdata.h"
  351. #include <string>
  352. #include <fstream>
  353. #include <vector>
  354. #include "load.h"
  355.  
  356. using namespace std;
  357.  
  358.  
  359. /*Options should include:
  360.  
  361. - Target directory (where to save the text files)
  362. */
  363.  
  364.  
  365. string remove_extension(string name = "")
  366. {
  367.     string newname = "";
  368.     for(unsigned int x = 0; x < name.size(); x++)
  369.     {
  370.         if(name[x] == '.')
  371.         {
  372.             break;
  373.         }
  374.         newname = (newname + name[x]);
  375.     }
  376.     return newname;
  377. }
  378.  
  379. void unpack_project(const pdata& dat)
  380. {
  381.     string temps = "", line, file_chosen;
  382.     bool fbegin = false;
  383.     file_chosen = choose_file_to_unpack(dat.options.txt_file_directory);
  384.     temps = file_chosen;
  385.     if(temps == "xcancel")
  386.     {
  387.         return;
  388.     }
  389.     if(is_file(file_chosen) == false)
  390.     {
  391.         cls();
  392.         cout<< "ERROR: File \""<< file_chosen<< "\" could not be located!"<< endl;
  393.         wait();
  394.         return;
  395.     }
  396.     ifstream in;
  397.     ofstream out;
  398.     vector<string> file = vector<string>();
  399.     vector<file_data> vfiles = vector<file_data>();
  400.     in.open(temps.c_str(), ios::in);
  401.     while(in.good())
  402.     {
  403.         getline(in, line);
  404.         file.push_back(line);
  405.     }
  406.     in.close();
  407.     vfiles.clear();
  408.     for(unsigned int x = 0; x < file.size(); x++)
  409.     {
  410.         if((line_is_headerfooter(file[x]) == true) && (fbegin == false))
  411.         {
  412.             fbegin = true;
  413.             vfiles.push_back(file_data());
  414.             vfiles[(vfiles.size() - 1)].name = get_headerfooter_name(file[x]);
  415.             x++; //skiping the implied balnk line
  416.             continue; //and going to the next one.
  417.         }
  418.         if(fbegin == true)
  419.         {
  420.             //check if it's a header, if not, then keep going
  421.             if(line_is_headerfooter(file[x]) == true)
  422.             {
  423.                 temps.clear();
  424.                 fbegin = false;
  425.                 continue;
  426.             }
  427.             if(line_is_headerfooter(file[x]) == false)
  428.             {
  429.                 vfiles[(vfiles.size() - 1)].text.push_back(file[x]);
  430.             }
  431.         }
  432.     }
  433.     //now we have a vector of the files represented by each instance of file_data in vector vfiles.
  434.     //lets now create the project folder.
  435.     create_folder(remove_extension(file_chosen));
  436.  
  437.     /*if the user decides not to over-write and existing folder,
  438.     we should not write to it, because it may contain stuff.*/
  439.     if(is_folder(remove_extension(file_chosen)) == false)
  440.     {
  441.         cls();
  442.         cout<< "ERROR: Folder could not be found."<< endl;
  443.         wait();
  444.         return;
  445.     }
  446.     //now we are ready to start creating the .cpp and .h file!!!  YAY
  447.     file_chosen = remove_extension(file_chosen);
  448.     if(is_folder(file_chosen) == true)
  449.     {
  450.         temps.clear();
  451.         for(unsigned int x = 0; x < vfiles.size(); x++)
  452.         {
  453.             //construct directory to the new file
  454.             temps = (file_chosen + "\\" + vfiles[x].name);
  455.             out.open(temps.c_str(), ios::out);
  456.             for(unsigned int y = 0; y < vfiles[x].text.size(); y++)
  457.             {
  458.                 out<< vfiles[x].text[y];
  459.                 if(y < (vfiles[x].text.size() - 1))
  460.                 {
  461.                     out<< endl;
  462.                 }
  463.             }
  464.             out.close();
  465.         }
  466.     }
  467.     cls();
  468.     cout<< "Project successfully reconstructed!"<< endl;
  469.     wait();
  470. }
  471.  
  472. vector<string> add_file_to_vector(file_data file, vector<string> text = vector<string>())
  473. {
  474.     text.push_back("");
  475.     text.push_back("\\\\");
  476.     text[(text.size() - 1)] = (text[(text.size() - 1)] + file.name + " begin==================================================================");
  477.     text.push_back("");
  478.     for(unsigned int x = 0; x < file.text.size(); x++)
  479.     {
  480.         text.push_back(file.text[x]);
  481.     }
  482.     text.push_back("");
  483.     text.push_back("\\\\");
  484.     text[(text.size() - 1)] = (text[(text.size() - 1)] + file.name + " END==================================================================");
  485.     return text;
  486. }
  487.  
  488. bool project_has_text(vector<file_data> files)
  489. {
  490.     int number_of_lines = 0;
  491.     for(unsigned int x = 0; x < files.size(); x++)
  492.     {
  493.         for(unsigned int y = 0; y < files[x].text.size(); y++)
  494.         {
  495.             if(files[x].text[y] > "")
  496.             {
  497.                 number_of_lines++;
  498.             }
  499.         }
  500.     }
  501.     if(number_of_lines == 0)
  502.     {
  503.         return false;
  504.     }
  505.     if(number_of_lines > 0)
  506.     {
  507.         return true;
  508.     }
  509.     return false;
  510. }
  511.  
  512. int total_number_of_coded_lines(vector<string> f)
  513. {
  514.     if(f.size() == 0)
  515.     {
  516.         return 0;
  517.     }
  518.     int number_of_lines = 0;
  519.     for(unsigned int x = 0; x < f.size(); x++)
  520.     {
  521.         if(f[x] > "")
  522.         {
  523.             number_of_lines++;
  524.         }
  525.     }
  526.     return number_of_lines;
  527. }
  528.  
  529. /*CreateSourceText:
  530.  
  531. Return values-
  532. 1: invalid id
  533. 2: folder does not exist
  534. 3: No text
  535. 0: fine*/
  536. int CreateSourceText(const pdata& dat, int id = -1)
  537. {
  538.     if((unsigned(id) > (dat.projects.size() - 1)) || (id < 0))
  539.     {
  540.         return 1;
  541.     }
  542.     if(is_folder(dat.options.txt_file_directory) == false)
  543.     {
  544.         return 2;
  545.     }
  546.     if(project_has_text(dat.projects[id].files) == false)
  547.     {
  548.         return 3;
  549.     }
  550.     cls();
  551.     cout<< "Creating text file.  Please be patient."<< endl;
  552.     vector<string> file_text;
  553.     string savename = "";
  554.     savename = (dat.options.txt_file_directory + "\\" + dat.projects[id].name + ".txt");
  555.     ofstream out;
  556.     file_text.clear();
  557.     for(unsigned int x = 0; x < dat.projects[id].files.size(); x++)
  558.     {
  559.         file_text = add_file_to_vector(dat.projects[id].files[x], file_text);
  560.     }
  561.     out.open(savename.c_str(), ios::out);
  562.     out<< "Total number of coded lines: "<< total_number_of_coded_lines(file_text)<< endl;
  563.     for(unsigned int x = 0; x < file_text.size(); x++)
  564.     {
  565.         out<< file_text[x];
  566.         if(x < (file_text.size() - 1))
  567.         {
  568.             out<< endl;
  569.         }
  570.     }
  571.     out.close();
  572.     file_text.clear();
  573.     savename.clear();
  574.     cls();
  575.     cout<< "Text file completed."<< endl;
  576.     wait();
  577.     return 0;
  578. }
  579.  
  580. bool directory_already_added(const pdata& dat, string dir = "")
  581. {
  582.     if(dat.projects.size() == 0)
  583.     {
  584.         return false;
  585.     }
  586.     for(unsigned int x = 0; x < dat.projects.size(); x++)
  587.     {
  588.         if(dat.projects[x].directory == dir)
  589.         {
  590.             return true;
  591.         }
  592.     }
  593.     return false;
  594. }
  595.  
  596. /*dirtarg:  0, project directories; 1, target directory; 2 modify existing*/
  597. string modify_directory(const pdata& dat, int dirtarg = 0)
  598. {
  599.     string newdir = "";
  600.     while(newdir == "")
  601.     {
  602.         cls();
  603.         cout<< "Enter 'X' to cancel"<< endl;
  604.         if(dirtarg == 2)
  605.         {
  606.             cout<< "Enter 'D' to delete"<< endl;
  607.         }
  608.         cout<< endl<< endl<< endl;
  609.         if(dirtarg == 0)
  610.         {
  611.             cout<< "New Directory: ";
  612.         }
  613.         if(dirtarg == 1)
  614.         {
  615.             cout<< "Target Directory: ";
  616.         }
  617.         if(dirtarg == 2)
  618.         {
  619.             cout<< "Directory:  ";
  620.         }
  621.         cout.flush();
  622.         getline(cin, newdir);
  623.         if(newdir.size() == 1)
  624.         {
  625.             if(tolower(newdir[0]) == 'x')
  626.             {
  627.                 return "xcancel";
  628.             }
  629.             if(dirtarg == 2)
  630.             {
  631.                 if(tolower(newdir[0]) == 'd')
  632.                 {
  633.                     return "deletedir";
  634.                 }
  635.             }
  636.         }
  637.         if((dirtarg == 0) || (dirtarg == 2))
  638.         {
  639.             if(directory_already_added(dat, newdir) == true)
  640.             {
  641.                 newdir.clear();
  642.                 cls();
  643.                 cout<< "That directory is already in the list!"<< endl;
  644.                 wait();
  645.                 cls();
  646.                 continue;
  647.             }
  648.         }
  649.         if(is_folder(newdir) == false)
  650.         {
  651.             if(dirtarg == 1)
  652.             {
  653.                 newdir = get_current_directory();
  654.                 return newdir;
  655.             }
  656.             cls();
  657.             cout<< "That is an invalid folder name!"<< endl;
  658.             wait();
  659.             newdir.clear();
  660.             continue;
  661.         }
  662.         if((dirtarg == 1) || (dirtarg == 2))
  663.         {
  664.             if(folder_empty(newdir) == false)
  665.             {
  666.                 if(is_sure("WARNING: That directory contains files that may be over-written!  I recommend you choose an empty directory!\n\n\n Do you really want to use this directory?") == true)
  667.                 {
  668.                     return newdir;
  669.                 }
  670.                 newdir.clear();
  671.             }
  672.         }
  673.     }
  674.     if(is_folder(newdir) == true)
  675.     {
  676.         return newdir;
  677.     }
  678.     return "xcancel";
  679. }
  680.  
  681. void delete_project(vector<project_data>& projects, int id = -1)
  682. {
  683.     if((unsigned(id) > (projects.size() - 1)) || (id < 0))
  684.     {
  685.         return;
  686.     }
  687.     vector<project_data> temp;
  688.     temp = projects;
  689.     projects.clear();
  690.     for(unsigned int x = 0; x < temp.size(); x++)
  691.     {
  692.         if(signed(x) != id)
  693.         {
  694.             projects.push_back(temp[x]);
  695.         }
  696.     }
  697.     temp.clear();
  698. }
  699.  
  700. void mod_or_create(pdata& dat, int mod = 0, int project_number = -1, string name = "")
  701. {
  702.     int x = 0, sourcetxt = 0;
  703.     char ch;
  704.     string temps;
  705.     bool sure = false;
  706.     if(project_number == -1)
  707.     {
  708.         temps = modify_directory(dat, mod);
  709.         if(temps != "xcancel")
  710.         {
  711.             dat.projects.push_back(project_data());
  712.             dat.projects[(dat.projects.size() - 1)].directory = temps;
  713.             load_project_data(dat.projects[(dat.projects.size() - 1)]);
  714.         }
  715.         return;
  716.     }
  717.     while(x == 0)
  718.     {
  719.         temps.clear();
  720.         cls();
  721.         cout<< "              \""<< name<< "\" -- Choose:"<< endl<< endl<< endl<< endl;
  722.         cout<< " 1 -  Modify This Directory"<< endl;
  723.         cout<< " 2 -  Construct a Project Text File"<< endl;
  724.         cout<< " q -  Done"<< endl;
  725.         ch = gkey();
  726.         if(ch == '1')
  727.         {
  728.             temps = modify_directory(dat, mod);
  729.             if(temps == "deletedir")
  730.             {
  731.                 sure = is_sure("Are you absolutely sure you want to delete this project?");
  732.                 if(sure == true)
  733.                 {
  734.                     delete_project(dat.projects, project_number);
  735.                 }
  736.                 if(sure == false)
  737.                 {
  738.                     continue;
  739.                 }
  740.                 return;
  741.             }
  742.             if((temps != "xcancel") && (temps != "deletedir"))
  743.             {
  744.                 dat.projects[project_number].directory = temps;
  745.                 dat.projects[project_number].name.clear();
  746.                 dat.projects[project_number].files.clear();
  747.                 load_project_data(dat.projects[project_number]);
  748.             }
  749.             return;
  750.         }
  751.         if(ch == '2')
  752.         {
  753.             sourcetxt = CreateSourceText(dat, project_number);
  754.             if(sourcetxt == 1)
  755.             {
  756.                 cls();
  757.                 cout<< "ERROR: FUNCTION::int CreateSourceText(const pdata& dat, int id = -1) \"INVALID ID!\""<< endl;
  758.                 wait();
  759.             }
  760.             if(sourcetxt == 2)
  761.             {
  762.                 cls();
  763.                 cout<< "ERROR: Invalid Folder path!"<< endl;
  764.                 wait();
  765.             }
  766.             if(sourcetxt == 3)
  767.             {
  768.                 cls();
  769.                 cout<< "Nothing was created because there was no text."<< endl;
  770.                 wait();
  771.             }
  772.             continue;
  773.         }
  774.         if(ch == 'q')
  775.         {
  776.             return ;
  777.         }
  778.     }
  779. }
  780.  
  781. void modify_user_project_directories(pdata& dat)
  782. {
  783.     int x = 0, choice = 0;
  784.     string s_choice = "", temps = "";
  785.     while(x == 0)
  786.     {
  787.         s_choice.clear();
  788.         choice = 0;
  789.         cls();
  790.         cout<< "                Projects"<< endl<< endl<< endl<< endl;
  791.         for(unsigned int y = 0; y < dat.projects.size(); y++)
  792.         {
  793.             cout<< " "<< (y + 1)<< " -  "<< folder_name(dat.projects[y].directory)<< endl;
  794.         }
  795.         cout<< endl<< endl;
  796.         cout<< " a -  Add new directory"<< endl;
  797.         cout<< " w -  Target Directory: "<< dat.options.txt_file_directory<< endl;
  798.         cout<< endl;
  799.         cout<< " s -  Unpack a Text file project (must have been made by this program)"<< endl;
  800.         cout<< " q -  Exit"<< endl;
  801.         cout.flush();
  802.         getline(cin, s_choice);
  803.         if(string_is_number(s_choice) == true)
  804.         {
  805.             if(s_choice.size() < 11)
  806.             {
  807.                 choice = conv_str(s_choice);
  808.                 if((unsigned(choice) <= dat.projects.size()) && (choice > 0))
  809.                 {
  810.                     choice--;
  811.                     mod_or_create(dat, 2, choice, dat.projects[choice].name);
  812.                 }
  813.             }
  814.         }
  815.         if(string_is_number(s_choice) == false)
  816.         {
  817.             if(s_choice.size() == 1)
  818.             {
  819.                 s_choice[0] = tolower(s_choice[0]);
  820.                 if(s_choice[0] == 'w')
  821.                 {
  822.                     temps = modify_directory(dat, 1);
  823.                     if(temps != "xcancel")
  824.                     {
  825.                         dat.options.txt_file_directory = temps;
  826.                     }
  827.                 }
  828.                 if(s_choice[0] == 's')
  829.                 {
  830.                     unpack_project(dat);
  831.                 }
  832.                 if(s_choice[0] == 'a')
  833.                 {
  834.                     temps = modify_directory(dat);
  835.                     if(temps != "xcancel")
  836.                     {
  837.                         dat.projects.push_back(project_data());
  838.                         dat.projects[(dat.projects.size() - 1)].directory = temps;
  839.                         load_project_data(dat.projects[(dat.projects.size() - 1)]);
  840.                         save_everything(dat);
  841.                     }
  842.                 }
  843.                 if(s_choice[0] == 'q')
  844.                 {
  845.                     save_everything(dat);
  846.                     return;
  847.                 }
  848.             }
  849.         }
  850.     }
  851. }
  852.  
  853.  
  854.  
  855. \\directory_mod.cpp END==================================================================
  856.  
  857. \\directory_mod.h begin==================================================================
  858.  
  859. #ifndef DIRECTORY_MOD_H_INCLUDED
  860. #define DIRECTORY_MOD_H_INCLUDED
  861. #include <string>
  862. #include <vector>
  863. #include "pdata.h"
  864. using namespace std;
  865.  
  866. void modify_user_project_directories(pdata& dat);
  867.  
  868. #endif // DIRECTORY_MOD_H_INCLUDED
  869.  
  870.  
  871. \\directory_mod.h END==================================================================
  872.  
  873. \\fsys.cpp begin==================================================================
  874.  
  875. #include <iostream>
  876. #include <string>
  877. #include <vector>
  878. #include "common.h"
  879. #include <boost/filesystem.hpp>
  880. #include "fsys.h"
  881.  
  882. using namespace std;
  883.  
  884. bool is_file(string s)
  885. {
  886.     boost::filesystem::path p;
  887.     p = s;
  888.     if(boost::filesystem::is_regular_file(p) == true)
  889.     {
  890.         s.clear();
  891.         p.clear();
  892.         return true;
  893.     }
  894.     s.clear();
  895.     p.clear();
  896.     return false;
  897. }
  898.  
  899. bool is_folder(string s)
  900. {
  901.     boost::filesystem::path p;
  902.     p = s;
  903.     if(boost::filesystem::is_directory(p) == true)
  904.     {
  905.         s.clear();
  906.         p.clear();
  907.         return true;
  908.     }
  909.     s.clear();
  910.     p.clear();
  911.     return false;
  912. }
  913.  
  914. bool data_there(string f)
  915. {
  916.     if(is_file(f) == false)
  917.     {
  918.         return false;
  919.     }
  920.     ifstream in;
  921.     string line;
  922.     int number_of_lines = 0;
  923.     in.open(f.c_str(), ios::in);
  924.     while(in.good())
  925.     {
  926.         getline(in, line);
  927.         if(line > "")
  928.         {
  929.             number_of_lines++;
  930.         }
  931.     }
  932.     in.close();
  933.     if(number_of_lines == 0)
  934.     {
  935.         return false;
  936.     }
  937.     if(number_of_lines > 0)
  938.     {
  939.         return true;
  940.     }
  941.     return false;
  942. }
  943.  
  944. string get_current_directory()
  945. {
  946.     boost::filesystem::path p;
  947.     p.clear();
  948.     p = boost::filesystem::current_path();
  949.     p = (p.string() + "\\");
  950.     return p.string();
  951. }
  952.  
  953. string file_name(string path)
  954. {
  955.     if(is_file(path) == false)
  956.     {
  957.         return "ERROR";
  958.     }
  959.     boost::filesystem::path p;
  960.     p = path;
  961.     return p.filename().string();
  962. }
  963.  
  964. string folder_name(string folder)
  965. {
  966.     if(is_folder(folder) == false)
  967.     {
  968.         return "ERROR";
  969.     }
  970.     boost::filesystem::path p;
  971.     p = folder;
  972.     return p.filename().string();
  973. }
  974.  
  975. bool folder_empty(string f)
  976. {
  977.     boost::filesystem::path p;
  978.     if(is_folder(f) == false)
  979.     {
  980.         return true;
  981.     }
  982.     p = f;
  983.     vector<boost::filesystem::directory_entry> directories = vector<boost::filesystem::directory_entry>();
  984.     copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), back_inserter(directories));
  985.     if(directories.size() > 0)
  986.     {
  987.         directories.clear();
  988.         return false;
  989.     }
  990.     if(directories.size() == 0)
  991.     {
  992.         directories.clear();
  993.         return true;
  994.     }
  995.     return true;
  996. }
  997.  
  998. string extension(string name = "")
  999. {
  1000.     bool dotmet = false;
  1001.     string ext = "";
  1002.     for(unsigned int x = 0; x < name.size(); x++)
  1003.     {
  1004.         if(name[x] == '.')
  1005.         {
  1006.             dotmet = true;
  1007.         }
  1008.         if(dotmet == true)
  1009.         {
  1010.             ext = (ext + name[x]);
  1011.         }
  1012.     }
  1013.     return ext;
  1014. }
  1015.  
  1016. //creates a menu to choose the text file you want to "unpack"
  1017. //returns the entire directory
  1018. string choose_file_to_unpack(string folder)
  1019. {
  1020.     if(is_folder(folder) == false)
  1021.     {
  1022.         return "xcancel";
  1023.     }
  1024.     boost::filesystem::path p;
  1025.     p = folder;
  1026.     vector<boost::filesystem::directory_entry> directories = vector<boost::filesystem::directory_entry>(), fils = vector<boost::filesystem::directory_entry>();
  1027.     copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), back_inserter(directories));
  1028.     if(directories.size() == 0)
  1029.     {
  1030.         cls();
  1031.         cout<< "I could not find anything!"<< endl;
  1032.         wait();
  1033.         return "xcancel";
  1034.     }
  1035.     //filtering out everything but text files
  1036.     for(unsigned int x = 0; x < directories.size(); x++)
  1037.     {
  1038.         if(is_file(directories[x].path().string()) == true)
  1039.         {
  1040.             if(extension(directories[x].path().filename().string()) == ".txt")
  1041.             {
  1042.                 fils.push_back(directories[x]);
  1043.             }
  1044.         }
  1045.     }
  1046.     directories = fils;
  1047.     fils.clear();
  1048.     int x = 0, choice;
  1049.     string s_choice;
  1050.     while(x == 0)
  1051.     {
  1052.         s_choice.clear();
  1053.         choice = 0;
  1054.         cls();
  1055.         cout<< "                     \"Unpack\" a text-compiled Source code"<< endl;
  1056.         cout<< "These are the text files I found in your target directory:";
  1057.         cout<< endl<< endl<< endl<< endl;
  1058.         for(unsigned int y = 0; y < directories.size(); y++)
  1059.         {
  1060.             cout<< " "<< (y + 1)<< " -  "<< directories[y].path().filename().string()<< endl;
  1061.         }
  1062.         cout<< endl<< endl;
  1063.         cout<< " x -  Cancel"<< endl;
  1064.         cout.flush();
  1065.         getline(cin, s_choice);
  1066.         if(string_is_number(s_choice) == true)
  1067.         {
  1068.             if(s_choice.size() < 11)
  1069.             {
  1070.                 choice = conv_str(s_choice);
  1071.                 if((choice > 0) && (unsigned(choice) <= directories.size()))
  1072.                 {
  1073.                     choice--;
  1074.                     return directories[choice].path().string();
  1075.                 }
  1076.             }
  1077.         }
  1078.         if(string_is_number(s_choice) == false)
  1079.         {
  1080.             if(s_choice.size() == 1)
  1081.             {
  1082.                 s_choice[0] = tolower(s_choice[0]);
  1083.                 if(s_choice[0] == 'x')
  1084.                 {
  1085.                     return "xcancel";
  1086.                 }
  1087.             }
  1088.         }
  1089.     }
  1090.     return "xcancel";
  1091. }
  1092.  
  1093. //This is now universal.  This is for special operations where a folder needs to be created, and a file/folder-system is created within it.
  1094. void create_folder(string name)
  1095. {
  1096.     boost::filesystem::path p;
  1097.     p = name;
  1098.     if(boost::filesystem::is_directory(p) == true)
  1099.     {
  1100.         name.clear();
  1101.         name = ("There is already a directory named \"" + p.string() + "\"!  Are you sure you want to over-write it? (all contents in the folder will be deleted!)");
  1102.         if(is_sure(name) == false)
  1103.         {
  1104.             return;
  1105.         }
  1106.         boost::filesystem::remove_all(p);
  1107.     }
  1108.     boost::filesystem::create_directories(p);
  1109. }
  1110.  
  1111.  
  1112.  
  1113. \\fsys.cpp END==================================================================
  1114.  
  1115. \\fsys.h begin==================================================================
  1116.  
  1117. #ifndef FSYS_H_INCLUDED
  1118. #define FSYS_H_INCLUDED
  1119. #include <string>
  1120. #include <vector>
  1121. using namespace std;
  1122.  
  1123. bool is_file(string s = "");
  1124. bool is_folder(string s = "");
  1125. string get_current_directory();
  1126. string file_name(string path = "");
  1127. string folder_name(string folder = "");
  1128. bool folder_empty(string f = "");
  1129. bool data_there(string f = "");
  1130. string choose_file_to_unpack(string folder = "");
  1131. void create_folder(string name = "");
  1132.  
  1133.  
  1134. #endif // FSYS_H_INCLUDED
  1135.  
  1136.  
  1137. \\fsys.h END==================================================================
  1138.  
  1139. \\load.cpp begin==================================================================
  1140.  
  1141. #include <iostream>
  1142. #include <fstream>
  1143. #include <boost/filesystem.hpp>
  1144. #include <string>
  1145. #include <vector>
  1146. #include "pdata.h"
  1147. #include "load.h"
  1148. #include "fsys.h"
  1149.  
  1150. using namespace std;
  1151.  
  1152. string get_ext(string f = "")
  1153. {
  1154.     string temps = "";
  1155.     //reverse the string
  1156.     for(unsigned int x = (f.size() - 1); x >= 0; x--)
  1157.     {
  1158.         temps = (temps + f[x]);
  1159.         if(x == 0)
  1160.         {
  1161.             break;
  1162.         }
  1163.     }
  1164.     f = temps;
  1165.     temps.clear();
  1166.     /*Adding each character until we hit "."*/
  1167.     for(unsigned int x = 0; x < f.size(); x++)
  1168.     {
  1169.         temps = (temps + f[x]);
  1170.         if(f[x] == '.')
  1171.         {
  1172.             break;
  1173.         }
  1174.     }
  1175.     //now we got an extention, lets re-reverse it back to normal
  1176.     f = temps;
  1177.     temps.clear();
  1178.     for(unsigned int x = (f.size() - 1); x >= 0; x--)
  1179.     {
  1180.         temps = (temps + f[x]);
  1181.         if(x == 0)
  1182.         {
  1183.             break;
  1184.         }
  1185.     }
  1186.     f = temps;
  1187.     temps.clear();
  1188.     /*So, now we got a file extension.*/
  1189.     return f;
  1190. }
  1191.  
  1192. string file_extension(string fullpath)
  1193. {
  1194.     boost::filesystem::path p;
  1195.     p = fullpath;
  1196.     if(boost::filesystem::is_regular_file(p) == false)
  1197.     {
  1198.         fullpath.clear();
  1199.         return "";
  1200.     }
  1201.     return get_ext(p.filename().string());
  1202. }
  1203.  
  1204. void create_program_folders(const pdata& dat)
  1205. {
  1206.     boost::filesystem::path p;
  1207.     p.clear();
  1208.     p = (p.string() + dat.curdir + dat.project_saves_folder_name);
  1209.     if(boost::filesystem::is_directory(p) == false)
  1210.     {
  1211.         boost::filesystem::create_directory(p);
  1212.     }
  1213.     p.clear();
  1214. }
  1215.  
  1216. bool is_project_extension(string ext = "")
  1217. {
  1218.     /*This is so that I can easily add new extensions if I need to.*/
  1219.     vector<string> extensions = {
  1220.         ".cpp",
  1221.         ".h"
  1222.     };
  1223.     if(ext == "")
  1224.     {
  1225.         return false;
  1226.     }
  1227.     for(unsigned int x = 0; x < extensions.size(); x++)
  1228.     {
  1229.         if(ext == extensions[x])
  1230.         {
  1231.             ext.clear();
  1232.             extensions.clear();
  1233.             return true;
  1234.         }
  1235.     }
  1236.     ext.clear();
  1237.     extensions.clear();
  1238.     return false;
  1239. }
  1240.  
  1241. void load_file(file_data& filex, string directory = "")
  1242. {
  1243.     if(is_file(directory) == false)
  1244.     {
  1245.         return;
  1246.     }
  1247.     boost::filesystem::path p;
  1248.     p = directory;
  1249.     ifstream in;
  1250.     string line;
  1251.     filex.text.clear();
  1252.     in.open(directory.c_str(), ios::in);
  1253.     while(in.good())
  1254.     {
  1255.         getline(in, line);
  1256.         filex.text.push_back(line);
  1257.     }
  1258.     in.close();
  1259.     filex.name = p.filename().string();
  1260.     line.clear();
  1261.     directory.clear();
  1262.     p.clear();
  1263. }
  1264.  
  1265. /*Loads a project based on the directory it is located.*/
  1266. void load_project_data(project_data& project)
  1267. {
  1268.     project.files.clear();
  1269.     if(is_folder(project.directory) == false)
  1270.     {
  1271.         project.directory = "ERROR: NO_ID";
  1272.         return;
  1273.     }
  1274.     boost::filesystem::path p;
  1275.     p = project.directory;
  1276.     project.name = p.filename().string();
  1277.     vector<boost::filesystem::directory_entry> directories = vector<boost::filesystem::directory_entry>();
  1278.     copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), back_inserter(directories));
  1279.     for(unsigned int x = 0; x < directories.size(); x++)
  1280.     {
  1281.         //if it is a project extension (contains code)
  1282.         if(is_project_extension(file_extension(directories[x].path().string())) == true)
  1283.         {
  1284.             //add a clean new data structure to the vector
  1285.             project.files.push_back(file_data());
  1286.             //load the data into it
  1287.             load_file(project.files[(project.files.size() - 1)], directories[x].path().string());
  1288.         }
  1289.     }
  1290.     p.clear();
  1291.     directories.clear();
  1292. }
  1293.  
  1294. void load_project_saves(pdata& dat)
  1295. {
  1296.     dat.projects.clear();
  1297.     ifstream in;
  1298.     string line, save_file_path;
  1299.     save_file_path = (dat.curdir + dat.project_saves_folder_name + "\\" + dat.projects_file_name);
  1300.     if(is_file(save_file_path) == false)
  1301.     {
  1302.         return;
  1303.     }
  1304.     in.open(save_file_path.c_str(), ios::in);
  1305.     while(in.good())
  1306.     {
  1307.         getline(in, line);
  1308.         if(is_folder(line) == false)
  1309.         {
  1310.             continue;
  1311.         }
  1312.         dat.projects.push_back(project_data());
  1313.         dat.projects[(dat.projects.size() - 1)].directory = line;
  1314.     }
  1315.     in.close();
  1316.     /*I chose to load outside of the while loop to
  1317.     minimize the ammount of time the file is open.*/
  1318.     for(unsigned int x = 0; x < dat.projects.size(); x++)
  1319.     {
  1320.         //loading up all of the projects now.
  1321.         load_project_data(dat.projects[x]);
  1322.     }
  1323.     line.clear();
  1324.     save_file_path.clear();
  1325. }
  1326.  
  1327. void load_options(pdata& dat)
  1328. {
  1329.     string options_file;
  1330.     options_file = (dat.curdir + dat.project_saves_folder_name + "\\Options for YoU.jon");
  1331.     if((is_file(options_file) == false) || (data_there(options_file) == false))
  1332.     {
  1333.         return;
  1334.     }
  1335.     dat.options.txt_file_directory.clear();
  1336.     ifstream in;
  1337.     string line;
  1338.     vector<string> f = vector<string>();
  1339.     in.open(options_file.c_str(), ios::in);
  1340.     while(in.good())
  1341.     {
  1342.         getline(in, line);
  1343.         f.push_back(line);
  1344.     }
  1345.     in.close();
  1346.     if(f.size() == 0)
  1347.     {
  1348.         return;
  1349.     }
  1350.     if(f.size() >= 1)
  1351.     {
  1352.         dat.options.txt_file_directory = f[0];
  1353.     }
  1354.     f.clear();
  1355.     line.clear();
  1356. }
  1357.  
  1358. void load_everything(pdata& dat)
  1359. {
  1360.     cout.flush();
  1361.     create_program_folders(dat);
  1362.  
  1363.     //load all the projects
  1364.     load_project_saves(dat);
  1365.  
  1366.     //loading options
  1367.     load_options(dat);
  1368. }
  1369.  
  1370. void save_everything(const pdata& dat)
  1371. {
  1372.     create_program_folders(dat);
  1373.     string projects_file = "", options_file = "";
  1374.     ofstream out;
  1375.     projects_file = (dat.curdir + dat.project_saves_folder_name + "\\" + dat.projects_file_name);
  1376.     options_file = (dat.curdir + dat.project_saves_folder_name + "\\" + dat.options_file_name);
  1377.     out.open(projects_file.c_str(), ios::out);
  1378.     for(unsigned int x = 0; x < dat.projects.size(); x++)
  1379.     {
  1380.         out<< dat.projects[x].directory;
  1381.         if(x < (dat.projects.size() - 1))
  1382.         {
  1383.             out<< endl;
  1384.         }
  1385.     }
  1386.     out.close();
  1387.     out.open(options_file.c_str(), ios::out);
  1388.     out<< dat.options.txt_file_directory;
  1389.     out.close();
  1390.     cout.flush();
  1391. }
  1392.  
  1393.  
  1394.  
  1395. \\load.cpp END==================================================================
  1396.  
  1397. \\load.h begin==================================================================
  1398.  
  1399. #ifndef LOAD_H_INCLUDED
  1400. #define LOAD_H_INCLUDED
  1401. #include <string>
  1402. #include <vector>
  1403. #include "pdata.h"
  1404. using namespace std;
  1405.  
  1406. string file_extension(string fullpath = "");
  1407. void load_project_data(project_data& project);
  1408. void load_everything(pdata& dat);
  1409. void save_everything(const pdata& dat);
  1410.  
  1411.  
  1412. #endif // LOAD_H_INCLUDED
  1413.  
  1414.  
  1415. \\load.h END==================================================================
  1416.  
  1417. \\main.cpp begin==================================================================
  1418.  
  1419. #include <iostream>
  1420. #include "load.h"
  1421. #include "pdata.h"
  1422. #include "directory_mod.h"
  1423.  
  1424. using namespace std;
  1425.  
  1426. int main()
  1427. {
  1428.     pdata dat;
  1429.     load_everything(dat);
  1430.     modify_user_project_directories(dat);
  1431.     return 0;
  1432. }
  1433.  
  1434.  
  1435. \\main.cpp END==================================================================
  1436.  
  1437. \\pdata.h begin==================================================================
  1438.  
  1439. #ifndef PDATA_H_INCLUDED
  1440. #define PDATA_H_INCLUDED
  1441. #include <string>
  1442. #include <vector>
  1443. #include "fsys.h"
  1444. using namespace std;
  1445.  
  1446. struct file_data{
  1447.     string name = "No_Name";
  1448.     vector<string> text = vector<string>();
  1449. };
  1450.  
  1451. struct project_data{
  1452.     vector<file_data> files = vector<file_data>();
  1453.     string directory = "";
  1454.     string name = "No Name";
  1455. };
  1456.  
  1457. struct options_data{
  1458.     string txt_file_directory = get_current_directory();
  1459. };
  1460.  
  1461. struct pdata{
  1462.     //includes the terminating slash.
  1463.     string curdir = get_current_directory();
  1464.     //For easy modification in case of conflictions
  1465.     string project_saves_folder_name = "Saved Projects";
  1466.     string projects_file_name = "Project Directories.jon";
  1467.     string options_file_name = "Options for YoU.jon";
  1468.  
  1469.     //projects
  1470.     vector<project_data> projects = vector<project_data>();
  1471.  
  1472.     //options
  1473.     options_data options = options_data();
  1474. };
  1475.  
  1476. #endif // PDATA_H_INCLUDED
  1477.  
  1478.  
  1479. \\pdata.h END==================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement