There are plenty of if
s and else
s, and someone else told me that using switch
/case
statements could really clean up the code. However, he didn't implement it for me, so I don't know what I should switch/case.
Also take note that the if (!stricmp(argv[arg], "-vmf"))
and if (!stricmp(argv[arg], "-bsp"))
blocks are exactly the same, minus the usage of vmf
and bsp
, and that the wad
block is somewhat similar.
#include <cstdio>
#include <cstring>
#include <fstream>
#include <sys\stat.h>
#include "arg.h"
#include "files.h"
#include "stricmp.h"
using namespace std;
//"ifError" takes all the argument flags, as well as a string to output. If silent flag is
//enabled, then "ifError" will not output that text. I put this into the function to reduce
//lines and characters of code.
void ifError(argFlags& argFlags, char* errorMessage)
{
if (!argFlags.sil)
{
if (!argFlags.err)
{
printf("Converts DOOM WAD into VMF and/or BSP file(s).\n\n");
printf("DOOM2BSP [-silent] [-WAD [path][name]] [-VMF [path]] [-BSP [path]]\n\n");
printf(" -WAD Location of WAD.\n");
printf(" -VMF Create VMF file(s) in path.\n");
printf(" -BSP Create BSP file(s) in path.\n");
printf(" -silent Disable text output.\n\n");
}
printf(errorMessage);
}
argFlags.err = true;
}
//Checks if directory exists.
bool checkDir(char* path)
{
struct stat st;
if (!stat(path, &st))
if (st.st_mode & S_IFDIR != 0)
return true;
return false;
}
//This function analyzes our arguments, deals with them accordingly, and decides if there
//are any errors or not. Also opens wad if it exists.
void handleArgs(argFlags& argFlags, files& files, int& argc, char**& argv)
{
//Analyzes all arguments to find "-silent" arg. If silent arg is found, the silent flag
//is turned on, signifying that this program should not output any text.
for (int arg = 1; arg < argc; ++arg)
if (!stricmp(argv[arg], "-silent"))
argFlags.sil = true;
//Analyzes the rest of the arguments, with the silent flag in mind.
for (int arg = 1; arg < argc; ++arg)
{
//If the "-wad" arg is found, turn on wad flag. Look for wad name. If wad name is
//found, check if for its validity and existance. If check fails or if wad name arg
//is not found, output error.
if (!stricmp(argv[arg], "-wad"))
{
argFlags.wad = true;
if ((arg + 1) < argc)
{
files.wad.open(argv[arg + 1], fstream::out | fstream::binary);
if (!files.wad.good())
ifError(argFlags, "Error: WAD name is invalid or does not exist.\n");
}
else
ifError(argFlags, "Error: WAD name is not specified.\n");
}
//If the "-vmf" arg is found, turn on vmf flag. Look for vmf path name. If vmf path
//name is found, check for its validity and existance. If check fails or if vmf
//path arg is not found, output error.
else if (!stricmp(argv[arg], "-vmf"))
{
argFlags.vmf = true;
if ((arg + 1) < argc)
{
files.vmfPath = argv[arg + 1];
if (!checkDir(files.vmfPath))
ifError(argFlags, "Error: VMF path is invalid or does not exist.\n");
}
else
ifError(argFlags, "Error: VMF path is not specified.\n");
}
//If the "-bsp" arg is found, turn on bsp flag. Look for bsp path name. If bsp path
//name is found, check for its validity and existance. If check fails or if bsp
//path arg is not found, output error.
else if (!stricmp(argv[arg], "-bsp"))
{
argFlags.bsp = true;
if ((arg + 1) < argc)
{
files.bspPath = argv[arg + 1];
if (!checkDir(files.bspPath))
ifError(argFlags, "Error: BSP path is invalid or does not exist.\n");
}
else
ifError(argFlags, "Error: BSP path is not specified.\n");
}
}
//Decently self-explanatory.
if (argc == 1)
ifError(argFlags, "Error: No parameters.\n");
else if (!argFlags.wad)
ifError(argFlags, "Error: WAD parameters missing.\n");
else if ((!argFlags.vmf) && (!argFlags.bsp))
ifError(argFlags, "Error: VMF/BSP parameters missing.\n");
}