diff --git a/src/catcodec.cpp b/src/catcodec.cpp index ad387d4..e50df00 100644 --- a/src/catcodec.cpp +++ b/src/catcodec.cpp @@ -52,11 +52,11 @@ static void ReadCat(Samples &samples, FileReader &reader) reader.Seek(0); for (uint32_t i = 0; i < count; i++) { - samples.push_back(new Sample(reader)); + samples.emplace_back(reader); } - for (Samples::iterator iter = samples.begin(); iter != samples.end(); iter++) { - (*iter)->ReadCatEntry(reader, new_format); + for (auto iter = samples.begin(); iter != samples.end(); ++iter) { + iter->ReadCatEntry(reader, new_format); ShowProgress(); } } @@ -69,18 +69,18 @@ static void ReadCat(Samples &samples, FileReader &reader) static void WriteCat(Samples &samples, FileWriter &writer) { uint32_t offset = (uint32_t)samples.size() * 8; - for (Samples::iterator iter = samples.begin(); iter != samples.end(); iter++) { - Sample *sample = *iter; + for (auto iter = samples.begin(); iter != samples.end(); ++iter) { + Sample &sample = *iter; - sample->SetOffset(offset); - offset = sample->GetNextOffset(); + sample.SetOffset(offset); + offset = sample.GetNextOffset(); - writer.WriteDword(sample->GetOffset() | (1U << 31)); - writer.WriteDword(sample->GetSize()); + writer.WriteDword(sample.GetOffset() | (1U << 31)); + writer.WriteDword(sample.GetSize()); } - for (Samples::iterator iter = samples.begin(); iter != samples.end(); iter++) { - (*iter)->WriteCatEntry(writer); + for (auto iter = samples.begin(); iter != samples.end(); ++iter) { + iter->WriteCatEntry(writer); ShowProgress(); } } @@ -129,7 +129,7 @@ static void ReadSFO(Samples &samples, FileReader &reader) if (strlen(filename) + 1 > 255) throw "Filename is too long in " + reader.GetFilename() + " at [" + buffer + "]"; if (strlen(name) + 1 > 255) throw "Name is too long in " + reader.GetFilename() + " at [" + name + "]"; - samples.push_back(new Sample(filename, name)); + samples.emplace_back(filename, name); ShowProgress(); } @@ -144,13 +144,13 @@ static void WriteSFO(Samples &samples, FileWriter &writer) { writer.WriteString("// \"file name\" internal name\n"); - for (Samples::iterator iter = samples.begin(); iter != samples.end(); iter++) { - Sample *sample = *iter; + for (auto iter = samples.begin(); iter != samples.end(); ++iter) { + Sample &sample = *iter; - writer.WriteString("\"%s\" %s\n", sample->GetFilename().c_str(), sample->GetName().c_str()); + writer.WriteString("\"%s\" %s\n", sample.GetFilename().c_str(), sample.GetName().c_str()); - FileWriter sample_writer(sample->GetFilename()); - sample->WriteSample(sample_writer); + FileWriter sample_writer(sample.GetFilename()); + sample.WriteSample(sample_writer); sample_writer.Close(); ShowProgress(); @@ -202,7 +202,7 @@ int main(int argc, char *argv[]) strncpy(sfo_file, argv[2], sizeof(sfo_file)); char *ext = strrchr(sfo_file, '.'); if (ext == NULL || strlen(ext) != 4 || strcmp(ext, ".cat") != 0) { - throw string("Unexpected extension; expected \".cat\""); + throw std::string("Unexpected extension; expected \".cat\""); } strcpy(ext, ".sfo"); @@ -235,15 +235,10 @@ int main(int argc, char *argv[]) } if (_interactive) printf("\nDone\n"); - } catch (const string &s) { + } catch (const std::string &s) { fprintf(stderr, "An error occured: %s\n", s.c_str()); ret = -1; } - /* Clear up the samples */ - while (samples.size() != 0) { - delete samples.back(); - samples.pop_back(); - } return ret; } diff --git a/src/io.cpp b/src/io.cpp index 6257b26..2bc8798 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -23,7 +23,7 @@ #include "stdafx.h" #include "io.hpp" -FileReader::FileReader(string filename, bool binary) +FileReader::FileReader(const std::string &filename, bool binary) { this->file = fopen(filename.c_str(), binary ? "rb" : "r"); this->filename = filename; @@ -84,13 +84,13 @@ uint32_t FileReader::GetPos() return ftell(this->file); } -string FileReader::GetFilename() const +const std::string &FileReader::GetFilename() const { return this->filename; } -FileWriter::FileWriter(string filename, bool binary) +FileWriter::FileWriter(const std::string &filename, bool binary) { this->filename_new = filename + ".new"; this->filename = filename; @@ -156,7 +156,7 @@ uint32_t FileWriter::GetPos() return ftell(this->file); } -string FileWriter::GetFilename() const +const std::string &FileWriter::GetFilename() const { return this->filename; } @@ -168,7 +168,7 @@ void FileWriter::Close() this->file = NULL; /* Then remove the existing .bak file */ - string filename_bak = this->filename + ".bak"; + std::string filename_bak = this->filename + ".bak"; if (unlink(filename_bak.c_str()) != 0 && errno != ENOENT) { fprintf(stderr, "Warning: could not remove %s (%s)\n", filename_bak.c_str(), strerror(errno)); } diff --git a/src/io.hpp b/src/io.hpp index ecc298a..41118ec 100644 --- a/src/io.hpp +++ b/src/io.hpp @@ -23,15 +23,12 @@ #ifndef IO_H #define IO_H -/** A string is a string; simple as that */ -typedef std::string string; - /** * Simple class to perform binary and string reading from a file. */ class FileReader { FILE *file; ///< The file to be read by this instance - string filename; ///< The filename of the file + std::string filename; ///< The filename of the file public: /** @@ -39,7 +36,7 @@ class FileReader { * @param filename the file to read from * @param binary read the file as binary or text? */ - FileReader(string filename, bool binary = true); + FileReader(const std::string &filename, bool binary = true); /** * Cleans up our mess @@ -96,7 +93,7 @@ class FileReader { * Get the filename of this file. * @return the filename */ - string GetFilename() const; + const std::string &GetFilename() const; }; /** @@ -104,8 +101,8 @@ class FileReader { */ class FileWriter { FILE *file; ///< The file to be read by this instance - string filename; ///< The filename of the file - string filename_new; ///< The filename for the temporary file + std::string filename; ///< The filename of the file + std::string filename_new; ///< The filename for the temporary file public: /** @@ -113,7 +110,7 @@ class FileWriter { * @param filename the file to write to * @param binary write the file as binary or text? */ - FileWriter(string filename, bool binary = true); + FileWriter(const std::string &filename, bool binary = true); /** * Cleans up our mess @@ -162,7 +159,7 @@ class FileWriter { * Get the filename of this file. * @return the filename */ - string GetFilename() const; + const std::string &GetFilename() const; /** * Close the output, i.e. commit the file to disk. diff --git a/src/sample.cpp b/src/sample.cpp index d7085dd..3231e5a 100644 --- a/src/sample.cpp +++ b/src/sample.cpp @@ -55,7 +55,7 @@ static const uint32_t RIFF_HEADER_SIZE = 44; * @param reader the reader to read from * @return the read string */ -static string ReadString(FileReader &reader) +static std::string ReadString(FileReader &reader) { uint8_t name_len = reader.ReadByte(); char buffer[256]; @@ -71,7 +71,7 @@ static string ReadString(FileReader &reader) * @param str the string to write * @param writer the writer to write to */ -static void WriteString(const string str, FileWriter &writer) +static void WriteString(const std::string &str, FileWriter &writer) { uint8_t str_len = (uint8_t)(str.length() + 1); writer.WriteByte(str_len); @@ -79,31 +79,24 @@ static void WriteString(const string str, FileWriter &writer) } -Sample::Sample(FileReader &reader) : - sample_data(NULL) +Sample::Sample(FileReader &reader) { this->offset = reader.ReadDword() & 0x7FFFFFFF; this->size = reader.ReadDword(); } -Sample::Sample(string filename, string name) : +Sample::Sample(const std::string &filename, const std::string &name) : offset(0), name(name), - filename(filename), - sample_data(NULL) + filename(filename) { FileReader sample_reader(filename); this->ReadSample(sample_reader, false); } -Sample::~Sample() -{ - free(this->sample_data); -} - void Sample::ReadSample(FileReader &reader, bool check_size) { - assert(this->sample_data == NULL); + assert(this->sample_data.empty()); if (reader.ReadDword() != 'FFIR') throw "Unexpected chunk; expected \"RIFF\" in " + reader.GetFilename(); @@ -143,13 +136,13 @@ void Sample::ReadSample(FileReader &reader, bool check_size) this->sample_size = reader.ReadDword(); if (this->sample_size + RIFF_HEADER_SIZE > size) throw "Unexpected data chunk size in " + reader.GetFilename(); - this->sample_data = (uint8_t *)malloc(this->size - RIFF_HEADER_SIZE); - reader.ReadRaw(this->sample_data, this->size - RIFF_HEADER_SIZE); + this->sample_data.resize(this->size - RIFF_HEADER_SIZE); + reader.ReadRaw(this->sample_data.data(), this->size - RIFF_HEADER_SIZE); } void Sample::ReadCatEntry(FileReader &reader, bool new_format) { - assert(this->sample_data == NULL); + assert(this->sample_data.empty()); if (reader.GetPos() != this->GetOffset()) throw "Invalid offset in file " + reader.GetFilename(); @@ -158,8 +151,8 @@ void Sample::ReadCatEntry(FileReader &reader, bool new_format) if (!new_format && this->GetName().compare("Corrupt sound") == 0) { /* In the old format there was one sample that was raw PCM. */ this->sample_size = this->size; - this->sample_data = (uint8_t *)malloc(this->sample_size); - reader.ReadRaw(this->sample_data, this->sample_size); + this->sample_data.resize(this->sample_size); + reader.ReadRaw(this->sample_data.data(), this->sample_size); this->size += RIFF_HEADER_SIZE; } else { @@ -182,7 +175,7 @@ void Sample::ReadCatEntry(FileReader &reader, bool new_format) void Sample::WriteSample(FileWriter &writer) const { - assert(this->sample_data != NULL); + assert(!this->sample_data.empty()); writer.WriteDword('FFIR'); writer.WriteDword(this->size - 8); @@ -199,12 +192,12 @@ void Sample::WriteSample(FileWriter &writer) const writer.WriteDword('atad'); writer.WriteDword(this->sample_size); - writer.WriteRaw(this->sample_data, this->size - RIFF_HEADER_SIZE); + writer.WriteRaw(this->sample_data.data(), this->size - RIFF_HEADER_SIZE); } void Sample::WriteCatEntry(FileWriter &writer) const { - assert(this->sample_data != NULL); + assert(!this->sample_data.empty()); if (writer.GetPos() != this->GetOffset()) throw "Invalid offset when writing file " + writer.GetFilename(); @@ -217,12 +210,12 @@ void Sample::WriteCatEntry(FileWriter &writer) const WriteString(this->GetFilename(), writer); } -string Sample::GetName() const +const std::string &Sample::GetName() const { return this->name; } -string Sample::GetFilename() const +const std::string &Sample::GetFilename() const { return this->filename; } diff --git a/src/sample.hpp b/src/sample.hpp index cc3514c..2bc6d20 100644 --- a/src/sample.hpp +++ b/src/sample.hpp @@ -34,15 +34,15 @@ class Sample { uint32_t offset; ///< Offset from the begin of the cat uint32_t size; ///< The size of the WAV RIFF, i.e. excluding name and filename - string name; ///< The name of the sample - string filename; ///< The filename of the sample + std::string name; ///< The name of the sample + std::string filename; ///< The filename of the sample uint16_t num_channels; ///< Number of channels; either 1 or 2 uint32_t sample_rate; ///< Sample rate; either 11025, 22050 or 44100 uint16_t bits_per_sample; ///< Number of bits per sample; either 8 or 16 uint32_t sample_size; ///< The size of the raw data below - uint8_t *sample_data; ///< The actual raw sample data + std::vector sample_data; ///< The actual raw sample data public: /** @@ -58,13 +58,7 @@ class Sample { * @param filename the file to read the sample from * @param name the name of the sample */ - Sample(string filename, string name); - - /** - * Cleans up our mess. - */ - ~Sample(); - + Sample(const std::string &filename, const std::string &name); /** * Reads a sample from a reader. @@ -101,13 +95,13 @@ class Sample { * Get the name of the sample. * @return the name of the sample */ - string GetName() const; + const std::string &GetName() const; /** * Get the filename of the sample * @return the filename of the sample */ - string GetFilename() const; + const std::string &GetFilename() const; /** @@ -137,6 +131,6 @@ class Sample { }; /** Lets have us a vector of samples */ -typedef std::vector Samples; +using Samples = std::vector; #endif /* SAMPLE_HPP */ diff --git a/src/stdafx.h b/src/stdafx.h index 32f00a5..6d93fb8 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -33,9 +34,6 @@ #include #if defined(_MSC_VER) - typedef unsigned char uint8_t; - typedef unsigned short uint16_t; - typedef unsigned int uint32_t; #define UNUSED #define fileno _fileno