+++ /dev/null
-#ifndef MSP_DATAFILE_BINARYDICT_H_
-#define MSP_DATAFILE_BINARYDICT_H_
-
-#include <string>
-
-namespace Msp {
-namespace DataFile {
-
-/**
-Stores statement information for binary files.
-*/
-struct DictEntry
-{
- std::string keyword;
- std::string args;
-
- DictEntry() { }
- DictEntry(const std::string &k, const std::string &a): keyword(k), args(a) { }
-
- bool operator<(const DictEntry &o) const
- { return keyword<o.keyword || (keyword==o.keyword && args<o.args); }
-};
-
-} // namespace DataFile
-} // namespace Msp
-
-#endif
first(true),
float_precision(32)
{
- dict[-1] = DictEntry("__kwd", "iss");
- dict[-2] = DictEntry("__str", "is");
- dict[-3] = DictEntry("__flt", "i");
+ dict[-1] = StatementKey("__kwd", "iss");
+ dict[-2] = StatementKey("__str", "is");
+ dict[-3] = StatementKey("__flt", "i");
}
Statement BinaryParser::parse(bool raw)
if(!valid_signatures[j])
throw bad_definition("__kwd");
- dict[id] = DictEntry(kw, args);
+ dict[id] = StatementKey(kw, args);
}
else if(st.keyword=="__str")
{
if(!in)
return Statement();
- const DictEntry &de = get_item(dict, id);
+ const StatementKey &key = get_item(dict, id);
Statement result;
- result.keyword = de.keyword;
+ result.keyword = key.keyword;
result.source = src;
- for(unsigned j = 0; j<de.args.size(); ++j)
+ for(unsigned j=0; j<key.signature.size(); ++j)
{
- switch(de.args[j])
+ switch(key.signature[j])
{
case IntType::signature:
result.args.push_back(parse_int());
#define MSP_DATAFILE_BINARYPARSER_H_
#include <map>
-#include "binarydict.h"
#include "parsermode.h"
#include "type.h"
class BinaryParser: public ParserMode
{
private:
- typedef std::map<int, DictEntry> Dictionary;
+ typedef std::map<int, StatementKey> Dictionary;
typedef std::map<unsigned, std::string> StringMap;
Dictionary dict;
next_str_id(1),
float_precision(32)
{
- dict[DictEntry("__kwd", "iss")] = -1;
- dict[DictEntry("__str", "is")] = -2;
- dict[DictEntry("__flt", "i")] = -3;
+ dict[StatementKey("__kwd", "iss")] = -1;
+ dict[StatementKey("__str", "is")] = -2;
+ dict[StatementKey("__flt", "i")] = -3;
}
void BinaryWriter::set_float_precision(unsigned fp)
void BinaryWriter::write_(const Statement &st)
{
- int id = get_item(dict, DictEntry(st.keyword, st.get_signature()));
+ int id = get_item(dict, StatementKey(st.keyword, st.get_signature()));
write_int(id);
for(Statement::Arguments::const_iterator j = st.args.begin(); j!=st.args.end(); ++j)
void BinaryWriter::collect_keywords(const Statement &st)
{
- DictEntry de(st.keyword, st.get_signature());
+ StatementKey key(st.keyword, st.get_signature());
- if(!dict.count(de))
+ if(!dict.count(key))
{
Statement kst;
kst.keyword = "__kwd";
kst.args.push_back(next_kwd_id);
- kst.args.push_back(de.keyword);
- kst.args.push_back(de.args);
+ kst.args.push_back(key.keyword);
+ kst.args.push_back(key.signature);
write_(kst);
- dict[de] = next_kwd_id++;
+ dict[key] = next_kwd_id++;
}
for(ValueArray::const_iterator i = st.args.begin(); i!=st.args.end(); ++i)
#define MSP_DATAFILE_BINARYWRITER_H_
#include <map>
-#include "binarydict.h"
+#include "statement.h"
#include "type.h"
#include "writermode.h"
class BinaryWriter: public WriterMode
{
private:
- typedef std::map<DictEntry, int> Dictionary;
+ typedef std::map<StatementKey, int> Dictionary;
typedef std::map<std::string, unsigned> StringMap;
Dictionary dict;
try
{
- ActionKey key(st.keyword, st.get_signature());
+ StatementKey key(st.keyword, st.get_signature());
if(!aux_loaders.empty() && !has_action(key))
{
void Loader::add(const string &kwd, LoaderAction *act)
{
- ActionKey key(kwd, (act ? act->get_signature() : "*"));
+ StatementKey key(kwd, (act ? act->get_signature() : "*"));
ActionMap::iterator i = actions.find(key);
if(i!=actions.end())
{
aux_loaders.push_back(&ldr);
}
-bool Loader::has_action(const ActionKey &key) const
+bool Loader::has_action(const StatementKey &key) const
{
- ActionMap::const_iterator i = actions.lower_bound(ActionKey(key.keyword, string()));
+ ActionMap::const_iterator i = actions.lower_bound(StatementKey(key.keyword, string()));
for(; (i!=actions.end() && i->first.keyword==key.keyword); ++i)
if(signature_match(key.signature, i->first.signature))
return true;
return false;
}
-LoaderAction *Loader::find_action(const ActionKey &key) const
+LoaderAction *Loader::find_action(const StatementKey &key) const
{
- ActionMap::const_iterator begin = actions.lower_bound(ActionKey(key.keyword, string()));
- ActionMap::const_iterator end = actions.upper_bound(ActionKey(key.keyword, "~"));
+ ActionMap::const_iterator begin = actions.lower_bound(StatementKey(key.keyword, string()));
+ ActionMap::const_iterator end = actions.upper_bound(StatementKey(key.keyword, "~"));
if(begin==end)
throw unknown_keyword(key.keyword);
return cur_st->source;
}
-
-Loader::ActionKey::ActionKey(const string &k, const string &s):
- keyword(k),
- signature(s)
-{ }
-
-bool Loader::ActionKey::operator<(const ActionKey &other) const
-{
- if(keyword!=other.keyword)
- return keyword<other.keyword;
- return signature<other.signature;
-}
-
} // namespace DataFile
} // namespace Msp
class Loader
{
private:
- struct ActionKey
- {
- std::string keyword;
- std::string signature;
-
- ActionKey(const std::string &, const std::string &);
-
- bool operator<(const ActionKey &) const;
- };
-
- typedef std::map<ActionKey, LoaderAction *> ActionMap;
+ typedef std::map<StatementKey, LoaderAction *> ActionMap;
ActionMap actions;
const Statement *cur_st;
void add_auxiliary_loader(Loader &);
private:
- bool has_action(const ActionKey &) const;
- LoaderAction *find_action(const ActionKey &) const;
+ bool has_action(const StatementKey &) const;
+ LoaderAction *find_action(const StatementKey &) const;
protected:
/** Returns the source of the statement being processed. This can be used
{ return append(v); }
};
+struct StatementKey
+{
+ std::string keyword;
+ std::string signature;
+
+ StatementKey() { }
+ StatementKey(const std::string &k, const std::string &s): keyword(k), signature(s) { }
+
+ bool operator<(const StatementKey &o) const
+ { return keyword<o.keyword || (keyword==o.keyword && signature<o.signature); }
+};
+
} // namespace DataFile
} // namespace Msp