Skip to content

Commit ba90e4d

Browse files
add invert and passthrough option
1 parent 58c8a09 commit ba90e4d

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ As an alternative to the ```git submodule``` commands, the ```--recursive``` opt
1818
```
1919
write_shm [OPTION...]
2020
21-
-n, --name arg shared memory name (mandatory)
22-
-r, --repeat repeat input if input size is smaller than shared memory
23-
-h, --help print usage
21+
-n, --name arg shared memory name (mandatory)
22+
-i, --invert invert all input bits
23+
-r, --repeat repeat input if input size is smaller than shared memory
24+
-p, --passthrough output everything that is written to the shared memory to stdout
25+
-h, --help print usage
2426
```
2527

2628
## Libraries

src/main.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ int main(int argc, char **argv) {
1616
};
1717

1818
options.add_options()("n,name", "shared memory name (mandatory)", cxxopts::value<std::string>());
19+
options.add_options()("i,invert", "invert all input bits", cxxopts::value<bool>()->default_value("false"));
1920
options.add_options()("r,repeat",
2021
"repeat input if input size is smaller than shared memory",
2122
cxxopts::value<bool>()->default_value("false"));
23+
options.add_options()("p,passthrough",
24+
"output everything that is written to the shared memory to stdout",
25+
cxxopts::value<bool>()->default_value("false"));
2226
options.add_options()("h,help", "print usage");
2327

2428
// parse arguments
@@ -31,6 +35,7 @@ int main(int argc, char **argv) {
3135
}
3236

3337
const auto REPEAT_INPUT = args["repeat"].as<bool>();
38+
const auto PASSTHROUGH = args["passthrough"].as<bool>();
3439

3540
// print usage
3641
if (args.count("help")) {
@@ -84,15 +89,23 @@ int main(int argc, char **argv) {
8489

8590
auto *shm_data = shm->get_addr<char *>();
8691

92+
const int INVERT_MASK = args["invert"].as<bool>() ? ~0 : 0;
93+
8794
// copy file to shm (and buffer)
8895
std::size_t remaining = SHM_SIZE;
8996
std::size_t pos = 0;
9097
while (remaining) {
91-
const auto C = std::cin.get();
92-
if (C == EOF) break;
98+
auto c = std::cin.get();
99+
if (c == EOF) break;
93100

94-
shm_data[pos] = static_cast<char>(C);
95-
if (REPEAT_INPUT) { in_buffer[pos] = static_cast<char>(C); }
101+
c ^= INVERT_MASK;
102+
103+
shm_data[pos] = static_cast<char>(c);
104+
if (PASSTHROUGH) {
105+
std::cout.put(static_cast<char>(c));
106+
std::cout.flush();
107+
}
108+
if (REPEAT_INPUT) { in_buffer[pos] = static_cast<char>(c); }
96109

97110
pos++;
98111
remaining--;
@@ -105,6 +118,10 @@ int main(int argc, char **argv) {
105118
const auto COPY_SIZE = std::min(BUF_SIZE, remaining);
106119

107120
memcpy(shm_data + pos, in_buffer.get(), COPY_SIZE);
121+
if (PASSTHROUGH) {
122+
std::cout.write(in_buffer.get(), static_cast<std::streamsize>(COPY_SIZE));
123+
std::cout.flush();
124+
}
108125

109126
pos += COPY_SIZE;
110127
remaining -= COPY_SIZE;

0 commit comments

Comments
 (0)