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

📄 utility.cpp

📁 FreeAMP(MP3播放)程序源代码-用来研究MP3解码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
void ToLower(char *s)
{
    char *p;
    
    for(p = s; *p != '\0'; p++)
       *p = tolower(*p);
}      

void ReplaceSpaces(string &in, string &encoded)
{
    encoded = "";
    
    for (unsigned int i = 0; i < in.size(); i++)
    {
        if (in[i] == ' ')
            encoded += "%20";
        else if ((unsigned char)in[i] > 127)
        {
            char enc[10];
            sprintf(enc, "%%%02X", in[i] & 0xFF);
            enc[3] = 0;
            encoded += enc;
        }
        else
            encoded += in[i];
    }
}

#ifdef WIN32
#elif __BEOS__

#include <be/app/Roster.h>
#include <be/be_apps/NetPositive/NetPositive.h>

void LaunchBrowser(const char* url)
{
    status_t err;

    BMessenger netpositive(B_NETPOSITIVE_APP_SIGNATURE, -1, &err);
    if (err == B_OK)
    {
        BMessage msg(B_NETPOSITIVE_OPEN_URL);
        msg.AddString("be:url", url);
        err = netpositive.SendMessage(&msg);
        if (err < B_OK)
        {
            printf("error sending msg to netpositive: %s\n", strerror(err));
        }
    }
    else
    {
        const char *browser = "NetPositive";
        char *command = new char[strlen(browser) + strlen(url) + 10];
        sprintf(command, "%s \"%s\" &", browser, url);
        system(command);
        delete[] command;
    }
}

#else
void LaunchBrowser(const char* url)
{
    char         url2[_MAX_PATH];
    char         lockfile[255];
    int          lockfile_fd;
    struct stat  sb;
    char        *home, *browser;

    browser = new char[10];
    strcpy(browser, "netscape");

    sprintf(url2, "openURL(%s)", url);

    if (!strcmp(browser, "netscape"))
    {
        home = getenv("HOME");
        if (!home) {
            home = new char[5];
            strcpy(home, "/");
        }

        sprintf(lockfile,"%.200s/.netscape/lock",home);
        if (fork() > 0) {
            delete [] browser;
            delete [] home;
            return;
        }

        if ((lockfile_fd = lstat(lockfile, &sb))!=-1)
        {
            execlp("netscape", "netscape", "-remote", url2, NULL);
        } 
        else
        {
            execlp("netscape", "netscape", url, NULL);
        }
        perror("Could not launch netscape");
    _exit(0);
    }
    else
    {
        if (fork() > 0) {
            delete [] browser;
        return;
        }
        
        char *command = new char[strlen(browser) + strlen(url) + 10];
        sprintf(command, "%s \"%s\"", browser, url);

        system(command);

        delete [] command;
    _exit(0);
    }
}
#endif


void FindMusicFiles(const char* rootPath, 
                    vector<string>& urls, 
                    vector<string>& queries)
{
    HANDLE findFileHandle = NULL;
    WIN32_FIND_DATA findData;
    string findPath;
    string::size_type pos = string::npos;

    vector<string>::iterator query = queries.begin();

    // first run each query on this directory
    for(;query != queries.end(); query++)
    {
        findPath = rootPath;
        findPath += DIR_MARKER_STR;
        pos = findPath.size();
        findPath += *query;

        findFileHandle = FindFirstFile((char *)findPath.c_str(), &findData);

        if(findFileHandle != INVALID_HANDLE_VALUE)
        {
            do
            {
                findPath.replace(pos, 
                                 findPath.size() - pos, 
                                 findData.cFileName);
                
                char url[MAX_PATH + 8];
                uint32 length = sizeof(url);

                FilePathToURL(findPath.c_str(), url, &length);

                urls.push_back(url);

            }while(FindNextFile(findFileHandle, &findData));

            FindClose(findFileHandle);
        }
    }

    // next find all the directories in this directory and
    // and run the queries on them
#ifdef WIN32
    findPath.replace(pos, 
                     findPath.size() - pos, 
                     "*.*");
#else
    findPath.replace(pos,
                     findPath.size() - pos,
                     "*");
#endif

    findFileHandle = FindFirstFile((char *)findPath.c_str(), &findData);

    if(findFileHandle != INVALID_HANDLE_VALUE)
    {
        do
        {
            if(strcmp(findData.cFileName, ".") && 
               strcmp(findData.cFileName, ".."))
            {
                findPath.replace(pos, 
                                 findPath.size() - pos, 
                                 findData.cFileName);
                struct stat st;

                stat(findPath.c_str(), &st);

                if(st.st_mode & _S_IFDIR)
                {
                    FindMusicFiles(findPath.c_str(),
                                   urls,
                                   queries);
                }
            }

        }while(FindNextFile(findFileHandle, &findData));

        FindClose(findFileHandle);
    }
}

