C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 1st, 2009, 07:51 PM
larssss larssss is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 30 larssss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 54 m 18 sec
Reputation Power: 1
Talking General - The MadCmd.

This is an interprenter i've been working on for a long time...
Well il hand you the source file... but some functions might not work because i will not give you all the compiled files... only the interprenter source... so ... remember to include the lib libwsock32.lib i think it's named.

Source:
Code:
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <conio.h>
#include <windows.h>
#include <winsock.h>
#include <fstream>
#include <direct.h>


using namespace std;

vector<string> R;
vector<string> R2;
string thisstring;
string userpass;
bool shutdown1 = false;
bool match = false;
bool return_true = false;
double a;
string b;
double c;
double sum;
string operator_calc;
vector<string> character;
bool valid = false;
string userpass2;
string decuser;
string decpass;
HKEY hkey;
DWORD buffersize = 1024;
char* lpdata = new char[buffersize+30];

string XOR(string value,string key);

void StringExplode(string str, string separator, vector<string>* results);
void regRead(string hkeyS, string location, string search);
void validate(string username, string password, bool valid1, vector<string> R);



int main(){
    _chdir(".\projects\MadCmd");
    cout << "Please enter a username and password, user  password." << endl;
    getline(cin, userpass);
    StringExplode(userpass, " ", &R);
    if(R.size() > 1){
                validate(R[0], R[1], false, R);
    }
    if(R[0] == "root" && R[1] == "root"){
            valid = true;
    }
    if(valid != false){
             cout << "Welcome " << decuser << endl;
             R.clear();
             thisstring.clear();
    while(shutdown1 != true){
                    // get input
                    if(!getline(cin, thisstring)){
                                     cout << "error!" << endl;
                    }
                    
                    // Divide the string into an array of strings
                    StringExplode(thisstring, " ", &R);
                    // look through the command list
                    if(R[0] == "exit" || R[0] == "bye"){
                             return 0;
                             match = true;
                     }
                     if(R[0] == "calc"){
                             if(!R.empty()){
                                     if(R.size() == 4 || R.size() == 2){
                                                 if(R.size() == 2){
                                                             R.clear();
                                                             R2.clear();
                                                             StringExplode(thisstring, " ", &R);
                                                             StringExplode(R[1], "+", &R2);
                                                             operator_calc = "+";
                                                             if(R2.size() <= 1){
                                                                     R.clear();
                                                                     R2.clear();
                                                                     StringExplode(thisstring, " ", &R);
                                                                     StringExplode(R[1], "*", &R2);
                                                                     operator_calc = "*";
                                                                     if(R2.size() <= 1){
                                                                                      R2.clear();
                                                                                      R.clear();
                                                                                      StringExplode(thisstring, " ", &R);
                                                                                      StringExplode(R[1], "/", &R2);
                                                                                      operator_calc = "/";
                                                                                      if(R2.size() <= 1){
                                                                                                   R2.clear();
                                                                                                   R.clear();
                                                                                                   cout << "There is an error with the calc syntax";
                                                                                                   StringExplode("", " ", &R);
                                                                                                   StringExplode("", " ", &R2);
                                                                                                   match = true;
                                                                                      }
                                                                     }
                                                             }                                                            
                                                 }          
                                               if(R.size() == 4){
                                                           a = atof(R[1].c_str());
                                               } else {
                                                      a = atof(R2[0].c_str());
                                               }
                                               if(R.size() == 4){
                                                           b = R[2];
                                               } else {
                                                      b = operator_calc;
                                               }
                                               if(R.size() == 4){
                                                           c = atof(R[3].c_str());
                                               } else {
                                                      c = atof(R2[1].c_str());
                                               }
                                               if(b == "+"){
                                                    sum = (a+c);
                                                    cout << fixed << sum;
                                               }
                                               if(b == "*"){
                                                    sum = (a*c);
                                                    cout << fixed << sum;
                                               }
                                               if(b == "/"){
                                                    sum = (a/c);
                                                    cout << fixed << sum;
                                               }
                                               if(b == "square"){
                                                    sum = sqrt(a);
                                                    cout << fixed << sum;
                                               }
                                               match = true;
                                     } else {
                                            cout << "Error to many parameters or to few :P";
                                            match = true;
                                            }
                             } else {
                                    cout << "Enter something in the line";
                             }
                     }
                     if(R[0] == "help" || R[0] == "?"){
                             cout << endl << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|Commands:     |      Explanation                             |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|calc          |      Simple calculator. eksample: calc 1+60  |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|exit          |      Exit the program                        |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|bye           |      Exit the program                        |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|do            |      Make a loop. (works only with print)    |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|cls           |      Clear the screen.                       |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|run<          |      Run a program in the same folder        |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|dir           |      Show the current directory              |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|logout        |      Logout from the current user            |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|create        |      for now only user to create.            |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|delete        |      delete the file only in dir and over    |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|delete user   |      delete a user profile from the system   |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|connect ip    |      connect to a specified ip adress        |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << "|disconnect    |      will only work when connacted           |" << endl;
                             cout << "+--------------+----------------------------------------------+" << endl;
                             cout << endl;
                             
                             match = true;
                     }
                     if(R[0] == "do" || R[0] == "DO"){
                             if(R[1].length() >= 1){
                                            R.clear();
                                            R2.clear();
                                            StringExplode(thisstring, "'", &R2);
                                            if(R2[0] == "do print(" || R2[0] == "DO PRINT("){
                                                     StringExplode(R2[2], " ", &R);
                                                     if(R[1] == "loop"){
                                                             int i = 0;
                                                             int i2 = atoi(R[2].c_str());
                                                             while(i <= i2){
                                                                     cout << R2[1] << endl;
                                                                     i++;
                                                             }
                                                     } else {
                                                            cout << R2[1] << endl;
                                                     }
                                            }
                             }
                             match = true;
                     }

                     if(R[0] == "cls" || R[0] == "CLS"){
                             system("CLS");
                             match = true;
                     }
                     if(R[0] == "run<" || R[0] == "RUN<"){
                             StringExplode(thisstring, "<", &R2);
                             cout << R2[1] << endl;
                             system("PAUSE");
                             string str1 = "START ";
                             string str2 = R2[1];
                             string str3 = " /MAX notepad.exe";
                             string action = str1 + str2 + str3;
                             system(action.c_str());
                             cout << R2[1].c_str() << endl;
                             system("PAUSE");
                             match = true;
                             R2.clear();
                     }
                     if(R[0] == "dir" || R[0] == "DIR"){
                             system("dir");
                             match = true;
                     }
                     if(R[0] == "create" || R[0] == "CREATE"){
                             if(R[1] == "user" || R[1] == "USER"){
                                     ofstream userfile("./lib/users.dat", ios::out | ios::app | ios::binary);
                                     string username = XOR(R[2], R[3]);
                                     string password = XOR(R[3], R[3]);
                                     userfile << username << " " << password << "\n";
                                     userfile.close();
                                     cout << "The user " << R[2] << " has been created." << endl;
                                     match = true;
                             }
                     }
                     if(R[0] == "logout" || R[0] == "LOGOUT"){
                             R.clear();
                             R2.clear();
                             thisstring.clear();
                             userpass.clear();
                             shutdown1 = false;
                             match = false;
                             return_true = false;
                             operator_calc.clear();
                             character.clear();
                             valid = false;
                             userpass2.clear();
                             decuser.clear();
                             decpass.clear();
                             main();
                     }
                     
                     if(R[0] == "delete" || R[0] == "DELETE"){
                             if(R.size() >= 2){
                                         if(R[1] == "user"){
                                                 ifstream userfile_r;
                                                 userfile_r.open("./lib/users.dat");
                                                 ofstream userfile_w;
                                                 userfile_w.open("./lib/users.dat.temp", ios::app | ios::out);
                                                 string copy;
                                                 bool found = false;
                                                 int i = 0;
                                                 while(found != true){
                                                             getline(userfile_r, copy);
                                                             if(copy.length() == 0){
                                                                            found = true;
                                                             }
                                                             R2.clear();
                                                             StringExplode(copy, " ", &R2);
                                                             cout << R[2] << " " << XOR(R2[0], R[3]) << endl;
                                                             if(R[2] == XOR(R2[0], R[3])){
                                                                        cout <<  "The user has been deleted";
                                                             } 
                                                             else {
                                                                  if(found == false){
                                                                           userfile_w << copy << endl;
                                                                           
                                                                           cout << "Segment " << i << "copyed" << endl;
                                                                  }
                                                             }
                                                             i++;
                                                             copy.clear();
                                                 }
                                                 userfile_r.close();
                                                 userfile_w.close();
                                                 remove("./lib/users.dat");
                                                 rename("./lib/users.dat.temp", "./lib/users.dat");
                                                 match = true;
                                                 R.clear();
                                                 R2.clear();
                                                 copy.clear();
                                         }else {
                                               string deletes = "del ";
                                               string wtdelete = R[1];
                                               string dsum = deletes + wtdelete;
                                               system(dsum.c_str());
                                               match = true;
                                               cout << "The file " << R[1] << " was deleted." << endl;
                                         }
                             }
                     }
                     if(R[0] == "view" || R[0] == "VIEW"){
                             ifstream userfile_r;
                             userfile_r.open("./lib/users.dat");
                             string output;
                             bool reading = true;
                             while(reading == true){
                                           getline(userfile_r, output);
                                           StringExplode(output, " ", &R2);
                                           if(reading != false){
                                                      cout << XOR(R2[0], R[1]) << endl;
                                           }
                                           if(output.length() == 0){
                                                              reading = false;
                                           }
                                           output.clear();
                                           R2.clear();
                             }
                             userfile_r.close();
                             match = true;
                     }
                     if(R[0] == "copy" || R[0] == "COPY"){
                             fstream file_r(R[1].c_str(), ios::in | ios::binary);
                             fstream file_w(R[2].c_str(), ios::out | ios::binary);
                             double begin = file_r.tellg();
                             file_r.seekg (0, ios::end);
                             double end = file_r.tellg();
                             file_r.seekg(0, ios::beg);
                             double size = end-begin;
                             double size_d;
                             char buffer;
                             double size_view;
                             char * ext; 
                             if(size == 0){
                                     cout << "File not found: " << R[1];
                             }
                             if(size < 1024){
                                     size_view = size;
                                     ext = "Bytes";
                             }
                             if(size > 1024){
                                     size_view = size/1024;
                                     ext = "KB";
                             }
                             if(size/1024 > 1024){
                                          size_view = size/1024/1024;
                                          ext = "MB";
                             }
                             if(size/1024/1024 > 1024){
                                               size_view = size/1024/1024/1024;
                                               ext = "GB";
                             }
                                          
                             cout << "Size of the file is: " << size_view << ext << endl;
                             int i = 0;
                             double percent_done;
                             while( file_r.read((char*)&buffer,sizeof(buffer))){
                                    file_w << buffer;
                                    size_d = size_d + sizeof(buffer);
                                    if(i == 10000000){
                                         percent_done = size_d / size * 100;
                                         cout << percent_done << fixed << " % done       ";
                                         int size_d2 = size_d;
                                         int size2 = size;
                                         if(ext == "MB"){
                                                size_d2 = size_d/1024/1024;
                                                size2 = size/1024/1024;
                                         }
                                         if(ext == "KB"){
                                                size_d2 = size_d/1024;
                                                size2 = size/1024;
                                         }
                                         if(ext == "GB"){
                                                size_d2 = size_d/1024/1024/1024;
                                                size2 = size/1024/1024/1024;
                                         }
                                         if(ext == "Bytes"){
                                                size_d2 = size_d;
                                                size2 = size;
                                         }
                                         cout << size_d2 << "/" << size2 << ext << " copyed." << endl;
                                         i = 0;
                                    }
                                    i++;
                             }
                             percent_done = 0;
                             size_d = 0;
                             file_r.close();
                             file_w.close();
                             match = true;
                             cout << "The file was successfully copyed from: " << R[1] << endl;
                             cout << "The file was successfully copyed to: " << R[2] << endl;
                             free(&buffer);
                     }
                     if(R[0] == "readreg" || R[0] == "READREG"){
                             if(R.size() == 4){
                                       regRead(R[1], R[2], R[3]);
                                       cout << "Registery key is open: Memory location = " << hkey << endl;
                                       cout << "The value of the key is: " << lpdata << endl << endl;
                                       match = true;
                                       ZeroMemory(&lpdata, sizeof(lpdata));
                             }
                     }
                     if(R[0] == "cd" || R[0] == "CD"){
                             string call = "cd " + R[1];
                             cout << call << endl;
                             _chdir(R[1].c_str());
                             match = true;
                     }
                     
                     // check if the command was listed
                     if(match == false){
                              int i = 0;
                              cout << "The command: ";
                              while(i <= R.size()-1){
                                      cout << R[i] << " ";
                                      i++;
                              }
                              cout << "was not found.";
                              cout << endl;
                     }
	                 cout << endl;
	                 // clear the array
	                 R.clear();
	                 match = false;
	                 return_true = false;
    }               
} else {
       cout << "Username or password is incorrect!" << endl;
       R.clear();
       R2.clear();
       thisstring.clear();
       userpass.clear();
       shutdown1 = false;
       match = false;
       return_true = false;
       operator_calc.clear();
       character.clear();
       valid = false;
       userpass2.clear();
       decuser.clear();
       decpass.clear();
       main();
}
}

