📄 hxinternetconfigutils.cpp
字号:
/* ***** BEGIN LICENSE BLOCK *****
* Version: RCSL 1.0/RPSL 1.0
*
* Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file, are
* subject to the current version of the RealNetworks Public Source License
* Version 1.0 (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the RealNetworks Community Source License Version 1.0
* (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
* in which case the RCSL will apply. You may also obtain the license terms
* directly from RealNetworks. You may not use this file except in
* compliance with the RPSL or, if you have a valid RCSL with RealNetworks
* applicable to this file, the RCSL. Please see the applicable RPSL or
* RCSL for the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the portions
* it created.
*
* This file, and the files included with this file, is distributed and made
* available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
#include "hxtypes.h"
#include "hxinternetconfigutils.h"
#include "hx_moreprocesses.h" // for GetCurrentAppSignature
#include "string.h"
#ifndef _MAC_MACHO
#include <Processes.h>
#endif
#include "hxstrutl.h"
#ifndef _CARBON
#define ICAttr long
#endif
ICInstance HXInternetConfigMac::m_icInstance = NULL;
UInt32 HXInternetConfigMac::m_refcount = 0;
#ifdef _CARBON
const ICAttr kNoAttrChange = kICAttrNoChange;
#else
const ICAttr kNoAttrChange = ICattr_no_change;
#endif
static void ConcatPString(StringPtr mainStr, ConstStr255Param suffix)
{
short len1 = mainStr[0];
short len2 = suffix[0];
if (len1 + len2 > 255)
{
len2 = 255 - len1;
}
BlockMoveData(&suffix[1], &mainStr[len1 + 1], len2);
mainStr[0] += len2;
}
static void CopyPString(ConstStr255Param source, StringPtr dest)
{
BlockMoveData(source, dest, 1 + source[0]);
}
static Boolean PStringInString(ConstStr255Param searchStr, ConstStr255Param biggerStr)
{
char csearch[256]; /* Flawfinder: ignore */
char cbigger[256]; /* Flawfinder: ignore */
Boolean bFound;
CopyP2CString(searchStr, csearch, 256);
CopyP2CString(biggerStr, cbigger, 256);
bFound = (NULL != strstr(cbigger, csearch));
return bFound;
}
Boolean HXInternetConfigMac::StartUsingIC(void)
{
Boolean bResult;
bResult = false;
if (!ICStart)
{
// we didn't link to InternetConfig
return false;
}
// are there other instances already?
if (m_refcount > 0)
{
m_refcount++;
bResult = true;
}
else
{
// make a new instance
OSStatus status;
if (m_icInstance) return true;
// start internet config
status = ICStart(&m_icInstance, GetCurrentAppSignature());
if (status == noErr)
{
#ifndef _CARBON
status = ICFindConfigFile(m_icInstance, 0, NULL);
#endif
}
else
{
(void) ICStop(m_icInstance);
}
if (status == noErr)
{
// IC is started now, so increase the refcount
m_refcount++;
bResult = true;
}
}
return bResult;
}
void HXInternetConfigMac::StopUsingIC(void)
{
// are there outstanding instances besides us?
if (m_refcount > 1)
{
m_refcount--;
}
else if (m_refcount == 1)
{
// we're the last instance, so close our connection
// to the component
(void) ICStop(m_icInstance);
m_icInstance = NULL;
m_refcount--;
}
}
Boolean HXInternetConfigMac::StartReadingICPrefs(void)
{
OSStatus status;
Boolean bResult;
bResult = false;
if (StartUsingIC())
{
status = ICBegin(m_icInstance, icReadOnlyPerm);
if (status == noErr)
{
bResult = true;
}
}
return bResult;
}
Boolean HXInternetConfigMac::StartWritingICPrefs(void)
{
OSStatus status;
Boolean bResult;
bResult = false;
if (StartUsingIC())
{
status = ICBegin(m_icInstance, icReadWritePerm);
if (status == noErr)
{
bResult = true;
}
}
return bResult;
}
void HXInternetConfigMac::StopUsingICPrefs(void)
{
(void) ICEnd(m_icInstance);
StopUsingIC();
}
// IsInternetConfigAvailable initializes internet config if necessary
// and returns our connection
Boolean HXInternetConfigMac::IsInternetConfigAvailable(void)
{
if (StartUsingIC())
{
StopUsingIC();
return true;
}
return false;
}
// GetInstance initializes InternetConfig, if necessary, and returns
// our instance of the IC component
ICInstance HXInternetConfigMac::GetInstance(void)
{
return m_icInstance;
}
// GetFileTypeFromName returns the appropriate file type for
// a given file name, or else zero if none is found
OSType HXInternetConfigMac::GetFileTypeFromName(ConstStr255Param fileName)
{
ICMapEntry mapEntry;
OSStatus status;
OSType result;
if (!StartUsingIC()) return 0;
result = (OSType) 0;
status = ICMapFilename(m_icInstance, fileName, &mapEntry);
if (status == noErr)
{
#ifdef _CARBON
result = mapEntry.fileType;
#else
result = mapEntry.file_type;
#endif
}
StopUsingIC();
return result;
}
// GetFileCreatorFromName returns the appropriate file creator for
// a given file name, or else zero if none is found
OSType HXInternetConfigMac::GetFileCreatorFromName(ConstStr255Param fileName)
{
ICMapEntry mapEntry;
OSStatus status;
OSType result;
if (!StartUsingIC()) return 0;
result = (OSType) 0;
status = ICMapFilename(m_icInstance, fileName, &mapEntry);
if (status == noErr)
{
#ifdef _CARBON
result = mapEntry.fileCreator;
#else
result = mapEntry.file_creator;
#endif
}
StopUsingIC();
return result;
}
// GetFileCreatorFromName returns the appropriate file creator for
// a given file name, or else zero if none is found
OSErr HXInternetConfigMac::GetFileExtensionFromType(OSType fType, OSType fCreator, ConstStr255Param fileName, StringPtr extension)
{
ICMapEntry mapEntry;
OSStatus status;
if (!StartUsingIC()) return icInternalErr;
status = ICMapTypeCreator(m_icInstance, fType, fCreator, fileName, &mapEntry);
if (status == noErr)
{
// copy the extension (pascal string)
CopyPString(mapEntry.extension, extension);
}
StopUsingIC();
return (OSErr) status;
}
// LaunchURL launches the URL pointed to by the c-string
OSErr HXInternetConfigMac::LaunchURL(const char *url)
{
OSStatus status;
if (!StartUsingIC()) return icInternalErr;
long startSel;
long endSel;
startSel = 0;
endSel = strlen(url);
status = ICLaunchURL(m_icInstance, "\p", (char *) url,
strlen(url), &startSel, &endSel);
StopUsingIC();
return (OSErr) status;
}
// GetFTPHelper gets the signature and name of the ftp helper app.
// Either parameter can be nil if the result isn't desired.
OSErr HXInternetConfigMac::GetFTPHelper(OSType *signature, StringPtr helperName)
{
OSStatus status;
if (!StartUsingIC()) return icInternalErr;
long dataSize;
ICAppSpec targetSpec;
ICAttr icAttr;
// construct our key string for getting the ftp helper by concatenating
// ftp to the standard pascal helper prefix
#define kICHelperFTP kICHelper "ftp"
dataSize = sizeof(ICAppSpec);
// get the preference information for the ftp helper
status = ICGetPref(m_icInstance, kICHelperFTP, &icAttr, (Ptr) &targetSpec, &dataSize);
if (status == noErr)
{
if (signature) *signature = targetSpec.fCreator;
if (helperName) CopyPString(targetSpec.name, helperName);
}
StopUsingIC();
return (OSErr) status;
}
// GetHTTPHelper gets the signature and name of the http helper app.
// Either parameter can be nil if the result isn't desired.
OSErr HXInternetConfigMac::GetHTTPHelper(OSType *signature, StringPtr helperName)
{
OSStatus status;
if (!StartUsingIC()) return icInternalErr;
long dataSize;
ICAppSpec targetSpec;
ICAttr icAttr;
// construct our key string for getting the ftp helper by concatenating
// ftp to the standard pascal helper prefix
#define kICHelperHTTP kICHelper "http"
dataSize = sizeof(ICAppSpec);
// get the preference information for the ftp helper
status = ICGetPref(m_icInstance, kICHelperHTTP, &icAttr, (Ptr) &targetSpec, &dataSize);
if (status == noErr)
{
if (signature) *signature = targetSpec.fCreator;
if (helperName) CopyPString(targetSpec.name, helperName);
}
StopUsingIC();
return (OSErr) status;
}
// GetEmailAddress returns user's email address
// returns FALSE if not found.
BOOL HXInternetConfigMac::GetEmailAddress(CHXString& strEmail)
{
return GetICPreferenceString(kICEmail, strEmail);
}
// GetICPreferenceBoolean gets an IC preference that is a simple string
// returns FALSE if not found
// keys are in InternetConfig.h
BOOL HXInternetConfigMac::GetICPreferenceBoolean( ConstStr255Param keyPascalString, BOOL& outBool )
{
OSStatus status;
BOOL bSuccess = FALSE;
outBool = FALSE;
if (StartUsingIC())
{
long dataSize = sizeof(Boolean);
ICAttr icAttr;
Boolean icBool;
status = ICGetPref(m_icInstance, keyPascalString, &icAttr, (char*) &icBool, &dataSize);
bSuccess = (status == noErr && dataSize == sizeof(Boolean));
if (bSuccess)
{
outBool = (icBool ? TRUE : FALSE);
}
StopUsingIC();
}
return bSuccess;
}
// GetICPreference gets an IC preference that is a simple string
// returns FALSE if not found
// keys are in InternetConfig.h
BOOL HXInternetConfigMac::GetICPreferenceString(ConstStr255Param keyPascalString, CHXString& strResult)
{
OSStatus status;
BOOL bSuccess = FALSE;
strResult.Empty();
if (StartUsingIC())
{
long textSize = 255;
ICAttr icAttr;
Str255 bufferStr;
status = ICGetPref(m_icInstance, keyPascalString, &icAttr, (char*) bufferStr, &textSize);
bSuccess = (status == noErr && textSize <= 255);
if (bSuccess)
{
strResult.SetFromStr255(bufferStr);
}
StopUsingIC();
}
return bSuccess;
}
// GetICPreferenceStringList returns a STR#-style IC preference
// returns FALSE if not found
// keys are in InternetConfig.h
// the list is returns with items separated by the outputSeparator character
BOOL HXInternetConfigMac::GetICPreferenceStringList(ConstStr255Param keyPascalString, char outputSeparator, CHXString& strResult)
{
OSStatus status;
BOOL bSuccess = FALSE;
strResult.Empty();
if (StartUsingIC())
{
const int kBuffSize = 1024;
long dataSize = kBuffSize;
ICAttr icAttr;
char buffer[kBuffSize]; /* Flawfinder: ignore */
status = ICGetPref(m_icInstance, keyPascalString, &icAttr, (char*) buffer, &dataSize);
bSuccess = (status == noErr && dataSize <= kBuffSize && dataSize >= sizeof(short));
if (bSuccess)
{
// buffer is a short number of strings, followed by Pascal strings
//
// combine them into the output CHXString, separated by outputSeparator characters
short numItems = *(short *) buffer;
StringPtr pCurrentStr = (StringPtr) &buffer[2];
CHXString strTemp;
for (int idx = 0; idx < numItems; idx++)
{
strTemp.SetFromStr255(pCurrentStr);
if ((!strTemp.IsEmpty()) && (!strResult.IsEmpty()))
{
strResult += outputSeparator;
}
strResult += strTemp;
pCurrentStr += (1 + pCurrentStr[0]);
}
}
StopUsingIC();
}
return bSuccess;
}
// SetProtocol
const unsigned char kHelper[] = kICHelper; // "\pHelper
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -