View Full Version: Command Line 2

C++ Game Dev > C++ Works in Progress > Command Line 2


Title: Command Line 2
Description: Compression, Decompression & cmd.exe-ish


TheDarkJay - September 29, 2006 06:43 PM (GMT)
user posted image

Okay, I got really bored and noticed that my school didn't give us access to the command line. Being me I couldn't resist putting cmd.exe on my iPOD. But then I decided to go a bit further, and make my own Custom Command-Line thingy.

So far aside from working almost like the standard Command line, I've added some zLib compression/decompression routines to it as well. So it's not really doing a lot.

CODE

#include <iostream>
#include <fstream>
#include <string>
#include <zlib.h>

#define EXIT_C2 1
#define C2_COMPRESS 2
#define C2_DECOMPRESS 3

bool decompress_one_file(const char *infilename, const char *outfilename);
bool compress_one_file(const char *infilename, const char *outfilename);

void Player_Control_run();

int main(int argc, char *argv[])
{
Player_Control_run();
}

inline void Run_Command(std::string command)
{
 std::string Str;
 std::string Str2;
 
int Command;
if ( command == "C2_EXIT" ) Command = EXIT_C2;
else if ( command == "C2_COMPRESS" ) Command = C2_COMPRESS;
else if ( command == "C2_DECOMPRESS" ) Command = C2_DECOMPRESS;

switch( Command )
{
 case  EXIT_C2:
 exit(0);
 break;
 
 case C2_COMPRESS:

  std::cout << "Enter the name of the File you want to compress: ";
  getline(std::cin, Str);
  std::cout << "Enter the destination/name of the Compressed File: ";
  getline(std::cin, Str2);
 
 if ( compress_one_file( Str.c_str(), Str2.c_str() ) )
 std::cout << "\n The File Has Been Decompressed Successfully"  << std::endl;
 else std::cout << "\n ERROR: The File was not successfully decompressed.";  
 break;

 case C2_DECOMPRESS:
     
 std::cout << "Enter the name of the File you want to decompress: ";
 getline(std::cin, Str);
 std::cout << "Enter the destination/name of the decompressed File: ";
 getline(std::cin, Str2);

 if ( decompress_one_file( Str.c_str() , Str2.c_str() ) )
 std::cout << "\n The File Has Been Decompressed Successfully"  << std::endl;
 else std::cout << "\n ERROR: The File was not successfully decompressed.";
 break;
 
 default:
 std::system( command.c_str() );
 break;
}
}

inline void Player_Control_run()
{
std::string Command = "";
while ( true )
{
 getline ( std::cin, Command );
 Run_Command( Command.c_str() );
 Command = "";
}
exit(0);
}

inline bool decompress_one_file(const char *infilename, const char *outfilename)
{
   gzFile infile = gzopen(infilename, "rb");
   FILE *outfile = fopen(outfilename, "wb");
   if (!infile || !outfile) return false;

   char buffer[128];
   int num_read = 0;
   while ( ( num_read = gzread(infile, buffer, sizeof(buffer) ) ) > 0) {
      fwrite(buffer, 1, num_read, outfile);
     }

   gzclose(infile);
   fclose(outfile);
   return true;
}

inline bool compress_one_file(const char *infilename, const char *outfilename)
{
   FILE *infile = fopen(infilename, "rb");
   gzFile outfile = gzopen(outfilename, "wb");
   if (!infile || !outfile) return false;

   char inbuffer[128];
   int num_read = 0;
   unsigned long total_read = 0, total_wrote = 0;
   while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0) {
      total_read += num_read;
      gzwrite(outfile, inbuffer, num_read);
   }
   fclose(infile);
   gzclose(outfile);
   return true;
}



It's relatively simple as you can see, though It's only a early work-in-progress and is just something I'm toying with until I can get back into the Programming mood and get back to work on Ruins Of Time.

I want to make it work better with .bat files. It can run them if you call them from the exe but loading them into the exe doesn't work yet, also they won't run CMD2 (as I've started calling it) commands.

I also want to come up with more commands to run on it :D

Download the executable (with zlib1.dll) here:
Download CMD2.zip




* Hosted for free by InvisionFree