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
e4a06da1
Commit
e4a06da1
authored
Dec 16, 2015
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fs/io/OutputStream: use C++ exceptions in Write()
parent
36d6ead6
Hide whitespace changes
Inline
Side-by-side
Showing
24 changed files
with
217 additions
and
309 deletions
+217
-309
PlaylistFile.cxx
src/PlaylistFile.cxx
+2
-5
PlaylistSave.cxx
src/PlaylistSave.cxx
+1
-3
StateFile.cxx
src/StateFile.cxx
+4
-9
StateFile.hxx
src/StateFile.hxx
+1
-1
DirectorySave.cxx
src/db/plugins/simple/DirectorySave.cxx
+0
-3
SimpleDatabasePlugin.cxx
src/db/plugins/simple/SimpleDatabasePlugin.cxx
+4
-10
SimpleDatabasePlugin.hxx
src/db/plugins/simple/SimpleDatabasePlugin.hxx
+1
-1
Service.cxx
src/db/update/Service.cxx
+1
-3
ToOutputStream.cxx
src/encoder/ToOutputStream.cxx
+4
-5
ToOutputStream.hxx
src/encoder/ToOutputStream.hxx
+2
-3
BufferedOutputStream.cxx
src/fs/io/BufferedOutputStream.cxx
+15
-48
BufferedOutputStream.hxx
src/fs/io/BufferedOutputStream.hxx
+5
-35
FileOutputStream.cxx
src/fs/io/FileOutputStream.cxx
+15
-30
FileOutputStream.hxx
src/fs/io/FileOutputStream.hxx
+1
-1
GzipOutputStream.cxx
src/fs/io/GzipOutputStream.cxx
+14
-23
GzipOutputStream.hxx
src/fs/io/GzipOutputStream.hxx
+2
-4
OutputStream.hxx
src/fs/io/OutputStream.hxx
+4
-4
StdioOutputStream.hxx
src/fs/io/StdioOutputStream.hxx
+1
-4
RecorderOutputPlugin.cxx
src/output/plugins/RecorderOutputPlugin.cxx
+45
-15
WriteFile.cxx
test/WriteFile.cxx
+1
-4
run_encoder.cxx
test/run_encoder.cxx
+37
-39
run_gunzip.cxx
test/run_gunzip.cxx
+12
-7
run_gzip.cxx
test/run_gzip.cxx
+6
-4
test_vorbis_encoder.cxx
test/test_vorbis_encoder.cxx
+39
-48
No files found.
src/PlaylistFile.cxx
View file @
e4a06da1
...
...
@@ -239,8 +239,7 @@ SavePlaylistFile(const PlaylistFileContents &contents, const char *utf8path,
for
(
const
auto
&
uri_utf8
:
contents
)
playlist_print_uri
(
bos
,
uri_utf8
.
c_str
());
if
(
!
bos
.
Flush
(
error
))
return
false
;
bos
.
Flush
();
fos
.
Commit
();
return
true
;
...
...
@@ -415,9 +414,7 @@ spl_append_song(const char *utf8path, const DetachedSong &song, Error &error)
playlist_print_song
(
bos
,
song
);
if
(
!
bos
.
Flush
(
error
))
return
false
;
bos
.
Flush
();
fos
.
Commit
();
idle_add
(
IDLE_STORED_PLAYLIST
);
...
...
src/PlaylistSave.cxx
View file @
e4a06da1
...
...
@@ -86,9 +86,7 @@ spl_save_queue(const char *name_utf8, const Queue &queue, Error &error)
for
(
unsigned
i
=
0
;
i
<
queue
.
GetLength
();
i
++
)
playlist_print_song
(
bos
,
queue
.
Get
(
i
));
if
(
!
bos
.
Flush
(
error
))
return
false
;
bos
.
Flush
();
fos
.
Commit
();
idle_add
(
IDLE_STORED_PLAYLIST
);
...
...
src/StateFile.cxx
View file @
e4a06da1
...
...
@@ -75,12 +75,12 @@ StateFile::Write(BufferedOutputStream &os)
playlist_state_save
(
os
,
partition
.
playlist
,
partition
.
pc
);
}
inline
bool
StateFile
::
Write
(
OutputStream
&
os
,
Error
&
error
)
inline
void
StateFile
::
Write
(
OutputStream
&
os
)
{
BufferedOutputStream
bos
(
os
);
Write
(
bos
);
return
bos
.
Flush
(
error
);
bos
.
Flush
(
);
}
void
...
...
@@ -90,13 +90,8 @@ StateFile::Write()
"Saving state file %s"
,
path_utf8
.
c_str
());
try
{
Error
error
;
FileOutputStream
fos
(
path
);
if
(
!
Write
(
fos
,
error
))
{
LogError
(
error
);
return
;
}
Write
(
fos
);
fos
.
Commit
();
}
catch
(
const
std
::
exception
&
e
)
{
LogError
(
e
);
...
...
src/StateFile.hxx
View file @
e4a06da1
...
...
@@ -60,7 +60,7 @@ public:
void
CheckModified
();
private
:
bool
Write
(
OutputStream
&
os
,
Error
&
error
);
void
Write
(
OutputStream
&
os
);
void
Write
(
BufferedOutputStream
&
os
);
/**
...
...
src/db/plugins/simple/DirectorySave.cxx
View file @
e4a06da1
...
...
@@ -90,9 +90,6 @@ directory_save(BufferedOutputStream &os, const Directory &directory)
if
(
!
child
.
IsMount
())
directory_save
(
os
,
child
);
if
(
!
os
.
Check
())
return
;
}
for
(
const
auto
&
song
:
directory
.
songs
)
...
...
src/db/plugins/simple/SimpleDatabasePlugin.cxx
View file @
e4a06da1
...
...
@@ -364,8 +364,8 @@ SimpleDatabase::GetStats(const DatabaseSelection &selection,
return
::
GetStats
(
*
this
,
selection
,
stats
,
error
);
}
bool
SimpleDatabase
::
Save
(
Error
&
error
)
void
SimpleDatabase
::
Save
()
{
{
const
ScopeDatabaseLock
protect
;
...
...
@@ -395,16 +395,12 @@ SimpleDatabase::Save(Error &error)
db_save_internal
(
bos
,
*
root
);
if
(
!
bos
.
Flush
(
error
))
{
return
false
;
}
bos
.
Flush
();
#ifdef ENABLE_ZLIB
if
(
gzip
!=
nullptr
)
{
bool
success
=
gzip
->
Flush
(
error
);
gzip
->
Flush
(
);
gzip
.
reset
();
if
(
!
success
)
return
false
;
}
#endif
...
...
@@ -413,8 +409,6 @@ SimpleDatabase::Save(Error &error)
FileInfo
fi
;
if
(
GetFileInfo
(
path
,
fi
))
mtime
=
fi
.
GetModificationTime
();
return
true
;
}
bool
...
...
src/db/plugins/simple/SimpleDatabasePlugin.hxx
View file @
e4a06da1
...
...
@@ -83,7 +83,7 @@ public:
return
*
root
;
}
bool
Save
(
Error
&
error
);
void
Save
(
);
/**
* Returns true if there is a valid database file on the disk.
...
...
src/db/update/Service.cxx
View file @
e4a06da1
...
...
@@ -130,9 +130,7 @@ UpdateService::Task()
if
(
modified
||
!
next
.
db
->
FileExists
())
{
try
{
Error
error
;
if
(
!
next
.
db
->
Save
(
error
))
LogError
(
error
,
"Failed to save database"
);
next
.
db
->
Save
();
}
catch
(
const
std
::
exception
&
e
)
{
LogError
(
e
,
"Failed to save database"
);
}
...
...
src/encoder/ToOutputStream.cxx
View file @
e4a06da1
...
...
@@ -22,8 +22,8 @@
#include "EncoderInterface.hxx"
#include "fs/io/OutputStream.hxx"
bool
EncoderToOutputStream
(
OutputStream
&
os
,
Encoder
&
encoder
,
Error
&
error
)
void
EncoderToOutputStream
(
OutputStream
&
os
,
Encoder
&
encoder
)
{
while
(
true
)
{
/* read from the encoder */
...
...
@@ -31,11 +31,10 @@ EncoderToOutputStream(OutputStream &os, Encoder &encoder, Error &error)
char
buffer
[
32768
];
size_t
nbytes
=
encoder_read
(
&
encoder
,
buffer
,
sizeof
(
buffer
));
if
(
nbytes
==
0
)
return
true
;
return
;
/* write everything to the stream */
if
(
!
os
.
Write
(
buffer
,
nbytes
,
error
))
return
false
;
os
.
Write
(
buffer
,
nbytes
);
}
}
src/encoder/ToOutputStream.hxx
View file @
e4a06da1
...
...
@@ -24,9 +24,8 @@
struct
Encoder
;
class
OutputStream
;
class
Error
;
bool
EncoderToOutputStream
(
OutputStream
&
os
,
Encoder
&
encoder
,
Error
&
error
);
void
EncoderToOutputStream
(
OutputStream
&
os
,
Encoder
&
encoder
);
#endif
src/fs/io/BufferedOutputStream.cxx
View file @
e4a06da1
...
...
@@ -26,7 +26,7 @@
#include <stdio.h>
bool
BufferedOutputStream
::
AppendToBuffer
(
const
void
*
data
,
size_t
size
)
BufferedOutputStream
::
AppendToBuffer
(
const
void
*
data
,
size_t
size
)
noexcept
{
auto
r
=
buffer
.
Write
();
if
(
r
.
size
<
size
)
...
...
@@ -37,46 +37,36 @@ BufferedOutputStream::AppendToBuffer(const void *data, size_t size)
return
true
;
}
bool
void
BufferedOutputStream
::
Write
(
const
void
*
data
,
size_t
size
)
{
if
(
gcc_unlikely
(
last_error
.
IsDefined
()))
/* the stream has already failed */
return
false
;
/* try to append to the current buffer */
if
(
AppendToBuffer
(
data
,
size
))
return
true
;
return
;
/* not enough room in the buffer - flush it */
if
(
!
Flush
())
return
false
;
Flush
();
/* see if there's now enough room */
if
(
AppendToBuffer
(
data
,
size
))
return
true
;
return
;
/* too large for the buffer: direct write */
return
os
.
Write
(
data
,
size
,
last_error
);
os
.
Write
(
data
,
size
);
}
bool
void
BufferedOutputStream
::
Write
(
const
char
*
p
)
{
return
Write
(
p
,
strlen
(
p
));
Write
(
p
,
strlen
(
p
));
}
bool
void
BufferedOutputStream
::
Format
(
const
char
*
fmt
,
...)
{
if
(
gcc_unlikely
(
last_error
.
IsDefined
()))
return
false
;
auto
r
=
buffer
.
Write
();
if
(
r
.
IsEmpty
())
{
if
(
!
Flush
())
return
false
;
Flush
();
r
=
buffer
.
Write
();
}
...
...
@@ -90,8 +80,7 @@ BufferedOutputStream::Format(const char *fmt, ...)
/* buffer was not large enough; flush it and try
again */
if
(
!
Flush
())
return
false
;
Flush
();
r
=
buffer
.
Write
();
...
...
@@ -112,37 +101,15 @@ BufferedOutputStream::Format(const char *fmt, ...)
}
buffer
.
Append
(
size
);
return
true
;
}
bool
void
BufferedOutputStream
::
Flush
()
{
if
(
!
Check
())
return
false
;
auto
r
=
buffer
.
Read
();
if
(
r
.
IsEmpty
())
return
true
;
bool
success
=
os
.
Write
(
r
.
data
,
r
.
size
,
last_error
);
if
(
gcc_likely
(
success
))
buffer
.
Consume
(
r
.
size
);
return
success
;
}
bool
BufferedOutputStream
::
Flush
(
Error
&
error
)
{
if
(
!
Check
(
error
))
return
false
;
auto
r
=
buffer
.
Read
();
if
(
r
.
IsEmpty
())
return
true
;
return
;
bool
success
=
os
.
Write
(
r
.
data
,
r
.
size
,
error
);
if
(
gcc_likely
(
success
))
buffer
.
Consume
(
r
.
size
);
return
success
;
os
.
Write
(
r
.
data
,
r
.
size
);
buffer
.
Consume
(
r
.
size
);
}
src/fs/io/BufferedOutputStream.hxx
View file @
e4a06da1
...
...
@@ -23,67 +23,37 @@
#include "check.h"
#include "Compiler.h"
#include "util/DynamicFifoBuffer.hxx"
#include "util/Error.hxx"
#include <stddef.h>
class
OutputStream
;
class
Error
;
/**
* An #OutputStream wrapper that buffers its output to reduce the
* number of OutputStream::Write() calls.
*
* It simplifies error handling by managing an #Error attribute.
* Invoke any number of writes, and check for errors in the end using
* Check().
*/
class
BufferedOutputStream
{
OutputStream
&
os
;
DynamicFifoBuffer
<
char
>
buffer
;
Error
last_error
;
public
:
BufferedOutputStream
(
OutputStream
&
_os
)
:
os
(
_os
),
buffer
(
32768
)
{}
bool
Write
(
const
void
*
data
,
size_t
size
);
bool
Write
(
const
char
*
p
);
void
Write
(
const
void
*
data
,
size_t
size
);
void
Write
(
const
char
*
p
);
gcc_printf
(
2
,
3
)
bool
Format
(
const
char
*
fmt
,
...);
/**
* Returns false if an error has occurred.
*/
gcc_pure
bool
Check
()
const
{
return
!
last_error
.
IsDefined
();
}
/**
* Returns false if an error has occurred. In that case, a
* copy of the #Error is returned.
*/
bool
Check
(
Error
&
error
)
const
{
if
(
last_error
.
IsDefined
())
{
error
.
Set
(
last_error
);
return
false
;
}
else
return
true
;
}
void
Format
(
const
char
*
fmt
,
...);
/**
* Write buffer contents to the #OutputStream.
*/
bool
Flush
();
bool
Flush
(
Error
&
error
);
void
Flush
();
private
:
bool
AppendToBuffer
(
const
void
*
data
,
size_t
size
);
bool
AppendToBuffer
(
const
void
*
data
,
size_t
size
)
noexcept
;
};
#endif
src/fs/io/FileOutputStream.cxx
View file @
e4a06da1
...
...
@@ -21,9 +21,6 @@
#include "FileOutputStream.hxx"
#include "fs/FileSystem.hxx"
#include "system/Error.hxx"
#include "util/Error.hxx"
#include <system_error>
#ifdef WIN32
...
...
@@ -50,26 +47,19 @@ BaseFileOutputStream::Tell() const
return
uint64_t
(
high
)
<<
32
|
uint64_t
(
low
);
}
bool
BaseFileOutputStream
::
Write
(
const
void
*
data
,
size_t
size
,
Error
&
error
)
void
BaseFileOutputStream
::
Write
(
const
void
*
data
,
size_t
size
)
{
assert
(
IsDefined
());
DWORD
nbytes
;
if
(
!
WriteFile
(
handle
,
data
,
size
,
&
nbytes
,
nullptr
))
{
error
.
FormatLastError
(
"Failed to write to %s"
,
path
.
ToUTF8
().
c_str
());
return
false
;
}
if
(
size_t
(
nbytes
)
!=
size
)
{
error
.
FormatLastError
(
ERROR_DISK_FULL
,
"Failed to write to %s"
,
path
.
ToUTF8
().
c_str
());
return
false
;
}
if
(
!
WriteFile
(
handle
,
data
,
size
,
&
nbytes
,
nullptr
))
throw
FormatLastError
(
"Failed to write to %s"
,
GetPath
().
c_str
());
return
true
;
if
(
size_t
(
nbytes
)
!=
size
)
throw
FormatLastError
(
ERROR_DISK_FULL
,
"Failed to write to %s"
,
GetPath
().
c_str
());
}
void
...
...
@@ -143,22 +133,17 @@ BaseFileOutputStream::Tell() const
return
fd
.
Tell
();
}
bool
BaseFileOutputStream
::
Write
(
const
void
*
data
,
size_t
size
,
Error
&
error
)
void
BaseFileOutputStream
::
Write
(
const
void
*
data
,
size_t
size
)
{
assert
(
IsDefined
());
ssize_t
nbytes
=
fd
.
Write
(
data
,
size
);
if
(
nbytes
<
0
)
{
error
.
FormatErrno
(
"Failed to write to %s"
,
GetPath
().
c_str
());
return
false
;
}
else
if
((
size_t
)
nbytes
<
size
)
{
error
.
FormatErrno
(
ENOSPC
,
"Failed to write to %s"
,
GetPath
().
c_str
());
return
false
;
}
return
true
;
if
(
nbytes
<
0
)
throw
FormatErrno
(
"Failed to write to %s"
,
GetPath
().
c_str
());
else
if
((
size_t
)
nbytes
<
size
)
throw
FormatErrno
(
ENOSPC
,
"Failed to write to %s"
,
GetPath
().
c_str
());
}
void
...
...
src/fs/io/FileOutputStream.hxx
View file @
e4a06da1
...
...
@@ -119,7 +119,7 @@ public:
uint64_t
Tell
()
const
;
/* virtual methods from class OutputStream */
bool
Write
(
const
void
*
data
,
size_t
size
,
Error
&
error
)
override
;
void
Write
(
const
void
*
data
,
size_t
size
)
override
;
};
class
FileOutputStream
final
:
public
BaseFileOutputStream
{
...
...
src/fs/io/GzipOutputStream.cxx
View file @
e4a06da1
...
...
@@ -21,7 +21,6 @@
#include "GzipOutputStream.hxx"
#include "lib/zlib/Domain.hxx"
#include "lib/zlib/Error.hxx"
#include "util/Error.hxx"
GzipOutputStream
::
GzipOutputStream
(
OutputStream
&
_next
)
throw
(
ZlibError
)
:
next
(
_next
)
...
...
@@ -47,8 +46,8 @@ GzipOutputStream::~GzipOutputStream()
deflateEnd
(
&
z
);
}
bool
GzipOutputStream
::
Flush
(
Error
&
error
)
void
GzipOutputStream
::
Flush
()
{
/* no more input */
z
.
next_in
=
nullptr
;
...
...
@@ -60,21 +59,18 @@ GzipOutputStream::Flush(Error &error)
z
.
avail_out
=
sizeof
(
output
);
int
result
=
deflate
(
&
z
,
Z_FINISH
);
if
(
z
.
next_out
>
output
&&
!
next
.
Write
(
output
,
z
.
next_out
-
output
,
error
))
return
false
;
if
(
z
.
next_out
>
output
)
next
.
Write
(
output
,
z
.
next_out
-
output
);
if
(
result
==
Z_STREAM_END
)
return
true
;
else
if
(
result
!=
Z_OK
)
{
error
.
Set
(
zlib_domain
,
result
,
zError
(
result
));
return
false
;
}
}
break
;
else
if
(
result
!=
Z_OK
)
throw
ZlibError
(
result
);
}
}
bool
GzipOutputStream
::
Write
(
const
void
*
_data
,
size_t
size
,
Error
&
error
)
void
GzipOutputStream
::
Write
(
const
void
*
_data
,
size_t
size
)
{
/* zlib's API requires non-const input pointer */
void
*
data
=
const_cast
<
void
*>
(
_data
);
...
...
@@ -88,15 +84,10 @@ GzipOutputStream::Write(const void *_data, size_t size, Error &error)
z
.
avail_out
=
sizeof
(
output
);
int
result
=
deflate
(
&
z
,
Z_NO_FLUSH
);
if
(
result
!=
Z_OK
)
{
error
.
Set
(
zlib_domain
,
result
,
zError
(
result
));
return
false
;
}
if
(
result
!=
Z_OK
)
throw
ZlibError
(
result
);
if
(
z
.
next_out
>
output
&&
!
next
.
Write
(
output
,
z
.
next_out
-
output
,
error
))
return
false
;
if
(
z
.
next_out
>
output
)
next
.
Write
(
output
,
z
.
next_out
-
output
);
}
return
true
;
}
src/fs/io/GzipOutputStream.hxx
View file @
e4a06da1
...
...
@@ -28,8 +28,6 @@
#include <assert.h>
#include <zlib.h>
class
Error
;
/**
* A filter that compresses data written to it using zlib, forwarding
* compressed data in the "gzip" format.
...
...
@@ -52,10 +50,10 @@ public:
* Finish the file and write all data remaining in zlib's
* output buffer.
*/
bool
Flush
(
Error
&
error
);
void
Flush
(
);
/* virtual methods from class OutputStream */
bool
Write
(
const
void
*
data
,
size_t
size
,
Error
&
error
)
override
;
void
Write
(
const
void
*
data
,
size_t
size
)
override
;
};
#endif
src/fs/io/OutputStream.hxx
View file @
e4a06da1
...
...
@@ -21,18 +21,18 @@
#define MPD_OUTPUT_STREAM_HXX
#include "check.h"
#include "Compiler.h"
#include <stddef.h>
class
Error
;
class
OutputStream
{
public
:
OutputStream
()
=
default
;
OutputStream
(
const
OutputStream
&
)
=
delete
;
virtual
bool
Write
(
const
void
*
data
,
size_t
size
,
Error
&
error
)
=
0
;
/**
* Throws std::exception on error.
*/
virtual
void
Write
(
const
void
*
data
,
size_t
size
)
=
0
;
};
#endif
src/fs/io/StdioOutputStream.hxx
View file @
e4a06da1
...
...
@@ -22,7 +22,6 @@
#include "check.h"
#include "OutputStream.hxx"
#include "fs/AllocatedPath.hxx"
#include "Compiler.h"
#include <stdio.h>
...
...
@@ -34,12 +33,10 @@ public:
StdioOutputStream
(
FILE
*
_file
)
:
file
(
_file
)
{}
/* virtual methods from class OutputStream */
bool
Write
(
const
void
*
data
,
size_t
size
,
gcc_unused
Error
&
error
)
override
{
void
Write
(
const
void
*
data
,
size_t
size
)
override
{
fwrite
(
data
,
1
,
size
,
file
);
/* this class is debug-only and ignores errors */
return
true
;
}
};
...
...
src/output/plugins/RecorderOutputPlugin.cxx
View file @
e4a06da1
...
...
@@ -95,7 +95,7 @@ class RecorderOutput {
/**
* Writes pending data from the encoder to the output file.
*/
bool
EncoderToFile
(
Error
&
error
);
void
EncoderToFile
(
);
void
SendTag
(
const
Tag
&
tag
);
...
...
@@ -175,12 +175,12 @@ RecorderOutput::Create(const ConfigBlock &block, Error &error)
return
recorder
;
}
inline
bool
RecorderOutput
::
EncoderToFile
(
Error
&
error
)
inline
void
RecorderOutput
::
EncoderToFile
()
{
assert
(
file
!=
nullptr
);
return
EncoderToOutputStream
(
*
file
,
*
encoder
,
erro
r
);
EncoderToOutputStream
(
*
file
,
*
encode
r
);
}
inline
bool
...
...
@@ -213,9 +213,11 @@ RecorderOutput::Open(AudioFormat &audio_format, Error &error)
}
if
(
!
HasDynamicPath
())
{
if
(
!
EncoderToFile
(
error
))
{
try
{
EncoderToFile
();
}
catch
(
const
std
::
exception
&
e
)
{
encoder
->
Close
();
delete
file
;
error
.
Set
(
recorder_domain
,
e
.
what
())
;
return
false
;
}
}
else
{
...
...
@@ -237,8 +239,15 @@ RecorderOutput::Commit(Error &error)
/* flush the encoder and write the rest to the file */
bool
success
=
encoder_end
(
encoder
,
error
)
&&
EncoderToFile
(
error
);
bool
success
=
encoder_end
(
encoder
,
error
);
if
(
success
)
{
try
{
EncoderToFile
();
}
catch
(...)
{
encoder
->
Close
();
throw
;
}
}
/* now really close everything */
...
...
@@ -328,9 +337,12 @@ RecorderOutput::ReopenFormat(AllocatedPath &&new_path, Error &error)
AudioFormat as before */
assert
(
new_audio_format
==
effective_audio_format
);
if
(
!
EncoderToOutputStream
(
*
new_file
,
*
encoder
,
error
))
{
try
{
EncoderToOutputStream
(
*
new_file
,
*
encoder
);
}
catch
(
const
std
::
exception
&
e
)
{
encoder
->
Close
();
delete
new_file
;
error
.
Set
(
recorder_domain
,
e
.
what
());
return
false
;
}
...
...
@@ -376,9 +388,19 @@ RecorderOutput::SendTag(const Tag &tag)
}
Error
error
;
if
(
!
encoder_pre_tag
(
encoder
,
error
)
||
!
EncoderToFile
(
error
)
||
!
encoder_tag
(
encoder
,
tag
,
error
))
if
(
!
encoder_pre_tag
(
encoder
,
error
))
{
LogError
(
error
);
return
;
}
try
{
EncoderToFile
();
}
catch
(
const
std
::
exception
&
e
)
{
LogError
(
e
);
return
;
}
if
(
!
encoder_tag
(
encoder
,
tag
,
error
))
LogError
(
error
);
}
...
...
@@ -393,9 +415,17 @@ RecorderOutput::Play(const void *chunk, size_t size, Error &error)
return
size
;
}
return
encoder_write
(
encoder
,
chunk
,
size
,
error
)
&&
EncoderToFile
(
error
)
?
size
:
0
;
if
(
!
encoder_write
(
encoder
,
chunk
,
size
,
error
))
return
0
;
try
{
EncoderToFile
();
}
catch
(
const
std
::
exception
&
e
)
{
error
.
Set
(
recorder_domain
,
e
.
what
());
return
0
;
}
return
size
;
}
typedef
AudioOutputWrapper
<
RecorderOutput
>
Wrapper
;
...
...
test/WriteFile.cxx
View file @
e4a06da1
...
...
@@ -43,10 +43,7 @@ Copy(OutputStream &dest, int src)
if
(
nbytes
==
0
)
return
true
;
if
(
!
dest
.
Write
(
buffer
,
nbytes
,
error
))
{
fprintf
(
stderr
,
"%s
\n
"
,
error
.
GetMessage
());
return
false
;
}
dest
.
Write
(
buffer
,
nbytes
);
}
}
...
...
test/run_encoder.cxx
View file @
e4a06da1
...
...
@@ -63,60 +63,58 @@ int main(int argc, char **argv)
ConfigBlock
block
;
block
.
AddBlockParam
(
"quality"
,
"5.0"
,
-
1
);
Error
error
;
const
auto
encoder
=
encoder_init
(
*
plugin
,
block
,
error
);
if
(
encoder
==
NULL
)
{
LogError
(
error
,
"Failed to initialize encoder"
);
return
EXIT_FAILURE
;
}
try
{
Error
error
;
const
auto
encoder
=
encoder_init
(
*
plugin
,
block
,
error
);
if
(
encoder
==
NULL
)
{
LogError
(
error
,
"Failed to initialize encoder"
);
return
EXIT_FAILURE
;
}
/* open the encoder */
/* open the encoder */
AudioFormat
audio_format
(
44100
,
SampleFormat
::
S16
,
2
);
if
(
argc
>
2
)
{
if
(
!
audio_format_parse
(
audio_format
,
argv
[
2
],
false
,
error
))
{
LogError
(
error
,
"Failed to parse audio format"
);
AudioFormat
audio_format
(
44100
,
SampleFormat
::
S16
,
2
);
if
(
argc
>
2
)
{
if
(
!
audio_format_parse
(
audio_format
,
argv
[
2
],
false
,
error
))
{
LogError
(
error
,
"Failed to parse audio format"
);
return
EXIT_FAILURE
;
}
}
if
(
!
encoder
->
Open
(
audio_format
,
error
))
{
LogError
(
error
,
"Failed to open encoder"
);
return
EXIT_FAILURE
;
}
}
if
(
!
encoder
->
Open
(
audio_format
,
error
))
{
LogError
(
error
,
"Failed to open encoder"
);
return
EXIT_FAILURE
;
}
StdioOutputStream
os
(
stdout
);
StdioOutputStream
os
(
stdout
);
EncoderToOutputStream
(
os
,
*
encoder
);
if
(
!
EncoderToOutputStream
(
os
,
*
encoder
,
error
))
{
LogError
(
error
);
return
EXIT_FAILURE
;
}
/* do it */
/* do it */
ssize_t
nbytes
;
while
((
nbytes
=
read
(
0
,
buffer
,
sizeof
(
buffer
)))
>
0
)
{
if
(
!
encoder_write
(
encoder
,
buffer
,
nbytes
,
error
))
{
LogError
(
error
,
"encoder_write() failed"
);
return
EXIT_FAILURE
;
}
ssize_t
nbytes
;
while
((
nbytes
=
read
(
0
,
buffer
,
sizeof
(
buffer
)))
>
0
)
{
if
(
!
encoder_write
(
encoder
,
buffer
,
nbytes
,
error
))
{
LogError
(
error
,
"encoder_write() failed"
);
return
EXIT_FAILURE
;
EncoderToOutputStream
(
os
,
*
encoder
);
}
if
(
!
EncoderToOutputStream
(
os
,
*
encoder
,
error
))
{
LogError
(
error
);
if
(
!
encoder_end
(
encoder
,
error
))
{
LogError
(
error
,
"encoder_flush() failed"
);
return
EXIT_FAILURE
;
}
}
if
(
!
encoder_end
(
encoder
,
error
))
{
LogError
(
error
,
"encoder_flush() failed"
);
return
EXIT_FAILURE
;
}
EncoderToOutputStream
(
os
,
*
encoder
);
if
(
!
EncoderToOutputStream
(
os
,
*
encoder
,
error
))
{
LogError
(
error
);
encoder
->
Close
();
encoder
->
Dispose
();
return
EXIT_SUCCESS
;
}
catch
(
const
std
::
exception
&
e
)
{
LogError
(
e
);
return
EXIT_FAILURE
;
}
encoder
->
Close
();
encoder
->
Dispose
();
}
test/run_gunzip.cxx
View file @
e4a06da1
...
...
@@ -21,6 +21,7 @@
#include "fs/io/GunzipReader.hxx"
#include "fs/io/FileReader.hxx"
#include "fs/io/StdioOutputStream.hxx"
#include "Log.hxx"
#include "util/Error.hxx"
#include <stdio.h>
...
...
@@ -36,8 +37,7 @@ Copy(OutputStream &dest, Reader &src, Error &error)
if
(
nbytes
==
0
)
return
!
error
.
IsDefined
();
if
(
!
dest
.
Write
(
buffer
,
nbytes
,
error
))
return
false
;
dest
.
Write
(
buffer
,
nbytes
);
}
}
...
...
@@ -66,11 +66,16 @@ main(int argc, gcc_unused char **argv)
Path
path
=
Path
::
FromFS
(
argv
[
1
]);
Error
error
;
if
(
!
CopyGunzip
(
stdout
,
path
,
error
))
{
fprintf
(
stderr
,
"%s
\n
"
,
error
.
GetMessage
());
try
{
Error
error
;
if
(
!
CopyGunzip
(
stdout
,
path
,
error
))
{
fprintf
(
stderr
,
"%s
\n
"
,
error
.
GetMessage
());
return
EXIT_FAILURE
;
}
return
EXIT_SUCCESS
;
}
catch
(
const
std
::
exception
&
e
)
{
LogError
(
e
);
return
EXIT_FAILURE
;
}
return
EXIT_SUCCESS
;
}
test/run_gzip.cxx
View file @
e4a06da1
...
...
@@ -41,8 +41,7 @@ Copy(OutputStream &dest, int src, Error &error)
return
true
;
}
if
(
!
dest
.
Write
(
buffer
,
nbytes
,
error
))
return
false
;
dest
.
Write
(
buffer
,
nbytes
);
}
}
...
...
@@ -50,8 +49,11 @@ static bool
CopyGzip
(
OutputStream
&
_dest
,
int
src
,
Error
&
error
)
{
GzipOutputStream
dest
(
_dest
);
return
Copy
(
dest
,
src
,
error
)
&&
dest
.
Flush
(
error
);
if
(
!
Copy
(
dest
,
src
,
error
))
return
false
;
dest
.
Flush
();
return
true
;
}
static
bool
...
...
test/test_vorbis_encoder.cxx
View file @
e4a06da1
...
...
@@ -51,72 +51,63 @@ main(gcc_unused int argc, gcc_unused char **argv)
const
auto
encoder
=
encoder_init
(
*
plugin
,
block
,
IgnoreError
());
assert
(
encoder
!=
NULL
);
/* open the encoder */
try
{
/* open the encoder */
AudioFormat
audio_format
(
44100
,
SampleFormat
::
S16
,
2
);
success
=
encoder
->
Open
(
audio_format
,
IgnoreError
());
assert
(
success
);
AudioFormat
audio_format
(
44100
,
SampleFormat
::
S16
,
2
);
success
=
encoder
->
Open
(
audio_format
,
IgnoreError
());
assert
(
success
);
StdioOutputStream
os
(
stdout
);
StdioOutputStream
os
(
stdout
);
Error
error
;
if
(
!
EncoderToOutputStream
(
os
,
*
encoder
,
error
))
{
LogError
(
error
);
return
EXIT_FAILURE
;
}
EncoderToOutputStream
(
os
,
*
encoder
);
/* write a block of data */
/* write a block of data */
success
=
encoder_write
(
encoder
,
zero
,
sizeof
(
zero
),
IgnoreError
());
assert
(
success
);
success
=
encoder_write
(
encoder
,
zero
,
sizeof
(
zero
),
IgnoreError
());
assert
(
success
);
if
(
!
EncoderToOutputStream
(
os
,
*
encoder
,
error
))
{
LogError
(
error
);
return
EXIT_FAILURE
;
}
EncoderToOutputStream
(
os
,
*
encoder
);
/* write a tag */
/* write a tag */
success
=
encoder_pre_tag
(
encoder
,
IgnoreError
());
assert
(
success
);
success
=
encoder_pre_tag
(
encoder
,
IgnoreError
());
assert
(
success
);
if
(
!
EncoderToOutputStream
(
os
,
*
encoder
,
error
))
{
LogError
(
error
);
return
EXIT_FAILURE
;
}
EncoderToOutputStream
(
os
,
*
encoder
);
Tag
tag
;
Tag
tag
;
{
TagBuilder
tag_builder
;
tag_builder
.
AddItem
(
TAG_ARTIST
,
"Foo"
);
tag_builder
.
AddItem
(
TAG_TITLE
,
"Bar"
);
tag_builder
.
Commit
(
tag
);
}
{
TagBuilder
tag_builder
;
tag_builder
.
AddItem
(
TAG_ARTIST
,
"Foo"
);
tag_builder
.
AddItem
(
TAG_TITLE
,
"Bar"
);
tag_builder
.
Commit
(
tag
);
}
success
=
encoder_tag
(
encoder
,
tag
,
IgnoreError
());
assert
(
success
);
success
=
encoder_tag
(
encoder
,
tag
,
IgnoreError
());
assert
(
success
);
if
(
!
EncoderToOutputStream
(
os
,
*
encoder
,
error
))
{
LogError
(
error
);
return
EXIT_FAILURE
;
}
EncoderToOutputStream
(
os
,
*
encoder
);
/* write another block of data */
/* write another block of data */
success
=
encoder_write
(
encoder
,
zero
,
sizeof
(
zero
),
IgnoreError
());
assert
(
success
);
success
=
encoder_write
(
encoder
,
zero
,
sizeof
(
zero
),
IgnoreError
());
assert
(
success
);
/* finish */
/* finish */
success
=
encoder_end
(
encoder
,
IgnoreError
());
assert
(
success
);
success
=
encoder_end
(
encoder
,
IgnoreError
());
assert
(
success
);
if
(
!
EncoderToOutputStream
(
os
,
*
encoder
,
error
))
{
LogError
(
error
);
EncoderToOutputStream
(
os
,
*
encoder
);
encoder
->
Close
();
encoder
->
Dispose
();
return
EXIT_SUCCESS
;
}
catch
(
const
std
::
exception
&
e
)
{
LogError
(
e
);
return
EXIT_FAILURE
;
}
encoder
->
Close
();
encoder
->
Dispose
();
}
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