home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / esqlc / esql / samples.c / gwutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-21  |  6.5 KB  |  216 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  FILE: gwutil.c
  4. //
  5. //      Generic Embedded SQL for C Win16/Win32 utilities
  6. //
  7. //  FUNCTIONS:
  8. //
  9. //      GetOption() - Get next command line option and parameter
  10. //      PrintLine() - Appends a line to an edit control
  11. //      PrintStr() - Appends a string to an edit control
  12. //
  13. //  COMMENTS:
  14. //
  15. //      Copyright (C) 1992 - 1994 Microsoft Corporation
  16. //
  17. //////////////////////////////////////////////////////////////////////////////
  18.  
  19. #include <windows.h>            // standard Windows header
  20. #include <windowsx.h>           // standard Windows header
  21. #include <stddef.h>             // standard C run-time header    
  22. #include <ctype.h>              // standard C run-time header
  23. #include <string.h>             // standard C run-time header
  24. #include "gwutil.h"             // utility header
  25.  
  26. // GLOBAL VARIABLES
  27.  
  28. ///////////////////////////////////////////////////////////////////////////////
  29. //
  30. //  FUNCTION: GetOption()
  31. //
  32. //      Get next command line option and param
  33. //
  34. //  PARAMETERS:
  35. //
  36. //      argc - count of command line args
  37. //      argv - array of command line argument strings
  38. //      pszValidOpts - string of valid, case-sensitive option characters,
  39. //                     a colon ':' following a given character means that
  40. //                     option can take a parameter
  41. //      ppszParam - pointer to a pointer to a string for output
  42. //
  43. //  RETURNS:
  44. //
  45. //      If valid option is found, the character value of that option
  46. //          is returned, and *ppszParam points to the parameter if given,
  47. //          or is NULL if no param
  48. //      If standalone parameter (with no option) is found, 1 is returned,
  49. //          and *ppszParam points to the standalone parameter
  50. //      If option is found, but it is not in the list of valid options,
  51. //          -1 is returned, and *ppszParam points to the invalid argument
  52. //      When end of argument list is reached, 0 is returned, and
  53. //          *ppszParam is NULL
  54. //
  55. //  COMMENTS:
  56. //
  57. ///////////////////////////////////////////////////////////////////////////////
  58.  
  59. int GetOption (
  60.     int argc,
  61.     char** argv,
  62.     char* pszValidOpts,
  63.     char** ppszParam)
  64. {
  65.     static int iArg = 1;
  66.     char chOpt;
  67.     char* psz = NULL;
  68.     char* pszParam = NULL;
  69.  
  70.     if (iArg < argc)
  71.     {
  72.         psz = &(argv[iArg][0]);
  73.         if (*psz == '-' || *psz == '/')
  74.         {
  75.             // we have an option specifier
  76.             chOpt = argv[iArg][1];
  77.             if (isalnum(chOpt) || ispunct(chOpt))
  78.             {
  79.                 // we have an option character
  80.                 psz = strchr(pszValidOpts, chOpt);
  81.                 if (psz != NULL)
  82.                 {
  83.                     // option is valid, we want to return chOpt
  84.                     if (psz[1] == ':')
  85.                     {
  86.                         // option can have a parameter
  87.                         psz = &(argv[iArg][2]);
  88.                         if (*psz == '\0')
  89.                         {
  90.                             // must look at next argv for param
  91.                             if (iArg+1 < argc)
  92.                             {
  93.                                 psz = &(argv[iArg+1][0]);
  94.                                 if (*psz == '-' || *psz == '/')
  95.                                 {
  96.                                     // next argv is a new option, so param
  97.                                     // not given for current option
  98.                                 }
  99.                                 else
  100.                                 {
  101.                                     // next argv is the param
  102.                                     iArg++;
  103.                                     pszParam = psz;
  104.                                 }
  105.                             }
  106.                             else
  107.                             {
  108.                                 // reached end of args looking for param
  109.                             }
  110.  
  111.                         }
  112.                         else
  113.                         {
  114.                             // param is attached to option
  115.                             pszParam = psz;
  116.                         }
  117.                     }
  118.                     else
  119.                     {
  120.                         // option is alone, has no parameter
  121.                     }
  122.                 }
  123.                 else
  124.                 {
  125.                     // option specified is not in list of valid options
  126.                     chOpt = -1;
  127.                     pszParam = &(argv[iArg][0]);
  128.                 }
  129.             }
  130.             else
  131.             {
  132.                 // though option specifier was given, option character
  133.                 // is not alpha or was was not specified
  134.                 chOpt = -1;
  135.                 pszParam = &(argv[iArg][0]);
  136.             }
  137.         }
  138.         else
  139.         {
  140.             // standalone arg given with no option specifier
  141.             chOpt = 1;
  142.             pszParam = &(argv[iArg][0]);
  143.         }
  144.     }
  145.     else
  146.     {
  147.         // end of argument list
  148.         chOpt = 0;
  149.     }
  150.  
  151.     iArg++;
  152.     *ppszParam = pszParam;
  153.     return (chOpt);
  154. }
  155.  
  156. //////////////////////////////////////////////////////////////////////////////
  157. //
  158. //  FUNCTION: PrintLine()
  159. //
  160. //      Adds a line to an edit control, after current contents
  161. //
  162. //  PARAMETERS:
  163. //
  164. //      hwndEdit - handle to edit control
  165. //      lpstrLine - string to add
  166. //
  167. //  RETURNS: TRUE
  168. //
  169. //  COMMENTS:
  170. //
  171. //////////////////////////////////////////////////////////////////////////////
  172.  
  173. BOOL PrintLine (
  174.     HWND hwndEdit,
  175.     LPSTR lpstrLine)
  176. {
  177.     PrintStr(hwndEdit, lpstrLine);
  178.     PrintStr(hwndEdit, "\r\n");
  179.     return (TRUE);
  180. }
  181.  
  182. ///////////////////////////////////////////////////////////////////////////////
  183. //
  184. //  FUNCTION: PrintStr()
  185. //
  186. //      Adds a string to an edit control, after current contents
  187. //
  188. //  PARAMETERS:
  189. //
  190. //      hwndEdit - handle to edit control
  191. //      lpstrStr - string to add
  192. //
  193. //  RETURNS: TRUE
  194. //
  195. //  COMMENTS:
  196. //
  197. ///////////////////////////////////////////////////////////////////////////////
  198.  
  199. BOOL PrintStr (
  200.     HWND hwndEdit,
  201.     LPSTR lpstrStr)
  202. {
  203.     int iEndIndex;
  204.  
  205.     // get char index of last character in edit control
  206.     iEndIndex = Edit_GetTextLength(hwndEdit);
  207.  
  208.     // set selection to index at end of edit control text
  209.     Edit_SetSel(hwndEdit, iEndIndex, iEndIndex);
  210.  
  211.     // replace above selection with string
  212.     Edit_ReplaceSel(hwndEdit, lpstrStr);
  213.  
  214.     return (TRUE);
  215. }
  216.