Skip to content
This repository has been archived by the owner on Sep 15, 2022. It is now read-only.

Commit

Permalink
Remove using namespace std
Browse files Browse the repository at this point in the history
From general advice, removed "using namespace std" and added appropriate prefixing where necessary in order to keep function calls explicit to their respective libraries.
  • Loading branch information
resistiv committed Apr 22, 2022
1 parent 06502a6 commit 3ad407b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
35 changes: 18 additions & 17 deletions src/Extractor.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#include "Extractor.hpp"
using namespace std;

// Constructor
Extractor::Extractor(string fileName)
Extractor::Extractor(std::string fileName)
{
this->fileName = fileName;

// Attempt file open
inFile.open(fileName, ios::in | ios::binary);
inFile.open(fileName, std::ios::in | std::ios::binary);
if (!inFile.is_open())
{
cerr << "Could not open file \"" << fileName << "\" for reading, aborting." << endl;
std::cerr << "Could not open file \"" << fileName << "\" for reading, aborting." << std::endl;
return;
}

Expand All @@ -19,7 +18,7 @@ Extractor::Extractor(string fileName)
GetFullPathName(fileName.c_str(), MAX_PATH, fullPath, nullptr);

// Append output signifier
outputDir = string(fullPath) + "_out";
outputDir = std::string(fullPath) + "_out";

// Create main output dir
if (CreateDir(outputDir) == -1)
Expand All @@ -39,12 +38,12 @@ int Extractor::ExtractFiles()
for (int i = 0; i < fileCount; i++)
{
char nameBuf;
string outFileName = "";
string outSubDir = "";
std::string outFileName = "";
std::string outSubDir = "";
int fileLength;

// Seek to file offset
inFile.seekg(fileIndexes[i], ios_base::beg);
inFile.seekg(fileIndexes[i], std::ios_base::beg);

// Read file name (null-terminated, null-padded to next four byte border)
inFile.read(&nameBuf, sizeof(nameBuf));
Expand All @@ -60,26 +59,26 @@ int Extractor::ExtractFiles()
// Create all needed subdirectories
if (CreateSubDirs(outputDir, outFileName) == -1)
{
cerr << "Failed to generate sub-directories for file \"" << outFileName << "\", aborting." << endl;
std::cerr << "Failed to generate sub-directories for file \"" << outFileName << "\", aborting." << std::endl;
return -1;
}

// Skip to next four-byte border
inFile.seekg(3 - (outFileName.size() % 4), ios_base::cur);
inFile.seekg(3 - (outFileName.size() % 4), std::ios_base::cur);

// Read file length
inFile.read((char *)&fileLength, sizeof(fileLength));

// Initialize output file
ofstream outFile(outputDir + "\\" + outFileName, ios::out | ios::binary);
std::ofstream outFile(outputDir + "\\" + outFileName, std::ios::out | std::ios::binary);
if (!outFile.is_open())
{
cerr << "Could not open file \"" << outputDir + "\\" + outFileName << "\" for writing, aborting." << endl;
std::cerr << "Could not open file \"" << outputDir + "\\" + outFileName << "\" for writing, aborting." << std::endl;
return -1;
}

// Output file info
cout << std::hex << "[" << outFileName << "] Offset: 0x" << fileIndexes[i] << " | Length: 0x" << fileLength << std::dec << endl;
std::cout << std::hex << "[" << outFileName << "] Offset: 0x" << fileIndexes[i] << " | Length: 0x" << fileLength << std::dec << std::endl;

// Read file
int bytesLeft = fileLength;
Expand Down Expand Up @@ -122,23 +121,23 @@ void Extractor::ReadTOC()
}

// Creates a directory; returns -1 if the creation failed
int Extractor::CreateDir(string directory)
int Extractor::CreateDir(std::string directory)
{
// Create directory
if (!CreateDirectory(directory.c_str(), NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
{
cerr << "Failed to create output directory with path \"" << directory << "\", aborting." << endl;
std::cerr << "Failed to create output directory with path \"" << directory << "\", aborting." << std::endl;
return -1;
}
return 0;
}

// Generates all the needed sub-directories for a nested file
int Extractor::CreateSubDirs(string topDir, string workingList)
int Extractor::CreateSubDirs(std::string topDir, std::string workingList)
{
// Find dir delimiter
unsigned int slashIndex = workingList.find("\\");
if (slashIndex == string::npos)
if (slashIndex == std::string::npos)
return 0;

// Update strings
Expand All @@ -150,6 +149,8 @@ int Extractor::CreateSubDirs(string topDir, string workingList)
return -1;

CreateSubDirs(topDir, workingList);

return 0;
}

bool Extractor::IsReady() const
Expand Down
15 changes: 7 additions & 8 deletions src/Extractor.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#ifndef EXTRACTOR_H
#define EXTRACTOR_H
using namespace std;

#include <algorithm>
#include <fstream>
Expand All @@ -13,19 +12,19 @@ using namespace std;
class Extractor
{
public:
Extractor(string fileName);
Extractor(std::string fileName);
int ExtractFiles();
bool IsReady() const;
private:
void ReadTOC();
int CreateDir(string directory);
int CreateSubDirs(string topDir, string workingList);
int CreateDir(std::string directory);
int CreateSubDirs(std::string topDir, std::string workingList);
bool isReady = false;
string fileName;
ifstream inFile;
std::string fileName;
std::ifstream inFile;
int fileCount;
vector<int> fileIndexes;
string outputDir;
std::vector<int> fileIndexes;
std::string outputDir;
char outBuf[BUFF_SIZE];
};

Expand Down
5 changes: 2 additions & 3 deletions src/Globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
#define GLOBALS_H

#include <string>
using namespace std;

const string VERSION = "1.1";
const string USAGE = " <PAK file path>";
const std::string VERSION = "1.2";
const std::string USAGE = " <PAK file path>";

#endif
11 changes: 5 additions & 6 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
#include <fstream>
#include "Extractor.hpp"
#include "Globals.hpp"
using namespace std;

// Prototypes
int WriteUsage();

int main(int argc, char** argv)
{
cout << "VRPE++ v" << VERSION << " by Kaitlyn NeSmith" << endl;
std::cout << "VRPE++ v" << VERSION << " by Kaitlyn NeSmith" << std::endl;

// Test args
if (argc != 2)
Expand All @@ -19,25 +18,25 @@ int main(int argc, char** argv)
Extractor ext(argv[1]);
if (!ext.IsReady())
{
cerr << "Extractor initialization failed, aborting." << endl;
std::cerr << "Extractor initialization failed, aborting." << std::endl;
return EXIT_FAILURE;
}

// Extract files, check for error
if (ext.ExtractFiles() == -1)
{
cerr << "Extraction failed, aborting." << endl;
std::cerr << "Extraction failed, aborting." << std::endl;
return EXIT_FAILURE;
}

cout << "Done." << endl;
std::cout << "Done." << std::endl;

return EXIT_SUCCESS;
}

// Writes usage to output
int WriteUsage()
{
cout << "Usage: VRPE" << USAGE << endl;
std::cout << "Usage: VRPE" << USAGE << std::endl;
return EXIT_FAILURE;
}

0 comments on commit 3ad407b

Please sign in to comment.