static sal_Bool lookupProfile()

in main/sal/osl/w32/profile.cxx [2244:2646]


static sal_Bool lookupProfile(const sal_Unicode *strPath, const sal_Unicode *strFile, sal_Unicode *strProfile)
{
	sal_Char *pChr, *pStr;
	sal_Char Buffer[4096] = "";
	sal_Char Product[132] = "";

    ::osl::LongPathBuffer< sal_Unicode > aPath( MAX_LONG_PATH );
    aPath[0] = 0;
	DWORD dwPathLen = 0;

	if (*strPath == L'"')
	{
		int i = 0;

		strPath++;

		while ((strPath[i] != L'"') && (strPath[i] != L'\0'))
			i++;

		WideCharToMultiByte(CP_ACP,0, reinterpret_cast<LPCWSTR>(strPath), i, Product, sizeof(Product), NULL, NULL);
		Product[i] = '\0';
		strPath += i;

		if (*strPath == L'"')
			strPath++;

		if ( (*strPath == L'/') || (*strPath == L'\\') )
		{
			strPath++;
		}
	}

	else
	{
		/* if we have not product identfication, do a special handling for soffice.ini */
		if (rtl_ustr_ascii_compare(strFile, SVERSION_PROFILE) == 0)
		{
			rtl_uString * strSVProfile  = NULL;
			rtl_uString * strSVFallback = NULL;
			rtl_uString * strSVLocation = NULL;
			rtl_uString * strSVName     = NULL;
            ::osl::LongPathBuffer< sal_Char > aDir( MAX_LONG_PATH );
			oslProfile hProfile;

			rtl_uString_newFromAscii(&strSVFallback, SVERSION_FALLBACK);
			rtl_uString_newFromAscii(&strSVLocation, SVERSION_LOCATION);
			rtl_uString_newFromAscii(&strSVName, SVERSION_NAME);

			/* open sversion.ini in the system directory, and try to locate the entry
			   with the highest version for StarOffice */
			if (osl_getProfileName( strSVFallback, strSVName, &strSVProfile))
            {
				hProfile = osl_openProfile(strSVProfile, osl_Profile_READLOCK);
                if (hProfile)
                {
                    osl_getProfileSectionEntries(
                        hProfile, SVERSION_SECTION, Buffer, sizeof(Buffer));

                    for (pChr = Buffer; *pChr != '\0'; pChr += strlen(pChr) + 1)
                    {
                        if ((strnicmp(
                                 pChr, SVERSION_SOFFICE,
                                 sizeof(SVERSION_SOFFICE) - 1)
                             == 0)
                            && (stricmp(Product, pChr) < 0))
                        {
                            osl_readProfileString(
                                hProfile, SVERSION_SECTION, pChr, aDir,
                                aDir.getBufSizeInSymbols(), "");

                            /* check for existence of path */
                            if (access(aDir, 0) >= 0)
                                strcpy(Product, pChr);
                        }
                    }

                    osl_closeProfile(hProfile);
                }
				rtl_uString_release(strSVProfile);
				strSVProfile = NULL;
			}

			/* open sversion.ini in the users directory, and try to locate the entry
			   with the highest version for StarOffice */
			if ((strcmp(SVERSION_LOCATION, SVERSION_FALLBACK) != 0) &&
			    (osl_getProfileName(strSVLocation, strSVName, &strSVProfile)))
            {
				hProfile = osl_openProfile(strSVProfile, osl_Profile_READLOCK);
                if (hProfile)
                {
                    osl_getProfileSectionEntries(
                        hProfile, SVERSION_SECTION, Buffer, sizeof(Buffer));

                    for (pChr = Buffer; *pChr != '\0'; pChr += strlen(pChr) + 1)
                    {
                        if ((strnicmp(
                                 pChr, SVERSION_SOFFICE,
                                 sizeof(SVERSION_SOFFICE) - 1)
                             == 0)
                            && (stricmp(Product, pChr) < 0))
                        {
                            osl_readProfileString(
                                hProfile, SVERSION_SECTION, pChr, aDir,
                                aDir.getBufSizeInSymbols(), "");

                            /* check for existence of path */
                            if (access(aDir, 0) >= 0)
                                strcpy(Product, pChr);
                        }
                    }

                    osl_closeProfile(hProfile);
                }
				rtl_uString_release(strSVProfile);
			}

			rtl_uString_release(strSVFallback);
			rtl_uString_release(strSVLocation);
			rtl_uString_release(strSVName);

			/* remove any trailing build number */
			if ((pChr = strrchr(Product, '/')) != NULL)
				*pChr = '\0';
		}
	}

	/* if we have an userid option eg. "-userid:rh[/usr/home/rh/staroffice]",
	   this will supercede all other locations */
	{
		sal_uInt32 n, nArgs = osl_getCommandArgCount();

		for (n = 0; n < nArgs; n++)
		{
			rtl_uString * strCommandArg = NULL;

			if ((osl_getCommandArg( n, &strCommandArg ) == osl_Process_E_None) &&
				((strCommandArg->buffer[0] == L'-') || (strCommandArg->buffer[0] == L'+')) &&
				(rtl_ustr_ascii_compare_WithLength(strCommandArg->buffer, RTL_CONSTASCII_LENGTH(SVERSION_OPTION), SVERSION_OPTION)))
			{
				sal_Unicode *pCommandArg = strCommandArg->buffer + RTL_CONSTASCII_LENGTH(SVERSION_OPTION);
				sal_Int32 nStart, nEnd;

				if (((nStart = rtl_ustr_indexOfChar(pCommandArg, L'[')) != -1) &&
					((nEnd = rtl_ustr_indexOfChar(pCommandArg + nStart + 1, L']')) != -1))
				{
					dwPathLen = nEnd;
				    copy_ustr_n(aPath, pCommandArg + nStart + 1, dwPathLen);
					aPath[dwPathLen] = 0;

					/* build full path */
					if ((aPath[dwPathLen - 1] != L'/') && (aPath[dwPathLen - 1] != L'\\'))
					{
						copy_ustr_n(aPath + dwPathLen++, L"/", 2);
					}

					if (*strPath)
					{
						copy_ustr_n(aPath + dwPathLen, strPath, rtl_ustr_getLength(strPath)+1);
						dwPathLen += rtl_ustr_getLength(strPath);
					}
					else
					{
                        ::osl::LongPathBuffer< sal_Char > aTmpPath( MAX_LONG_PATH );
						int n;

						if ((n = WideCharToMultiByte(CP_ACP,0, ::osl::mingw_reinterpret_cast<LPCWSTR>(aPath), -1, aTmpPath, aTmpPath.getBufSizeInSymbols(), NULL, NULL)) > 0)
						{
							strcpy(aTmpPath + n, SVERSION_USER);
							if (access(aTmpPath, 0) >= 0)
							{
								dwPathLen += MultiByteToWideChar( CP_ACP, 0, SVERSION_USER, -1, reinterpret_cast<LPWSTR>(aPath + dwPathLen), aPath.getBufSizeInSymbols() - dwPathLen );
							}
						}
					}

					break;
				}
			}
		}
	}


	if (dwPathLen == 0)
	{
		rtl_uString * strExecutable = NULL;
		rtl_uString * strTmp = NULL;
		sal_Int32 nPos;

		/* try to find the file in the directory of the executbale */
		if (osl_getExecutableFile(&strTmp) != osl_Process_E_None)
			return (sal_False);

		/* convert to native path */
		if (osl_getSystemPathFromFileURL(strTmp, &strExecutable) != osl_File_E_None)
		{
			rtl_uString_release(strTmp);
			return sal_False;
		}

		rtl_uString_release(strTmp);

		/* separate path from filename */
		if ((nPos = rtl_ustr_lastIndexOfChar(strExecutable->buffer, L'\\')) == -1)
		{
			if ((nPos = rtl_ustr_lastIndexOfChar(strExecutable->buffer, L':')) == -1)
			{
				return sal_False;
			}
			else
			{
                copy_ustr_n(aPath, strExecutable->buffer, nPos);
                aPath[nPos] = 0;
                dwPathLen = nPos;
			}
		}
		else
		{
            copy_ustr_n(aPath, strExecutable->buffer, nPos);
            dwPathLen = nPos;
			aPath[dwPathLen] = 0;
		}

		/* if we have no product identification use the executable file name */
		if (*Product == 0)
		{
			WideCharToMultiByte(CP_ACP,0, reinterpret_cast<LPCWSTR>(strExecutable->buffer + nPos + 1), -1, Product, sizeof(Product), NULL, NULL);

			/* remove extension */
			if ((pChr = strrchr(Product, '.')) != NULL)
				*pChr = '\0';
		}

		rtl_uString_release(strExecutable);

		/* remember last subdir */
		nPos = rtl_ustr_lastIndexOfChar(aPath, L'\\');

		copy_ustr_n(aPath + dwPathLen++, L"\\", 2);

		if (*strPath)
		{
			copy_ustr_n(aPath + dwPathLen, strPath, rtl_ustr_getLength(strPath)+1);
			dwPathLen += rtl_ustr_getLength(strPath);
		}

		{
            ::osl::LongPathBuffer< sal_Char > aTmpPath( MAX_LONG_PATH );

			WideCharToMultiByte(CP_ACP,0, ::osl::mingw_reinterpret_cast<LPCWSTR>(aPath), -1, aTmpPath, aTmpPath.getBufSizeInSymbols(), NULL, NULL);

			/* if file not exists, remove any specified subdirectories
			   like "bin" or "program" */

			if (((access(aTmpPath, 0) < 0) && (nPos != -1)) || (*strPath == 0))
			{
				static sal_Char *SubDirs[] = SVERSION_DIRS;

				int i = 0;
				pStr = aTmpPath + nPos;

				for (i = 0; i < (sizeof(SubDirs) / sizeof(SubDirs[0])); i++)
					if (strnicmp(pStr + 1, SubDirs[i], strlen(SubDirs[i])) == 0)
					{
						if ( *strPath == 0)
						{
							strcpy(pStr + 1,SVERSION_USER);
							if ( access(aTmpPath, 0) < 0 )
							{
								*(pStr+1)='\0';
							}
							else
							{
                                dwPathLen = nPos + MultiByteToWideChar( CP_ACP, 0, SVERSION_USER, -1, reinterpret_cast<LPWSTR>(aPath + nPos + 1), aPath.getBufSizeInSymbols() - (nPos + 1) );
							}
						}
						else
						{
							copy_ustr_n(aPath + nPos + 1, strPath, rtl_ustr_getLength(strPath)+1);
							dwPathLen = nPos + 1 + rtl_ustr_getLength(strPath);
						}

						break;
					}
			}
		}

		if ((aPath[dwPathLen - 1] != L'/') && (aPath[dwPathLen - 1] != L'\\'))
		{
            aPath[dwPathLen++] = L'\\';
			aPath[dwPathLen] = 0;
		}

		copy_ustr_n(aPath + dwPathLen, strFile, rtl_ustr_getLength(strFile)+1);

		{
            ::osl::LongPathBuffer< sal_Char > aTmpPath( MAX_LONG_PATH );

			WideCharToMultiByte(CP_ACP,0, ::osl::mingw_reinterpret_cast<LPCWSTR>(aPath), -1, aTmpPath, aTmpPath.getBufSizeInSymbols(), NULL, NULL);

			if ((access(aTmpPath, 0) < 0) && (strlen(Product) > 0))
			{
				rtl_uString * strSVFallback = NULL;
				rtl_uString * strSVProfile  = NULL;
				rtl_uString * strSVLocation = NULL;
				rtl_uString * strSVName     = NULL;
				oslProfile hProfile;

				rtl_uString_newFromAscii(&strSVFallback, SVERSION_FALLBACK);
				rtl_uString_newFromAscii(&strSVLocation, SVERSION_LOCATION);
				rtl_uString_newFromAscii(&strSVName, SVERSION_NAME);

				/* open sversion.ini in the system directory, and try to locate the entry
				   with the highest version for StarOffice */
				if (osl_getProfileName(strSVLocation, strSVName, &strSVProfile))
                {
					hProfile = osl_openProfile(
                        strSVProfile, osl_Profile_READLOCK);
                    if (hProfile)
                    {
                        osl_readProfileString(
                            hProfile, SVERSION_SECTION, Product, Buffer,
                            sizeof(Buffer), "");
                        osl_closeProfile(hProfile);

                        /* if not found, try the fallback */
                        if ((strlen(Buffer) <= 0)
                            && (strcmp(SVERSION_LOCATION, SVERSION_FALLBACK)
                                != 0))
                        {
                            if (osl_getProfileName(
                                    strSVFallback, strSVName, &strSVProfile))
                            {
                                hProfile = osl_openProfile(
                                    strSVProfile, osl_Profile_READLOCK);
                                if (hProfile)
                                {
                                    osl_readProfileString(
                                        hProfile, SVERSION_SECTION, Product,
                                        Buffer, sizeof(Buffer), "");
                                }
                            }

                            osl_closeProfile(hProfile);
                        }

                        if (strlen(Buffer) > 0)
                        {
                            dwPathLen = MultiByteToWideChar(
                                CP_ACP, 0, Buffer, -1, ::osl::mingw_reinterpret_cast<LPWSTR>(aPath), aPath.getBufSizeInSymbols() );
                            dwPathLen -=1;

                            /* build full path */
                            if ((aPath[dwPathLen - 1] != L'/')
                                && (aPath[dwPathLen - 1] != L'\\'))
                            {
                                copy_ustr_n(aPath + dwPathLen++, L"\\", 2);
                            }

                            if (*strPath)
                            {
                                copy_ustr_n(aPath + dwPathLen, strPath, rtl_ustr_getLength(strPath)+1);
                                dwPathLen += rtl_ustr_getLength(strPath);
                            }
                            else
                            {
                                ::osl::LongPathBuffer< sal_Char > aTmpPath( MAX_LONG_PATH );
                                int n;

                                if ((n = WideCharToMultiByte(
                                         CP_ACP,0, ::osl::mingw_reinterpret_cast<LPCWSTR>(aPath), -1, aTmpPath,
                                         aTmpPath.getBufSizeInSymbols(), NULL, NULL))
                                    > 0)
                                {
                                    strcpy(aTmpPath + n, SVERSION_USER);
                                    if (access(aTmpPath, 0) >= 0)
                                    {
                                        dwPathLen += MultiByteToWideChar(
                                            CP_ACP, 0, SVERSION_USER, -1,
                                            reinterpret_cast<LPWSTR>(aPath + dwPathLen),
                                            aPath.getBufSizeInSymbols() - dwPathLen );
                                    }
                                }
                            }
                        }
                    }

					rtl_uString_release(strSVProfile);
				}

				rtl_uString_release(strSVFallback);
				rtl_uString_release(strSVLocation);
				rtl_uString_release(strSVName);
			}
		}

		aPath[dwPathLen] = 0;
	}

	/* copy filename */
	copy_ustr_n(strProfile, aPath, dwPathLen+1);

	return sal_True;
}