Skip to main content
deleted 19 characters in body; edited title; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Any Suggestions for my C++? Converting Bash commands into Windows cmd commands

I have the following C++code that I wrote today, it converts a couple of bash commands into windows cmdWindows cmd commands, as well as adding some commands too. This is only my second day programming in C++, so any and all suggestions would be helpful.

Any Suggestions for my C++?

I have the following C++ that I wrote today, it converts a couple of bash commands into windows cmd commands, as well as adding some commands too. This is only my second day programming in C++, so any and all suggestions would be helpful.

Converting Bash commands into Windows cmd commands

I have the following code that converts a couple of bash commands into Windows cmd commands, as well as adding some commands. This is only my second day programming in C++, so any and all suggestions would be helpful.

Tweeted twitter.com/#!/StackCodeReview/status/220375253891825664
Source Link
Joe Loo
  • 241
  • 1
  • 5

Any Suggestions for my C++?

I have the following C++ that I wrote today, it converts a couple of bash commands into windows cmd commands, as well as adding some commands too. This is only my second day programming in C++, so any and all suggestions would be helpful.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <regex>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>

using namespace std;
using namespace std::tr1;

class Parser
{
    regex ls_regex, mkdir_regex, sleep_regex, rand_regex, rm_regex, touch_regex, cat_regex, open_regex, cp_regex, lat_regex;
    string touch_match, sleep_match, mkdir_match, ls_match, rm_match, open_match, cp_match;
   public:
    void SetRegexes();
    void RunCommand(string);
    string ParseCommand(string);
    string SearchString(int, string);
};

void Parser::SetRegexes()
{
    // Regex is the bash command, match is the cmd command
    ls_regex = "^ls$";
    ls_match = "dir";
    mkdir_regex = "^mkdir";
    mkdir_match = "md";
    sleep_regex = "^sleep$";
    sleep_match = "pause";
    rand_regex = "^timerand$";
    rm_regex = "^rm";
    rm_match = "del";
    touch_regex = "^touch";
    cat_regex = "^cat";
    open_regex = "^open";
    open_match = "notepad";
    cp_regex = "^cp";
    cp_match = "copy";
    lat_regex = "^lat";
}

string Parser::ParseCommand(string unparsed)
{
    // Checks if the regex can be found in the unparsed string
    if (regex_search(unparsed, ls_regex))
    {
        // Unparsed now is actually parsed
        unparsed = unparsed.replace(0, 3, ls_match);
    }
    else if (regex_search(unparsed, mkdir_regex))
    {
        unparsed = unparsed.replace(0, 5, mkdir_match);
    }
    else if (regex_search(unparsed, sleep_regex))
    {
        unparsed = unparsed.replace(0, 4, sleep_match);
    }
    else if (regex_search(unparsed, rand_regex))
    {
        srand(time(NULL));
        cout << rand() << endl;
        return " ";
    }
    else if (regex_search(unparsed, rm_regex))
    {
        unparsed = unparsed.replace(0, 2, rm_match);
    }
    else if (regex_search(unparsed, touch_regex))
    {
        string tfile;
        for (int i = 0; i < unparsed.length(); i++)
        {
            if (i > 5)
            {
                tfile += unparsed[i];
            }
        }
        ofstream create_file(tfile);
        return " ";
    }
    else if (regex_search(unparsed, cat_regex))
    {
        string op_line;
        string catfile;
        for (int i = 0; i < unparsed.length(); i++)
        {
            if (i > 3)
            {
                catfile += unparsed[i];
            }
        }
        ifstream input_file(catfile);
        while (input_file.good())
        {
            getline(input_file, op_line);
            cout << op_line;
        }
        cout << endl;
        return " ";
    }
    else if (regex_search(unparsed, open_regex))
    {
        unparsed = unparsed.replace(0, 5, open_match + " ");
    }
    else if (regex_search(unparsed, cp_regex))
    {
        unparsed = unparsed.replace(0, 4, cp_match + " ");
    }
    else if (regex_search(unparsed, lat_regex))
    {
        string latfile;
        string op_line;
        char holder;
        for (int i = 0; i < unparsed.length(); i++)
        {
            if (i > 3)
            {
                latfile += unparsed[i];
            }
        }
        cout << latfile << endl << endl;
        ifstream input_file(latfile);
        while (input_file.good())
        {
            getline(input_file, op_line);
            cout << op_line << endl;
            // getch() makes it so the letters are not echoed to the keyoard
            holder = getch();
        }
        return " ";
    }
    return unparsed;
}

void Parser::RunCommand(string parsed)
{
    // Converts to a different type so it can be used in the system method
    const char *cparsed = parsed.c_str();
    system(cparsed);
}

void _tmain(int argc, _TCHAR* argv[])
{
    string name;
    int line_counter = 0;
    Parser main_parser;
    main_parser.SetRegexes();
    string command;
    // Provides a never-ending command line interface
    while (true)
    {
        line_counter++;
        cout << "<" << line_counter << name << "-> "; 
        //cin >> command;
        getline(cin, command);
        command = main_parser.ParseCommand(command);
        main_parser.RunCommand(command);
    }
}