Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
258ecb76
Commit
258ecb76
authored
Oct 23, 2021
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PlaylistFile: add class PlaylistFileEditor
parent
6f595e9a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
30 deletions
+83
-30
PlaylistFile.cxx
src/PlaylistFile.cxx
+55
-26
PlaylistFile.hxx
src/PlaylistFile.hxx
+28
-4
No files found.
src/PlaylistFile.cxx
View file @
258ecb76
...
...
@@ -34,7 +34,6 @@
#include "config/Defaults.hxx"
#include "Idle.hxx"
#include "fs/Limits.hxx"
#include "fs/AllocatedPath.hxx"
#include "fs/Traits.hxx"
#include "fs/FileSystem.hxx"
#include "fs/FileInfo.hxx"
...
...
@@ -173,11 +172,8 @@ ListPlaylistFiles()
}
static
void
SavePlaylistFile
(
const
PlaylistFileContents
&
contents
,
const
char
*
utf8path
)
SavePlaylistFile
(
Path
path_fs
,
const
PlaylistFileContents
&
contents
)
{
assert
(
utf8path
!=
nullptr
);
const
auto
path_fs
=
spl_map_to_fs
(
utf8path
);
assert
(
!
path_fs
.
IsNull
());
FileOutputStream
fos
(
path_fs
);
...
...
@@ -191,12 +187,11 @@ SavePlaylistFile(const PlaylistFileContents &contents, const char *utf8path)
fos
.
Commit
();
}
PlaylistFileContents
LoadPlaylistFile
(
const
char
*
utf8path
)
static
PlaylistFileContents
LoadPlaylistFile
(
Path
path_fs
)
try
{
PlaylistFileContents
contents
;
const
auto
path_fs
=
spl_map_to_fs
(
utf8path
);
assert
(
!
path_fs
.
IsNull
());
TextFile
file
(
path_fs
);
...
...
@@ -251,16 +246,31 @@ try {
throw
;
}
void
spl_move_index
(
const
char
*
utf8path
,
unsigned
src
,
unsigned
dest
)
{
if
(
src
==
dest
)
/* this doesn't check whether the playlist exists, but
what the hell.. */
return
;
static
PlaylistFileContents
MaybeLoadPlaylistFile
(
Path
path_fs
,
PlaylistFileEditor
::
LoadMode
load_mode
)
try
{
if
(
load_mode
==
PlaylistFileEditor
::
LoadMode
::
NO
)
return
{};
auto
contents
=
LoadPlaylistFile
(
utf8path
);
return
LoadPlaylistFile
(
path_fs
);
}
catch
(
const
PlaylistError
&
error
)
{
if
(
error
.
GetCode
()
==
PlaylistResult
::
NO_SUCH_LIST
&&
load_mode
==
PlaylistFileEditor
::
LoadMode
::
TRY
)
return
{};
throw
;
}
PlaylistFileEditor
::
PlaylistFileEditor
(
const
char
*
name_utf8
,
LoadMode
load_mode
)
:
path
(
spl_map_to_fs
(
name_utf8
)),
contents
(
MaybeLoadPlaylistFile
(
path
,
load_mode
))
{
}
void
PlaylistFileEditor
::
MoveIndex
(
unsigned
src
,
unsigned
dest
)
{
if
(
src
>=
contents
.
size
()
||
dest
>=
contents
.
size
())
throw
PlaylistError
(
PlaylistResult
::
BAD_RANGE
,
"Bad range"
);
...
...
@@ -270,13 +280,38 @@ spl_move_index(const char *utf8path, unsigned src, unsigned dest)
const
auto
dest_i
=
std
::
next
(
contents
.
begin
(),
dest
);
contents
.
insert
(
dest_i
,
std
::
move
(
value
));
}
SavePlaylistFile
(
contents
,
utf8path
);
void
PlaylistFileEditor
::
RemoveIndex
(
unsigned
i
)
{
if
(
i
>=
contents
.
size
())
throw
PlaylistError
(
PlaylistResult
::
BAD_RANGE
,
"Bad range"
);
contents
.
erase
(
std
::
next
(
contents
.
begin
(),
i
));
}
void
PlaylistFileEditor
::
Save
()
{
SavePlaylistFile
(
path
,
contents
);
idle_add
(
IDLE_STORED_PLAYLIST
);
}
void
spl_move_index
(
const
char
*
utf8path
,
unsigned
src
,
unsigned
dest
)
{
if
(
src
==
dest
)
/* this doesn't check whether the playlist exists, but
what the hell.. */
return
;
PlaylistFileEditor
editor
(
utf8path
,
PlaylistFileEditor
::
LoadMode
::
YES
);
editor
.
MoveIndex
(
src
,
dest
);
editor
.
Save
();
}
void
spl_clear
(
const
char
*
utf8path
)
{
const
auto
path_fs
=
spl_map_to_fs
(
utf8path
);
...
...
@@ -317,15 +352,9 @@ spl_delete(const char *name_utf8)
void
spl_remove_index
(
const
char
*
utf8path
,
unsigned
pos
)
{
auto
contents
=
LoadPlaylistFile
(
utf8path
);
if
(
pos
>=
contents
.
size
())
throw
PlaylistError
(
PlaylistResult
::
BAD_RANGE
,
"Bad range"
);
contents
.
erase
(
std
::
next
(
contents
.
begin
(),
pos
));
SavePlaylistFile
(
contents
,
utf8path
);
idle_add
(
IDLE_STORED_PLAYLIST
);
PlaylistFileEditor
editor
(
utf8path
,
PlaylistFileEditor
::
LoadMode
::
YES
);
editor
.
RemoveIndex
(
pos
);
editor
.
Save
();
}
void
...
...
src/PlaylistFile.hxx
View file @
258ecb76
...
...
@@ -20,6 +20,8 @@
#ifndef MPD_PLAYLIST_FILE_HXX
#define MPD_PLAYLIST_FILE_HXX
#include "fs/AllocatedPath.hxx"
#include <vector>
#include <string>
...
...
@@ -27,12 +29,37 @@ struct ConfigData;
class
DetachedSong
;
class
SongLoader
;
class
PlaylistVector
;
class
AllocatedPath
;
typedef
std
::
vector
<
std
::
string
>
PlaylistFileContents
;
extern
bool
playlist_saveAbsolutePaths
;
class
PlaylistFileEditor
{
const
AllocatedPath
path
;
PlaylistFileContents
contents
;
public
:
enum
class
LoadMode
{
NO
,
YES
,
TRY
,
};
/**
* Throws on error.
*/
explicit
PlaylistFileEditor
(
const
char
*
name_utf8
,
LoadMode
load_mode
);
void
MoveIndex
(
unsigned
src
,
unsigned
dest
);
void
RemoveIndex
(
unsigned
i
);
void
Save
();
private
:
void
Load
();
};
/**
* Perform some global initialization, e.g. load configuration values.
*/
...
...
@@ -55,9 +82,6 @@ spl_map_to_fs(const char *name_utf8);
PlaylistVector
ListPlaylistFiles
();
PlaylistFileContents
LoadPlaylistFile
(
const
char
*
utf8path
);
void
spl_move_index
(
const
char
*
utf8path
,
unsigned
src
,
unsigned
dest
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment