Skip to content

Commit

Permalink
Some more Modern C++ isms
Browse files Browse the repository at this point in the history
* Replace 0 with nullptr when possible.
* Replace assert() with thrown runtime_error.
* Remove tr1 from cstdint import
  • Loading branch information
markuspeloquin committed Apr 9, 2020
1 parent 6d223c5 commit d31c35d
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 37 deletions.
5 changes: 3 additions & 2 deletions decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class Wave_decoder : public flacsplit::Basic_decoder {
Wave_decoder(const std::string &, FILE *);

virtual ~Wave_decoder() noexcept {
// TODO figure out if this closes the file
sox_close(_fmt);
}

Expand Down Expand Up @@ -242,7 +243,7 @@ Flac_decoder::write_callback(const FLAC__Frame *frame,
_last_frame = frame;
std::copy(buffer, buffer + frame->header.channels,
_last_buffer.get());
_last_status = 0;
_last_status = nullptr;
_frame_retrieved = false;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
Expand Down Expand Up @@ -285,7 +286,7 @@ Wave_decoder::Wave_decoder(const std::string &path, FILE *fp) :
_transp()
{
Sox_init::init();
if (!(_fmt = sox_open_read(path.c_str(), 0, 0, 0)))
if (!(_fmt = sox_open_read(path.c_str(), nullptr, nullptr, nullptr)))
throw flacsplit::Sox_error("sox_open_read() error");

try {
Expand Down
10 changes: 6 additions & 4 deletions encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

#include <cassert>
#if FLACPP_API_VERSION_CURRENT <= 8
# include <cerrno>
#endif
#include <cstdint>
#include <memory>
#include <sstream>
#include <tr1/cstdint>
#include <stdexcept>

#include <FLAC++/encoder.h>

Expand Down Expand Up @@ -255,8 +255,10 @@ FLAC__StreamEncoderSeekStatus
Flac_encoder::seek_callback(FLAC__uint64 absolute_byte_offset)
{
long off = absolute_byte_offset;
assert(static_cast<FLAC__uint64>(off) ==
absolute_byte_offset);
if (static_cast<FLAC__uint64>(off) !=
absolute_byte_offset) {
throw std::runtime_error("bad offset");
}

return fseek(_fp, off, SEEK_SET) ?
FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR :
Expand Down
5 changes: 2 additions & 3 deletions gain_analysis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
#include <cstdlib>
#include <cstring>
#include <exception>
#include <stdexcept>

#include "errors.hpp"
#include "gain_analysis.h"
Expand Down Expand Up @@ -183,7 +182,7 @@ class Analyzer {
* \throw Bad_samplefreq
*/
Analyzer(unsigned long freq) :
_ctx(0)
_ctx(nullptr)
{
enum replaygain_status status;
_ctx = replaygain_alloc(freq, &status);
Expand All @@ -195,7 +194,7 @@ class Analyzer {
case REPLAYGAIN_OK:
break;
default:
assert(0);
throw std::runtime_error("invalid status");
}
}

Expand Down
32 changes: 17 additions & 15 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#include <sys/stat.h>
#include <unistd.h>

#include <cassert>
#include <cerrno>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <fstream>
Expand All @@ -26,7 +26,6 @@
#include <memory>
#include <sstream>
#include <vector>
#include <tr1/cstdint>

#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
Expand Down Expand Up @@ -81,7 +80,7 @@ void usage(const boost::program_options::options_description &);

class Cuetools_cd {
public:
Cuetools_cd() : _cd(0) {}
Cuetools_cd() : _cd(nullptr) {}
Cuetools_cd(const Cd *cd) : _cd(const_cast<Cd *>(cd)) {}
~Cuetools_cd()
{ if (_cd) cd_delete(_cd); }
Expand Down Expand Up @@ -266,18 +265,15 @@ find_file(const std::string &path, std::string &out_path, bool use_flac)
}
}

assert(errno == ENOENT);
if (errno != ENOENT)
throw std::runtime_error("bad errno");
out_path = path;
return 0;
return nullptr;
}

#if 0
std::string
frametime(uint32_t frames)
{
// get rid of complaint about the function being unused
if (0) frametime(0);

frametime(uint32_t frames) {
uint16_t seconds = frames / 75;
frames -= seconds * 75;
uint16_t minutes = seconds / 60;
Expand Down Expand Up @@ -420,9 +416,11 @@ once(const std::string &cue_path, const struct options *options)
if (track_get_mode(track) != MODE_AUDIO) {
if (i == tracks-1)
break;
else
else {
// this is possible, but I won't handle it
assert("mixed track types... screw this" &&0);
throw std::runtime_error(
"mixed track types... screw this");
}
}

uint32_t begin_ = track_get_start(track);
Expand Down Expand Up @@ -494,7 +492,7 @@ once(const std::string &cue_path, const struct options *options)
replaygain::Sample_accum rg_accum;
std::unique_ptr<replaygain::Analyzer> rg_analyzer;
std::unique_ptr<double> rg_samples;
double *double_samples[] = { 0, 0 };
double *double_samples[] = { nullptr, nullptr };
unsigned dimens[] = { 0, 0 };

std::unique_ptr<Replaygain_stats> gain_stats(
Expand Down Expand Up @@ -586,8 +584,12 @@ once(const std::string &cue_path, const struct options *options)
double begin_sample =
static_cast<uint64_t>(begin[i]) *
decoder->sample_rate() / 75.;
assert(decoder->total_samples() >
begin_sample);
if (decoder->total_samples() <=
begin_sample) {
throw std::runtime_error(
"beginning offset isn't "
"where it was expected");
}
samples = decoder->total_samples() -
begin_sample;
}
Expand Down
11 changes: 7 additions & 4 deletions replaygain_writer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -91,7 +90,8 @@ class Metadata_editor {
virtual int seek_callback(off_t offset, int whence)
{
long loffset = offset;
assert(loffset == offset);
if (loffset != offset)
throw std::runtime_error("bad narrow cast");

return fseek(_fp, offset, whence);
}
Expand Down Expand Up @@ -239,15 +239,18 @@ flacsplit::Replaygain_writer_impl::find_comment()
if (comment)
return comment;
} while (iter->next());
return 0;
return nullptr;
}

inline void
flacsplit::Replaygain_writer_impl::add_replaygain(
const flacsplit::Replaygain_stats &gain_stats)
{
FLAC::Metadata::VorbisComment *comment = find_comment();
assert(comment && "Vorbis comment should have been present");
if (!comment) {
throw std::runtime_error(
"Vorbis comment should have been present");
}

append_replaygain_tags(*comment, gain_stats);
}
Expand Down
12 changes: 6 additions & 6 deletions sanitize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

#include <cassert>
#include <cstdint>
#include <stdexcept>
#include <vector>
#include <tr1/cstdint>

#include <unicode/utf8.h>

Expand All @@ -32,7 +32,7 @@ const char *LATIN_MAP[] = {
"DH",
"N",
"O", "O", "O", "O", "O",
0,
nullptr,
"O",
"U", "U", "U", "U",
"Y",
Expand All @@ -46,7 +46,7 @@ const char *LATIN_MAP[] = {
"dh",
"n",
"o", "o", "o", "o", "o",
0,
nullptr,
"o",
"u", "u", "u", "u",
"y",
Expand Down Expand Up @@ -98,8 +98,8 @@ flacsplit::sanitize(const std::string &str)
while (i < length) {
int32_t c;
U8_NEXT(s, i, length, c);
// strings should be pre-encoded in utf8
assert(c > 0);
if (c <= 0)
throw std::runtime_error("not pre-encoded as utf8");

if (isdigit(c) || c == 0x20 ||
(0x41 <= c && c < 0x5b) ||
Expand Down
4 changes: 2 additions & 2 deletions transcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "transcode.hpp"

flacsplit::Music_info::Music_info() :
_parent(0),
_parent(nullptr),
_track(0)
{}

Expand All @@ -35,7 +35,7 @@ flacsplit::Music_info::create_hidden(const Music_info &parent)
}

flacsplit::Music_info::Music_info(const Cdtext *cdtext0) :
_parent(0),
_parent(nullptr),
_track(0)
{
Cdtext *cdtext = const_cast<Cdtext *>(cdtext0);
Expand Down
2 changes: 1 addition & 1 deletion transcode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#ifndef TRANSCODE_HPP
#define TRANSCODE_HPP

#include <cstdint>
#include <cstdio>
#include <memory>
#include <string>
#include <tr1/cstdint>

struct Cdtext;

Expand Down

0 comments on commit d31c35d

Please sign in to comment.