Example #1
0
/* extract CSV items using undocumented 'AfxExtractSubString()' to speed up (maybe) */
bool	CCsvFile::GetItem(CStringW &item, const CStringW &line, WORD &index)
{
	CStringW last_item;
	bool	ret = false;

	last_item.Empty();
	if(!AfxExtractSubString(item,line,index,','))
		return false;

	if(item.Left(1) == _T("\""))
	{
		do
		{
			if(item.Right(1) == _T("\""))
			{
				if(!last_item.IsEmpty())
					item = last_item + item;
				item.Trim(_T("\""));
				ret = true;
				break;
			}
			last_item += item + _T(",");
		}
		while(AfxExtractSubString(item,line,++index,','));
	}
	else
		ret = true;
	return ret;
}
Example #2
0
void IniFile::Parse(LPCVOID data, size_t size)
{
    LPCWSTR begin = (LPCWSTR)data;
    LPCWSTR end = begin + size / sizeof(*begin);

    if (*begin == 0xFEFF)
        ++begin;

    Section* currentSection = nullptr;
    CString* varToAppend = nullptr;

    while (1)
    {
        const wchar_t* eol = GetEol(begin, end);
        if (begin >= eol)
            break;

        CStringW line(begin, int(eol - begin));
        begin = eol + 1;

        line.Trim(L" \t\r\n");

        if (varToAppend)
        {
            bool appendNext = line.Right(1) == L"\\";
            if (appendNext)
                line.Delete(line.GetLength() - 1);
            *varToAppend += line;
            if (!appendNext)
                varToAppend = nullptr;
            continue;
        }

        if (line.IsEmpty() || line[0] == L';')
            continue;

        if (line[0] == L'[' && line[line.GetLength() - 1] == L']')
        {
            currentSection = &m_sections[line.Trim(L"[]")];
            continue;
        }

        if (!currentSection)
            continue;

        int equalPos = line.Find(L'=');
        if (equalPos == -1 || equalPos == 0)
            continue;
        CStringW var = line.Left(equalPos);
        CStringW text = line.Mid(equalPos + 1);
        if (text.Right(1) == L"\\")
        {
            text.Delete(text.GetLength() - 1);
            varToAppend = &(*currentSection)[var];
        }

        (*currentSection)[var] = text;
    }
}
Example #3
0
BOOL CFCacheImpl::AddFile(
    LPCWSTR lpFilePath,
    ULONGLONG qwFileSize
    )
{
    BOOL retval = FALSE;
    int nRetCode;
    char* szError = NULL;
    CStringA strSql;
    CStringW strFilePath;
    CStringW strExt;
    int nExt;

    if (!m_pDbConnect)
        goto clean0;

    if (!lpFilePath)
        goto clean0;

    strFilePath = lpFilePath;
    strFilePath.MakeLower();

    nExt = strFilePath.ReverseFind(_T('.'));
    if (nExt != -1 && nExt > strFilePath.ReverseFind(_T('\\')))
    {
        strExt = strFilePath.Right(strFilePath.GetLength() - nExt);
    }

    if (strExt.IsEmpty())
    {
        strExt = _T(".n/a");
    }

    strSql.Format("insert into files values('%s', '%s', %I64d)",
                  KUTF16_To_UTF8(strFilePath),
                  KUTF16_To_UTF8(strExt),
                  qwFileSize);
    nRetCode = sqlite3_exec(m_pDbConnect, strSql, NULL, NULL, &szError);
    if (nRetCode)
        goto clean0;

    retval = TRUE;

clean0:
    return retval;
}
Example #4
0
BOOL CFCacheImpl::DelFile(LPCWSTR lpFilePath)
{
    BOOL retval = FALSE;
    int nRetCode;
    char* szError = NULL;
    CStringA strSql;
    CStringW strFilePath;
    int nExt;
    CStringW strExt;
    sqlite3_stmt* pStmt = NULL;
    ULONGLONG qwSize;

    if (!m_pDbConnect)
        goto clean0;

    if (!lpFilePath)
        goto clean0;

    strFilePath = lpFilePath;
    strFilePath.MakeLower();

    nExt = strFilePath.ReverseFind(_T('.'));
    if (nExt != -1 && nExt > strFilePath.ReverseFind(_T('\\')))
    {
        strExt = strFilePath.Right(strFilePath.GetLength() - nExt);
    }

    if (strExt.IsEmpty())
    {
        strExt = _T(".n/a");
    }

    // 获得大小
    strSql.Format("select size from files where path = '%s'", KUTF16_To_UTF8(strFilePath));
    nRetCode = sqlite3_prepare(m_pDbConnect, strSql, -1, &pStmt, 0);
    if (nRetCode)
        goto clean0;

    nRetCode = sqlite3_step(pStmt);
    if (SQLITE_ROW != nRetCode)
        goto clean0;

    qwSize = sqlite3_column_int64(pStmt, 0);

    // 删除文件
    strSql.Format("delete from files where path = '%s'", KUTF16_To_UTF8(strFilePath));
    nRetCode = sqlite3_exec(m_pDbConnect, strSql, NULL, NULL, &szError);
    if (nRetCode)
        goto clean0;

    // 从Top100中删除
    strSql.Format("delete from top100 where path = '%s'", KUTF16_To_UTF8(strFilePath));
    nRetCode = sqlite3_exec(m_pDbConnect, strSql, NULL, NULL, &szError);

    // 从Exts中去除大小
    strSql.Format("insert into exts values('%s', -%I64d, -1)", KUTF16_To_UTF8(strExt), qwSize);
    nRetCode = sqlite3_exec(m_pDbConnect, strSql, NULL, NULL, &szError);

    // 从总大小中去除大小
    strSql.Format("insert into info values(-%I64d, -1)", qwSize);
    nRetCode = sqlite3_exec(m_pDbConnect, strSql, NULL, NULL, &szError);

    retval = TRUE;

clean0:
    if (pStmt)
    {
        sqlite3_finalize(pStmt);
        pStmt = NULL;
    }

    return retval;
}
Example #5
0
HRESULT ProfileInfo::Read() {
    HRESULT hr = S_OK;

    LFile profileFile(m_csFilename);

    if (!profileFile.Exists())
        hr = E_PM_FILE_NOTEXIST;

    if (SUCCEEDED(hr)) {
        LURESULT lr = profileFile.Open();
        if (lr == LFILE_ERR_OPEN || lr == LFILE_ERR_ALREADY_OPEN)
            hr = E_PM_FILE_OPEN;
    }

    if (SUCCEEDED(hr)) {
        LBuffer fileBuffer(256);

        DWORD dwBytesRead;
        CStringW csLine;
        UINT uiLinesRead = 0;

        // Read BOM (first 2 bytes)
        LURESULT lr = profileFile.ReadRaw(&fileBuffer, 0, 2, &dwBytesRead);
        if (lr != S_OK)
            hr = E_PM_FILE_READ;


        bool bFirstBlock = true;
        if (SUCCEEDED(hr)) {
            do {
                lr = profileFile.ReadRaw(&fileBuffer, 0, fileBuffer.GetSize(), &dwBytesRead);
                if (lr != S_OK)
                    hr = E_PM_FILE_READ;

                if (SUCCEEDED(hr) && bFirstBlock) {
                    WCHAR *pIdent = (WCHAR *)fileBuffer.GetBuffer();
                    if (wcsncmp(pIdent, L"lpp_", 4) != 0)
                        hr = E_PM_WRONG_FORMAT;
                    bFirstBlock = false;
                }

                if (SUCCEEDED(hr)) {
                    WCHAR *pBuffer = (WCHAR *)fileBuffer.GetBuffer();
                    UINT dwCharsRead = dwBytesRead / sizeof(WCHAR);
                    for (int i = 0; i <  dwCharsRead; ++i) {
                        if (pBuffer[i] == L'\n') {
                            CString csKey;
                            CString csValue;
                            //Ignore lines which begins with % or #
                            if (csLine[0] != L'%' && csLine[0] != L'#') {
                                int iBlankPos = csLine.Find(L'=');
                                if (iBlankPos >= 0) {
#ifdef _UNICODE
                                    csKey = csLine.Left(iBlankPos);
                                    csValue = csLine.Right(csLine.GetLength() - (iBlankPos+1));
#else
                                    CStringW csKeyW = csLine.Left(iBlankPos);
                                    int nLen = csKeyW.GetLength();
                                    char *szRet = new char[nLen + 1];
                                    WideCharToMultiByte(CP_ACP, 0, csKeyW, -1, 
                                        szRet, nLen + 1, NULL, NULL);
                                    csKey = szRet;
                                    delete szRet;

                                    CStringW csValueW = csLine.Right(csLine.GetLength() - (iBlankPos+1)); 
                                    nLen = csValueW.GetLength();
                                    szRet = new char[nLen + 1];
                                    WideCharToMultiByte(CP_ACP, 0, csValueW, -1, 
                                        szRet, nLen + 1, NULL, NULL);
                                    csValue = szRet;
                                    delete szRet;
#endif
                                    if (csKey == _T("lpp_id")) {
                                        m_iProfileID = _ttoi64((LPCTSTR)csValue);
                                    } else if (csKey == _T("lpp_version")) {
                                        m_iProfileVersion = _ttoi((LPCTSTR)csValue);
                                    } else if (csKey == _T("lpp_title")) {
                                        m_csTitle = csValue;
                                    } else if (csKey == _T("lpp_type")) {
                                        m_iProfileType = _ttoi((LPCTSTR)csValue);
                                    } else {
                                        m_aKeys.Add(csKey);
                                        m_aValues.Add(csValue);
                                    }
                                }
                            }
                            csLine.Empty();
                        } else {
                            csLine += pBuffer[i];
                        }
                    }
                }

            } while (dwBytesRead == fileBuffer.GetSize());
        }

        profileFile.Close();
    }

    ExtractTargetFormat();
    ExtractStorageDistribution();

    return hr;
}