Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include "pch.h"
  2. #include <fstream>
  3. #include <cassert>
  4. #include <cstdio>
  5.  
  6. static int readuint32(std::ifstream& file) {
  7.     unsigned char buffer[4];
  8.     file.read((char *)&buffer,  4);
  9.     int num = buffer[3] | buffer[2] << 8 | buffer[1] << 16 | buffer[0] << 24;
  10.     return num;
  11. }
  12.  
  13. static int printHeaderInfo(std::ifstream& file)
  14. {
  15.     std::printf("Size of DIR file: %d kilobytes...\n", readuint32(file)); // to not use local variable when not needed
  16.     int structNum = readuint32(file); // read
  17.     std::printf("Number of DIR structs: %d\n\n", structNum);
  18.     return structNum; // return
  19. }
  20.  
  21. static std::string readFileNames(int mockStrLen, std::ifstream& file)
  22. {
  23.     std::string temp(mockStrLen, 0); // create temp string the size of mockstrlen
  24.     file.read(&temp[0], mockStrLen); // set the pointer to the start of the string and read mockstrlen amount of bytes
  25.     return temp;
  26. }
  27.  
  28. static std::string dirToArcFileName(std::string dirFile)
  29. {
  30.     if (dirFile.size() <= 3) {
  31.         perror("filename is less than or equal to 3 chars!!!");
  32.     }
  33.     else
  34.         return (dirFile.substr(0, dirFile.size() - 3)).append("arc");
  35. }
  36.  
  37. int main(int argc, char** argv)
  38. {
  39.     if (argc <= 1) {
  40.         std::printf("ARC+DIR file reader,\nUsage: drag and drop .dir file onto program.\nBy Ambrosia, 2019");
  41.         return 1;
  42.     }
  43.     std::ifstream dirFile;
  44.  
  45.     dirFile.open(argv[1], std::ios::binary | std::ios::in);
  46.     if (dirFile.is_open()) {
  47.         int structNum, offset, size, mockStrLen;
  48.         std::string fileName, arcFile;
  49.  
  50.         std::printf("arc file : %s \n", dirToArcFileName(argv[1]).c_str());
  51.         structNum = printHeaderInfo(dirFile); // get header info
  52.  
  53.         for (int i = 0; i < structNum; i++) { // loop
  54.             offset = readuint32(dirFile); // read uint
  55.             size = readuint32(dirFile); //  read uint
  56.             mockStrLen = readuint32(dirFile); // read uint
  57.             fileName = readFileNames(mockStrLen, dirFile);
  58.            
  59.             std::printf("%d: filename : %s \n", i, fileName.c_str());
  60.         }
  61.  
  62.     }
  63.     else {
  64.         perror(argv[1]);
  65.         return 1;
  66.     }
  67.     dirFile.close();
  68.     getchar();
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement