2014-10-29 05:20:24 +01:00
|
|
|
|
2015-06-14 23:25:49 +02:00
|
|
|
#include "io/idemowriter.h"
|
|
|
|
#include "io/demoreader.h"
|
2015-06-19 20:15:23 +02:00
|
|
|
#include "json_checker/JSON_checker.h"
|
2015-05-13 02:39:56 +02:00
|
|
|
#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]);
|
2015-06-25 06:00:30 +02:00
|
|
|
if (inputFile == outputFile)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: Input and output file cannot be the same!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-13 02:39:56 +02:00
|
|
|
|
|
|
|
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-06-18 22:11:49 +02:00
|
|
|
IDemoWriter::FreeDemoWriter(writer);
|
|
|
|
|
2015-05-13 02:39:56 +02:00
|
|
|
fclose(inputFp);
|
|
|
|
fclose(outputFp);
|
2015-06-19 20:15:23 +02:00
|
|
|
|
2015-07-06 06:12:04 +02:00
|
|
|
/*if (outputType == FileType::Json)
|
2015-06-19 20:15:23 +02:00
|
|
|
{
|
|
|
|
FILE* outputFp = fopen(outputFile.c_str(), "rb");
|
|
|
|
JSON_checker jc = new_JSON_checker(20);
|
|
|
|
int next_char = 0;
|
|
|
|
while ((next_char = fgetc(outputFp)) > 0)
|
|
|
|
{
|
|
|
|
if (!JSON_checker_char(jc, next_char))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "JSON_checker_char: syntax error\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!JSON_checker_done(jc))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "JSON_checker_end: syntax error\n");
|
|
|
|
}
|
|
|
|
fclose(outputFp);
|
2015-07-06 06:12:04 +02:00
|
|
|
}*/
|
2014-10-29 05:20:24 +01:00
|
|
|
return 0;
|
|
|
|
}
|