/*---------------------------------------------------------------------- | NPT_DirectorySplitFilePath +---------------------------------------------------------------------*/ NPT_Result NPT_DirectorySplitFilePath(const char* filepath, NPT_String& path, NPT_String& filename) { if (!filepath || filepath[0] == '\0') return NPT_ERROR_INVALID_PARAMETERS; path = filepath; char last_char; NPT_Int32 i = path.GetLength(); do { last_char = path[i-1]; if (last_char == '\\' || last_char == '/') break; } while (--i); // we need at least one delimiter and it cannot be last if (i == 0 || i == (NPT_Int32)path.GetLength()) { return NPT_ERROR_INVALID_PARAMETERS; } // assign filename filename = filepath+i; // truncate path & remove trailing slashes NPT_CHECK_FATAL(path.SetLength(i-1)); // remove excessive delimiters path.TrimRight("/"); path.TrimRight("\\"); return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | NPT_DirectoryAppendToPath +---------------------------------------------------------------------*/ NPT_Result NPT_DirectoryAppendToPath(NPT_String& path, const char* value) { if (!value) return NPT_ERROR_INVALID_PARAMETERS; NPT_String tmp = value; // make sure path will end with only one trailing delimiter path.TrimRight('/'); path.TrimRight('\\'); path += NPT_DIR_DELIMITER_STR; // make sure value to append doesn't start with delimiters tmp.TrimLeft('/'); tmp.TrimLeft('\\'); // append value path += tmp; // replace delimiters with the proper one for the platform path.Replace((NPT_DIR_DELIMITER_CHR == '/')?'\\':'/', NPT_DIR_DELIMITER_CHR); return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | NPT_DirectoryCreate +---------------------------------------------------------------------*/ NPT_Result NPT_DirectoryCreate(const char* path, bool create_parents) { NPT_Result res = NPT_SUCCESS; NPT_String fullpath = path; // replace delimiters with the proper one for the platform fullpath.Replace((NPT_DIR_DELIMITER_CHR == '/')?'\\':'/', NPT_DIR_DELIMITER_CHR); // remove excessive delimiters fullpath.TrimRight(NPT_DIR_DELIMITER_CHR); if (create_parents) { NPT_String parent_path; // look for a delimiter from the beginning int delimiter = fullpath.Find(NPT_DIR_DELIMITER_CHR, 1); while (delimiter > 0 && NPT_SUCCEEDED(res)) { // copy the path up to the delimiter parent_path = fullpath.SubString(0, delimiter); // create the directory non recursively res = NPT_DirectoryCreate(parent_path, false); // look for the next delimiter delimiter = fullpath.Find(NPT_DIR_DELIMITER_CHR, delimiter + 1); } if (NPT_FAILED(res)) return res; } // create directory return NPT_Directory::Create(fullpath); }
/*---------------------------------------------------------------------- | CUPnPVirtualPathDirectory::SplitPath +---------------------------------------------------------------------*/ bool CUPnPVirtualPathDirectory::SplitPath(const char* object_id, NPT_String& share_name, NPT_String& path) { int index = 0; NPT_String id = object_id; id.TrimRight("/"); // reset output params first share_name = ""; path = ""; if (id.StartsWith("virtualpath://upnproot")) { index = 22; } else if (id.StartsWith("virtualpath://upnpmusic")) { index = 23; } else if (id.StartsWith("virtualpath://upnpvideo")) { index = 23; } else if (id.StartsWith("virtualpath://upnppictures")) { index = 26; } else { return false; } // nothing to split if (id.GetLength() <= (NPT_Cardinal)index) { return true; } // invalid id! if (id[index] != '/') { return false; } // look for share index = id.Find('/', index+1); share_name = id.SubString(0, (index==-1)?id.GetLength():index); if (index >= 0) { path = id.SubString(index+1); } return true; }
/*---------------------------------------------------------------------- | CUPnPVirtualPathDirectory::GetDirectory +---------------------------------------------------------------------*/ bool CUPnPVirtualPathDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items) { NPT_String path = strPath.c_str(); CMediaSource share; CFileItemPtr item; vector<CStdString> paths; path.TrimRight("/"); if (path == "virtualpath://upnproot") { // music item.reset(new CFileItem("virtualpath://upnpmusic/", true)); item->SetLabel("Music Files"); item->SetLabelPreformated(true); items.Add(item); // video item.reset(new CFileItem("virtualpath://upnpvideo/", true)); item->SetLabel("Video Files"); item->SetLabelPreformated(true); items.Add(item); // pictures item.reset(new CFileItem("virtualpath://upnppictures/", true)); item->SetLabel("Picture Files"); item->SetLabelPreformated(true); items.Add(item); // music library item.reset(new CFileItem("musicdb://", true)); item->SetLabel("Music Library"); item->SetLabelPreformated(true); items.Add(item); // video library item.reset(new CFileItem("videodb://", true)); item->SetLabel("Video Library"); item->SetLabelPreformated(true); items.Add(item); return true; } else if (path == "virtualpath://upnpmusic" || path == "virtualpath://upnpvideo" || path == "virtualpath://upnppictures") { // look for all shares given a container VECSOURCES *shares = NULL; if (path == "virtualpath://upnpmusic") { shares = g_settings.GetSourcesFromType("upnpmusic"); } else if (path == "virtualpath://upnpvideo") { shares = g_settings.GetSourcesFromType("upnpvideo"); } else if (path == "virtualpath://upnppictures") { shares = g_settings.GetSourcesFromType("upnppictures"); } if (shares) { for (unsigned int i = 0; i < shares->size(); i++) { // Does this share contains any local paths? CMediaSource &share = shares->at(i); // reconstruct share name as it could have been replaced by // a path if there was just one entry NPT_String share_name = path + "/"; share_name += share.strName + "/"; if (GetMatchingSource((const char*)share_name, share, paths) && paths.size()) { item.reset(new CFileItem((const char*)share_name, true)); item->SetLabel(share.strName); item->SetLabelPreformated(true); items.Add(item); } } } return true; } else if (!GetMatchingSource((const char*)path, share, paths)) { // split to remove share name from path NPT_String share_name; NPT_String file_path; bool bret = SplitPath(path, share_name, file_path); if (!bret || share_name.GetLength() == 0 || file_path.GetLength() == 0) { return false; } // make sure the file_path is the beginning of a share paths if (!FindSourcePath(share_name, file_path, true)) return false; // use the share name to figure out what extensions to use if (share_name.StartsWith("virtualpath://upnpmusic")) { CDirectory::GetDirectory( (const char*)file_path, items, g_stSettings.m_musicExtensions); } else if (share_name.StartsWith("virtualpath://upnpvideo")) { CDirectory::GetDirectory( (const char*)file_path, items, g_stSettings.m_videoExtensions); } else if (share_name.StartsWith("virtualpath://upnppictures")) { CDirectory::GetDirectory( (const char*)file_path, items, g_stSettings.m_pictureExtensions); } // paths should be prefixed for (int i=0; i < (int) items.Size(); ++i) { items[i]->m_strPath = share_name + "/" + items[i]->m_strPath.c_str(); } } else { // use the path to figure out what extensions to use if (path.StartsWith("virtualpath://upnpmusic")) { SetMask(g_stSettings.m_musicExtensions); } else if (path.StartsWith("virtualpath://upnpvideo")) { SetMask(g_stSettings.m_videoExtensions); } else if (path.StartsWith("virtualpath://upnppictures")) { SetMask(g_stSettings.m_pictureExtensions); } // dont allow prompting, this is a background task // although I don't think it matters here SetAllowPrompting(false); // it's a virtual path, get all items CVirtualPathDirectory::GetDirectory(strPath, items); // paths should be prefixed for (int i=0; i < (int) items.Size(); ++i) { items[i]->m_strPath = strPath + "/" + items[i]->m_strPath.c_str(); } } // always return true // this will make sure that even if nothing is found in a share // it doesn't fail upnp return true; }