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

📄 updatemanager.cpp

📁 FreeAMP(MP3播放)程序源代码-用来研究MP3解码
💻 CPP
📖 第 1 页 / 共 4 页
字号:

                            }while(count > 0 && IsntError(result));
                            

                            close(fd);  
                            
                            if(IsntError(result))
                            {

                                // now that we have it we need to uncompress it
                                // old dest is new src...
                                strcpy(srcPath, destPath); 
                                char* cp = strrchr(destPath, '.');

                                if(cp)
                                    *cp = 0x00;
                                                          
                                gzFile gzfd = gzopen(srcPath, "rb");

                                if (gzfd == NULL)
                                    result = kError_FileNotFound;
                                else if((fd = open(destPath, openFlags, S_IREAD | S_IWRITE)) >=0)
                                {
                                    do
                                    {
                                        count = gzread(gzfd, buffer, bufferSize);

                                        if(count > 0)
                                        {
                                            write(fd, buffer, count);
                                        }

                                    }while(count > 0);

                                    close(fd);
                                }

                                if(gzfd != NULL)
                                    gzclose(gzfd);

                                remove(srcPath);

#ifdef WIN32
                                // when the file is unzipped the modification
                                // date is fubar'd. we need to set it so it
                                // is correct if this is a DATE file.
                                if(item->GetCurrentFileTime().size())
                                {
                                    uint32 month, day, year;

                                    sscanf(item->GetCurrentFileTime().c_str(),
                                                       "%lu-%lu-%lu",&year,&month,&day);

                                    HANDLE fileHandle;
                                    
                                    fileHandle = CreateFile(destPath, 
                                                            GENERIC_READ|GENERIC_WRITE,
                                                            0,
                                                            NULL,
                                                            OPEN_EXISTING,
                                                            FILE_ATTRIBUTE_NORMAL,
                                                            NULL);

                                    if(fileHandle != INVALID_HANDLE_VALUE)
                                    {
                                        SYSTEMTIME sysTime;
                                        FILETIME fileTime, modTime;

                                        memset(&sysTime, 0x00, sizeof(SYSTEMTIME));

                                        sysTime.wDay = day;
                                        sysTime.wMonth = month;
                                        sysTime.wYear = year;
    
                                        SystemTimeToFileTime(&sysTime, &fileTime);
                                        LocalFileTimeToFileTime(&fileTime, &modTime);

                                        SetFileTime(fileHandle, NULL, NULL, &modTime);

                                        CloseHandle(fileHandle);
                                    }

                                }
#endif
                            }
                        }
                        else
                        {
                            switch(errno)
                            {
                                case EEXIST:
                                    result = kError_FileExists;
                                    break;

                                case EACCES:
                                    result = kError_FileNoAccess;
                                    break;

                                case ENOENT:
                                    result = kError_FileNotFound;
                                    break;

                                case EMFILE:
                                    result = kError_FileNoHandles;
                                    break;

                                case EINVAL:
                                    result = kError_FileInvalidArg;
                                    break;
                
                            }
                        }
                        
                        break;
                    }

                    // 3xx: Redirection - Further action must be taken in order to
                    // complete the request
                    case '3':
                    {
                        char* cp = strstr(buffer, "Location:");
                        //int32 length;

                        if(cp)
                        {
                            cp += 9;

                            if(*cp == 0x20)
                                cp++;

                            char *end;
                            for(end = cp; end < buffer + total; end++)
                                if(*end=='\r' || *end == '\n') break;

                            *end = 0x00;

                            //cout << cp << endl;

                            if(305 == returnCode) // proxy
                            {
                                char* proxy = new char[strlen(cp) + 
                                                       strlen(item->GetCurrentFileURL().c_str()) + 1];

                                sprintf(proxy, "%s%s", cp, item->GetCurrentFileURL().c_str());

                                item->SetCurrentFileURL(proxy);

                                delete [] proxy;
                            }
                            else // redirect of some type
                            {
                                item->SetCurrentFileURL(cp);
                            }

                            result = DownloadItem(item, function, cookie);
                        }
                        
                        break;
                    }

                    // 4xx: Client Error - The request contains bad syntax or cannot
                    // be fulfilled
                    case '4':
                    {
                        switch(returnCode)
                        {
                            case 400:
                                result = kError_BadHTTPRequest;
                                break;

                            case 401:
                                result = kError_AccessNotAuthorized;
                                break;                           

                            case 403:
                                result = kError_AccessForbidden;
                                break;

                            case 404:
                                result = kError_FileNotFound;
                                break;

                            default:
                                result = kError_UnknownErr;
                                break;
                        }

                        break;
                    }

                    // 5xx: Server Error - The server failed to fulfill an apparently
                    // valid request
                    case '5':
                    {
                        result = kError_UnknownServerError;
                        break;
                    }
                }

            }

            // cleanup
            if(buffer)
                free(buffer);            
        }

        // cleanup
        if(s > 0)
            closesocket(s);

        if(destPath)
            delete [] destPath;

        if(srcPath)
            delete [] srcPath;
    }

    return result;

}

// Utility Functions
bool UpdateManager::IsEmpty()
{
    bool result;
    //m_mutex.Acquire();

    result = m_itemList.empty();

    //m_mutex.Release();
    return result;
}

uint32 UpdateManager::CountItems()
{
    uint32 result;
    //m_mutex.Acquire();

    result = m_itemList.size();

    //m_mutex.Release();
    return result;
}

UpdateItem* UpdateManager::ItemAt(uint32 index)
{
    UpdateItem* result = NULL;
    //m_mutex.Acquire();
    
    index = CheckIndex(index);

    if(index != kInvalidIndex)
    {
        result = m_itemList[index];
    }
    
    //m_mutex.Release();
    return result;
}

uint32 UpdateManager::IndexOf(UpdateItem* item)
{
    uint32 result = kInvalidIndex;
    uint32 index = 0;
    uint32 size = 0;

    assert(item);

    if(item)
    {
        size = m_itemList.size();

        for(index = 0; index < size; index++)
        {
            if(item == m_itemList[index])
            {
                result = index;
                break;
            }
        }
    }
    
    return result;
}

bool UpdateManager::HasItem(UpdateItem* item)
{
    return (IndexOf(item) != kInvalidIndex);
}

// Internal functions
inline uint32 UpdateManager::CheckIndex(uint32 index)
{
	// If we're dealing with a bogus index then set it to -1.
	if(index >= CountItems())
    {
		index = kInvalidIndex;
    }

	return index;
}

// parsing code
Error UpdateManager::BeginElement(string &element, AttrMap &attrMap)
{
	m_path += string("/") + element;

	if(m_path == "/VERSIONINFO/PLATFORM")
	{
        m_versionPlatform = attrMap["NAME"];
        m_versionArchitecture = attrMap["ARCHITECTURE"];
    }

    if(m_path == "/VERSIONINFO/PLATFORM/COMPONENT" &&
       m_versionPlatform == m_currentPlatform &&
       m_versionArchitecture == m_currentArchitecture)
    {
        UpdateItem* item;
        bool foundComponent = false;

        vector<UpdateItem*>::iterator i = m_itemList.begin();

        // is there a matching component on this machine
        for (; i != m_itemList.end(); i++)
        {
            item = *i;

            if(!strcasecmp(attrMap["NAME"].c_str(), 
                           item->GetLocalFileName().c_str()))
            {
                foundComponent = true;
                break;
            }
        }

        // need to add a new component to the list
        if(!foundComponent)
        {
            item = new UpdateItem();   
            m_itemList.push_back(item);
            item->SetLocalFileName(attrMap["NAME"]);
        }

        item->SetCurrentFileLocation(attrMap["LOCATION"]);

        string url = "http://";

        // where is the latest version located?
        url += kUpdateServer;
        url += kUpdatePath;
        url += attrMap["LOCATION"];

        item->SetCurrentFileURL(url);

        item->SetCurrentFileVersion(attrMap["VERSION"]);
        item->SetFileDescription(attrMap["DESCRIPTION"]);
        item->SetCurrentFileTime(attrMap["DATE"]);

        /*if(attrMap.find("NAME") != attrMap.end())
            cout << "Name: " << attrMap["NAME"] << endl;

        if(attrMap.find("DESCRIPTION") != attrMap.end())
            cout << "Description: " << attrMap["DESCRIPTION"] << endl;

        if(attrMap.find("VERSION") != attrMap.end())
            cout << "Version: " << attrMap["VERSION"] << endl;

        if(attrMap.find("LOCATION") != attrMap.end())
            cout << "Location: " << attrMap["LOCATION"] << endl;*/
    }

    return kError_NoErr;
}

Error UpdateManager::PCData(string &data)
{
    //cout << "PCData: " << data << endl;

	return kError_NoErr;
}


Error UpdateManager::EndElement(string &element)
{
    //cout << "EndElement: " << element << endl;

	char *ptr;
    int   offset;
    
    ptr = strrchr(m_path.c_str(), '/');
    if (ptr == NULL)
       return kError_NoErr;
       
    offset = ptr - m_path.c_str();
    m_path.erase(offset, m_path.length() - offset);
     
	return kError_NoErr;
}

⌨️ 快捷键说明

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