home *** CD-ROM | disk | FTP | other *** search
/ PC Administrator / spravce.iso / RunModule / src / CommandLine.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-31  |  3.7 KB  |  108 lines

  1. // CommandLine.cpp: implementation of the CCommandLine class.
  2. //
  3. /*
  4. Copyright 2001 Anish Mistry. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8.  
  9.    1. Redistributions of source code must retain the above copyright notice, 
  10.    this list of conditions and the following disclaimer.
  11.    2. Redistributions in binary form must reproduce the above copyright notice,
  12.    this list of conditions and the following disclaimer in the documentation 
  13.    and/or other materials provided with the distribution.
  14.  
  15. THIS SOFTWARE IS PROVIDED BY ANISH MISTRY ``AS IS'' AND ANY EXPRESS OR IMPLIED 
  16. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  17. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS 
  18. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  19. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  20. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  22. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.  
  25. The views and conclusions contained in the software and documentation are those
  26. of the authors and should not be interpreted as representing official policies,
  27. either expressed or implied, of Anish Mistry or AM Productions.
  28.  
  29. * Variation of the FreeBSD License. http://www.freebsd.org/copyright/freebsd-license.html
  30. */
  31. //////////////////////////////////////////////////////////////////////
  32.  
  33. #include "stdafx.h"
  34. #include "CommandLine.h"
  35.  
  36. //////////////////////////////////////////////////////////////////////
  37. // Construction/Destruction
  38. //////////////////////////////////////////////////////////////////////
  39.  
  40. CCommandLine::CCommandLine()
  41. {
  42.     m_nCurrentIndex = 0;
  43. }
  44.  
  45. CCommandLine::~CCommandLine()
  46. {
  47.  
  48. }
  49.  
  50. void CCommandLine::SetCommandLine(const char *pCmdLine)
  51. {// begin SetCommandLine
  52.     // set command line string
  53.     m_pCmdLine = pCmdLine;
  54.     // set default params
  55.     CCommandLine();
  56.  
  57. }// end SetCommandLine
  58.  
  59. char CCommandLine::GetNextSwitch()
  60. {// begin GetNextSwitch
  61.     for(int i = m_nCurrentIndex;m_pCmdLine[i] != '\0';i++)
  62.     {// begin move to the first switch
  63.         for(;m_pCmdLine[i] != '/';i++)
  64.             if(m_pCmdLine[i] == '\0')    // invalid command line
  65.                 return NULL;
  66.         if(m_pCmdLine[i] == '/')
  67.         {// begin parse command line
  68.             // move to the switch option
  69.             i++;
  70.             // create temporay character
  71.             char option = m_pCmdLine[i];
  72.             // make lowercase
  73.             CharLower(&option);
  74.             // advance index
  75.             i++;
  76.             m_nCurrentIndex = i;
  77.             return option;
  78.         }// end parse command line
  79.     }// end move to the first switch
  80.     return NULL;
  81. }// end GetNextSwitch
  82.  
  83. const char * CCommandLine::GetNextString(char *pBuffer)
  84. {// begin GetNextString
  85.     bool bIsQuoted = false;
  86.     char *pSourceString = (char *)(void *)&m_pCmdLine[m_nCurrentIndex];
  87.     // advance to the beginning of the string
  88.     for(;*pSourceString != '\0' && *pSourceString == ' ';pSourceString++);
  89.     // advance to the next character
  90. //    pSourceString++;
  91.     // get the length of the string
  92.     if(pSourceString[0] == '"')
  93.     {
  94.         // flag quoted string
  95.         bIsQuoted = true;
  96.         // advance to the next character
  97.         pSourceString++;
  98.     }
  99.     for(int i = 0;pSourceString[i] != '\0' && !(bIsQuoted && pSourceString[i] == '"') && !(!bIsQuoted && pSourceString[i] == ' ');i++);
  100.     // advance to next character
  101.     i++;
  102.     // copy the string into the buffer
  103.     lstrcpyn(pBuffer,pSourceString,i);
  104.     // adjust the current string index
  105.     m_nCurrentIndex += i;
  106.     return pBuffer;
  107. }// end GetNextString
  108.