Skip to content

Commit d2b97ca

Browse files
committed
Read files from stdin
Avoids argument length limitations with cmd line arguments
1 parent dca7c18 commit d2b97ca

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

include/openPMD/cli/convert-toml-json.hpp

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#pragma once
22

3+
#include <cstdint>
34
#include <cstdlib>
45
#include <cstring>
6+
#include <iterator>
57
#include <openPMD/auxiliary/JSON_internal.hpp>
68

79
#include <iostream>
@@ -91,8 +93,22 @@ class convert_json_toml
9193
}
9294
}
9395

94-
static auto merge(char const **begin, char const **end)
95-
-> openPMD::json::ParsedConfig
96+
struct ByLine : std::string
97+
{
98+
friend auto operator>>(std::istream &i, ByLine &l) -> std::istream &
99+
{
100+
decltype(auto) res = std::getline(i, l);
101+
if (res)
102+
{
103+
l.insert(0, 1, '@');
104+
}
105+
return res;
106+
}
107+
};
108+
using ByLineIterator = std::istream_iterator<ByLine>;
109+
110+
template <typename It>
111+
static auto merge(It begin, It end) -> openPMD::json::ParsedConfig
96112
{
97113
namespace json = openPMD::json;
98114
if (begin == end)
@@ -116,21 +132,40 @@ class convert_json_toml
116132
}
117133

118134
public:
135+
enum class UseStdinAs : std::uint8_t
136+
{
137+
InlineJson,
138+
ListOfJson
139+
};
140+
119141
static void run_application(
120-
int argc, char const **argv, void (*print_help_message)(char const *))
142+
int argc,
143+
char const **argv,
144+
UseStdinAs stdinconfig,
145+
void (*print_help_message)(char const *))
121146
{
122147
std::string jsonOrToml;
123148
switch (argc)
124149
{
125150
case 0:
126151
case 1:
127-
// Just read the whole stream into memory
128-
// Not very elegant, but we'll hold the entire JSON/TOML dataset
129-
// in memory at some point anyway, so it doesn't really matter
152+
switch (stdinconfig)
130153
{
154+
case UseStdinAs::InlineJson: {
155+
// Just read the whole stream into memory
156+
// Not very elegant, but we'll hold the entire JSON/TOML dataset
157+
// in memory at some point anyway, so it doesn't really matter
131158
std::stringbuf readEverything;
132159
std::cin >> &readEverything;
133160
jsonOrToml = readEverything.str();
161+
break;
162+
}
163+
case UseStdinAs::ListOfJson: {
164+
auto parsed_config =
165+
merge(ByLineIterator(std::cin), ByLineIterator{});
166+
with_parsed_cmdline_args(std::move(parsed_config));
167+
break;
168+
}
134169
}
135170
break;
136171
default:

src/IO/JSON/JSONIOHandlerImpl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2615,18 +2615,18 @@ else
26152615
fi
26162616
echo "Will merge files to '$serial_dir'." >&2
26172617
if [[ -e "$serial_dir" ]]; then
2618-
echo "Target dir already exists, aborting." >&2
2618+
echo "Target file already exists, aborting." >&2
26192619
exit 1
26202620
fi
2621-
if ! which openpmd-merge-json 2>/dev/null; then
2621+
if ! which openpmd-merge-json >/dev/null 2>&1; then
26222622
echo "Did not find 'openpmd-merge-json' on PATH, aborting." >&2
26232623
exit 1
26242624
fi
26252625
for file in "$parallel_dir"/mpi_rank_*.json; do
2626-
echo "@$file"
2626+
echo "$file"
26272627
done |
2628-
xargs openpmd-merge-json >"$serial_dir"
2629-
# TODO: xargs will only work up to a certain number of files)END";
2628+
openpmd-merge-json >"$serial_dir"
2629+
)END";
26302630
std::string const merge_script_path = dirpath + "/merge.sh";
26312631
std::fstream merge_file;
26322632
merge_file.open(

src/cli/convert-toml-json.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ equivalently from TOML to JSON.
1717

1818
int main(int argc, char const **argv)
1919
{
20-
convert_json_toml<from_format_to_format::switch_>::run_application(
21-
argc, argv, print_help_message);
20+
using convert = convert_json_toml<from_format_to_format::switch_>;
21+
convert::run_application(
22+
argc, argv, convert::UseStdinAs::InlineJson, print_help_message);
2223
}

src/cli/merge-json.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ determined by the type of the first file.
1717

1818
int main(int argc, char const **argv)
1919
{
20-
convert_json_toml<from_format_to_format::ID>::run_application(
21-
argc, argv, print_help_message);
20+
using convert = convert_json_toml<from_format_to_format::ID>;
21+
convert::run_application(
22+
argc, argv, convert::UseStdinAs::ListOfJson, print_help_message);
2223
}

0 commit comments

Comments
 (0)