#ifdef WIN32
bool ResolveLink(string& path)
{
    bool result = false;
    HRESULT hres = NULL;

    hres = CoInitialize(NULL);

    if(SUCCEEDED(hres))
    {
        IShellLink* psl = NULL;

        // Get a pointer to the IShellLink interface
        hres = CoCreateInstance(CLSID_ShellLink, 
                                NULL, 
                                CLSCTX_INPROC_SERVER, 
                                IID_IShellLink, 
                                (void**)&psl);

        if(SUCCEEDED(hres))
        {
            IPersistFile* ppf;

            // Get a pointer to the IPersistFile interface
            hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);

            if(SUCCEEDED(hres))
            {
                WORD wsz[MAX_PATH];

                // Ensure string is UNICODE
                MultiByteToWideChar(CP_ACP, 
                                    0, 
                                    path.c_str(), 
                                    -1, 
                                    wsz, 
                                    MAX_PATH);

                // Load Shortcut
                hres = ppf->Load(wsz, STGM_READ);

                if(SUCCEEDED(hres))
                {
                    // Resolve the link
                    hres = psl->Resolve(NULL, SLR_ANY_MATCH|SLR_NO_UI);
            
                    if(SUCCEEDED(hres))
                    {
                        WIN32_FIND_DATA wfd;
                        char buf[MAX_PATH];

                        // Resolve the link
                        hres = psl->GetPath(buf,sizeof(buf),&wfd, 0);

                        if(SUCCEEDED(hres))
                        {
                            path = buf;
                            result = true;
                        }
                    }
                }

                // Release the IPersist Interface
                ppf->Release();
            }
    
            // Release the IShellLink Interface
            psl->Release();
        }

        CoUninitialize(); 
    }

    return result;
}

#endif

#ifndef WIN32

const unsigned iCopyBufferSize = 8192;

bool CopyFile(const char *pExistingFileName, 
              const char *pNewFileName,      // name of new file
              bool bFailIfExists)      // operation if file exists
{
    FILE  *fpDest, *fpSrc;
    char   szBuffer[iCopyBufferSize];
    unsigned iRet;

    if (bFailIfExists && !access(pNewFileName, 0))
        return false;

    fpDest = fopen(pNewFileName, "wb");
    if (fpDest == NULL)
        return false;

    fpSrc = fopen(pExistingFileName, "rb");
    if (fpSrc == NULL)
        return false;

    for(;;)
    {
        iRet = fread(szBuffer, 1, iCopyBufferSize, fpSrc);
        if ((int)iRet < 0)
        {
            fclose(fpDest);
            fclose(fpSrc);
            return false;
        }

        if (fwrite(szBuffer, 1, iRet, fpDest) != iRet)
        {
            fclose(fpDest);
            fclose(fpSrc);
            return false;
        }
        if (iRet != iCopyBufferSize)
           break; 
    }

    fclose(fpDest);
    fclose(fpSrc);

    return true;
}

#endif

string FindFile(string oPath)
{
    char *findpath, *path, *filename, *slash;
    string retvalue = oPath;

    path = new char[_MAX_PATH];
    strcpy(path, oPath.c_str());

    slash = strrchr(path, DIR_MARKER);

    if (!slash)
        return retvalue;

    slash++;
    filename = new char[strlen(slash) + 1];
    strcpy(filename, slash);
    *slash = '\0';

    findpath = new char[strlen(path) + 1];
    strcpy(findpath, path);

    strcat(findpath, "*");
#ifdef WIN32
    strcat(findpath, ".*");
#endif

    WIN32_FIND_DATA find;
    HANDLE handle;

    handle = FindFirstFile(findpath, &find);
    if (handle != INVALID_HANDLE_VALUE) {
        do {
            if (!strcasecmp(find.cFileName, filename)) {
                retvalue = string(path) + string(find.cFileName);
                break;
            }
        }
        while (FindNextFile(handle, &find));
        FindClose(handle);
    }

    delete [] findpath;
    delete [] path;
    delete [] filename;

    return retvalue;
}

bool ShowHelp(FAContext *m_context, const char *helpurl)
{
    string  oHelpFile;
    char   *dir;
    uint32  len = _MAX_PATH;

    dir = new char[_MAX_PATH];

    m_context->prefs->GetPrefString(kInstallDirPref, dir, &len);
    oHelpFile = string(dir);

    oHelpFile += string(DIR_MARKER_STR);
#ifdef WIN32
    oHelpFile += string("help");
    oHelpFile += string(DIR_MARKER_STR);
#endif
#ifdef unix
    oHelpFile += string("../share/"BRANDING_APP_NAME"/help/");
#endif
    oHelpFile += string(helpurl);

    struct _stat   st;
    if (_stat(oHelpFile.c_str(), &st) != 0 || st.st_mode & S_IFREG == 0)
    {
         delete [] dir;
         return false;
    }

    len = _MAX_PATH;
    FilePathToURL(oHelpFile.c_str(), dir, &len);

#ifdef HAVE_GTK 
    LaunchBrowser(dir);
#endif
#ifdef WIN32

    Int32PropValue *pProp;
    HWND            hWnd;
    if (IsError(m_context->props->GetProperty("MainWindow", 
                (PropValue **)&pProp)))
       hWnd = NULL;
    else
       hWnd = (HWND)pProp->GetInt32();

    ShellExecute(hWnd, "open", dir, NULL, NULL, SW_SHOWNORMAL);
#endif
    delete [] dir;

    return true;
} 

bool GetProxySettings(FAContext *context, string &server, unsigned short &port)
{
    bool   useProxy;
    int32  numFields;
    uint32 length;
    char   proxyname[256], hostname[256];

    context->prefs->GetPrefBoolean(kUseProxyPref, &useProxy);

    length = sizeof(proxyname);
    context->prefs->GetPrefString(kProxyHostPref, proxyname, &length);

    if(useProxy)
    {
        numFields = sscanf(proxyname,
                           "http://%[^:/]:%hu", hostname, &port);
        if (numFields > 0)
        {
            server = string(hostname);
            if (numFields == 1)
               port = 80;

            return true;
        }
    }

    return false;
}

⌨️ 快捷键说明

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