//==================================================================================================
// P r o g r a m                                                                          Interface
// E n c r y p t
// C o n s o l e
//                                                                                By Bruno Bachelet
//==================================================================================================
// Copyright (c) 1999-2016
// Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net
//
// This file is part of the B++ Library. This library is free software; you can redistribute it
// and/or modify it under the terms of the GNU Library General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU Library General Public License for more details (http://www.gnu.org).

/*DESCRIPTION*/
/* This module provides facilities to execute the program in console mode. */

// File Name //-------------------------------------------------------------------------------------
#line __LINE__ "program/encrypt/console.hpp"

// Guardian //--------------------------------------------------------------------------------------
#ifndef guProgramEncryptConsole
#define guProgramEncryptConsole

// Headers //---------------------------------------------------------------------------------------
#include <bpp/calendar.hpp> /*INCLUDE*/

namespace bpp {

// Importation/Exportation //-----------------------------------------------------------------------
#ifdef PROGRAM_ENCRYPT_DLL
 #define dll_export DLL_EXPORT
#else
 #define dll_export DLL_IMPORT
#endif

// Namespaces //------------------------------------------------------------------------------------
#define public_area  programEncryptConsole
#define private_area programEncryptConsole_private

namespace public_area  { /*NAMESPACE*/ using namespace standard; }
namespace private_area { using namespace public_area; }

extern_module_name;

// Initialization //--------------------------------------------------------------------------------
#define iniProgramEncryptConsole
has_initializer;

// Macrocommands //---------------------------------------------------------------------------------

// Types & Classes //-------------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Functions Interface //---------------------------------------------------------------------------
namespace public_area { function tyReturn run(tyCardinal,tcString []); }

namespace private_area {
 function void showLine(tyByte *,tyCardinal=16);
 function void showTable(tyByte *);
}

// Errors //----------------------------------------------------------------------------------------
namespace public_area {
 /*ERROR*/ extern_error erSyntax; /* Syntax error in the command line. */
}

// Constants & Variables //-------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// X X X  Interface //------------------------------------------------------------------------------
namespace {}

// Functions Inline //------------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// End //-------------------------------------------------------------------------------------------
}
#undef dll_export
#undef public_area
#undef private_area
#endif
 
//==================================================================================================
// P r o g r a m                                                                     Implementation
// E n c r y p t
// C o n s o l e
//                                                                                By Bruno Bachelet
//==================================================================================================
// Copyright (c) 1999-2016
// Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net
//
// This file is part of the B++ Library. This library is free software; you can redistribute it
// and/or modify it under the terms of the GNU Library General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU Library General Public License for more details (http://www.gnu.org).

// File Name //-------------------------------------------------------------------------------------
#line __LINE__ "program/encrypt/console.cpp"

// DLL Belonging //---------------------------------------------------------------------------------
#define PROGRAM_ENCRYPT_DLL

// Headers //---------------------------------------------------------------------------------------
#include <bpp/program/encrypt/console.hpp> /*INTERFACE*/

