C++ text strings from a text file
i'm trying to do a linux bash simulator for my school. i have all commands "hard-coded" into the app. so every file has his specific command. for example, if you want to see testprog, you must type into console cat testprog, if you want to see testprog2, you must type cat testprog2. i'm doing this via conditions. but i have two problems, my app have 2548 lines of source, and when i loading lots of text string hard-coded into app, it slowing down the app, everything is loading too slow. so i thinked about that i will write all text strings into the file with extension, for example, *.DEF, and then i will easy load it from that *.DEF file all text strings. my second problem is that i need to get rid of that conditions. is there any good solution for it? i've tryed googling, "c++ filesystem" etc. but i didnt found any useful tutorial. thanks in advance for help.
why not have each file and folder in a seperate .dat file.
i.e you have the file contents for the files, and you have a print out of hte directory listing for any folders.
that way you could do:
if COMMAND == "ls" then print file(COMMAND['location']).
that will then print out the directory contents for that location.
saves you hard coding every command combination in there too.
thats how i'd make it.
Cheese is right in his methodology… instead of checking for an exact set of commands and arguments, you need to check for the command and then, through conditionals, perform the actions on the arguments. That would more closely simulate a shell. For example:
You're doing this (pseudocode):
if command = "cat file2.txt" then output("file2.txt")
etc...```
You should be doing this (**pseudocode** again):
```markupif command.starts_with("cat") then
{
cmd = cmd.replace("cat", "").trim
output(cmd)
}```
That way, you can re-use the command as many times as needed without adding extra unnecessary lines to your program.
I would address the problem by making an Interpreter class which would break each text line received in two parts: the command and the argument.
For each command that I would have to implement I would make a separate class to handle that command. The Interpreter should get in its initialization function a list of each object created from a command class and build an internal list with pairs like <pointer_to_command_object> - <text_identifying the command>.
This approach would give you much flexibility and an easy to extend framework, but definitely it is not the fastest approach for just few commands.