home *** CD-ROM | disk | FTP | other *** search
- // Script.cpp
- //
- // This file is part of Microsoft SQL Server online documentation.
- // Copyright (C) 1992-1997 Microsoft Corporation. All rights reserved.
- //
- // This source code is an intended supplement to the Microsoft SQL
- // Server online references and related electronic documentation.
- #include "stdafx.h"
-
- #include "Script.h"
-
- // Macros
- #define EOTEXTF 0x1a
- #define CMD_SCRIPTRUN "ScriptRun"
- #define CB_CMD_SCRIPTRUN sizeof(CMD_SCRIPTRUN) - 1
- #define CMD_LOADDATA "LoadData"
- #define CB_CMD_LOADDATA sizeof(CMD_LOADDATA) - 1
-
- static void FormatProgressText(PTSTR pFormatted, PSTR pUnformatted);
-
- /////////////////////////////////////////////////////////////////////////////
- // GetNextCmd -- GetNextCmd gets the next line from a command (.cmd) file.
- // The function examines the returned line to see if it's a recognized
- // command and returns the token for the command line.
- //
- // Returns: PROGRESSTEXT, SCRIPTRUN, LOADDATA, BADCOMMAND or EOF
- //
- // The caller is responsible for freeing the string pointer returned in
- // ppCurLine. This function (and the called GetNextLine) will destroy
- // the ppCommands pointer received by moving it forward through the
- // command string. If the pointer was dynamically allocated, then the
- // caller should hold onto the original pointer or memory corruption
- // will probably result.
- int GetNextCmd
- (
- PSTR* ppCommands,
- PTSTR* ppCurLine
- )
- {
- PSTR pCmdLine;
- PTSTR pArgs;
- int iToken = BADCOMMAND;
-
- if (GetNextLine(ppCommands, &pCmdLine) == EOF)
- {
- *ppCurLine = NULL;
- return (EOF);
- }
- else
- {
- pArgs = new TCHAR[strlen(pCmdLine) + 1];
-
- switch (*pCmdLine)
- {
- case ('s'):
- case ('S'):
- {
- if (strnicmp(pCmdLine, CMD_SCRIPTRUN, CB_CMD_SCRIPTRUN) == 0)
- {
- #ifdef _UNICODE
- MultiByteToWideChar(CP_ACP, 0, pCmdLine + sizeof(CMD_SCRIPTRUN),
- -1, pArgs, strlen(pCmdLine));
- #else
- strcpy(pArgs, pCmdLine + sizeof(CMD_SCRIPTRUN));
- #endif
- iToken = SCRIPTRUN;
- }
- break;
- }
-
- case ('l'):
- case ('L'):
- {
- if (strnicmp(pCmdLine, CMD_LOADDATA, CB_CMD_LOADDATA) == 0)
- {
- #ifdef _UNICODE
- MultiByteToWideChar(CP_ACP, 0, pCmdLine + CB_CMD_LOADDATA,
- -1, pArgs, strlen(pCmdLine));
- #else
- strcpy(pArgs, pCmdLine + CB_CMD_LOADDATA);
- #endif
- iToken = LOADDATA;
- }
-
- break;
- }
-
- case ('['):
- {
- FormatProgressText(pArgs, pCmdLine);
- iToken = PROGRESSTEXT;
- break;
- }
-
- default: // Bad command, return first word.
- {
- PTSTR pTrim = pArgs;
-
- #ifdef _UNICODE
- MultiByteToWideChar(CP_ACP, 0, pCmdLine, -1, pArgs,
- strlen(pCmdLine));
- #else
- strcpy(pArgs, pCmdLine);
- #endif
- while (*pTrim && !_istspace(*pTrim))
- pTrim++;
-
- *pTrim = (TCHAR) NULL;
- }
- }
- }
-
- delete [] pCmdLine;
- *ppCurLine = pArgs;
-
- return (iToken);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // GetNextLine -- GetNextLine walks a .cmd file yanking out a length of
- // text.
- //
- // Returns: EOF when the end of the string is reached. 0 otherwise.
- //
- // The caller is responsible for freeing the string pointer returned in
- // ppCmd. This function will destroy the ppCmdString pointer received
- // (by moving it forward through the command string). If the pointer was
- // dynamically allocated, then the caller should hold onto the original
- // pointer or memory corruption will probably result.
- int GetNextLine
- (
- PSTR* ppCmdString,
- PSTR* ppCmd
- )
- {
- PSTR pCmdString = *ppCmdString;
- PSTR pCmd;
- UINT cbCmd = 0;
- int nRet = 0;
- char c;
- UINT cbCmdString = strlen(pCmdString);
-
- *ppCmd = NULL;
-
- if (cbCmdString == 0)
- {
- return (EOF);
- }
-
- pCmd = new char[cbCmdString + 1];
- *ppCmd = pCmd;
-
- while ((c = *pCmdString++) != (char) NULL)
- {
- if (isspace(c))
- {
- if (cbCmd != 0)
- {
- if (c == '\n') // Respect LF as line terminator.
- {
- *pCmd = (char) NULL;
- *ppCmdString = pCmdString;
- return (0);
- }
-
- if (c != '\r') // Throw away CR in CRLF text files.
- {
- *pCmd++ = c;
- cbCmd++;
- }
- }
- }
- else
- {
- if (c == '/')
- {
- if (*pCmdString == '/')
- {
- pCmdString++;
- while ((c = *pCmdString++) != (char) NULL)
- {
- if (c == '\n')
- {
- pCmdString--;
- break;
- }
- }
- }
- else if (*pCmdString == '*')
- {
- pCmdString++;
- while ((c = *pCmdString++) != (char) NULL)
- {
- if (c == '*' && *pCmdString == '/')
- {
- pCmdString++;
- break;
- }
- }
- }
- else
- {
- *pCmd++ = c;
- cbCmd++;
- }
- }
- else if (c == EOTEXTF)
- {
- if (cbCmd == 0)
- {
- delete [] pCmd;
- *ppCmd = NULL;
- return (EOF);
- }
- else
- {
- *pCmd = (char) NULL;
- *ppCmdString = pCmdString;
- return (0);
- }
- }
- else
- {
- *pCmd++ = c;
- cbCmd++;
- }
- }
- }
-
- if (cbCmd == 0)
- {
- delete [] pCmd;
- *ppCmd = NULL;
- return (EOF);
- }
-
- *pCmd = (char) NULL;
- *ppCmdString = pCmdString;
- return (0);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // TrimQuotes -- Trim leading and trailing white-space and quotes from a
- // character string. Returns the length of the trimmed string.
- UINT TrimQuotes
- (
- PTSTR pUnquotedString,
- PTSTR pQuotedString
- )
- {
- UINT cbString;
- PTSTR pFirstValid = pQuotedString;
-
- while (*pFirstValid &&
- (_istspace(*pFirstValid) || *pFirstValid == _T('"')))
- {
- pFirstValid++;
- }
-
- cbString = _tcslen(pFirstValid);
- if (!cbString)
- {
- return (cbString);
- }
-
- if (pUnquotedString == pQuotedString)
- {
- memmove(pUnquotedString, pFirstValid, cbString * sizeof(TCHAR));
- }
- else
- {
- _tcscpy(pUnquotedString, pFirstValid);
- }
-
- pFirstValid = pUnquotedString;
- while (*pFirstValid && (*pFirstValid != _T('"')))
- {
- pFirstValid++;
- }
-
- *pFirstValid = (TCHAR) NULL;
- return (_tcslen(pUnquotedString));
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // FormatProgressText -- Trims progress text strings for display.
- void FormatProgressText
- (
- PTSTR pFormatted,
- PSTR pUnformatted
- )
- {
- PSTR pFirstValid = pUnformatted;
- #ifdef _UNICODE
- UINT cbUnformatted = strlen(pUnformatted);
- #endif
-
- while (*pFirstValid && (*pFirstValid == '['))
- {
- pFirstValid++;
- }
-
- #ifdef _UNICODE
- MultiByteToWideChar(CP_ACP, 0, pFirstValid, -1, pFormatted, cbUnformatted);
- #else
- strcpy(pFormatted, pFirstValid);
- #endif
-
- while (*pFormatted && (*pFormatted != _T(']')))
- {
- pFormatted++;
- }
- *pFormatted = (TCHAR) NULL;
- }
-