collapse collapse

* Who's Online

  • Dot Guests: 67
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11163
  • stats Total Topics: 1699
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 248

Author Topic: Challenge: Custom Zip File  (Read 3036 times)

0 Members and 1 Guest are viewing this topic.

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Challenge: Custom Zip File
« on: April 11, 2014, 05:04:04 pm »
Challenge: Create a simple class that works similarly to WinZip or Rar. No compression is needed for this challenge. The goal is to implement your own version of grouping multiple files into 1 file while still preserving the data.

Any extras you add in are up to you. There requirements to full fill are below.

:REQUIREMENTS:
1. Must be able to combine at least 6 files into one
2. Must be able to preserve folder structures
3. Must be able to extract files
4. Must be able to preserve files of different types


Lets do it!
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Challenge: Custom Zip File
« Reply #1 on: April 12, 2014, 04:05:03 pm »
EDIT: So.. there is some memory issue with this. I'm not sure where, but I'm working on fixing it... Damnit :(
EDIT2: Fixed it. I forgot to remove some padding I wrote to the file causing the offset to be 10 bytes off giving strange characters. Updated the code. I also added in some new code to grab the file from memory. Check it out. Updated attached file to reflect the new archive file.
EDIT3: Modified the code slightly to fall in line with certain etiquettes.

Here is my entry for this challenge. I based the entire system off of books. Each page in the book is a set of files and each book has a table of contents with offsets pointing to each chapter (specific file on a page). For now, all you can do is create books, read their stored data, and tear them apart. I can improve this and I most likely will later on for use in some of my projects that have a large amount of files.

FEATURES!
- Preserves folder structure and will recreate folder structure when tearing from the book.
- Easy to use
- Mostly error proof* All major points are checked for errors, but once things get rolling, the program assumes you won't have a power outage or something similar
- File structure is explained indepth in the header
- Very well commented

LIMITATIONS!
The limits of this program are basically described by the data types it uses.
It can store up to (unsigned short)[65,535] files and directories
It can store up to (unsigned long)[4,294,967,295]
Each page it adds to the book can also be around that size as long as it does not exceed a long's size;
You MUST use '\\' characters instead of forward slashes. Not doing so will cause bad things to happen... Like memory overflows.

A few things to note...
- This relies on the use of the dirent header which can be found here http://softagalleria.net/download/dirent/?C=M;O=D
The header is small and light weight which makes it good for small operations such as this.

- The class is not 100% cross platform. Each OS has different ways to create files/directories. Because of this, I focused on the Windows OS while making this.
Note that I tried to keep the data structures and functions very obvious. It should be easy to port this if anyone wants to do that.

- This was built for Multibyte projects. Attempting to use this in a unicode environment will cause it to fail. If you wanted to convert it over to unicode, you would have to change all the strings to wstrings and exchange a few functions that make heavy use of those strings.

:)

See if you can do better, or improve on my design if you don't know where to start.



[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]

OH YEA! If you need a example, I've included the entire project as a archived source in my own format. :)

