Example #1
0
/*--------------------------------------------------------------------------------------------------------------
 * FUNCTION: ModelBegin
 * DESCR:    Called to initialize the Model data base. Sets the alphabet, the key file name to "", and the
 *           mode to -1.
 * RETURNS:  Nothing.
 *------------------------------------------------------------------------------------------------------------*/
void ModelBegin
	(
	)
{
    ModelSetKeyFilename("");
    ModelSetMode(-1);
}
Example #2
0
/*--------------------------------------------------------------------------------------------------------------
 * FUNCTION: ControllerParseCmdLine()
 * DESCR:    Examines the command line for arguments and options. Information parsed on the command line is
 *           stored in variables in the Model. Suppose the command line that was typed is:
 *
 *           ./vigenere e -k key.txt < plain.txt > cipher.txt
 *
 *           Then pArgc will be 4, and pArgv[] will be,
 *
 *           pArgv[0] = "vigenere"
 *           pArgv[1] = "e"
 *           pArgv[2] = "-k"
 *           pArgv[3] = "key.txt"
 *
 *           Note that the "< plain.txt > cipher.txt" part is not passed to the program in the pArgv[] array.
 *
 * RETURNS:  Nothing.
 *------------------------------------------------------------------------------------------------------------*/
static void ControllerParseCmdLine
    (
    int   pArgc,
    char *pArgv[]
    )
{
    bool bKeyfile = false, bMode = false;
    int i;

    for (i = 1; i < pArgc; i++) {
        if (streq(pArgv[i], "e")) {
            /* Call ModelSetMode() to set the mode to VIGENERE_ENCRYPT */
            ModelSetMode(VIGENERE_ENCRYPT);
            /* Set bMode to true to indicate that a mode argument was found on the command line. */
            bMode = true;
        } else if (streq(pArgv[i], "d")) {
            /* Call ModelSetMode() to set the mode to VIGENERE_DECRYPT */
            ModelSetMode(VIGENERE_DECRYPT);
            /* Set bMode to true to indicate that a mode argument was found on the command line. */
            bMode = true;
        } else if (streq(pArgv[i], "-h")) {
            /* Call ViewHelp() to display the help information. */
            ViewHelp();
            /* Call MainTerminate() with an error code of 0 and "" as the format string. */
            MainTerminate( 0 , ""); 
        } else if (streq(pArgv[i], "-k")) {
            if (++i >= pArgc) MainTerminate(TERM_ERR_KEYFILE, "-k option, missing key file name.\n");
            ModelSetKeyFilename(pArgv[i]);
            bKeyfile = true;
        } else if (streq(pArgv[i], "-v")) {
            /* Call a certain View module function to display the version information. */
            ViewVersion();
            /* Call MainTerminate() like you did for the -h option to terminate the program. */
            MainTerminate(0, "");
        } else {
            MainTerminate(TERM_ERR_CMDLINE, "invalid command line option: %s\n", pArgv[i]);
        }
    }
    if (!bMode) {
        MainTerminate(TERM_ERR_CMDLINE, "missing mode (should be 'e' to encrypt or 'd' to decrypt\n");
    }
    if (!bKeyfile) {
        MainTerminate(TERM_ERR_CMDLINE, "missing -k 'keyfile' option. Use -h option for help.\n", pArgv[i]);
    }
}