// functions 
string XOR(string value,string key)
{
    string retval(value);

    short unsigned int klen=key.length();
    short unsigned int vlen=value.length();
    short unsigned int k=0;
    short unsigned int v=0;
    
    for(v;v<vlen;v++)
    {
        retval[v]=value[v]^key[k];
        k=(++k<klen?k:0);
    }
    
    return retval;
}
void StringExplode(string str, string separator, vector<string>* results){
    int found;
    found = str.find_first_of(separator);
    while(found != string::npos){
        if(found > 0){
            results->push_back(str.substr(0,found));
        }
        str = str.substr(found+1);
        found = str.find_first_of(separator);
    }
    if(str.length() > 0){
        results->push_back(str);
    }
    if(str == ""){
           results->push_back("Nothing here");
           results->push_back("Nothing here");
           results->push_back("Nothing here");
           results->push_back("Nothing here");
           results->push_back("Nothing here");
           results->push_back("Nothing here");
           results->push_back("Nothing here");
           results->push_back("Nothing here");
           results->push_back("Nothing here");
           results->push_back("Nothing here");
           match = true;
    }
    if(!found > 0){
              results->clear();
    }
}

void validate(string username, string password, bool valid1, vector<string> R){
     ifstream myfile;
     myfile.open ("./lib/users.dat");
     int i = 0;
     while(valid1 != true){
                  R.clear();
                 getline(myfile, userpass2);
                 StringExplode(userpass2, " ", &R);
                 decuser = XOR(R[0], password);
                 decpass = XOR(R[1], password);
             if(username == decuser && password == decpass){
                         cout << "Username accepted at line: " << i << endl;
                         valid = true;
                         valid1 = true;
             }
             else {    
                                cout << "Not found at id " << i << " nextline." << endl;
                                valid = false;
                                valid1 = false;
             }
             if(userpass2.length() == 0){
                   valid = false;
                   valid1 = true;
             }
             i++;
     }
     myfile.close();
}

