0

Does anybody knows why it is not extracting the mp3 from the path. szFile is a complete path to the file. Is there a more secure way to convert the data type ?

I am new to coding in C++ and I do not know hot to manage well the data types conversion, I am currently using Visual Studio 2019 for my project

    Node* Load(LPCWSTR szFile, Node* Song, HWND hWnd, std::string Album_, std::string Name, int a)
    {
        
        try
        {
            std::wstring ws(szFile);
            std::string filename = std::string(ws.begin(), ws.end());
            std::regex re("[a-zA-Z0-9]*.mp3");
            std::smatch match;
            std::regex_match (filename, match, re);
            Song->Name = match.str(1);
            if (a == 0)
            {
                if (Album_ != "")
                {
                    std::string SQL_base_albumes = "INSERT INTO ALBUMES VALUES";
                    SQL_base_albumes += " " + Album_;
                    char* Error;
                    connection = sqlite3_exec(Database, SQL_base_albumes.c_str(), NULL, 0, &Error);
                    if (connection != SQLITE_OK)
                    {
                        std::string str(Error);
                        std::wstring Conversion = std::wstring(str.begin(), str.end());
                        LPCWSTR Convertido = Conversion.c_str();
                        ::MessageBox(hWnd, Convertido, L"Error | Carga de MP3", NULL);
                    }
                }
            }

            Cleanup(Song);
            Song->ready = false;

            if (SUCCEEDED(CoCreateInstance(CLSID_FilterGraph,
                NULL,
                CLSCTX_INPROC_SERVER,
                IID_IGraphBuilder,
                (void**)&Song->Graphs)))
            {
                Song->Graphs->QueryInterface(IID_IMediaControl, (void**)&Song->Controls);
                Song->Graphs->QueryInterface(IID_IMediaEventEx, (void**)&Song->Events);
                Song->Graphs->QueryInterface(IID_IBasicAudio, (void**)&Song->Basic);
                Song->Graphs->QueryInterface(IID_IMediaSeeking, (void**)&Song->Seeking);
                HRESULT Graphics = Song->Graphs->RenderFile(szFile, NULL);

                if (SUCCEEDED(Graphics))
                {
                    Song->ready = true;
                    if (Song->Seeking)
                    {
                        Song->Seeking->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME);
                        Song->Seeking->GetDuration(&Song->duration);

                        if (a == 0)
                        {
                            std::string SQL_base_canciones = "INSERT INTO MUSICA VALUES";
                            Song->Album_p = Album_;
                            SQL_base_canciones += "('" + Song->Name + "','" + Album_ + "');";
                            char* Error_;
                            connection = sqlite3_exec(Database, SQL_base_canciones.c_str(), NULL, 0, &Error_);
                            if (connection != SQLITE_OK)
                            {
                                std::string str(Error_);
                                std::wstring Conversion = std::wstring(str.begin(), str.end());
                                LPCWSTR Convertido = Conversion.c_str();
                                ::MessageBox(hWnd, Convertido, L"Error | Carga de MP3", NULL);
                                return NULL;
                            }
                        }
                    }
                    else
                    {
                        ::MessageBox(hWnd, L"No ha sido posible cargar y establecer los tiempos de su MP3", L"Error | Carga de MP3", NULL);
                    }
                }
                else
                {
                    ::MessageBox(hWnd, L"No ha sido posible abrir el archivo seleccionado", L"Error | Carga de MP3", NULL);
                }
            }
            else
            {
                ::MessageBox(hWnd, L"No ha sido posible cargar su archivo.", L"Error | Carga de MP3", NULL);
            }
            return Song;
        }
        catch(...)
        {
            return NULL;
        }

    }
Miguel Estrada
  • 31
  • 1
  • 1
  • 8
  • 1
    Your regular expression doesn't have any capture groups. Which part of the match do you expect `match.str(1)` to extract, and why? – Igor Tandetnik Nov 08 '20 at 14:07
  • I am trying to extract the filename from the path, for example 'Hola.mp3' from the whole path. – Miguel Estrada Nov 08 '20 at 14:20
  • 1
    You probably want `match.str(0)`; index 0 is for the whole match. Or, since you are apparently coding for Windows, just call `PathFindFileName(szFile)`; that returns a pointer to file name portion. – Igor Tandetnik Nov 08 '20 at 14:22

0 Answers0