Skip to content

Commit 018c7e5

Browse files
Generalize paths
1 parent 24dc4f6 commit 018c7e5

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

source/main.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,21 +277,39 @@ void scanDir(std::vector<u8>& outData, nlohmann::json& outSheet, const std::file
277277

278278
int main(int argc, char** argv)
279279
{
280-
if (argc != 2)
280+
if (argc < 2 || argc > 3)
281281
{
282-
printf("Usage: %s <EventsGallery directory>\n", argv[0]);
282+
printf("Usage: %s <EventsGallery directory> [personal data directory]\n", argv[0]);
283283
return 1;
284284
}
285285

286-
std::filesystem::create_directory("out");
287-
288-
std::filesystem::path gallery(argv[1]);
286+
// Resolve the gallery and output paths to absolute up front, since we may
287+
// change the working directory below.
288+
std::filesystem::path gallery = std::filesystem::absolute(argv[1]);
289289
if (!std::filesystem::exists(gallery))
290290
{
291291
printf("Gallery directory does not exist\n");
292292
return 2;
293293
}
294294

295+
const std::filesystem::path outDir = std::filesystem::current_path() / "out";
296+
297+
// The core library loads its personal (base stat) data files relative to the
298+
// current working directory. Allow the caller to point at the folder holding
299+
// them by switching into it; gallery/output paths stay absolute regardless.
300+
if (argc == 3)
301+
{
302+
std::filesystem::path personal = std::filesystem::absolute(argv[2]);
303+
if (!std::filesystem::exists(personal))
304+
{
305+
printf("Personal data directory does not exist\n");
306+
return 2;
307+
}
308+
std::filesystem::current_path(personal);
309+
}
310+
311+
std::filesystem::create_directory(outDir);
312+
295313
for (const int& gen : {4, 5, 6, 7})
296314
{
297315
std::filesystem::path dir = gallery / "Released" / ("Gen " + std::to_string(gen));
@@ -341,7 +359,8 @@ int main(int argc, char** argv)
341359
int error = BZ2_bzBuffToBuffCompress(
342360
(char*)compData, &compressedSize, (char*)data.data(), data.size(), 5, 0, 0);
343361

344-
std::string outPath = "out/data" + std::to_string(gen) + ".bin.bz2";
362+
std::string outPath =
363+
(outDir / ("data" + std::to_string(gen) + ".bin.bz2")).string();
345364
FILE* outFile = fopen(outPath.c_str(), "wb");
346365
auto written = fwrite(compData, 1, compressedSize, outFile);
347366
fclose(outFile);
@@ -361,7 +380,7 @@ int main(int argc, char** argv)
361380
BZ2_bzBuffToBuffCompress(
362381
(char*)compData, &compressedSize, (char*)jsonData.data(), jsonData.size(), 5, 0, 0);
363382

364-
outPath = "out/sheet" + std::to_string(gen) + ".json.bz2";
383+
outPath = (outDir / ("sheet" + std::to_string(gen) + ".json.bz2")).string();
365384
outFile = fopen(outPath.c_str(), "wb");
366385
fwrite(compData, 1, compressedSize, outFile);
367386
fclose(outFile);

0 commit comments

Comments
 (0)