void regRead(string hkeyS, string location, string search){
     if(hkeyS == "HKEY_CLASSES_ROOT"){
              RegOpenKeyEx(HKEY_LOCAL_MACHINE,
              location.c_str(), NULL, KEY_READ,&hkey);
     }
     if(hkeyS == "HKEY_CURRENT_USER"){
              RegOpenKeyEx(HKEY_CURRENT_USER,
              location.c_str(), NULL, KEY_READ,&hkey);
     }
     if(hkeyS == "HKEY_LOCAL_MACHINE"){
              RegOpenKeyEx(HKEY_LOCAL_MACHINE,
              location.c_str(), NULL, KEY_READ,&hkey);
     }
     if(hkeyS == "HKEY_USERS"){
              RegOpenKeyEx(HKEY_USERS,
              location.c_str(), NULL, KEY_READ,&hkey);
     }
     if(hkeyS == "HKEY_CURRENT_CONFIG"){
              RegOpenKeyEx(HKEY_CURRENT_CONFIG,
              location.c_str(), NULL, KEY_READ,&hkey);
     }
     RegQueryValueEx(hkey, search.c_str(), NULL, NULL, (LPBYTE)lpdata, &buffersize);
     RegCloseKey(hkey);
     hkeyS.clear();
     location.clear();
     search.clear();
}



ROCK ON!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - The MadCmd.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek