Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version
Go to top

XGetopt - A Unix-compatible getopt() for MFC and Win32

, 20 May 2003
XGetopt provides drop-in Unix-style command line processing for Win32 and MFC applications
//==========================================
// LIBCTINY - Matt Pietrek 2001
// MSDN Magazine, January 2001
//==========================================


// NOTE:  THIS CODE HAS BEEN MODIFIED FOR THIS DEMO APP

// The original source for the LIBCTINY library may be
// found here:  www.wheaty.net


#include "stdafx.h"
#include "argcargv.h"

#define _MAX_CMD_LINE_ARGS  128

TCHAR * _ppszArgv[_MAX_CMD_LINE_ARGS+1];

int __cdecl _ConvertCommandLineToArgcArgv(LPCTSTR lpszSysCmdLine)
{
	if ( lpszSysCmdLine == NULL || lpszSysCmdLine[0] == 0 )
		return 0;

	static LPVOID pHeap = NULL;

	// Set to no argv elements, in case we have to bail out
	_ppszArgv[0] = 0;

	// First get a pointer to the system's version of the command line, and
	// figure out how long it is.
	int cbCmdLine = lstrlen( lpszSysCmdLine );

	// Allocate memory to store a copy of the command line.  We'll modify
	// this copy, rather than the original command line.  Yes, this memory
	// currently doesn't explicitly get freed, but it goes away when the
	// process terminates.
	if (pHeap)					// in case we are called multiple times
	{
		HeapFree( GetProcessHeap(), 0, pHeap );
		pHeap = NULL;
	}

	pHeap = HeapAlloc( GetProcessHeap(), 0, cbCmdLine*sizeof(TCHAR)+16 );

	if ( !pHeap )
		return 0;

	LPTSTR pszCmdLine = (LPTSTR) pHeap;

	// Copy the system version of the command line into our copy
	lstrcpy( pszCmdLine, lpszSysCmdLine );

	if ( _T('"') == *pszCmdLine )   // If command line starts with a quote ("),
	{							// it's a quoted filename.	Skip to next quote.
		pszCmdLine++;

		_ppszArgv[0] = pszCmdLine;	// argv[0] == executable name

		while ( *pszCmdLine && (*pszCmdLine != _T('"')) )
			pszCmdLine++;

		if ( *pszCmdLine )		// Did we see a non-NULL ending?
			*pszCmdLine++ = _T('\0');	// Null terminate and advance to next char
		else
			return 0;			// Oops!  We didn't see the end quote
	}
	else	// A regular (non-quoted) filename
	{
		_ppszArgv[0] = pszCmdLine;	// argv[0] == executable name

		while ( *pszCmdLine && (_T(' ') != *pszCmdLine) && (_T('\t') != *pszCmdLine) )
			pszCmdLine++;

		if ( *pszCmdLine )
			*pszCmdLine++ = _T('\0');	// Null terminate and advance to next char
	}

	// Done processing argv[0] (i.e., the executable name).  Now do th
	// actual arguments

	int argc = 1;

	while ( 1 )
	{
		// Skip over any whitespace
		while ( *pszCmdLine && (_T(' ') == *pszCmdLine) || (_T('\t') == *pszCmdLine) )
			pszCmdLine++;

		if ( _T('\0') == *pszCmdLine ) // End of command line???
			return argc;

		if ( _T('"') == *pszCmdLine )   // Argument starting with a quote???
		{
			pszCmdLine++;	// Advance past quote character

			_ppszArgv[ argc++ ] = pszCmdLine;
			_ppszArgv[ argc ] = 0;

			// Scan to end quote, or NULL terminator
			while ( *pszCmdLine && (*pszCmdLine != _T('"')) )
				pszCmdLine++;

			if ( _T('\0') == *pszCmdLine )
				return argc;

			if ( *pszCmdLine )
				*pszCmdLine++ = _T('\0');	// Null terminate and advance to next char
		}
		else						// Non-quoted argument
		{
			_ppszArgv[ argc++ ] = pszCmdLine;
			_ppszArgv[ argc ] = 0;

			// Skip till whitespace or NULL terminator
			while ( *pszCmdLine && (_T(' ') != *pszCmdLine) && (_T('\t') != *pszCmdLine) )
				pszCmdLine++;

			if ( _T('\0') == *pszCmdLine )
				return argc;

			if ( *pszCmdLine )
				*pszCmdLine++ = _T('\0');	// Null terminate and advance to next char
		}

		if ( argc >= (_MAX_CMD_LINE_ARGS) )
			return argc;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.
 
Recently, I have moved to Los Angeles where I am doing consulting and development work.
 
For consulting and custom software development, please see www.hdsoft.org.






| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 21 May 2003
Article Copyright 2002 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid