Get rid of fieldsplitter

It was used more before, now it was just used to split author names
into component parts. This can be much better done with a dedicated
function.
This commit is contained in:
Ilari Liusvaara 2011-09-17 10:15:35 +03:00
parent 0c4431001c
commit e0a4bfa510
12 changed files with 57 additions and 177 deletions

View file

@ -1,7 +1,7 @@
FONT_SRC := unifontfull-5.1.20080820.hex
CC := g++-4.5
HOSTCC = $(CC)
OBJECTS = controllerdata.o fieldsplit.o memorymanip.o misc.o movie.o moviefile.o render.o rom.o zip.o fonts/font.o keymapper.o window.o window-sdl.o settings.o framerate.o mainloop.o rrdata.o png.o lsnesrc.o memorywatch.o command.o avsnoop.o moviedata.o controller.o framebuffer.o
OBJECTS = controllerdata.o memorymanip.o misc.o movie.o moviefile.o render.o rom.o zip.o fonts/font.o keymapper.o window.o window-sdl.o settings.o framerate.o mainloop.o rrdata.o png.o lsnesrc.o memorywatch.o command.o avsnoop.o moviedata.o controller.o framebuffer.o
#AVI dumper
OBJECTS += avidump/avidump-control.o avidump/avidump.o avidump/sox.o

View file

@ -1,7 +1,6 @@
#ifndef _controllerdata__hpp__included__
#define _controllerdata__hpp__included__
#include "fieldsplit.hpp"
#include <vector>
#include <stdexcept>

View file

@ -1,27 +0,0 @@
#include "lsnes.hpp"
#include "fieldsplit.hpp"
#include <iostream>
fieldsplitter::fieldsplitter(const std::string& _line) throw(std::bad_alloc)
{
line = _line;
position = 0;
}
fieldsplitter::operator bool() throw()
{
return (position < line.length());
}
fieldsplitter::operator std::string() throw(std::bad_alloc)
{
size_t nextp, oldp = position;
nextp = line.find_first_of("|", position);
if(nextp > line.length()) {
position = line.length();
return line.substr(oldp);
} else {
position = nextp + 1;
return line.substr(oldp, nextp - oldp);
}
}

View file

@ -1,46 +0,0 @@
#ifndef _fieldsplit_hpp__included__
#define _fieldsplit_hpp__included__
#include <string>
#include <stdexcept>
/**
* \brief Class for splitting string to fields.
*
* Splits string to fields on |
*/
class fieldsplitter
{
public:
/**
* \brief Create new string splitter
*
* Creates a new string splitter to split specified string.
* \param _line The line to split.
* \throws std::bad_alloc Out of memory.
*/
fieldsplitter(const std::string& _line) throw(std::bad_alloc);
/**
* \brief More fields coming
*
* Checks if more fields are coming.
*
* \return True if more fields are coming, otherwise false.
*/
operator bool() throw();
/**
* \brief Read next field
*
* Reads the next field and returns it. If field doesn't exist, it reads as empty string.
*
* \return The read field.
* \throws std::bad_alloc
*/
operator std::string() throw(std::bad_alloc);
private:
std::string line;
size_t position;
};
#endif

View file

@ -7,7 +7,6 @@
#include <set>
#include "misc.hpp"
#include "memorymanip.hpp"
#include "fieldsplit.hpp"
#include "command.hpp"
namespace

View file

@ -2,6 +2,7 @@
#include "mainloop.hpp"
#include "command.hpp"
#include "lua.hpp"
#include "moviedata.hpp"
#include "rrdata.hpp"
#include "lsnes.hpp"
#include "rom.hpp"
@ -46,12 +47,8 @@ struct moviefile generate_movie_template(std::vector<std::string> cmdline, loade
movie.gamename = o.substr(11);
if(o.length() >= 9 && o.substr(0, 9) == "--author=") {
std::string line = o.substr(9);
fieldsplitter f(line);
std::string full = f;
std::string nick = f;
if(full == "" && nick == "")
throw std::runtime_error("Bad author name, one of full or nickname must be present");
movie.authors.push_back(std::make_pair(full, nick));
auto g = split_author(line);
movie.authors.push_back(g);
}
}

View file

@ -5,7 +5,6 @@
#include <snes/snes.hpp>
#include "rom.hpp"
#include "memorymanip.hpp"
#include "fieldsplit.hpp"
#include "misc.hpp"
#include <sstream>
#include <iomanip>

View file

@ -31,29 +31,18 @@ namespace
{
numeric_setting savecompression("savecompression", 0, 9, 7);
class get_gamename_cmd : public command
{
public:
get_gamename_cmd() throw(std::bad_alloc) : command("get-gamename") {}
void invoke(const std::string& args) throw(std::bad_alloc, std::runtime_error)
{
function_ptr_command get_gamename("get-gamename", "Get the game name",
"Syntax: get-gamename\nPrints the game name\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
if(args != "")
throw std::runtime_error("This command does not take parameters");
messages << "Game name is '" << our_movie.gamename << "'" << std::endl;
}
std::string get_short_help() throw(std::bad_alloc) { return "Get the game name"; }
std::string get_long_help() throw(std::bad_alloc)
{
return "Syntax: get-gamename\n"
"Prints the game name\n";
}
} getnamec;
});
class print_authors_cmd : public command
{
public:
print_authors_cmd() throw(std::bad_alloc) : command("show-authors") {}
void invoke(const std::string& args) throw(std::bad_alloc, std::runtime_error)
function_ptr_command show_authors("show-authors", "Show the run authors",
"Syntax: show-authors\nShows the run authors\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error)
{
if(args != "")
throw std::runtime_error("This command does not take parameters");
@ -62,88 +51,39 @@ namespace
messages << (idx++) << ": " << i->first << "|" << i->second << std::endl;
}
messages << "End of authors list" << std::endl;
}
std::string get_short_help() throw(std::bad_alloc) { return "Show the run authors"; }
std::string get_long_help() throw(std::bad_alloc)
{
return "Syntax: show-authors\n"
"Shows the run authors\n";
}
} getauthorc;
});
class add_author_command : public command
{
public:
add_author_command() throw(std::bad_alloc) : command("add-author") {}
void invoke(const std::string& args) throw(std::bad_alloc, std::runtime_error)
{
function_ptr_command add_author("add-author", "Add an author",
"Syntax: add-author <fullname>\nSyntax: add-author |<nickname>\n"
"Syntax: add-author <fullname>|<nickname>\nAdds a new author\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
tokensplitter t(args);
fieldsplitter f(t.tail());
std::string full = f;
std::string nick = f;
if(full == "" && nick == "")
throw std::runtime_error("Bad author name");
our_movie.authors.push_back(std::make_pair(full, nick));
messages << (our_movie.authors.size() - 1) << ": " << full << "|" << nick << std::endl;
}
std::string get_short_help() throw(std::bad_alloc) { return "Add an author"; }
std::string get_long_help() throw(std::bad_alloc)
{
return "Syntax: add-author <fullname>\n"
"Syntax: add-author |<nickname>\n"
"Syntax: add-author <fullname>|<nickname>\n"
"Adds a new author\n";
}
} addauthorc;
auto g = split_author(t.tail());
our_movie.authors.push_back(g);
messages << (our_movie.authors.size() - 1) << ": " << g.first << "|" << g.second << std::endl;
});
class remove_author_command : public command
{
public:
remove_author_command() throw(std::bad_alloc) : command("remove-author") {}
void invoke(const std::string& args) throw(std::bad_alloc, std::runtime_error)
{
function_ptr_command remove_author("remove-author", "Remove an author",
"Syntax: remove-author <id>\nRemoves author with ID <id>\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
tokensplitter t(args);
uint64_t index = parse_value<uint64_t>(t.tail());
if(index >= our_movie.authors.size())
throw std::runtime_error("No such author");
our_movie.authors.erase(our_movie.authors.begin() + index);
}
std::string get_short_help() throw(std::bad_alloc) { return "Remove an author"; }
std::string get_long_help() throw(std::bad_alloc)
{
return "Syntax: remove-author <id>\n"
"Removes author with ID <id>\n";
}
} removeauthorc;
});
class edit_author_command : public command
{
public:
edit_author_command() throw(std::bad_alloc) : command("edit-author") {}
void invoke(const std::string& args) throw(std::bad_alloc, std::runtime_error)
{
function_ptr_command edit_author("edit-author", "Edit an author",
"Syntax: edit-author <authorid> <fullname>\nSyntax: edit-author <authorid> |<nickname>\n"
"Syntax: edit-author <authorid> <fullname>|<nickname>\nEdits author name\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
tokensplitter t(args);
uint64_t index = parse_value<uint64_t>(t);
if(index >= our_movie.authors.size())
throw std::runtime_error("No such author");
fieldsplitter f(t.tail());
std::string full = f;
std::string nick = f;
if(full == "" && nick == "") {
messages << "syntax: edit-author <authornum> <author>" << std::endl;
return;
}
our_movie.authors[index] = std::make_pair(full, nick);
}
std::string get_short_help() throw(std::bad_alloc) { return "Edit an author"; }
std::string get_long_help() throw(std::bad_alloc)
{
return "Syntax: edit-author <authorid> <fullname>\n"
"Syntax: edit-author <authorid> |<nickname>\n"
"Syntax: edit-author <authorid> <fullname>|<nickname>\n"
"Edits author name\n";
}
} editauthorc;
auto g = split_author(t.tail());
our_movie.authors[index] = g;
});
void warn_hash_mismatch(const std::string& mhash, const loaded_slot& slot,
const std::string& name)
@ -156,6 +96,25 @@ namespace
}
}
std::pair<std::string, std::string> split_author(const std::string& author) throw(std::bad_alloc,
std::runtime_error)
{
std::string _author = author;
std::string fullname;
std::string nickname;
size_t split = _author.find_first_of("|");
if(!split) {
fullname = _author;
} else {
fullname = _author.substr(0, split);
nickname = _author.substr(split + 1);
}
if(fullname == "" && nickname == "")
throw std::runtime_error("Bad author name");
return std::make_pair(fullname, nickname);
}
//Save state.
void do_save_state(const std::string& filename) throw(std::bad_alloc,
std::runtime_error)

