⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unix_pref.cpp

📁 linux下的一款播放器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/////////////////////////////////////////////////////////////////////////////// 	call open_pref() to automatically create the correct UNIX specific //	preference object.    //CUnixPref * CUnixPref::open_pref(			const char* pCompanyName, 			const char* pProductName, 			int nProdMajorVer, 			int nProdMinorVer){    CUnixPref *cp =  new CUnixPref(	pCompanyName, 	pProductName, 	nProdMajorVer, 	nProdMinorVer);    if (!SUCCEEDED(cp->last_error())) {	delete cp;	cp = NULL;    }    return cp;}/////////////////////////////////////////////////////////////////////////////// 	Constructor NOTE: use open_pref() to create an instance of this class//CUnixPref::CUnixPref(    const char* pCompanyName,     const char* pProductName,     int nProdMajorVer,     int nProdMinorVer){       m_pPath	= NULL;    mFile	= NULL;    mFileID	= -1;    mLastError	= HXR_OK;    m_bWrite	= FALSE;    m_RootKeyName[0] = '\0';    init_pref( pCompanyName,pProductName,nProdMajorVer,nProdMinorVer);          }/////////////////////////////////////////////////////////////////////////////// 	class destructor CUnixPref::~CUnixPref (void){    commit_prefs();    HX_VECTOR_DELETE(m_pPath);}/*  delete_pref deletes the preference specified by Key from the Buffer. */	                                                                            HX_RESULT CUnixPref::delete_pref(const char* pPrefKey){       return write_pref(pPrefKey, NULL);}///////////////////////////////////////////////////////////////////////////////   init opens pref file if found, otherwise it creates it;//   reads the pref file into an internal buffer.//HX_RESULT CUnixPref::init_pref(             const char* pCompanyName,             const char* pProductName,             int nProdMajorVer,             int nProdMinorVer){    m_nMajor = nProdMajorVer;    m_nMinor = nProdMinorVer;    m_pCompany = FixupCompany(pCompanyName);    m_pProduct = pProductName;        HX_RESULT		theErr = HXR_OK;    char 					stmp[32]; /* Flawfinder: ignore */#if defined(_SUN) || defined(_HPUX)    char        rcPath[ _POSIX_PATH_MAX ]; /* Flawfinder: ignore */    CUnixPrefUtils::GetPrefPath(rcPath, PATH_MAX, pCompanyName);#else    char        rcPath[ PATH_MAX ]; /* Flawfinder: ignore */    CUnixPrefUtils::GetPrefPath(rcPath, PATH_MAX, pCompanyName);#endif#ifndef _VXWORKS#if defined(_BEOS)    SafeStrCat( rcPath, "/", sizeof(rcPath));#else    //::strcat( rcPath, "/." );    SafeStrCat( rcPath, "/", sizeof(rcPath));  //Note that the "." has been removed. Files no longuer start with "."#endif /* _BEOS */    if ( pProductName )    {	SafeStrCat( rcPath, pProductName, sizeof(rcPath));	// remove any , or space    	char * pComa = (char*) HXFindChar(rcPath, ',');    	if(pComa)    	{      	    *pComa = 0;    	}    	pComa = (char*) HXFindChar(rcPath, ' ');    	if(pComa)    	{      	    *pComa = 0;    	}	SafeStrCat( rcPath, "_", sizeof(rcPath));    }    ::sprintf(stmp, "%d_%d", nProdMajorVer,nProdMinorVer); /* Flawfinder: ignore */    SafeStrCat(rcPath, stmp, sizeof(rcPath));    m_pPath = new char[ ::strlen( rcPath ) + 1 ];    strcpy( m_pPath, rcPath ); /* Flawfinder: ignore */    // we're done now if the environment has been primed    CHXString prefTester ;    ConstructPref("ArePrefsLoaded", prefTester);    if (CIGetenv(prefTester))    {	return HXR_OK; // already loaded :)    }    // open pref file for reading and writing; if it doesn't exist    // create one.    if ( (mFile = fopen ( m_pPath, "r"))  == NULL )    {#ifdef _DEBUG	fprintf(stderr,"Can't open file: %s.\n", m_pPath);#endif    }    else    {	mFileID = fileno(mFile);    }#endif /* _VXWORKS */        IHXBuffer* buf = new CHXBuffer((UCHAR*)(new_string("1")), 2);    buf->AddRef();    write_pref(ARE_PREFS_LOADED_PREF, buf);    buf->Release();#ifndef _VXWORKS    if (!theErr && mFile > 0)    {	// Read the contents of the file into memory.	size_t lNumTotalRead = 0;	struct stat stat_buf;	char* pfile_data = 0;	int err = stat(m_pPath, &stat_buf);	if ( !err && stat_buf.st_size > 0 )	{	    pfile_data = new char [ stat_buf.st_size + 10];	    size_t  lNumRead = 1;	    while (!feof(mFile))	    {		lNumRead =  fread(pfile_data+lNumTotalRead, 1, 16000, mFile);		if (lNumRead > 0)		{		    lNumTotalRead += lNumRead;		}		else		{		    break;		}	    }            pfile_data[lNumTotalRead] = '\0';	    HX_ASSERT(lNumTotalRead <= stat_buf.st_size);	}	// Now break the data into lines delimited by newlines, and then	// break those strings up into name/value pairs.	if ( lNumTotalRead > 0 && stat_buf.st_size > 0 )	{	    char* snl = 0;	    char* seq = 0;	    char* sname= 0;	    char* svalue= 0;	    char* stmp = pfile_data;	    int name_len = 0;	    snl = strtok( stmp, "\n");	    while( snl )	    {		// now break up each line into name/value pairs...		seq = strstr( snl, "=" );		if(seq)		{		    name_len = seq-snl;		    char* keyname = new char [name_len+1];		    strncpy( keyname, snl, name_len); /* Flawfinder: ignore */		    keyname[name_len] = '\0';		    char* lwr = new char[strlen(keyname) + 1];		    strcpy(lwr, keyname); /* Flawfinder: ignore */		    seq++;		    		    // if we find in environ already, ignore		    IHXBuffer *pBuffer = NULL;		    if (HXR_OK ==read_pref(lwr, pBuffer))		    {			pBuffer->Release();		    }		    else		    {			CHXString assn;			ConstructPrefAssignment(lwr, seq, assn, FALSE);			HX_VERIFY(-1 != CIPutenv(assn));		    }		    HX_VECTOR_DELETE(keyname);		    HX_VECTOR_DELETE(lwr);		}		snl = strtok( NULL, "\n");	    }	}	HX_VECTOR_DELETE(pfile_data);    }    if ( mFile )    {	// unlock the file         ::fclose( mFile );        mFile = NULL;	mFileID = -1;    }#endif /* _VXWORKS */    m_bWrite = FALSE;      // we use write_pref internally, but we don't want to consider our                           // database to be modified at this point.    mLastError = theErr;    return theErr;      }HX_RESULT CUnixPref::remove_pref(const char* pPrefKey){    return write_pref(pPrefKey, NULL);}HX_RESULT CUnixPref::remove_indexed_pref(const char* pPrefKey){    return HXR_NOTIMPL;}//// Access a preferences file subtree by appending a string to the Root// Key.  //HX_RESULT CUnixPref::BeginSubPref(const char* szSubPref){    HX_ASSERT((strlen(m_RootKeyName) + 1 + strlen(szSubPref) + 1) <= _MAX_KEY);    // Just add sub-pref to the root key name    SafeStrCat(m_RootKeyName,"\\", _MAX_KEY);    SafeStrCat(m_RootKeyName,szSubPref, _MAX_KEY);    return HXR_OK;}HX_RESULT CUnixPref::EndSubPref(){    // Find the \ character from the end of the string    char* pSlash = HXReverseFindChar(m_RootKeyName,'\\');    // if we find the \ character we NULL terminate at this point removing the last sub-pref added    if (pSlash)    {	*pSlash = '\0';	return HXR_OK;    }    else	return HXR_FAIL;}//// Return the value of subkey number nIndex under the current m_RootKeyName.//HX_RESULT CUnixPref::GetPrefKey(UINT32 nIndex, IHXBuffer*& pBuffer){    UINT32 		i;         // index of the current subkey    POSITION		mapPosition = NULL;    CHXString		key;    HX_RESULT		result = HXR_FAIL;    CHXString prefFamily;    ConstructFamily(prefFamily);    UINT32 nPrefFamilyLen = strlen(prefFamily);        UINT32 counter = 0;        for (char **ppEnv = environ ; *ppEnv ; ++ppEnv)    {	if (!strnicmp(prefFamily, *ppEnv, nPrefFamilyLen))	{	    // make sure this isn't the head of the family	    if ('=' == (*ppEnv)[nPrefFamilyLen])		continue;	    	    if (counter == nIndex)	    {		char *value = (*ppEnv) + nPrefFamilyLen + 1;                char *ueValue = NULL;                UnescapeNewLine(value, ueValue); 		pBuffer = new CHXBuffer;		pBuffer->AddRef();		pBuffer->Set((UCHAR*)(ueValue? ueValue : value), strlen(ueValue ? ueValue : value) + 1);                HX_VECTOR_DELETE(ueValue);		return HXR_OK;	    }	    counter++;	}    }        return HXR_FAIL;}BOOLCUnixPref::EscapeNewLine(const char* pLine, char*& pTmpOutLine){    char    hexBuf[3] = {0}; /* Flawfinder: ignore */    pTmpOutLine = NULL;    char* pOutLine = NULL;    const char* pInputLine = pLine;    while (*pLine)    {	// escape \n, = and %        if (*pLine == '\n' || *pLine == '=' || *pLine == '%')        {	    if (!pOutLine)	    {	        pTmpOutLine = pOutLine = new char[strlen(pInputLine) * 3 + 1];		*pOutLine = '\0';		strncpy(pOutLine, pInputLine, pLine - pInputLine); /* Flawfinder: ignore */		pOutLine += pLine - pInputLine;	    }	    sprintf(hexBuf,"%02x", (UCHAR)*pLine); /* Flawfinder: ignore */	    *pOutLine++ = '%';	    *pOutLine++ = hexBuf[0];	    *pOutLine++ = hexBuf[1];        }	else if (pOutLine)	{	    *pOutLine++ = *pLine;	}	pLine++;    }    if (pOutLine) *pOutLine = '\0';    return (pOutLine ? TRUE : FALSE);}BOOLCUnixPref::UnescapeNewLine(const char* pLine, char*& pTmpOutLine){    char    hexBuf[3] = {0}; /* Flawfinder: ignore */    pTmpOutLine = NULL;    char* pOutLine = NULL;    const char* pInputLine = pLine;    while (*pLine)    {        if (*pLine == '%')        {	    if (!pOutLine)	    {	        pTmpOutLine = pOutLine = new char[strlen(pInputLine)+1];		*pOutLine = '\0';		strncpy(pOutLine, pInputLine, pLine - pInputLine); /* Flawfinder: ignore */		pOutLine += pLine - pInputLine;	    }            char hexBuf[3]; /* Flawfinder: ignore */	    if(pLine[1] &&    // check for overbound condition	       pLine[2])	    {	        pLine++;  // walk past '%'	        hexBuf[0] = *pLine++;	        hexBuf[1] = *pLine;	        hexBuf[2] = '\0';	        *pOutLine++ = (char)strtol(hexBuf, NULL, 16);  	    }	        }	else if (pOutLine)	{	    *pOutLine++ = *pLine;	}	pLine++;    }    if (pOutLine) *pOutLine = '\0';    return (pOutLine ? TRUE : FALSE);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -