home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / devtools / samples / ODBC / loaddata / script.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-20  |  8.7 KB  |  315 lines

  1. // Script.cpp
  2. //
  3. // This file is part of Microsoft SQL Server online documentation.
  4. // Copyright (C) 1992-1997 Microsoft Corporation. All rights reserved.
  5. //
  6. // This source code is an intended supplement to the Microsoft SQL
  7. // Server online references and related electronic documentation.
  8. #include "stdafx.h"
  9.  
  10. #include "Script.h"
  11.  
  12. // Macros
  13. #define EOTEXTF             0x1a
  14. #define CMD_SCRIPTRUN       "ScriptRun"
  15. #define CB_CMD_SCRIPTRUN    sizeof(CMD_SCRIPTRUN) - 1
  16. #define CMD_LOADDATA        "LoadData"
  17. #define CB_CMD_LOADDATA     sizeof(CMD_LOADDATA) - 1
  18.  
  19. static void FormatProgressText(PTSTR pFormatted, PSTR pUnformatted);
  20.  
  21. /////////////////////////////////////////////////////////////////////////////
  22. // GetNextCmd -- GetNextCmd gets the next line from a command (.cmd) file.
  23. //  The function examines the returned line to see if it's a recognized 
  24. //  command and returns the token for the command line. 
  25. //
  26. // Returns:  PROGRESSTEXT, SCRIPTRUN, LOADDATA, BADCOMMAND or EOF
  27. //
  28. // The caller is responsible for freeing the string pointer returned in 
  29. //  ppCurLine. This function (and the called GetNextLine) will destroy 
  30. //  the ppCommands pointer received by moving it forward through the 
  31. //  command string. If the pointer was dynamically allocated, then the 
  32. //  caller should hold onto the original pointer or memory corruption 
  33. //  will probably result.
  34. int GetNextCmd
  35.     (
  36.     PSTR* ppCommands,
  37.     PTSTR* ppCurLine
  38.     )
  39.     {
  40.     PSTR        pCmdLine;
  41.     PTSTR       pArgs;
  42.     int         iToken = BADCOMMAND;
  43.  
  44.     if (GetNextLine(ppCommands, &pCmdLine) == EOF)
  45.         {
  46.         *ppCurLine = NULL;
  47.         return (EOF);
  48.         }
  49.     else
  50.         {
  51.         pArgs = new TCHAR[strlen(pCmdLine) + 1];
  52.  
  53.         switch (*pCmdLine)
  54.             {
  55.             case ('s'):
  56.             case ('S'):
  57.                 {
  58.                 if (strnicmp(pCmdLine, CMD_SCRIPTRUN, CB_CMD_SCRIPTRUN) == 0)
  59.                     {
  60. #ifdef _UNICODE
  61.                     MultiByteToWideChar(CP_ACP, 0, pCmdLine + sizeof(CMD_SCRIPTRUN),
  62.                         -1, pArgs, strlen(pCmdLine));
  63. #else
  64.                     strcpy(pArgs, pCmdLine + sizeof(CMD_SCRIPTRUN));
  65. #endif
  66.                     iToken = SCRIPTRUN;
  67.                     }
  68.                 break;
  69.                 }
  70.  
  71.             case ('l'):
  72.             case ('L'):
  73.                 {
  74.                 if (strnicmp(pCmdLine, CMD_LOADDATA, CB_CMD_LOADDATA) == 0)
  75.                     {
  76. #ifdef _UNICODE
  77.                     MultiByteToWideChar(CP_ACP, 0, pCmdLine + CB_CMD_LOADDATA,
  78.                         -1, pArgs, strlen(pCmdLine));
  79. #else
  80.                     strcpy(pArgs, pCmdLine + CB_CMD_LOADDATA);
  81. #endif
  82.                     iToken = LOADDATA;
  83.                     }
  84.  
  85.                 break;
  86.                 }
  87.  
  88.             case ('['):
  89.                 {
  90.                 FormatProgressText(pArgs, pCmdLine);
  91.                 iToken = PROGRESSTEXT;
  92.                 break;
  93.                 }
  94.  
  95.             default:    // Bad command, return first word.
  96.                 {
  97.                 PTSTR   pTrim = pArgs;
  98.  
  99. #ifdef _UNICODE
  100.                 MultiByteToWideChar(CP_ACP, 0, pCmdLine, -1, pArgs,
  101.                     strlen(pCmdLine));
  102. #else
  103.                 strcpy(pArgs, pCmdLine);
  104. #endif
  105.                 while (*pTrim && !_istspace(*pTrim))
  106.                     pTrim++;
  107.  
  108.                 *pTrim = (TCHAR) NULL;
  109.                 }
  110.             }
  111.         }
  112.  
  113.     delete [] pCmdLine;
  114.     *ppCurLine = pArgs;
  115.  
  116.     return (iToken);
  117.     }
  118.  
  119. /////////////////////////////////////////////////////////////////////////////
  120. // GetNextLine --  GetNextLine walks a .cmd file yanking out a length of 
  121. //  text.
  122. //
  123. // Returns:  EOF when the end of the string is reached. 0 otherwise.
  124. //
  125. // The caller is responsible for freeing the string pointer returned in 
  126. //  ppCmd. This function will destroy the ppCmdString pointer received 
  127. //  (by moving it forward through the command string). If the pointer was 
  128. //  dynamically allocated, then the caller should hold onto the original 
  129. //  pointer or memory corruption will probably result.
  130. int GetNextLine
  131.     (
  132.     PSTR* ppCmdString,
  133.     PSTR* ppCmd
  134.     )
  135.     {
  136.     PSTR        pCmdString = *ppCmdString;
  137.     PSTR        pCmd;
  138.     UINT        cbCmd = 0;
  139.     int         nRet = 0;
  140.     char        c;
  141.     UINT        cbCmdString = strlen(pCmdString);
  142.  
  143.     *ppCmd = NULL;
  144.  
  145.     if (cbCmdString == 0)
  146.         {
  147.         return (EOF);
  148.         }
  149.  
  150.     pCmd  = new char[cbCmdString + 1];
  151.     *ppCmd = pCmd;
  152.  
  153.     while ((c = *pCmdString++) != (char) NULL)
  154.         {
  155.         if (isspace(c))
  156.             {
  157.             if (cbCmd != 0)
  158.                 {
  159.                 if (c == '\n')          // Respect LF as line terminator.
  160.                     {
  161.                     *pCmd = (char) NULL;
  162.                     *ppCmdString = pCmdString;
  163.                     return (0);
  164.                     }
  165.  
  166.                 if (c != '\r')          // Throw away CR in CRLF text files.
  167.                     {
  168.                     *pCmd++ = c;
  169.                     cbCmd++;
  170.                     }
  171.                 }
  172.             }
  173.         else
  174.             {
  175.             if (c == '/')
  176.                 {
  177.                 if (*pCmdString == '/')
  178.                     {
  179.                     pCmdString++;
  180.                     while ((c = *pCmdString++) != (char) NULL)
  181.                         {
  182.                         if (c == '\n')
  183.                             {
  184.                             pCmdString--;
  185.                             break;
  186.                             }
  187.                         }
  188.                     }
  189.                 else if (*pCmdString == '*')
  190.                     {
  191.                     pCmdString++;
  192.                     while ((c = *pCmdString++) != (char) NULL)
  193.                         {
  194.                         if (c == '*' && *pCmdString == '/')
  195.                             {
  196.                             pCmdString++;
  197.                             break;
  198.                             }
  199.                         }
  200.                     }
  201.                 else 
  202.                     {
  203.                     *pCmd++ = c;
  204.                     cbCmd++;
  205.                     }
  206.                 }
  207.             else if (c == EOTEXTF)
  208.                 {
  209.                 if (cbCmd == 0)
  210.                     {
  211.                     delete [] pCmd;
  212.                     *ppCmd = NULL;
  213.                     return (EOF);
  214.                     }
  215.                 else
  216.                     {
  217.                     *pCmd = (char) NULL;
  218.                     *ppCmdString = pCmdString;
  219.                     return (0);
  220.                     }
  221.                 }
  222.             else 
  223.                 {
  224.                 *pCmd++ = c;
  225.                 cbCmd++;
  226.                 }
  227.             }
  228.         }
  229.  
  230.     if (cbCmd == 0)
  231.         {
  232.         delete [] pCmd;
  233.         *ppCmd = NULL;
  234.         return (EOF);
  235.         }
  236.  
  237.     *pCmd = (char) NULL;
  238.     *ppCmdString = pCmdString;
  239.     return (0);
  240.     }
  241.  
  242. /////////////////////////////////////////////////////////////////////////////
  243. // TrimQuotes -- Trim leading and trailing white-space and quotes from a
  244. //  character string. Returns the length of the trimmed string.
  245. UINT TrimQuotes
  246.     (
  247.     PTSTR pUnquotedString,
  248.     PTSTR pQuotedString
  249.     )
  250.     {
  251.     UINT    cbString;
  252.     PTSTR   pFirstValid = pQuotedString;
  253.  
  254.     while (*pFirstValid && 
  255.         (_istspace(*pFirstValid) || *pFirstValid == _T('"')))
  256.         {
  257.         pFirstValid++;
  258.         }
  259.  
  260.     cbString = _tcslen(pFirstValid);
  261.     if (!cbString)
  262.         {
  263.         return (cbString);
  264.         }
  265.  
  266.     if (pUnquotedString == pQuotedString)
  267.         {
  268.         memmove(pUnquotedString, pFirstValid, cbString * sizeof(TCHAR));
  269.         }
  270.     else
  271.         {
  272.         _tcscpy(pUnquotedString, pFirstValid);
  273.         }
  274.  
  275.     pFirstValid = pUnquotedString;
  276.     while (*pFirstValid && (*pFirstValid != _T('"')))
  277.         {
  278.         pFirstValid++;
  279.         }
  280.  
  281.     *pFirstValid = (TCHAR) NULL;
  282.     return (_tcslen(pUnquotedString));
  283.     }
  284.  
  285. /////////////////////////////////////////////////////////////////////////////
  286. // FormatProgressText -- Trims progress text strings for display.
  287. void FormatProgressText
  288.     (
  289.     PTSTR pFormatted,
  290.     PSTR pUnformatted
  291.     )
  292.     {
  293.     PSTR    pFirstValid = pUnformatted;
  294. #ifdef _UNICODE
  295.     UINT    cbUnformatted = strlen(pUnformatted);
  296. #endif
  297.  
  298.     while (*pFirstValid && (*pFirstValid == '['))
  299.         {
  300.         pFirstValid++;
  301.         }
  302.  
  303. #ifdef _UNICODE
  304.     MultiByteToWideChar(CP_ACP, 0, pFirstValid, -1, pFormatted, cbUnformatted);
  305. #else
  306.     strcpy(pFormatted, pFirstValid);
  307. #endif
  308.  
  309.     while (*pFormatted && (*pFormatted != _T(']')))
  310.         {
  311.         pFormatted++;
  312.         }
  313.     *pFormatted = (TCHAR) NULL;
  314.     }
  315.