/*-------------------------------------------------------------------------------------------------------------- * FUNCTION: MainTerminate * DESCR: Terminates the program. If pTermCode is 0, then the program terminated normally without error. If * pTermCode is nonzero, then the program terminated abnormally because of some sort of error. In the * latter case, a formatted error message should be displayed. * RETURNS: Nothing * NOTE: This is a variadic function because the ... following the required pFmt parameter states that * there may be 0, 1, 2, ... optional parameters. Variadic arguments are handled by preprocessor * macros defined in stdarg.h, in particular: va_list, va_start, va_arg, and va_end. * NOTE: This function works similarly to printf(). A typical call would be something like, * * MainTerminate(-1, "An error code %d occurred in %s", err_code, some_string_var); * * pTermCode would be initialized to -1 when the function is called. pFmt would be initialized to * "An error code %d occurred in %s", and there would be two variadic arguments: err_code which * had better be an int variable, and some_string_var which had better be of the data type char *. * If you located the source code for the printf() function in the C Standard Library, you will see * that it looks very similar to this, only without the pTermCode parameters, and it will handle * more format specifiers than just %c, %d, and %s. *------------------------------------------------------------------------------------------------------------*/ void MainTerminate ( int pTermCode, char *pFmt, ... ) { if (pTermCode != 0) { char *fp, msg[4096]; sprintf(msg, "%s: ", BINARY); va_list argp; va_start(argp, pFmt); for (fp = pFmt; fp && *fp; fp++) { if (*fp != '%') { StrCatChar(msg, *fp); } else { switch (*++fp) { case 'c': StrCatChar(msg, (char)va_arg(argp, int)); break; case 'd': StrCatInt(msg, va_arg(argp, int)); break; case 's': strcat(msg, va_arg(argp, char *)); break; } } } va_end(argp); ViewPrintStr(msg); }
/*-------------------------------------------------------------------------------------------------------------- * FUNCTION: ControllerRun * DESCR: Called after the Controller is initialized in ControllerBegin and after the command line has been * parsed. Reads the key from the specified key file name. Calls ControllerEncryptDecrypt to encrypt * or decrypt a message. Finally calls ViewPrintStr to print the encrypted or decrypted message. * RETURNS: Nothing. * PSEUDOCODE: * Define a char array named key which is of length MAX_MSG_LEN+1. * Define a char array named msgOut which is of length MAX_MSG_LEN+1. * Call ModelGetKeyFilename() to get the key file name that was parsed from the command line. * Call FileReadStr() and pass the key file name and the key array as parameters. This will read the key * from the file. * Call ModelSetKey() to store the key that was read from the file. * Call ModelGetMode() to get the mode from the Model (the mode was parsed from the command line). * Call ControllerEncryptDecrypt() and pass the mode and msgOut as parameters. * Call ViewPrintStr() and pass msgOut as the parameter. *------------------------------------------------------------------------------------------------------------*/ void ControllerRun() { char *key[MAX_MSG_LEN+1]; char *msgOut[MAX_MSG_LEN+1]; *key=ModelGetKeyFilename(); FileReadStr(*key,*msgOut); ModelSetKey(*msgOut); ControllerEncryptDecrypt(ModelGetMode(),*msgOut); ViewPrintStr(*msgOut); }
/*-------------------------------------------------------------------------------------------------------------- * FUNCTION: ControllerRun * DESCR: Called after the Controller is initialized in ControllerBegin and after the command line has been * parsed. Reads the key from the specified key file name. Calls ControllerEncryptDecrypt to encrypt * or decrypt a message. Finally calls ViewPrintStr to print the encrypted or decrypted message. * RETURNS: Nothing. * PSEUDOCODE: * Define a char array named key which is of length MAX_MSG_LEN+1. * Define a char array named msgOut which is of length MAX_MSG_LEN+1. * Call ModelGetKeyFilename() to get the key file name that was parsed from the command line. * Call FileReadStr() and pass the key file name and the key array as parameters. This will read the key * from the file. * Call ModelSetKey() to store the key that was read from the file. * Call ModelGetMode() to get the mode from the Model (the mode was parsed from the command line). * Call ControllerEncryptDecrypt() and pass the mode and msgOut as parameters. * Call ViewPrintStr() and pass msgOut as the parameter. *------------------------------------------------------------------------------------------------------------*/ void ControllerRun ( ) { char key[MAX_MSG_LEN+1]; char msgOut[MAX_MSG_LEN+1]; strcpy(key,ModelGetKeyFilename()); FileReadStr(key,key); ModelSetKey(key); ControllerEncryptDecrypt(ModelGetMode(), msgOut); ViewPrintStr(msgOut); }