Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
superg committed Nov 6, 2020
1 parent 300592c commit dc74e69
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 0 deletions.
54 changes: 54 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
cmake_minimum_required(VERSION 3.14)

project("srmtools")

# build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the build type" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "RelWithDebInfo" "MinSizeRel")
endif()
# hide other incomplete configurations from Visual Studio
set(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE INTERNAL "Active configuration" FORCE)
mark_as_advanced(FORCE CMAKE_CONFIGURATION_TYPES)

# installation
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/INSTALL" CACHE PATH "Installation prefix" FORCE)
endif()
set(CMAKE_INSTALL_MESSAGE LAZY)

set(CMAKE_CXX_STANDARD 17)

# Visual Studio settings
if(MSVC)
# build MT configuration by default
string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")

# enable multithreaded build
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")

# disable annoying warnings
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# packaging
set(CPACK_GENERATOR "ZIP")
set(CPACK_PACKAGE_FILE_NAME "srm-tools-0.9")
include(CPack)

macro(SetTargetCategory target category)
file(GLOB_RECURSE files LIST_DIRECTORIES false RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *)
foreach(i IN LISTS files)
get_filename_component(source_path "${i}" PATH)
string(REPLACE "/" "\\" source_path "${source_path}")
source_group("${source_path}" FILES ${i})
endforeach()

set_property(TARGET ${target} PROPERTY FOLDER ${category})
endmacro()

add_subdirectory("src")
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
add_executable(srm2sav srm2sav.cxx)
add_executable(sav2srm sav2srm.cxx)

install(TARGETS srm2sav sav2srm DESTINATION "bin")

SetTargetCategory(srm2sav "")
SetTargetCategory(sav2srm "")
73 changes: 73 additions & 0 deletions src/sav2srm.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <vector>



using namespace std;



void sav2srm(const filesystem::path &in_fn, const filesystem::path &out_fn)
{
ifstream ifs(in_fn, ifstream::binary);
if(!ifs.is_open())
throw runtime_error("unable to open input file");

auto data_size(filesystem::file_size(in_fn));

std::vector<uint8_t> data(data_size);
ifs.read((char *)data.data(), data_size);

ofstream ofs(out_fn, ofstream::binary);
if(!ofs.is_open())
throw runtime_error("unable to create output file");

for(auto const &e : data)
{
uint16_t d = e << 8 | 0xff;
ofs.write((char *)&d, sizeof(d));
}
}



int main(int argc, char *argv[])
{
int exit_code(0);

try
{
if(argc == 2)
{
filesystem::path in_fn(argv[1]);
filesystem::path out_fn(argv[1]);
out_fn.replace_extension(".srm");

if(in_fn == out_fn)
throw runtime_error("overwriting input file");

sav2srm(in_fn, out_fn);
}
else
{
cout << "usage: sav2srm <SAV-file>" << endl;
exit_code = 1;
}
}
catch(const exception &e)
{
cerr << "error: " << e.what() << endl;
exit_code = 2;
}
catch(...)
{
cerr << "error: unhandled exception" << endl;
exit_code = 3;
}

return exit_code;
}
75 changes: 75 additions & 0 deletions src/srm2sav.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <vector>



using namespace std;



void srm2sav(const filesystem::path &in_fn, const filesystem::path &out_fn)
{
ifstream ifs(in_fn, ifstream::binary);
if(!ifs.is_open())
throw runtime_error("unable to open input file");

auto data_size(filesystem::file_size(in_fn));
if(data_size & 1)
throw runtime_error("file is not SRM file");

std::vector<uint16_t> data(data_size / sizeof(uint16_t));
ifs.read((char *)data.data(), data_size);

ofstream ofs(out_fn, ofstream::binary);
if(!ofs.is_open())
throw runtime_error("unable to create output file");

for(auto const &e : data)
{
uint8_t d = e >> 8 & 0xff;
ofs.write((char *)&d, sizeof(d));
}
}



int main(int argc, char *argv[])
{
int exit_code(0);

try
{
if(argc == 2)
{
filesystem::path in_fn(argv[1]);
filesystem::path out_fn(argv[1]);
out_fn.replace_extension(".sav");

if(in_fn == out_fn)
throw runtime_error("overwriting input file");

srm2sav(in_fn, out_fn);
}
else
{
cout << "usage: srm2sav <SRM-file>" << endl;
exit_code = 1;
}
}
catch(const exception &e)
{
cerr << "error: " << e.what() << endl;
exit_code = 2;
}
catch(...)
{
cerr << "error: unhandled exception" << endl;
exit_code = 3;
}

return exit_code;
}

0 comments on commit dc74e69

Please sign in to comment.