home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////////////////////////////////
- //
- // FILE: gwutil.c
- //
- // Generic Embedded SQL for C Win16/Win32 utilities
- //
- // FUNCTIONS:
- //
- // GetOption() - Get next command line option and parameter
- // PrintLine() - Appends a line to an edit control
- // PrintStr() - Appends a string to an edit control
- //
- // COMMENTS:
- //
- // Copyright (C) 1992 - 1994 Microsoft Corporation
- //
- //////////////////////////////////////////////////////////////////////////////
-
- #include <windows.h> // standard Windows header
- #include <windowsx.h> // standard Windows header
- #include <stddef.h> // standard C run-time header
- #include <ctype.h> // standard C run-time header
- #include <string.h> // standard C run-time header
- #include "gwutil.h" // utility header
-
- // GLOBAL VARIABLES
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: GetOption()
- //
- // Get next command line option and param
- //
- // PARAMETERS:
- //
- // argc - count of command line args
- // argv - array of command line argument strings
- // pszValidOpts - string of valid, case-sensitive option characters,
- // a colon ':' following a given character means that
- // option can take a parameter
- // ppszParam - pointer to a pointer to a string for output
- //
- // RETURNS:
- //
- // If valid option is found, the character value of that option
- // is returned, and *ppszParam points to the parameter if given,
- // or is NULL if no param
- // If standalone parameter (with no option) is found, 1 is returned,
- // and *ppszParam points to the standalone parameter
- // If option is found, but it is not in the list of valid options,
- // -1 is returned, and *ppszParam points to the invalid argument
- // When end of argument list is reached, 0 is returned, and
- // *ppszParam is NULL
- //
- // COMMENTS:
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- int GetOption (
- int argc,
- char** argv,
- char* pszValidOpts,
- char** ppszParam)
- {
- static int iArg = 1;
- char chOpt;
- char* psz = NULL;
- char* pszParam = NULL;
-
- if (iArg < argc)
- {
- psz = &(argv[iArg][0]);
- if (*psz == '-' || *psz == '/')
- {
- // we have an option specifier
- chOpt = argv[iArg][1];
- if (isalnum(chOpt) || ispunct(chOpt))
- {
- // we have an option character
- psz = strchr(pszValidOpts, chOpt);
- if (psz != NULL)
- {
- // option is valid, we want to return chOpt
- if (psz[1] == ':')
- {
- // option can have a parameter
- psz = &(argv[iArg][2]);
- if (*psz == '\0')
- {
- // must look at next argv for param
- if (iArg+1 < argc)
- {
- psz = &(argv[iArg+1][0]);
- if (*psz == '-' || *psz == '/')
- {
- // next argv is a new option, so param
- // not given for current option
- }
- else
- {
- // next argv is the param
- iArg++;
- pszParam = psz;
- }
- }
- else
- {
- // reached end of args looking for param
- }
-
- }
- else
- {
- // param is attached to option
- pszParam = psz;
- }
- }
- else
- {
- // option is alone, has no parameter
- }
- }
- else
- {
- // option specified is not in list of valid options
- chOpt = -1;
- pszParam = &(argv[iArg][0]);
- }
- }
- else
- {
- // though option specifier was given, option character
- // is not alpha or was was not specified
- chOpt = -1;
- pszParam = &(argv[iArg][0]);
- }
- }
- else
- {
- // standalone arg given with no option specifier
- chOpt = 1;
- pszParam = &(argv[iArg][0]);
- }
- }
- else
- {
- // end of argument list
- chOpt = 0;
- }
-
- iArg++;
- *ppszParam = pszParam;
- return (chOpt);
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: PrintLine()
- //
- // Adds a line to an edit control, after current contents
- //
- // PARAMETERS:
- //
- // hwndEdit - handle to edit control
- // lpstrLine - string to add
- //
- // RETURNS: TRUE
- //
- // COMMENTS:
- //
- //////////////////////////////////////////////////////////////////////////////
-
- BOOL PrintLine (
- HWND hwndEdit,
- LPSTR lpstrLine)
- {
- PrintStr(hwndEdit, lpstrLine);
- PrintStr(hwndEdit, "\r\n");
- return (TRUE);
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: PrintStr()
- //
- // Adds a string to an edit control, after current contents
- //
- // PARAMETERS:
- //
- // hwndEdit - handle to edit control
- // lpstrStr - string to add
- //
- // RETURNS: TRUE
- //
- // COMMENTS:
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- BOOL PrintStr (
- HWND hwndEdit,
- LPSTR lpstrStr)
- {
- int iEndIndex;
-
- // get char index of last character in edit control
- iEndIndex = Edit_GetTextLength(hwndEdit);
-
- // set selection to index at end of edit control text
- Edit_SetSel(hwndEdit, iEndIndex, iEndIndex);
-
- // replace above selection with string
- Edit_ReplaceSel(hwndEdit, lpstrStr);
-
- return (TRUE);
- }
-