View file

@ -18,6 +18,9 @@ extern bool system_corrupt;
std::vector<char>& get_host_memory();
movie& get_movie();
std::pair<std::string, std::string> split_author(const std::string& author) throw(std::bad_alloc,
std::runtime_error);
void do_save_state(const std::string& filename) throw(std::bad_alloc, std::runtime_error);
void do_save_movie(const std::string& filename) throw(std::bad_alloc, std::runtime_error);
void do_load_state(struct moviefile& _movie, int lmode);

View file

@ -5,6 +5,7 @@
#include "zip.hpp"
#include "misc.hpp"
#include "rrdata.hpp"
#include "moviedata.hpp"
#include <sstream>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
@ -88,10 +89,8 @@ void read_authors_file(zip_reader& r, std::vector<std::pair<std::string, std::st
std::string x;
while(std::getline(m, x)) {
strip_CR(x);
fieldsplitter fields(x);
std::string y = static_cast<std::string>(fields);
std::string z = static_cast<std::string>(fields);
authors.push_back(std::make_pair(y, z));
auto g = split_author(x);
authors.push_back(g);
}
delete &m;
} catch(...) {

View file

@ -16,7 +16,6 @@ using SNES::cartridge;
#include <boost/iostreams/device/back_inserter.hpp>
#include "rom.hpp"
#include "command.hpp"
#include "fieldsplit.hpp"
#include "zip.hpp"
#include "misc.hpp"
#include "memorymanip.hpp"

View file

@ -9,7 +9,6 @@
#include <csignal>
#include "keymapper.hpp"
#include "framerate.hpp"
#include "fieldsplit.hpp"
#include <sstream>
#include <fstream>