TEST_F(TestURIUtils, SplitPath) { CStdStringArray strarray; strarray = URIUtils::SplitPath("http://www.test.com/path/to/movie.avi"); EXPECT_STREQ("http://www.test.com/", strarray.at(0).c_str()); EXPECT_STREQ("path", strarray.at(1).c_str()); EXPECT_STREQ("to", strarray.at(2).c_str()); EXPECT_STREQ("movie.avi", strarray.at(3).c_str()); }
vector<string> StringUtils::Split(const CStdString& input, const CStdString& delimiter, unsigned int iMaxStrings /* = 0 */) { CStdStringArray result; SplitString(input, delimiter, result, iMaxStrings); vector<string> strArray; for (unsigned int index = 0; index < result.size(); index++) strArray.push_back(result.at(index)); return strArray; }
TEST_F(TestURIUtils, SplitPathLocal) { #ifndef TARGET_LINUX const char *path = "C:\\path\\to\\movie.avi"; #else const char *path = "/path/to/movie.avi"; #endif CStdStringArray strarray; strarray = URIUtils::SplitPath(path); #ifndef TARGET_LINUX EXPECT_STREQ("C:", strarray.at(0).c_str()); #else EXPECT_STREQ("", strarray.at(0).c_str()); #endif EXPECT_STREQ("path", strarray.at(1).c_str()); EXPECT_STREQ("to", strarray.at(2).c_str()); EXPECT_STREQ("movie.avi", strarray.at(3).c_str()); }
int CGUIWindowAddonBrowser::SelectAddonID(const vector<ADDON::TYPE> &types, CStdString &addonID, bool showNone /*= false*/) { CStdStringArray addonIDs; if (!addonID.IsEmpty()) addonIDs.push_back(addonID); int retval = SelectAddonID(types, addonIDs, showNone, false); if (addonIDs.size() > 0) addonID = addonIDs.at(0); else addonID = ""; return retval; }
CStdString CVideoDatabaseFile::TranslateUrl(const CURL& url) { CStdString strFileName=URIUtils::GetFileName(url.Get()); if (strFileName.empty()) return ""; CStdString strPath = URIUtils::GetDirectory(url.Get()); if (strPath.empty()) return ""; CStdString strExtension = URIUtils::GetExtension(strFileName); URIUtils::RemoveExtension(strFileName); if (!StringUtils::IsNaturalNumber(strFileName)) return ""; long idDb=atol(strFileName.c_str()); CStdStringArray pathElem; StringUtils::SplitString(strPath, "/", pathElem); if (pathElem.size() == 0) return ""; CStdString itemType = pathElem.at(2); VIDEODB_CONTENT_TYPE type; if (itemType.Equals("movies") || itemType.Equals("recentlyaddedmovies")) type = VIDEODB_CONTENT_MOVIES; else if (itemType.Equals("episodes") || itemType.Equals("recentlyaddedepisodes")) type = VIDEODB_CONTENT_EPISODES; else if (itemType.Equals("musicvideos") || itemType.Equals("recentlyaddedmusicvideos")) type = VIDEODB_CONTENT_MUSICVIDEOS; else return ""; CVideoDatabase videoDatabase; if (!videoDatabase.Open()) return ""; CStdString realFilename; videoDatabase.GetFilePathById(idDb, realFilename, type); return realFilename; }
bool CWebSocketV8::Handshake(const char* data, size_t length, std::string &response) { string strHeader(data, length); const char *value; HttpParser header; if (header.addBytes(data, length) != HttpParser::Done) { CLog::Log(LOGINFO, "WebSocket [hybi-10]: incomplete handshake received"); return false; } // The request must be GET value = header.getMethod(); if (value == NULL || strnicmp(value, WS_HTTP_METHOD, strlen(WS_HTTP_METHOD)) != 0) { CLog::Log(LOGINFO, "WebSocket [hybi-10]: invalid HTTP method received (GET expected)"); return false; } // The request must be HTTP/1.1 or higher int pos; if ((pos = strHeader.find(WS_HTTP_TAG)) == string::npos) { CLog::Log(LOGINFO, "WebSocket [hybi-10]: invalid handshake received"); return false; } pos += strlen(WS_HTTP_TAG); istringstream converter(strHeader.substr(pos, strHeader.find_first_of(" \r\n\t", pos) - pos)); float fVersion; converter >> fVersion; if (fVersion < 1.1f) { CLog::Log(LOGINFO, "WebSocket [hybi-10]: invalid HTTP version %f (1.1 or higher expected)", fVersion); return false; } string websocketKey, websocketProtocol; // There must be a "Host" header value = header.getValue("host"); if (value == NULL || strlen(value) == 0) { CLog::Log(LOGINFO, "WebSocket [hybi-10]: \"Host\" header missing"); return true; } // There must be a base64 encoded 16 byte (=> 24 byte as base64) "Sec-WebSocket-Key" header value = header.getValue(WS_HEADER_KEY_LC); if (value == NULL || (websocketKey = value).size() != 24) { CLog::Log(LOGINFO, "WebSocket [hybi-10]: invalid \"Sec-WebSocket-Key\" received"); return true; } // There might be a "Sec-WebSocket-Protocol" header value = header.getValue(WS_HEADER_PROTOCOL_LC); if (value && strlen(value) > 0) { CStdStringArray protocols; StringUtils::SplitString(value, ",", protocols); for (unsigned int index = 0; index < protocols.size(); index++) { if (protocols.at(index).Trim().Equals(WS_PROTOCOL_JSONRPC)) { websocketProtocol = WS_PROTOCOL_JSONRPC; break; } } } CHttpResponse httpResponse(HTTP::Get, HTTP::SwitchingProtocols, HTTP::Version1_1); httpResponse.AddHeader(WS_HEADER_UPGRADE, WS_HEADER_UPGRADE_VALUE); httpResponse.AddHeader(WS_HEADER_CONNECTION, WS_HEADER_UPGRADE); httpResponse.AddHeader(WS_HEADER_ACCEPT, calculateKey(websocketKey)); if (!websocketProtocol.empty()) httpResponse.AddHeader(WS_HEADER_PROTOCOL, websocketProtocol); char *responseBuffer; int responseLength = httpResponse.Create(responseBuffer); response = std::string(responseBuffer, responseLength); m_state = WebSocketStateConnected; return true; }
TEST(TestStringUtils, SplitString) { CStdStringArray varresults; EXPECT_EQ(9, StringUtils::SplitString("a,b,c,de,,,fg,,", ",", varresults)); EXPECT_STREQ("a", varresults.at(0).c_str()); EXPECT_STREQ("b", varresults.at(1).c_str()); EXPECT_STREQ("c", varresults.at(2).c_str()); EXPECT_STREQ("de", varresults.at(3).c_str()); EXPECT_STREQ("", varresults.at(4).c_str()); EXPECT_STREQ("", varresults.at(5).c_str()); EXPECT_STREQ("fg", varresults.at(6).c_str()); EXPECT_STREQ("", varresults.at(7).c_str()); EXPECT_STREQ("", varresults.at(8).c_str()); varresults.clear(); varresults = StringUtils::SplitString("g,h,ij,k,lm,,n", ","); EXPECT_STREQ("g", varresults.at(0).c_str()); EXPECT_STREQ("h", varresults.at(1).c_str()); EXPECT_STREQ("ij", varresults.at(2).c_str()); EXPECT_STREQ("k", varresults.at(3).c_str()); EXPECT_STREQ("lm", varresults.at(4).c_str()); EXPECT_STREQ("", varresults.at(5).c_str()); EXPECT_STREQ("n", varresults.at(6).c_str()); }
void CMusikSourcesCtrl::DoDrag( CMusikPropTreeItem* pItem ) { if ( !pItem ) return; COleDataSource datasrc; HGLOBAL hgDrop; DROPFILES* pDrop; CStringList lsDraggedFiles; POSITION pos; CString sFile; UINT uBuffSize = 0; TCHAR* pszBuff; FORMATETC etc = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL }; // get a list of filenames with the currently // selected items... CStdStringArray files; int nMode = pItem->GetPlaylistType(); // standard playlist dragged if ( nMode == MUSIK_PLAYLIST_TYPE_STANDARD ) m_Library->GetStdPlaylistFns( pItem->GetPlaylistID(), files, false ); // now playing dragged.. else if ( nMode == MUSIK_SOURCES_TYPE_NOWPLAYING ) { if ( m_Player->GetPlaylist() ) { m_Library->BeginTransaction(); for ( size_t i = 0; i < m_Player->GetPlaylist()->GetCount(); i++ ) files.push_back( m_Player->GetPlaylist()->GetField( i, MUSIK_LIBRARY_TYPE_FILENAME ) ); m_Library->EndTransaction(); } } // library playlist dragged else if ( nMode == MUSIK_SOURCES_TYPE_LIBRARY ) { CMainFrame* pMain = (CMainFrame*)m_Parent; if ( pMain->m_LibPlaylist ) { m_Library->BeginTransaction(); for ( size_t i = 0; i < pMain->m_LibPlaylist->GetCount(); i++ ) files.push_back( pMain->m_LibPlaylist->GetField( i, MUSIK_LIBRARY_TYPE_FILENAME ) ); m_Library->EndTransaction(); } } else if ( nMode == MUSIK_PLAYLIST_TYPE_DYNAMIC ) MessageBox( "This operation is not supported yet.", "Musik", MB_ICONINFORMATION | MB_OK ); if ( !files.size() ) return; // CStringList containing files for ( size_t i = 0; i < files.size(); i++ ) { lsDraggedFiles.AddTail( files.at( i ) ); uBuffSize += files.at( i ).GetLength() + 1; } files.clear(); // Add 1 extra for the final null char, and the size of the DROPFILES struct. uBuffSize = sizeof(DROPFILES) + sizeof(TCHAR) * (uBuffSize + 1); // Allocate memory from the heap for the DROPFILES struct. hgDrop = GlobalAlloc ( GHND | GMEM_SHARE, uBuffSize ); if ( !hgDrop ) return; pDrop = (DROPFILES*) GlobalLock ( hgDrop ); if ( !pDrop ) { GlobalFree ( hgDrop ); return; } // Fill in the DROPFILES struct. pDrop->pFiles = sizeof(DROPFILES); // If we're compiling for Unicode, set the Unicode flag in the struct to // indicate it contains Unicode strings. #ifdef _UNICODE pDrop->fWide = TRUE; #endif; // Copy all the filenames into memory after the end of the DROPFILES struct. pos = lsDraggedFiles.GetHeadPosition(); pszBuff = (TCHAR*) (LPBYTE(pDrop) + sizeof(DROPFILES)); while ( pos ) { lstrcpy ( pszBuff, (LPCTSTR) lsDraggedFiles.GetNext ( pos ) ); pszBuff = 1 + _tcschr ( pszBuff, '\0' ); } GlobalUnlock ( hgDrop ); // Put the data in the data source. datasrc.CacheGlobalData ( CF_HDROP, hgDrop, &etc ); // Add in our own custom data, so we know that the drag originated from our // window. CMyDropTarget::DragEnter() checks for this custom format, and // doesn't allow the drop if it's present. This is how we prevent the user // from dragging and then dropping in our own window. // The data will just be a dummy bool. HGLOBAL hgBool; hgBool = GlobalAlloc ( GHND | GMEM_SHARE, sizeof(bool) ); if ( NULL == hgBool ) { GlobalFree ( hgDrop ); return; } // Put the data in the data source. etc.cfFormat = m_DropID; datasrc.CacheGlobalData ( m_DropID, hgBool, &etc ); // Start the drag 'n' drop! DROPEFFECT dwEffect = datasrc.DoDragDrop ( DROPEFFECT_COPY | DROPEFFECT_MOVE ); // If the DnD completed OK, we remove all of the dragged items from our // list. switch ( dwEffect ) { case DROPEFFECT_COPY: case DROPEFFECT_MOVE: { // the copy completed successfully // do whatever we need to do here TRACE0( "DND from playlist completed successfully. The data has a new owner.\n" ); } break; case DROPEFFECT_NONE: { // This needs special handling, because on NT, DROPEFFECT_NONE // is returned for move operations, instead of DROPEFFECT_MOVE. // See Q182219 for the details. // So if we're on NT, we check each selected item, and if the // file no longer exists, it was moved successfully and we can // remove it from the list. if ( m_IsWinNT ) { // the copy completed successfully // on a windows nt machine. // do whatever we need to do here bool bDeletedAnything = false; if ( ! bDeletedAnything ) { // The DnD operation wasn't accepted, or was canceled, so we // should call GlobalFree() to clean up. GlobalFree ( hgDrop ); GlobalFree ( hgBool ); TRACE0( "DND had a problem. We had to manually free the data.\n" ); } } // not windows NT else { // We're on 9x, and a return of DROPEFFECT_NONE always means // that the DnD operation was aborted. We need to free the // allocated memory. GlobalFree ( hgDrop ); GlobalFree ( hgBool ); TRACE0( "DND had a problem. We had to manually free the data.\n" ); } } break; } }
void CMusikSourcesCtrl::OnDropFiles(HDROP hDropInfo) { // set cursor back to hour glass SetCursor( LoadCursor( NULL, IDC_WAIT ) ); // see if the drag landed on an existing // playlist, if it did, we'll append CPoint pos; ::GetCursorPos( &pos ); ScreenToClient( &pos ); CMusikPropTreeItem* pItem = FindItem( pos ); // make sure the item isn't root if ( pItem != NULL && pItem->IsRootLevel() ) return; if ( pItem ) { KillFocus(); pItem->Select( TRUE ); SetFocusedItem( pItem ); } // dnd stuff size_t nNumFiles; TCHAR szNextFile [MAX_PATH]; SHFILEINFO rFileInfo; nNumFiles = DragQueryFile ( hDropInfo, -1, NULL, 0 ); CStdStringArray files; CStdString sTemp; for ( size_t i = 0; i < nNumFiles; i++ ) { if ( DragQueryFile( hDropInfo, i, szNextFile, MAX_PATH ) > 0 ) { // get the filetype. if its a directory // that was dropped, we'll want to // recurse it and add all the supported // media files... SHGetFileInfo( szNextFile, 0, &rFileInfo, sizeof( rFileInfo ), SHGFI_ATTRIBUTES ); if ( rFileInfo.dwAttributes & SFGAO_FOLDER ) { sTemp = szNextFile; sTemp += "\\*.*"; m_Dir.m_Dir = sTemp; m_Dir.m_Threaded = false; m_Dir.m_Target = &files; m_Dir.Run(); } // otherwise it was just a file... add it... else files.push_back( szNextFile ); } } DragFinish( hDropInfo ); // did we actually hit an item? if ( pItem ) { // standard playlist if ( pItem->GetPlaylistType() == MUSIK_PLAYLIST_TYPE_STANDARD ) m_Library->AppendStdPlaylist( pItem->GetPlaylistID(), files ); // hit now playing? else if ( pItem->GetPlaylistType() == MUSIK_SOURCES_TYPE_NOWPLAYING ) { m_Library->BeginTransaction(); CMusikSong song; for ( size_t i = 0; i < files.size(); i++ ) { // add song (if necessary) m_Library->AddSong( files.at( i ) ); m_Library->GetSongFromFilename( files.at( i ), song ); m_Player->GetPlaylist()->Add( song ); } m_Library->EndTransaction(); } } // else make a new playlist else { CStdString playlist_str; playlist_str.Format( _T( "New Playlist %d" ), m_StdPlaylists.size() ); m_Library->CreateStdPlaylist( playlist_str.c_str(), files ); LoadStdPlaylists(); } // didn't hit an item, a new // playlist was created, so it // was pushed to the back of the list if ( !pItem ) { pItem = m_StdPlaylists.at( m_StdPlaylists.size() - 1 ); // focus KillFocus(); pItem->Select( TRUE ); SetFocusedItem( pItem ); } if ( pItem ) SendNotify( PTN_SELCHANGE, pItem ); }
PyObject* Dialog_Browse(PyObject *self, PyObject *args) { int browsetype = 0; char useThumbs = false; char useFileDirectories = false; char enableMultiple = false; CStdString value; CStdStringArray valuelist; PyObject* unicodeLine[3]; string utf8Line[3]; char *cDefault = NULL; PyObject *result; for (int i = 0; i < 3; i++) unicodeLine[i] = NULL; if (!PyArg_ParseTuple(args, (char*)"iOO|Obbsb", &browsetype , &unicodeLine[0], &unicodeLine[1], &unicodeLine[2], &useThumbs, &useFileDirectories, &cDefault, &enableMultiple)) { return NULL; } for (int i = 0; i < 3; i++) { if (unicodeLine[i] && !PyXBMCGetUnicodeString(utf8Line[i], unicodeLine[i], i+1)) return NULL; } VECSOURCES *shares = g_settings.GetSourcesFromType(utf8Line[1]); if (!shares) return NULL; if (useFileDirectories && !utf8Line[2].size() == 0) utf8Line[2] += "|.rar|.zip"; value = cDefault; Py_BEGIN_ALLOW_THREADS if (browsetype == 1) { if (enableMultiple) CGUIDialogFileBrowser::ShowAndGetFileList(*shares, utf8Line[2], utf8Line[0], valuelist, 0 != useThumbs, 0 != useFileDirectories); else CGUIDialogFileBrowser::ShowAndGetFile(*shares, utf8Line[2], utf8Line[0], value, 0 != useThumbs, 0 != useFileDirectories); } else if (browsetype == 2) { if (enableMultiple) CGUIDialogFileBrowser::ShowAndGetImageList(*shares, utf8Line[0], valuelist); else CGUIDialogFileBrowser::ShowAndGetImage(*shares, utf8Line[0], value); } else CGUIDialogFileBrowser::ShowAndGetDirectory(*shares, utf8Line[0], value, browsetype != 0); Py_END_ALLOW_THREADS if (enableMultiple && (browsetype == 1 || browsetype == 2)) { result = PyTuple_New(valuelist.size()); if (!result) return NULL; for (int i = 0; i < valuelist.size(); i++) PyTuple_SetItem(result, i, PyString_FromString(valuelist.at(i).c_str())); return result; } else return Py_BuildValue((char*)"s", value.c_str()); }