Skip to content

Commit

Permalink
Better output file handling
Browse files Browse the repository at this point in the history
When -o is not specified in json2xom, derive the name of the output file
from the name of the input file by doing the following:

- Trim the filename at the last occurrence of ".json", if any.
- If the remaining filename already exists in the working directory,
append ".out"

I.e. save100.json becomes either 'save100' or 'save100.out' depending on
whether there is already a 'save100' in the working directory.
  • Loading branch information
tracktwo committed Jan 7, 2018
1 parent b6fdd30 commit 1c58e7c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 6 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ set (xcomsave_headers xcomio.h xcom.h util.h)

# Linux-specific configuration
if (UNIX)
set (CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++14)
set (CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++17)
endif (UNIX)

if (WIN32)
set (CMAKE_CXX_FLAGS "-std:c++17 -EHsc")
add_definitions(-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE)
endif (WIN32)

# External dependencies - json, lzo, zlib.
# They're all fairly small and simple enough to statically link into
# the project rather than adding them as cmake subdirectories. Zlib in
Expand All @@ -19,12 +24,6 @@ include_directories("minilzo-2.09" "json11" "zlib")
add_definitions("-DSTDC -DNO_GZIP")
file(GLOB zlib_sources zlib/*.c zlib/*.h)

if (WIN32)
add_definitions(-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE)
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -EHsc")
set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -EHsc")
endif (WIN32)

add_library (xcomsave ${xcomsave_sources} ${zlib_sources} ${xcomsave_headers})
set_target_properties(xcomsave PROPERTIES LINKER_LANGUAGE CXX)

Expand Down
8 changes: 7 additions & 1 deletion json2xcom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
#include <cassert>
#include <cstring>
#include <string>
#include <filesystem>

using namespace json11;
using namespace xcom;

namespace fs = std::experimental::filesystem;

struct property_dispatch
{
std::string name;
Expand Down Expand Up @@ -727,9 +730,12 @@ int main(int argc, char *argv[])
}

if (outfile.empty()) {
size_t pos = infile.find_last_of(".json");
size_t pos = infile.rfind(".json");
if (pos != std::string::npos) {
outfile = infile.substr(0, pos);
if (fs::exists(outfile)) {
outfile += ".out";
}
}
else {
outfile = infile + ".out";
Expand Down

0 comments on commit 1c58e7c

Please sign in to comment.