Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <fstream>
- #include <cassert>
- #include <cstdio>
- static int readuint32(std::ifstream& file) {
- unsigned char buffer[4];
- file.read((char *)&buffer, 4);
- int num = buffer[3] | buffer[2] << 8 | buffer[1] << 16 | buffer[0] << 24;
- return num;
- }
- static int printHeaderInfo(std::ifstream& file)
- {
- std::printf("Size of DIR file: %d kilobytes...\n", readuint32(file)); // to not use local variable when not needed
- int structNum = readuint32(file); // read
- std::printf("Number of DIR structs: %d\n\n", structNum);
- return structNum; // return
- }
- static std::string readFileNames(int mockStrLen, std::ifstream& file)
- {
- std::string temp(mockStrLen, 0); // create temp string the size of mockstrlen
- file.read(&temp[0], mockStrLen); // set the pointer to the start of the string and read mockstrlen amount of bytes
- return temp;
- }
- static std::string dirToArcFileName(std::string dirFile)
- {
- if (dirFile.size() <= 3) {
- perror("filename is less than or equal to 3 chars!!!");
- }
- else
- return (dirFile.substr(0, dirFile.size() - 3)).append("arc");
- }
- int main(int argc, char** argv)
- {
- if (argc <= 1) {
- std::printf("ARC+DIR file reader,\nUsage: drag and drop .dir file onto program.\nBy Ambrosia, 2019");
- return 1;
- }
- std::ifstream dirFile;
- dirFile.open(argv[1], std::ios::binary | std::ios::in);
- if (dirFile.is_open()) {
- int structNum, offset, size, mockStrLen;
- std::string fileName, arcFile;
- std::printf("arc file : %s \n", dirToArcFileName(argv[1]).c_str());
- structNum = printHeaderInfo(dirFile); // get header info
- for (int i = 0; i < structNum; i++) { // loop
- offset = readuint32(dirFile); // read uint
- size = readuint32(dirFile); // read uint
- mockStrLen = readuint32(dirFile); // read uint
- fileName = readFileNames(mockStrLen, dirFile);
- std::printf("%d: filename : %s \n", i, fileName.c_str());
- }
- }
- else {
- perror(argv[1]);
- return 1;
- }
- dirFile.close();
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement