glib/glibbackend/src/getProgPath.cpp
author Pat Downey <patd@symbian.org>
Fri, 04 Jun 2010 16:20:51 +0100
changeset 31 ce057bb09d0b
child 45 4b03adbd26ca
permissions -rw-r--r--
Revert last code drop.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     1
/*
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     3
* All rights reserved.
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     4
* This component and the accompanying materials are made available
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     6
* which accompanies this distribution, and is available
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     8
*
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
     9
* Initial Contributors:
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    11
*
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    12
* Contributors:
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    13
*
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    14
* Description: 
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    15
*
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    16
*/
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    17
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    18
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    19
#include "glibbackend.h"
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    20
#include <e32std.h>
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    21
#include <string.h>
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    22
#include <stdlib.h>
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    23
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    24
extern "C" EXPORT_C char *getProgPath(char *progName)
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    25
{
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    26
	TText16 iName16[KMaxFileName];
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    27
	char *progPath;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    28
	int pathLength;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    29
	RProcess iProcess;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    30
	char replacePath[KMaxFileName];
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    31
	
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    32
	/* replace / with // */
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    33
	strcpy(replacePath,progName);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    34
	char *temp = strchr(replacePath,'/');
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    35
	while (temp)
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    36
	{
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    37
		*temp = '\\';
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    38
		temp = strchr(replacePath,'/');
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    39
	} // end while
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    40
	
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    41
	//convert narrow chars to wide chars
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    42
	mbstowcs((wchar_t *)iName16, replacePath, strlen(replacePath)+1);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    43
	
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    44
	//create TDesC to pass to RProcess.Create()
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    45
	TBuf16<KMaxFileName> iCmdName16(iName16);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    46
	_LIT(KArg,"");
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    47
	
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    48
	// Check if Create if successful. If not successful
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    49
	// the executable by that name does not exits. Hence,
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    50
	// return NULL. 
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    51
	if(iProcess.Create(iCmdName16,KArg))
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    52
		progPath = NULL;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    53
	else
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    54
	{
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    55
		// If successful, convert from TDesC to narrow chars and 
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    56
		// return.
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    57
		TFileName fileName = iProcess.FileName();
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    58
		iProcess.Kill(0);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    59
		iProcess.Close();
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    60
		
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    61
		pathLength = fileName.Length();
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    62
		
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    63
		progPath = (char *)malloc(pathLength + 1);
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    64
		
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    65
		if(progPath == NULL)
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    66
			return NULL;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    67
		
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    68
		for(int i=0;i < pathLength;i++)
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    69
		{
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    70
			progPath[i] = fileName[i];
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    71
		} // end for 
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    72
		
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    73
		progPath[pathLength] = '\0';
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    74
	
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    75
	} // end if
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    76
	return progPath;
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    77
}
ce057bb09d0b Revert last code drop.
Pat Downey <patd@symbian.org>
parents:
diff changeset
    78