[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
main.cpp
Code: C++
  1. #include "CFileSystem.h"
  2.  
  3. int main(){
  4.         CFileSystem f;
  5.         char* file;
  6.         std::string s = "C:\\Users\\Celestialkey\\Desktop\\Developer Drive Backup\\Projects\\Active\\Engine\\DXGameCraft\\data";
  7.  
  8.         f.FastenBook(s, "C:\\Users\\Celestialkey\\Documents\\Visual Studio 2012\\Projects\\CelFiSys\\Release\\data.key");
  9.         f.OpenBook("C:\\Users\\Celestialkey\\Documents\\Visual Studio 2012\\Projects\\CelFiSys\\Release\\data.key");   
  10.  
  11.         std::cout << "Details...\n------------------------\n";
  12.         f.ReadTableOfContents();
  13.         f.ReadDirectories();
  14.         f.ReadFiles();
  15.         std::cout << "--------------------------\n";
  16. //      string getFile;
  17. //      cin >> getFile;
  18. //      long sizet = f.GrabFileSize(getFile);
  19. //      cout << "File size of demo.txt " << sizet << endl;
  20. //      if(!sizet){
  21. //              f.CloseBook();
  22. //              cout << "Did not find file." << endl;
  23. //              getchar();
  24. //              return 1;
  25. //      }
  26. //      file = new char[sizet];
  27. //
  28. //      f.GrabFile(getFile, file);
  29. //      cout << file << endl;
  30. //     
  31. //      delete [] file;
  32. //      getchar();
  33.         f.TearBook();
  34.         f.CloseBook();
  35.         return true;
  36. }

CFileSystem.h
Code: C++
  1. #pragma once
  2.  
  3. #include <dirent.h>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <fstream>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10.  
  11. struct sPageData{
  12.         char    fileName[50];
  13.         char    filePath[255];
  14.         unsigned long   fileOffset;
  15.         unsigned long   fileSize;
  16. };
  17.  
  18. struct sTableOfContents{
  19.         char    bookTitle[255];
  20.         unsigned short  numOfChapters;
  21.         unsigned short  numOfPages;
  22.         std::string     rootDir;
  23.         std::string*    directories;
  24. };
  25.  
  26.  
  27.  
  28. class CFileSystem{
  29. private:
  30.         // All private variables are used to open and read in
  31.         // books created earlier. All create variables are
  32.         // created and destroyed during the create process.
  33.         sTableOfContents*       _readBookContents;
  34.         std::vector<std::string>                _chapterList;
  35.         std::vector<sPageData>  _pageList;
  36.         std::ifstream                   _inBook;
  37.  
  38. private:
  39.         int             _analyzeDir(std::string, std::vector<std::string>&, std::vector<std::string>&);
  40.         int             _buildTableOfContents(std::vector<std::string>&, std::vector<std::string>&, sPageData*, sTableOfContents&, std::string&);
  41.         long    _calculateOffset(long, sPageData*);
  42.         int             _writeToManuscript(sTableOfContents&, std::vector<std::string>&, sPageData*, std::ofstream&);
  43.         bool    _findFile(std::string s, sPageData& );
  44. public:
  45.         // Initializers/Deconstructors
  46.         CFileSystem();
  47.         ~CFileSystem();
  48.  
  49.         // Initializing Functions
  50.         bool OpenBook(std::string bookname);
  51.         void CloseBook();
  52.  
  53.         // Appending Functions
  54.         // None yet
  55.  
  56.         // Creation Functions
  57.         bool FastenBook(std::string dir, std::string outBook);
  58.         bool TearBook(std::string inBook);
  59.         bool TearBook();
  60.  
  61.         // Output Functions
  62.         void ReadFiles();
  63.         void ReadDirectories();
  64.         void ReadTableOfContents(bool = false);
  65.  
  66.         // Grab Functions
  67.         bool GrabFile(std::string filename, void* out);
  68.         char* GrabFile(std::string filename);
  69.  
  70.         unsigned long GrabFileSize(std::string filename);
  71.        
  72. };
  73.  
  74. //////////////////////////////////////////////////////////////////////////////////
  75. // Book ]
  76. //              -> Leaf ]
  77. //                              -> Page
  78. //////////////////////////////////////////////////////////////////////////////////
  79. //                                                              Book Structure                                                                  //
  80. //////////////////////////////////////////////////////////////////////////////////
  81. /*
  82. //      RootDir         [BYTE][255]
  83. //      DirCount        [SHORT][1]
  84. //
  85. //      DirPath         [BYTE][255]
  86. //      ...
  87. //      DirPath         [BYTE][255]
  88. //////////////////////////////////////////////////////////////////////////////////
  89. //      FileCount       [SHORT][1]
  90. //////////////////////////////////////////////////////////////////////////////////
  91. //      FilePath        [BYTE][255]
  92. //      FileName        [BYTE][50]
  93. //      FileSize        [LONG][1]
  94. //      FileOffset      [LONG][1]
  95. //      ...
  96. //      FilePath        [BYTE][255]
  97. //      FileName        [BYTE][50]
  98. //      FileSize        [LONG][1]
  99. //      FileOffset      [LONG][1]
  100. //////////////////////////////////////////////////////////////////////////////////
  101. //      FileData        [BYTE][sizeof(fileData)]
  102. //      ...
  103. //      FileData        [BYTE][sizeof(fileData)]
  104. */
  105. //////////////////////////////////////////////////////////////////////////////////
  106. //                                                                      Explained                                                                       //
  107. //////////////////////////////////////////////////////////////////////////////////
  108. /*
  109.  Fastening Steps
  110.         1. Iterate through directory provided and create manuscript
  111.         2. Use manuscript to write the table of contents
  112.         3. Write the table of contents to the book with padding to ensure set header size
  113.         4. Write raw binary data for each file into book
  114.  
  115.  
  116. */

CFileSystem.cpp
Code: C++
  1. #include "CFileSystem.h"
  2. using namespace std;
  3.  
  4. CFileSystem::CFileSystem(){
  5.         _readBookContents = 0;
  6. }
  7. CFileSystem::~CFileSystem(){
  8.         if(_readBookContents)
  9.                 CloseBook();
  10. }
  11. int CFileSystem::_analyzeDir(string dir, vector<string>& sChapters, vector<string>&sPages) {
  12.         DIR *getDir;
  13.         getDir = opendir(dir.c_str());
  14.  
  15.         if(getDir == nullptr) {
  16.                 cerr << endl << "Error opening directory " << dir
  17.                          << " (errno: " << errno << ")" << endl;
  18.                 return errno;
  19.         }
  20.         struct dirent *directoryPointer;
  21.         struct stat filestat;
  22.         string path;
  23.         while( (directoryPointer = readdir(getDir)) ) {
  24.                 //Skip current object if it is this directory or parent directory
  25.                 if(!strncmp(directoryPointer->d_name, ".", 1) || !strncmp(directoryPointer->d_name, "..", 2))continue;
  26.                 if(dir==".") path = directoryPointer->d_name;
  27.                 else             path = dir + "\\" + directoryPointer->d_name;
  28.  
  29.                 // Check for invalid pointers
  30.                 if(stat(path.c_str(), &filestat)) continue;
  31.  
  32.                 // Add to either list
  33.                 if(S_ISDIR(filestat.st_mode)) {
  34.                         sChapters.push_back(path);
  35.                         _analyzeDir(path,sChapters,sPages);
  36.                         continue;
  37.                 }else{
  38.                         sPages.push_back(path);
  39.                 }
  40.         }
  41.         closedir(getDir);
  42.        
  43.         return 0;
  44. }
  45. int CFileSystem::_buildTableOfContents(vector<string>& sChapters, vector<string>&sPages, sPageData* fileList, sTableOfContents& table, string& dir){
  46.         int dirOffset = dir.size()-(dir.size()-dir.find_last_of("\\"));
  47.  
  48.         // Get file statistics
  49.         for(unsigned int i=0; i<sPages.size(); i++){
  50.                 // Size
  51.                 struct stat pageDetails;
  52.                 stat(sPages[i].c_str(), &pageDetails);
  53.                 fileList[i].fileSize = pageDetails.st_size;
  54.  
  55.                 // Find name
  56.                 // Locate the last '\', add 1 to the position, and save everything after that position to a string
  57.                 string name = sPages[i].substr(sPages[i].find_last_of('\\')+1, sPages[i].size());
  58.                 strcpy_s(fileList[i].fileName, 50, name.c_str());
  59.                
  60.                 // Path
  61.                 // Locate the last '\', save everything from position 0 to found '\'
  62.                 string path = sPages[i].substr(0, sPages[i].find_last_of('\\'));
  63.                 path = path.substr(dirOffset, path.size());
  64.                 path.insert(0, ".");
  65.                 strcpy_s(fileList[i].filePath, 255, path.c_str());
  66.         }
  67.         // Fill out rest of info for ToC
  68.         table.directories = new string[sChapters.size()];
  69.         for(unsigned int i=0; i<sChapters.size(); i++){
  70.                 sChapters[i] = sChapters[i].substr(dirOffset, sChapters[i].size());
  71.                 sChapters[i] = sChapters[i].insert(0, ".");
  72.                 table.directories[i] =sChapters[i];
  73.         }
  74.  
  75.         table.numOfChapters = sChapters.size();
  76.         table.numOfPages = sPages.size();
  77.  
  78.         return 0;
  79. }
  80. int CFileSystem::_writeToManuscript(sTableOfContents& table, vector<string>&sPages, sPageData* files, ofstream& out){
  81.         // Keep track of current offset
  82.         long curOffset = 0;
  83.         int structSize = 0;
  84.         long fileOffset = 0;
  85.  
  86.         // Get the size of each file header
  87.         // This can be found by looking at the logic structure
  88. //--------------------------------------
  89. //      FilePath        [BYTE][255]
  90. //      FileName        [BYTE][50]
  91. //      FileSize        [LONG][1]
  92. //      FileOffset      [LONG][1]
  93. //--------------------------------------
  94.         structSize =
  95.                 (sizeof(char)*255)
  96.                 +(sizeof(char)*50)
  97.                 +sizeof(unsigned long)
  98.                 +sizeof(unsigned long);
  99.         cout << "File header size: " << structSize << "\n";
  100.  
  101.         // For reference for the order, look back to the header
  102.         // First we write single data
  103.         // Buffer
  104.         char outc[255] = {'\0'};
  105.         out.write(reinterpret_cast <const char*> (&table.rootDir), sizeof(char)*255);
  106.         curOffset+= sizeof(char)*255;
  107.  
  108.         out.write(reinterpret_cast <const char*> (&table.numOfChapters), sizeof(unsigned short));
  109.         curOffset+= sizeof(unsigned short);
  110.  
  111.         for(unsigned short i=0; i < table.numOfChapters; i++){
  112.                 strcpy_s(outc, 255, table.directories[i].c_str());
  113.                 out.write((char*)outc, sizeof(char)*255);
  114.                 curOffset+= (sizeof(char)*255);
  115.                 memset(outc, 0, 255);
  116.  
  117.         }
  118.         out.write(reinterpret_cast <const char*> (&table.numOfPages), sizeof(unsigned short));
  119.         curOffset+= sizeof(unsigned short);
  120.  
  121.         // Calculate the offset for the first file
  122.         fileOffset =
  123.                 (sPages.size()*structSize)      // Number of headers that will be in the file * how large they are
  124.                 + curOffset;                                    // The current offset that the descriptor header took up
  125.  
  126.         // Write each file header to the book
  127.         for(unsigned int i=0; i<sPages.size(); i++){
  128.                 // Parse together the file name
  129.                 string file = files[i].filePath + string("\\") + files[i].fileName;
  130.  
  131.                 // Write the file path
  132.                 strcpy_s(outc, 255, files[i].filePath);
  133.                 out.write((char*)outc, sizeof(char)*255);
  134.                 memset(outc, 0, 255);
  135.  
  136.                 // Write the file name
  137.                 strcpy_s(outc, 255, files[i].fileName);
  138.                 out.write((char*)outc, sizeof(char)*50);
  139.                 memset(outc, 0, 255);
  140.  
  141.                 // Write file size
  142.                 out.write(reinterpret_cast <const char*> (&files[i].fileSize), sizeof(unsigned long));
  143.  
  144.                 // Write the future file offset
  145.                 out.write(reinterpret_cast <const char*> (&fileOffset), sizeof(unsigned long));
  146.  
  147.                 // Prepare next future file offset
  148.                 fileOffset += files[i].fileSize;
  149.         }
  150.  
  151.         // Write each file object to the book
  152.         for(unsigned int i=0; i<sPages.size(); i++){
  153.                 ifstream inFile;
  154.                 inFile.open(sPages[i].c_str(), ios::binary);
  155.                 if(inFile.is_open()){
  156.                         char* data = new char[files[i].fileSize];
  157.                         inFile.read(data, files[i].fileSize);
  158.                         out.write(data, files[i].fileSize);
  159.                         delete [] data;
  160.                         inFile.close();
  161.                 } else {
  162.                         cout << "Error opening file " << sPages[i] << endl
  163.                                 << "File may not exist or it may be in use by another program" << endl
  164.                                 << "User running the file may also not have permissions to it." << endl
  165.                                 << "Aborting book creation\n";
  166.                         return -1;
  167.                 }
  168.         }
  169.         return 0;
  170. }
  171. bool CFileSystem::_findFile(string s, sPageData& p){
  172.         // Attempt to find via full path
  173.         for(short i=0; i<_readBookContents->numOfPages; i++){
  174.                 // string::compare returns 0 when they are equal
  175.                 if(!s.compare(string(_pageList[i].filePath + string("\\") +_pageList[i].fileName))){
  176.                         p= _pageList[i];
  177.                         return true;
  178.                 }
  179.         }
  180.         // Attempt to find ANY file that has that name
  181.         for(short i=0; i<_readBookContents->numOfPages; i++){
  182.                 // string::compare returns 0 when they are equal
  183.                 if(!s.compare(_pageList[i].fileName)){
  184.                         p= _pageList[i];
  185.                         return true;
  186.                 }
  187.         }
  188.         return false;
  189. }
  190. void CFileSystem::ReadDirectories(){
  191.         cout << "ROOT: " << _readBookContents->rootDir<< endl;
  192.         for(unsigned int i=0; i<_chapterList.size(); i++)
  193.                 cout <<"SUB : "<< _chapterList[i].c_str() << endl;
  194. }
  195. void CFileSystem::ReadFiles(){
  196.         for(unsigned int i=0; i<_pageList.size(); i++){
  197.                 cout << _pageList[i].fileName << endl;
  198.         }
  199. }
  200. void CFileSystem::ReadTableOfContents(bool details){
  201.         if(!_readBookContents) return;
  202.         cout << "Root [" << _readBookContents->rootDir << "]\n"
  203.                  << "Number of Directories: " << _readBookContents->numOfChapters << endl
  204.                  << "Number of Files: " << _readBookContents->numOfPages << endl;
  205.         if(details){
  206.                 for(unsigned short i=0; i<_readBookContents->numOfPages; i++){
  207.                         cout << "File\n---------------------\n"
  208.                                 << "Name: " << _pageList[i].fileName << endl
  209.                                 << "Path: " << _pageList[i].filePath << endl
  210.                                 << "Size: " << _pageList[i].fileSize << "(bytes)" << endl
  211.                                 << "Offset: " << _pageList[i].fileOffset << endl << endl;
  212.                 }
  213.         }
  214. }
  215. bool CFileSystem::FastenBook(string dir, string outBook){
  216.         // Define what is required
  217.         vector<string> sChapters;
  218.         vector<string> sPages;
  219.         sPageData*                      files;  // Contains all file header data
  220.         sTableOfContents        dirs;   // Contains reference data
  221.  
  222.         dirs.rootDir = dir.substr(dir.find_last_of("\\"), dir.size());
  223.         dirs.rootDir = dirs.rootDir.insert(0, ".");
  224.  
  225.         // Build a file list.
  226.         _analyzeDir(dir, sChapters, sPages);
  227.  
  228.         // Create file entries
  229.         files = new sPageData[sPages.size()];
  230.  
  231.         // Fill requires data structures
  232.         _buildTableOfContents(sChapters, sPages, files, dirs, dir);
  233.  
  234.         // Open up the empty book
  235.         ofstream manuscript;
  236.         manuscript.open(outBook, ios::binary);
  237.                
  238.         _writeToManuscript(dirs, sPages, files, manuscript);
  239.  
  240.         manuscript.close();
  241.         delete [] files;
  242.         delete [] dirs.directories;
  243.         return true;
  244. }
  245. bool CFileSystem::OpenBook(string book){
  246.         // Check for an already opened book
  247.         if(_inBook.is_open()) return false;
  248.  
  249.         // Open the book
  250.         _inBook.open(book.c_str(), ios::binary);
  251.         if(!_inBook.is_open()) return false;
  252.         // Since the file exists, create the ToC
  253.         _readBookContents = new sTableOfContents;
  254.  
  255.         // Remember the file structure
  256. //      RootDir         [BYTE][255]
  257. //      DirCount        [SHORT][1]
  258. //      dirPath         [BYTE][255]
  259.         char buf[255] = {'\0'};
  260.         // Read in the root directory
  261.         _inBook.read(buf, sizeof(char)*255);
  262.         _readBookContents->rootDir=buf;
  263.  
  264.         // Read in the number of directories stored
  265.         _inBook.read(reinterpret_cast <char*> (&_readBookContents->numOfChapters), sizeof(unsigned short));
  266.  
  267.         // Allocate enough space for each directory path
  268.         _readBookContents->directories = new string[_readBookContents->numOfChapters];
  269.         for(unsigned short i=0; i< _readBookContents->numOfChapters; i++){
  270.                 _inBook.read(buf, sizeof(char)*255);
  271.                 _readBookContents->directories[i] = buf;
  272.                 _chapterList.push_back(buf);
  273.                 memset(buf, 0, 255);
  274.         }
  275.  
  276.         // Convert the relative paths into absolute paths
  277.         GetCurrentDirectory(255, buf);
  278.         cout << "Current Directory: " << buf << endl;
  279.         for(unsigned short i=0; i< _readBookContents->numOfChapters; i++){
  280.                 _chapterList[i] = _chapterList[i].substr(1, _chapterList[i].size());
  281.                 _chapterList[i] = _chapterList[i].insert(0, buf);
  282.                 _readBookContents->directories[i] = _chapterList[i];
  283.         }
  284.  
  285. //////////////////////////////////////////////////////////////////////////////////
  286. //      FileCount       [SHORT][1]
  287. //////////////////////////////////////////////////////////////////////////////////
  288. //      FilePath        [BYTE][255]
  289. //      FileName        [BYTE][50]
  290. //      FileSize        [LONG][1]
  291. //      FileOffset      [LONG][1]
  292. //////////////////////////////////////////////////////////////////////////////////
  293.         // Load in the next part describing the number of files
  294.         _inBook.read(reinterpret_cast <char*> (&_readBookContents->numOfPages), sizeof(unsigned short));
  295.         sPageData tmp;
  296.         for(unsigned short i=0; i<_readBookContents->numOfPages; i++){
  297.                 _inBook.read(reinterpret_cast <char*> (&tmp.filePath), sizeof(char)*255);
  298.                 _inBook.read(reinterpret_cast <char*> (&tmp.fileName), sizeof(char)*50);
  299.                 _inBook.read(reinterpret_cast <char*> (&tmp.fileSize), sizeof(unsigned long));
  300.                 _inBook.read(reinterpret_cast <char*> (&tmp.fileOffset), sizeof(unsigned long));
  301.                 _pageList.push_back(tmp);
  302.                 memset(&tmp, 0, sizeof sPageData);
  303.         }
  304.  
  305.         // The file is now "loaded" into memory.
  306.         // Since files can be very large in terms of size, it is
  307.         // best to only load in the memory pointers until
  308.         // the user requests a file in particular.
  309.         return true;
  310. }
  311. bool CFileSystem::TearBook(string b){
  312.         if(!_inBook.is_open())
  313.                 OpenBook(b);
  314.         TearBook();
  315.         CloseBook();
  316.         return true;
  317. }
  318. bool CFileSystem::TearBook(){
  319.         if(!_inBook.is_open()) return false;
  320.  
  321.         // First create the directory root
  322.         CreateDirectory(_readBookContents->rootDir.c_str(), NULL);
  323.         // Then create the directory structure
  324.         for(short i=0; i<_readBookContents->numOfChapters; i++){
  325.                 CreateDirectory(_readBookContents->directories[i].c_str(), NULL);
  326.         }
  327.  
  328.         // Tear each page from the book
  329.         for(short i=0; i<_readBookContents->numOfPages; i++){
  330.                 // Navigate to each offset and read it into a buffer
  331.                 char* buff = new char[_pageList[i].fileSize];
  332.                 _inBook.seekg(_pageList[i].fileOffset);
  333.                 _inBook.read(buff, _pageList[i].fileSize);
  334.  
  335.                 // Create the entire file path [ path + name ]
  336.                 string fp = _pageList[i].filePath + string("\\") + _pageList[i].fileName;
  337.                 ofstream outFile;
  338.                 outFile.open(fp.c_str(), ios::binary);
  339.                
  340.                 // This file may be corrupt in some way, attempt to restore
  341.                 // the rest of the files anyhow
  342.                 if(!outFile.is_open()){
  343.                         delete [] buff;
  344.                         continue;
  345.                 }
  346.  
  347.                 // Write the data to restore the file
  348.                 outFile.write(buff, _pageList[i].fileSize);
  349.                 outFile.close();
  350.                 delete [] buff;
  351.         }
  352.         return true;
  353. }
  354. void CFileSystem::CloseBook(){
  355.         if(_inBook.is_open()){
  356.                 _inBook.close();
  357.         }
  358.         if(_readBookContents){
  359.                 if(_readBookContents->directories){
  360.                         delete [] _readBookContents->directories;
  361.                 }
  362.                 delete _readBookContents;
  363.                 _readBookContents = 0;
  364.         }
  365. }
  366. bool CFileSystem::GrabFile(string f, void* out){
  367.         // Cast data into type
  368.         char* p = static_cast<char*>(out);
  369.         // Locate and load file into buffer from file
  370.         sPageData s;
  371.         if(!_findFile(f, s)) return false;
  372.         _inBook.seekg(s.fileOffset, ios::beg);
  373.         _inBook.read(p, s.fileSize);
  374.  
  375.         return true;
  376. }
  377. unsigned long CFileSystem::GrabFileSize(string f){
  378.         sPageData s;
  379.         if(!_findFile(f, s)) return 0;
  380.         return s.fileSize;
  381. }
« Last Edit: April 13, 2014, 12:54:26 pm by Celestialkey »
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

 

Donate


* Search


* Recent Posts

Image Comparison by Shishka
[May 15, 2017, 01:18:02 pm]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 pm]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 am]


Updates by Nathan
[October 30, 2015, 08:27:36 pm]


Re: Client-Server Messaging by Nathan
[October 25, 2015, 05:48:57 pm]