home *** CD-ROM | disk | FTP | other *** search
- /*
- **
- ** ArgsControlClass.cpp
- **
- ** (c) 1998 Didier Levet
- **
- ** $Revision: 1.3 $
- ** $State: Exp $
- ** $Date: 1998/10/21 13:52:17 $
- **
- ** $Log: ArgsControlClass.cpp $
- ** Revision 1.3 1998/10/21 13:52:17 kakace
- ** Use CStrArray class for ToolTypes strings
- **
- ** Revision 1.2 1998/10/17 16:21:46 kakace
- ** Quickly tested. Seems OK.
- **
- ** Revision 1.1 1998/10/03 14:32:31 kakace
- ** Initial revision
- **
- **
- */
-
-
- //----------------------------------------------------------------------------------------------------
-
- /// Includes
-
- // C/C++
-
- #ifndef _ARGSCONTROL_CLASS_HPP
- #include "ArgsControlClass.hpp"
- #endif
-
- #ifndef _INCLUDE_STDLIB_H
- #include <stdlib.h>
- #endif
-
- #ifndef _INCLUDE_STRING_H
- #include <string.h>
- #endif
-
- ///
-
-
- //----------------------------------------------------------------------------------------------------
- //========================================== Class CStartup ==========================================
- //----------------------------------------------------------------------------------------------------
-
- //==================================================================================================
- //
- // SYNOPSIS CStartup::CStartup
- // CStartup::CStartup(control[], array[], errorOutput, extendedHelp)
- // CStartup::CStartup(const CArgsControl, LONG, CInterface *, STRING);
- //
- // FUNCTION (Instance constructor)
- // Initialize parameters handling.
- // The commande line template used by ReadArgs() is created, the LONG array filled by
- // ReadArgs() is initialized with default values, and a RDArgs structure is created to provide
- // an extended help string to ReadArgs().
- // An array is allocated to hold all ToolTypes strings.
- //
- // INPUTS
- // control[] - Control array. It defines each argument.
- // array[] - Where to store these arguments.
- // errorOutput - Object that provide an API to display errors.
- // extendedHelp - Extended help provides by ReadArgs(). Default to NULL.
- //
- // PRECONDITIONS
- // - errorOutput must not be NULL.
- // - Control array must be initialized properly.
- // - 'array' must not be NULL.
- //
- // POSTCONDITIONS
- // - Command line pattern is created (for later use by ReadArgs()).
- // - iStatus == NO_ERROR if init was done successfully.
- //
- // NOTES
- // - extendedHelp string is not copied. Thus, this pointer must remain valid.
- //
- // SEE ALSO
- // Instance destructor.
- // CInterface
- //
- // HISTORY
- //
- //==================================================================================================
- /// CStartup::CONSTRUCTOR
-
-
- CStartup::CStartup(const CArgsControl control[], LONG array[], STRING extendedHelp)
- : piArgsArray(array), poArgsControl(control), oTTStrings(), piTTNumbers(NULL), iStatus(CStartup::NO_ERROR)
- {
- // Create the template for ReadArgs(), and allocate a vector for ToolTypes strings.
-
- InitCLITemplate();
- InitArrays();
-
- // Allocate our own RDArgs structure needed for extended help.
-
- if ( (pOwnArgs = (RDArgs *) oRDArgs.Allocate(CDOSObject::RDARGS)) != NULL)
- {
- pOwnArgs->RDA_ExtHelp = (char *) extendedHelp;
- }
- else iStatus = NO_MEMORY;
- }
- ///
-
- //==================================================================================================
- //
- // SYNOPSIS CStartup::~CStartup
- // CStartup::~CStartup()
- // CStartup::~CStartup();
- //
- // FUNCTION (Instance destructor)
- // Free all resources allocated by the constructor, or member functions.
- //
- // POSTCONDITIONS
- // - CLITemplate string buffer freed.
- // - DOS object (RDArgs) is freed if already allocated.
- // - RDArgs structure returned by ReadArgs() is freed.
- // - Vector and buffers allocated for ToolTypes strings copies are also freed.
- //
- // SEE ALSO
- // Instance constructor, CStartup::ParseCLIArgs(), CStartup::InitCLITemplate()
- //
- // HISTORY
- //
- //==================================================================================================
- /// CStartup::DESTRUCTOR
-
-
- CStartup::~CStartup()
- {
- delete [] piTTNumbers;
- delete [] pcCLITemplate; // delete [] NULL is safe.
- }
- ///
-
- //==================================================================================================
- //
- // SYNOPSIS CStartup::ParseArgs
- // ErrorCode = CStartup::ParseArgs()
- // Errors CStartup::ParseArgs();
- //
- // FUNCTION
- // Fetch settings from the icon's ToolTypes to alter default settings, then parse command line
- // parameters.
- //
- // RESULT
- // ErrorCode : NO_ERROR on success.
- //
- // POSTCONDITIONS
- // - LONG array is initialized with ToolTypes and CLI parameters values.
- //
- // SEE ALSO
- // CStartup::ParseToolTypes(), CStartup::ParseCLIArgs()
- //
- // HISTORY
- //
- //==================================================================================================
- /// CStartup::ParseArgs()
-
-
- CStartup::Errors CStartup::ParseArgs()
- {
- Errors err_code = iStatus;
-
- if (err_code == NO_ERROR) err_code = ParseToolTypes();
- if (err_code == NO_ERROR) err_code = ParseCLIArgs();
-
- return err_code;
- }
- ///
-
- //==================================================================================================
- //
- // SYNOPSIS CStartup::ParseCLIArgs
- // ErrorCode = CStartup::ParseCLIArgs()
- // Errors CStartup::ParseCLIArgs();
- //
- // FUNCTION
- // Fetch settings from the CLI command line using ReadArgs(). Numeric parameters are checked
- // against their respective bounds, if any. Further controls HAVE to be made by the client.
- //
- // RESULT
- // ErrorCode : NO_ERROR on success, READARGS_ERROR if ReadArgs() failed, or BAD_ARGUMENT if a
- // number is out of range.
- //
- // PRECONDITIONS
- // - pcCLITemplate is constructed properly (constructor's job).
- // - piArgsArray is initialized with default values (constructor's job).
- // - pOwnRDArgs is valid, or NULL.
- //
- // POSTCONDITIONS
- // - piArgsArray is filled with CLI parameters, unless an error occured.
- //
- // NOTES
- // - RDArgs structure is not freed here. This will be done by the destructor.
- //
- // SEE ALSO
- // Instance destructor.
- //
- // HISTORY
- //
- //==================================================================================================
- /// CStartup::ParseCLIArgs()
-
-
- CStartup::Errors CStartup::ParseCLIArgs()
- {
- if (DOSArgs.ReadArgs(pcCLITemplate, piArgsArray, pOwnArgs) != NULL)
- {
- int i;
- LONG value, lower_bound, upper_bound;
-
- for (i = 0; poArgsControl[i].pcArgName != NULL; i++)
- {
- if (piArgsArray[i] != 0
- && poArgsControl[i].bArgType == ARG_NUMBER
- && (poArgsControl[i].bArgFlags & ARGF_CHECKLIMITS) != 0)
- {
- value = *( (LONG *) piArgsArray[i] );
-
- lower_bound = (LONG) poArgsControl[i].iControl;
- upper_bound = (LONG) poArgsControl[i].iUpperBound;
-
- if (value < lower_bound || value > upper_bound)
- {
- iStatus = BAD_ARGUMENT;
- break;
- }
- }
- }
- }
- else iStatus = READARGS_ERROR;
-
- return iStatus;
- }
- ///
-
- //==================================================================================================
- //
- // SYNOPSIS CStartup::ParseToolTypes
- // ErrorCode = CStartup::ParseToolTypes(progName)
- // Errors CStartup::ParseToolTypes(STRING);
- //
- // FUNCTION
- // Fetch global settings from the icon's ToolTypes.
- //
- // INPUTS
- // progName - Program name or NULL.
- //
- // RESULT
- // ErrorCode : NO_ERROR on success, or NO_MEMORY.
- //
- // NOTES
- // Incorrect values are ignored.
- //
- // HISTORY
- //
- //==================================================================================================
- /// CStartup::ParseToolTypes()
-
-
- CStartup::Errors CStartup::ParseToolTypes(STRING progName)
- {
- CDiskObject icon;
-
- // Load the icon then check all possible tool types.
-
- if (icon.Load(progName) != NULL)
- {
- int i;
- STRING TT_name; // ToolType name.
- STRING TT_value; // ToolType value.
-
- // Loop for all possible ToolTypes, unless an error occurs.
-
- for (i = 0; (TT_name = poArgsControl[i].pcArgName) != NULL && iStatus == NO_ERROR; i++)
- {
- if ( (poArgsControl[i].bArgFlags & ARGF_WB) != 0
- && (TT_value = icon.FindToolType(TT_name)) != NULL)
- {
- ULONG offset = poArgsControl[i].wOffset;
-
- // Handle the current ToolType according to its type.
-
- switch(poArgsControl[i].bArgType)
- {
- // The current ToolType hold a string. It must be copied
- // because we will free the icon upon exit.
-
- case ARG_STRING:
- AllocateNewString(i, TT_value);
- break;
-
- // The current ToolType hold a number. If ARGF_CHECKLIMITS is specified
- // in the control structure, this number is checked against the specified
- // bounds. Out of bounds number are discarded.
-
- case ARG_NUMBER:
- {
- LONG value = atoi(TT_value);
- LONG check = poArgsControl[i].bArgFlags & ARGF_CHECKLIMITS;
-
- if ( check == 0 || (check != 0 && value >= (LONG) poArgsControl[i].iControl
- && value <= (LONG) poArgsControl[i].iUpperBound) )
- {
- piTTNumbers[iCurrentNumIndex] = value;
- piArgsArray[offset] = (LONG) &piTTNumbers[iCurrentNumIndex];
- ++iCurrentNumIndex;
- }
- }
- break;
-
- // The current ToolType is a boolean one.
-
- case ARG_BOOL:
- piArgsArray[offset] = -1;
- break;
-
- default:
- break;
- }
- }
- }
- }
-
- return iStatus;
- }
- ///
-
- //==================================================================================================
- //
- // SYNOPSIS CStartup::AllocateNewString
- // CStartup::AllocateNewString(ctrlIdx, str)
- // void CStartup::AllocateNewString(ULONG, STRING);
- //
- // FUNCTION
- // Allocate a new buffer to hold a copy of the string passed to this function.
- //
- // INPUTS
- // ctrlIdx - Index in the LONG array to whitch save the string pointer.
- // str - String to copy.
- //
- // PRECONDITIONS
- // String must not be NULL.
- //
- // INVARIANTS
- // iMaxStrings != 0
- //
- // POSTCONDITIONS
- // A new string buffer is available, otherwise iStatus is set to NO_MEMORY.
- //
- // HISTORY
- //
- //==================================================================================================
- /// CStartup::AllocateNewString() [PRIVATE]
-
-
- void CStartup::AllocateNewString(ULONG ctrlIdx, STRING str)
- {
- if (oTTStrings.Add(str) != FALSE)
- {
- piArgsArray[ctrlIdx] = (LONG) oTTStrings[oTTStrings.CurrentIndex() - 1].string();
- }
- else iStatus = NO_MEMORY;
- }
- ///
-
- //==================================================================================================
- //
- // SYNOPSIS CStartup::InitArrays
- // CStartup::InitArrays()
- // void CStartup::InitArrays();
- //
- // FUNCTION
- // Allocate a vector to hold all ToolTypes string pointers and numbers.
- //
- // POSTCONDITIONS
- // Arrays are allocated, or iStatus is set to NO_MEMORY.
- //
- // SEE ALSO
- // CStartup::DeleteArrays()
- //
- // HISTORY
- //
- //==================================================================================================
- /// CStartup::InitArrays() [PRIVATE]
-
-
- void CStartup::InitArrays()
- {
- ULONG max_strings = 0;
-
- iCurrentNumIndex = 0;
- iMaxNumbers = 0;
-
- for (int i = 0; poArgsControl[i].pcArgName != NULL; i++)
- {
- if ( (poArgsControl[i].bArgFlags & ARGF_WB) != 0)
- {
- if (poArgsControl[i].bArgType == ARG_NUMBER)
- {
- ++iMaxNumbers;
- }
-
- if (poArgsControl[i].bArgType == ARG_STRING)
- {
- ++max_strings;
- }
- }
- }
-
- if (max_strings > 0)
- {
- oTTStrings.extend(max_strings);
- }
-
- if (iMaxNumbers > 0)
- {
- if ( (piTTNumbers = new LONG[iMaxNumbers]) == NULL)
- {
- iStatus = NO_MEMORY;
- }
- }
- }
- ///
-
- //==================================================================================================
- //
- // SYNOPSIS CStartup::InitCLITemplate
- // CStartup::InitCLITemplate()
- // void CStartup::InitCLITemplate();
- //
- // FUNCTION
- // Create the template for ReadArgs() and initialize the LONG array with default values.
- //
- // PRECONDITIONS
- // - poArgsControl must point to an initialized CArgsControl structure.
- // - piArgsArray must not be NULL
- // These conditions are checked by the constructor (which call this function).
- //
- // POSTCONDITIONS
- // - pcCLITemplate is created, or iStatus is set to NO_MEMORY.
- //
- // NOTES
- // pcCLITemplate will be freed by the destructor.
- //
- // HISTORY
- //
- //==================================================================================================
- /// CStartup::InitCLITemplate() [PRIVATE]
-
-
- void CStartup::InitCLITemplate()
- {
- // Determine CLITemplate string size.
-
- int i;
- ULONG cli_pattern_size = 0;
-
- for (i = 0; poArgsControl[i].pcArgName != NULL; i++)
- {
- if ( (poArgsControl[i].bArgFlags & ARGF_CLI) != 0)
- {
- cli_pattern_size += 1 + strlen(poArgsControl[i].pcArgName) + strlen(poArgsControl[i].pcCLIModifier);
- // (+1 because of the coma between args, or string ending NULL).
- }
- }
-
- // Construct the CLI template string.
-
- if (cli_pattern_size != 0 && (pcCLITemplate = new char[cli_pattern_size]) != NULL)
- {
- for (i = 0; poArgsControl[i].pcArgName != NULL; i++)
- {
- if ( (poArgsControl[i].bArgFlags & ARGF_CLI) != 0)
- {
- if (i != 0) strcat(pcCLITemplate, ",");
-
- strcat(pcCLITemplate, poArgsControl[i].pcArgName);
- strcat(pcCLITemplate, poArgsControl[i].pcCLIModifier);
- }
-
- // Copy the default value (if any) to our array.
-
- if ( (poArgsControl[i].bArgFlags & ARGF_DEFAULTVALUE) != 0)
- {
- piArgsArray[i] = poArgsControl[i].iDefaultValue;
- }
- }
- }
- else iStatus = NO_MEMORY;
- }
- ///
-
-