/***************************************************************************
* sim_srev.c - description
* -------------------
* begin : Mon 19 Dec 2005 04:20:14 PM EST
* copyright : (C) 2005 by Terry D. Boldt
* email : fastsnip-wm1@yahoo.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/*
*
* QTAwk plugin function to duplicate functionality of builtin function "srev"
*/
#include "QTAwk_plugins.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
/* define some useful constants
*/
#define TRUE 1
#define FALSE 0
/* define End Of String marker
*/
#define EOS '\x000'
/* define initialization function
*/
unsigned init_sim_srev(
void)
{
/* array of pointers to string with argument names for loaded functions
* Here both functions take a single string function
*/
unsigned char *arguments[] = {
"string1",
NULL
};
/* declare error return variable
*/
unsigned char *reg_err;
/* call function to initialize for installing dynamic function(s)
*/
QTAwk_register_function_count(3);
/* now call function to register each dynamic function to load
*
* Note that 'QTAwk_register_function' returns a pointer to a string or NULL
* if no error in registering function. Here we are ignoring the return value
* until returning from the call.
* We "should" examine the return value for every call to 'QTAwk_register_function'
* and report any errors.
*/
reg_err = QTAwk_register_function("sim_srev_version",NULL,FALSE);
reg_err = QTAwk_register_function("sim_srev",arguments,FALSE);
reg_err = QTAwk_register_function("rev_caps",arguments,FALSE);
return reg_err ? -1 : 0;
} /* init_testrev */
/* function registered to accomplish actual work.
*
* All dynamically loaded functions have the following prototype form:
*
* Variable_ptr loaded_function(unsigned argument_count);
*
* Where 'Variable_ptr' is a QTAwk variable defined in the QTAwk plugin
* header file "QTAwk_plugins.h"
* Do NOT manipulate the fields in the structure directly. Use the function
* calls defined in "QTAwk_plugins.h" for that purpose.
*
*
*
*/
/* version string
*/
static unsigned char sim_srev_mod_version_string[] = "Simulate 'srev' Module version 1.00";
/* function to return module version string
*/
Variable_ptr sim_srev_version(
void)
{
Variable_ptr retval;
retval = QTAwk_get_variable_set((void *)sim_srev_mod_version_string,STRING_VAL);
return retval;
} /* sim_srev_version */
/* functio to duplicate 'srev' builtin function
*/
Variable_ptr sim_srev(
unsigned arg_cnt)
{
/* function to duplicate functionality of builtin function "srev"
* takes one argument, a string, reverses characters in string
* and returns reversed string
*/
Variable_ptr string_arg;
Variable_ptr return_string;
unsigned rt = STRING_VAL;
unsigned char *estr,
*bstr,
*rstr;
/* call function to get string to reverse
* NOTE could also have used the call:
* string_arg = QTAwk_get_argument(1,"string1");
* since the argument was named "string1" when the function was registered.
* Note: that the first argument in the call is now irrelevant and could have equally used:
* string_arg = QTAwk_get_argument(3,"string1");
*/
string_arg = QTAwk_get_argument(1,NULL);
/* get string value of argument and duplicate - never alter QTAwk variable strings directly
* That can lead to unpredictable results.
* NOTE: ignoring type of returned value in 'rt'. To be really cautious, we should check
* the value of this variable to ascertain that it is equal to the value specified.
*/
rstr = bstr = strdup((char *)QTAwk_get_value(string_arg,&rt));
estr = strchr(bstr,EOS) - 1;
printf("String to reverse: %s\n",rstr);
/* reverse string in place
*/
while ( estr > bstr ) {
*estr ^= *bstr;
*bstr ^= *estr;
*estr-- ^= *bstr++;
} /* end_while */
printf("Reversed String : %s\n",rstr);
/* Now get QTAwk variable for return value and set the value
*/
return_string = QTAwk_get_variable_set((void *)rstr,STRING_VAL);
/* must now free memory used in "local" copy of reversed string
*/
free(rstr);
/* return with return value in QTAwk variable
*/
return return_string;
} /* sim_srev */
Variable_ptr rev_caps(
unsigned arg_cnt)
{
Variable_ptr string_arg;
Variable_ptr return_string;
unsigned rt = STRING_VAL;
unsigned char *rstr;
unsigned char *bstr;
/* get argument with string to reverse capitals
*/
string_arg = QTAwk_get_argument(1,NULL);
/* duplicate string to get local working copy
*/
bstr = rstr = strdup((char *)QTAwk_get_value(string_arg,&rt));
/* Now do work of reversing caps
*/
while ( *rstr ) {
if ( isupper(*rstr) ) *rstr = tolower(*rstr);
else if ( islower(*rstr) ) *rstr = toupper(*rstr);
rstr++;
} /* endwhile */
/* get QTAwk variable to return result and set value to return string
*/
return_string = QTAwk_get_variable_set((void *)bstr,STRING_VAL);
/* must now free memory used in "local" copy of reversed string
*/
free(bstr);
/* return with return value in QTAwk variable
*/
return return_string;
} /* rev_caps*/
/* exit function - executed before unloading module
* Using default function name here.
*/
unsigned QTAwk_exit_load_module(
void)
{
printf("Unloading Simulate 'srev' builtin and reverse Caps functions module.\n");
} /* QTAwk_exit_load_module */