2014-10-29 05:20:24 +01:00
|
|
|
|
2015-05-13 02:39:56 +02:00
|
|
|
#include "idemowriter.h"
|
|
|
|
#include "demoreader.h"
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
2015-05-15 05:38:53 +02:00
|
|
|
#include <cassert>
|
2015-04-30 22:03:22 +02:00
|
|
|
|
2015-05-13 02:39:56 +02:00
|
|
|
std::string GetExtension(const std::string& filename)
|
2015-04-30 22:03:22 +02:00
|
|
|
{
|
2015-05-13 02:39:56 +02:00
|
|
|
size_t index = filename.find_last_of(".");
|
|
|
|
if (index != std::string::npos)
|
2015-04-30 22:03:22 +02:00
|
|
|
{
|
2015-05-13 02:39:56 +02:00
|
|
|
return filename.substr(index + 1);
|
2015-04-30 22:03:22 +02:00
|
|
|
}
|
2015-05-13 02:39:56 +02:00
|
|
|
return std::string();
|
2015-04-30 22:03:22 +02:00
|
|
|
}
|
2014-10-29 05:20:24 +01:00
|
|
|
|
2015-05-13 02:39:56 +02:00
|
|
|
enum class FileType
|
2015-05-04 02:13:35 +02:00
|
|
|
{
|
2015-05-13 02:39:56 +02:00
|
|
|
None,
|
|
|
|
Dem,
|
2015-05-15 05:38:53 +02:00
|
|
|
Json,
|
|
|
|
ConLog
|
2015-05-13 02:39:56 +02:00
|
|
|
};
|
2015-05-04 02:13:35 +02:00
|
|
|
|
2015-05-13 02:39:56 +02:00
|
|
|
FileType GetFileType(const std::string& filename)
|
|
|
|
{
|
|
|
|
std::string ext = GetExtension(filename);
|
|
|
|
if (ext == "dem")
|
|
|
|
{
|
|
|
|
return FileType::Dem;
|
|
|
|
}
|
|
|
|
if (ext == "json")
|
2015-05-04 04:40:41 +02:00
|
|
|
{
|
2015-05-13 02:39:56 +02:00
|
|
|
return FileType::Json;
|
2015-05-04 04:40:41 +02:00
|
|
|
}
|
2015-05-15 05:38:53 +02:00
|
|
|
if (ext == "con")
|
|
|
|
{
|
|
|
|
return FileType::ConLog;
|
|
|
|
}
|
2015-05-13 02:39:56 +02:00
|
|
|
return FileType::None;
|
2015-05-04 02:13:35 +02:00
|
|
|
}
|
|
|
|
|
2014-10-29 05:20:24 +01:00
|
|
|
int main(const int argc, const char* argv[])
|
|
|
|
{
|
2015-05-13 02:39:56 +02:00
|
|
|
if (argc != 3)
|
2014-10-29 05:20:24 +01:00
|
|
|
{
|
2015-05-15 08:56:52 +02:00
|
|
|
fprintf(stderr, "Usage: %s <in>.dem/json <out>.dem/json/con\n", argv[0]);
|
2014-10-29 05:20:24 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-13 02:39:56 +02:00
|
|
|
std::string inputFile(argv[1]);
|
|
|
|
std::string outputFile(argv[2]);
|
|
|
|
|
|
|
|
FileType inputType = GetFileType(inputFile);
|
|
|
|
FileType outputType = GetFileType(outputFile);
|
|
|
|
if (inputType == FileType::None)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: Bad type for input file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (outputType == FileType::None)
|
2014-10-29 05:20:24 +01:00
|
|
|
{
|
2015-05-13 02:39:56 +02:00
|
|
|
fprintf(stderr, "Error: Bad type for output file\n");
|
2014-10-29 05:20:24 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-13 02:39:56 +02:00
|
|
|
FILE* inputFp = fopen(inputFile.c_str(), "rb");
|
|
|
|
if (!inputFp)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: Could not open input file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* outputFp = fopen(outputFile.c_str(), "wb");
|
|
|
|
if (!outputFp)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: Could not open input file\n");
|
|
|
|
fclose(inputFp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-15 05:38:53 +02:00
|
|
|
IDemoWriter* writer = nullptr;
|
2015-05-13 02:39:56 +02:00
|
|
|
if (outputType == FileType::Dem)
|
|
|
|
{
|
|
|
|
writer = IDemoWriter::CreateDemoWriter(outputFp);
|
|
|
|
}
|
2015-05-15 05:38:53 +02:00
|
|
|
else if (outputType == FileType::Json)
|
2015-05-13 02:39:56 +02:00
|
|
|
{
|
|
|
|
writer = IDemoWriter::CreateJsonWriter(outputFp);
|
|
|
|
}
|
2015-05-15 05:38:53 +02:00
|
|
|
else if (outputType == FileType::ConLog)
|
|
|
|
{
|
|
|
|
writer = IDemoWriter::CreateConLogWriter(outputFp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
}
|
2015-05-13 02:39:56 +02:00
|
|
|
|
|
|
|
if (inputType == FileType::Dem)
|
|
|
|
{
|
|
|
|
DemoReader::ProcessDem(inputFp, writer);
|
|
|
|
}
|
2015-05-15 05:38:53 +02:00
|
|
|
else if (inputType == FileType::Json)
|
2015-05-13 02:39:56 +02:00
|
|
|
{
|
|
|
|
DemoReader::ProcessJson(inputFp, writer);
|
|
|
|
}
|
2015-05-15 05:38:53 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
}
|
2015-05-13 02:39:56 +02:00
|
|
|
fclose(inputFp);
|
|
|
|
fclose(outputFp);
|
|
|
|
|
|
|
|
IDemoWriter::FreeDemoWriter(writer);
|
2014-10-29 05:20:24 +01:00
|
|
|
return 0;
|
|
|
|
}
|