I have source code of a command line C game
, that waits for user's stdin
, and outputs stdout
. I want to give input from a text field through to the main
of this C
code and take the output in a text area field for example.
int mainer(int argc, char **argv) {
int i;
int hitme;
char ch;
prelim();
if (argc > 1) { // look for -f option
if (strcmp(argv[1], "-f")== 0) {
coordfixed = 1;
argc--;
argv++;
}
}
if (argc > 1) {
fromcommandline = 1;
line[0] = '\0';
while (--argc > 0) {
strcat(line, *(++argv));
strcat(line, " ");
}
}
else fromcommandline = 0;
// some other code
}
From the example here, I should do the following:
let args = [" ", "regular", "short", ""]
var cargs = args.map { strdup($0) }
let result = mainer(Int32(args.count), &cargs)
for ptr in cargs { free(ptr) }
How could I call the main function
, and keep it alive and keep giving to it arguments to make it act as command line
.
A command-line tool can be very useful for automating common tasks to boost developer productivity. While developing iOS applications we often make use of command-line tools like Fastlane and CocoaPods but it's less common to write your own command-line tools for daily use.
Open your scheme (⌘<) and select the Run > Arguments tab. Add the arguments you want to pass on launch one at a time. Double-click to edit any argument: The arguments are vended by CommandLine.
I would set up your app with a TextArea and a TextField, and provide alternatives to printf and scanf that either add text to the text area or get sent text from the textfield (maybe with an "Enter" UIButton). Then you can compile the C code and call it from Swift. Objective C is actually C way under the covers, so you should be able to to get this to run with just a little alteration.
In order to get the code to run you'll probably have to make this code run in it's own thread, and jump through some hoops to get those calls to behave correctly, but it should be doable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With