namespace bpp {

// Namespaces //------------------------------------------------------------------------------------
#define public_area  programEncryptConsole
#define private_area programEncryptConsole_private
#define dll_export   DLL_EXPORT

namespace public_area  {}
namespace private_area {}

static_module_name("Program/Encrypt/Console");

// Initialization //--------------------------------------------------------------------------------
#undef iniProgramEncryptConsole
static_constant(private_area::clInitializer,goInitializer);

// Errors //----------------------------------------------------------------------------------------
namespace public_area { static_error erSyntax; }

// Constants & Variables //-------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Static Members //--------------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Functions Implementation //----------------------------------------------------------------------
namespace public_area {
 //----------------------------------------------------------------------------------------------Run
 /*FUNCTION*/
 /* Start function of the program. It deals with the commands the user gives through a command
    line and executes the appropriate functions. */
 function tyReturn run(tyCardinal agCounter,tcString agParameterS[]) {
  method_name("run");

  clString  lcCommand;
  tyBoolean lcError;
  clString  lcKey;

  tyCardinal lcCounter   = 1;
  tyBoolean  lcDisplayed = true;

  environment_private::goProgramName = "E n c r y p t";

  try {
   if (agCounter==1) send_error(erSyntax);

   while (lcCounter<agCounter) {
    lcCommand=agParameterS[lcCounter];
    lcError=true;

    //---------------------------------------------------------------------------------------Context
    if (lcCommand=="+context") {
     lcError=false;
     environment::nextLine();
     environment::outContext();
    }

    //------------------------------------------------------------------------------------------Help
    else if (lcCommand=="+help") {
     lcError=false;
     environment::nextLine();
     environment::inform("-=-=- H E L P -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
     environment::out(" This program encodes or decodes a folder using a given key.",true,true);
     environment::nextLine();
     environment::out(" * Syntax: [options] [commands]",true,true);
     environment::nextLine();
     environment::out(" * Options:",true,true);
     environment::out("   -hide     = deactivates the display of information.",true,true);
     environment::out("   -key <x>  = encodes and decodes with the key <x>.",true,true);
     environment::out("   -file <x> = encodes and decodes with the key in the file <x>.",true,true);
     environment::nextLine();
     environment::out(" * Commands:",true,true);
     environment::out("   +context        = shows the context of the program's running.",true,true);
     environment::out("   +help           = shows this help.",true,true);
     environment::out("   +info           = shows information about the program.",true,true);
     environment::out("   +license        = shows the license of the program.",true,true);
     environment::out("   +encode <x> <y> = encodes the folder <x> in the archive <y>.",true,true);
     environment::out("   +decode <x> <y> = decodes a folder from the archive <x> and stores",true,true);
     environment::out("                     it in the path <y>.",true,true);
     environment::out("   +tables         = shows the encoding tables associated with the key.",true,true);
     environment::out("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-",true,true);
    }

    //------------------------------------------------------------------------------------------Info
    else if (lcCommand=="+info") {
     lcError=false;
     environment::nextLine();
     environment::outInformation();
    }

    //---------------------------------------------------------------------------------------License
    else if (lcCommand=="+license") {
     lcError=false;
     environment::nextLine();
     environment::outLicense();
    }

    //------------------------------------------------------------------------------------------Hide
    else if (lcCommand=="-hide") {
     lcDisplayed=false;
     lcError=false;
    }

    //-------------------------------------------------------------------------------------------Key
    else if (lcCommand=="-key") {
     if (agCounter-lcCounter>1) {
      lcKey=agParameterS[lcCounter+1];
      lcCounter+=1;
      lcError=false;
     }
    }

    //------------------------------------------------------------------------------------------File
    else if (lcCommand=="-file") {
     if (agCounter-lcCounter>1) {
      clInFile lcFin;

      open(lcFin,agParameterS[lcCounter+1],ios::in);
      getLine(lcFin,lcKey);
      close(lcFin);
      lcCounter+=1;
      lcError=false;
     }
    }
    //----------------------------------------------------------------------------------------Encode
    else if (lcCommand=="+encode") {
     if (agCounter-lcCounter>2) {
      if (lcDisplayed) environment::nextLine();

      encodeFolder(clString(agParameterS[lcCounter+2]),clString(agParameterS[lcCounter+1]),
                   lcKey,lcDisplayed);

      lcCounter+=2;
      lcError=false;
     }
    }

    //----------------------------------------------------------------------------------------Decode
    else if (lcCommand=="+decode") {
     if (agCounter-lcCounter>2) {
      if (lcDisplayed) environment::nextLine();

      decodeFolder(clString(agParameterS[lcCounter+2]),clString(agParameterS[lcCounter+1]),
                   lcKey,lcDisplayed);

      lcCounter+=2;
      lcError=false;
     }
    }

    //----------------------------------------------------------------------------------------Tables
    else if (lcCommand=="+tables") {
     tyByte     lcDecodingTable[256];
     tyByte     lcEncodingTable[256];
     tyByte     lcMask[256];
     tyCardinal lcNbIteration;

     environment::nextLine();
     environment::inform("Key = ",false);
     environment::out(lcKey.data(),true);

     lcNbIteration=buildEncodingTables(lcMask,lcEncodingTable,lcDecodingTable,lcKey);

     environment::nextLine();
     environment::inform("Mask Table:");
     private_area::showTable(lcMask);

     environment::nextLine();
     environment::inform("Encoding Table:");
     private_area::showTable(lcEncodingTable);

     environment::nextLine();
     environment::inform("Decoding Table:");
     private_area::showTable(lcDecodingTable);

     environment::nextLine();
     environment::inform("Iterations = ",false);
     environment::out(lcNbIteration,true);

     lcError=false;
    }

    if (lcError) send_error(erSyntax);
    lcCounter++;
   }
  }

  program_catch;
 }
}

namespace private_area {
 //-----------------------------------------------------------------------------------------ShowLine
 function void showLine(tyByte * agTable,tyCardinal agLength) {
  tyCardinal lcCounter = 0;
  tyInteger  lcNumber;

  environment::out("",false,true);

  while (lcCounter<agLength) {
   lcNumber=integer(agTable[lcCounter]);
   environment::out((lcNumber<10 ? "00" : (lcNumber<100 ? "0" : "")));
   environment::out(lcNumber);
   ++lcCounter;
   if (lcCounter<16) environment::out(" ");
  }

  environment::nextLine();
 }
 //----------------------------------------------------------------------------------------ShowTable
 function void showTable(tyByte * agTable) {
  tyCardinal lcCounter = 0;

  while (lcCounter<16) {
   showLine(agTable+16*lcCounter);
   ++lcCounter;
  }
 }
}

// X X X  Implementation //-------------------------------------------------------------------------
namespace {}

// I n i t i a l i z e r  Implementation //---------------------------------------------------------
namespace private_area {
 //--------------------------------------------------------------------------------------------Start
 property void clInitializer::start(void) {
  if (atCounter++ == 0) {
   try {
    #include <bpp/modules.hpp> /*NEED*/
    registerStop(this);
    environment::informInitialization(goModuleName);

    erSyntax.create("Encrypt - Use the '+help' parameter to get some help.");
   }

   initializer_catch;
  }
 }
 //---------------------------------------------------------------------------------------------Stop
 property void clInitializer::stop(void) { environment::informTermination(goModuleName); }
}

// End //-------------------------------------------------------------------------------------------
}