Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions src/catcodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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;
}
10 changes: 5 additions & 5 deletions src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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));
}
Expand Down
17 changes: 7 additions & 10 deletions src/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,20 @@
#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:
/**
* Create a new reader for the given file.
* @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
Expand Down Expand Up @@ -96,24 +93,24 @@ class FileReader {
* Get the filename of this file.
* @return the filename
*/
string GetFilename() const;
const std::string &GetFilename() const;
};

/**
* Simple class to perform binary and string writing to a file.
*/
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:
/**
* Create a new writer for the given file.
* @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
Expand Down Expand Up @@ -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.
Expand Down
39 changes: 16 additions & 23 deletions src/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -71,39 +71,32 @@ 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);
writer.WriteRaw((const uint8_t *)str.c_str(), str_len);
}


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();

Expand Down Expand Up @@ -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();

Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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();

Expand All @@ -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;
}
Expand Down
20 changes: 7 additions & 13 deletions src/sample.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> sample_data; ///< The actual raw sample data

public:
/**
Expand All @@ -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.
Expand Down Expand Up @@ -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;


/**
Expand Down Expand Up @@ -137,6 +131,6 @@ class Sample {
};

/** Lets have us a vector of samples */
typedef std::vector<Sample *> Samples;
using Samples = std::vector<Sample>;

#endif /* SAMPLE_HPP */
4 changes: 1 addition & 3 deletions src/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <cstdarg>
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <cstring>

Expand All @@ -33,9 +34,6 @@
#include <string>

#if defined(_MSC_VER)
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
#define UNUSED

#define fileno _fileno
Expand Down
Loading