Advertisement
Guest User

Untitled

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