Tags were not being set properly on postings

This commit is contained in:
John Wiegley 2010-03-17 02:18:46 -04:00
parent 0e34d08b8d
commit d7b8095c3d
3 changed files with 32 additions and 23 deletions

View file

@ -101,8 +101,10 @@ optional<string> item_t::get_tag(const mask_t& tag_mask,
return none; return none;
} }
item_t::string_map::iterator item_t::set_tag(const string& tag, item_t::string_map::iterator
const optional<string>& value) item_t::set_tag(const string& tag,
const optional<string>& value,
const bool overwrite_existing)
{ {
assert(! tag.empty()); assert(! tag.empty());
@ -116,14 +118,20 @@ item_t::string_map::iterator item_t::set_tag(const string& tag,
if (data && data->empty()) if (data && data->empty())
data = none; data = none;
std::pair<string_map::iterator, bool> result string_map::iterator i = metadata->find(tag);
= metadata->insert(string_map::value_type(tag, tag_data_t(data, false))); if (i == metadata->end()) {
assert(result.second); std::pair<string_map::iterator, bool> result
= metadata->insert(string_map::value_type(tag, tag_data_t(data, false)));
return result.first; assert(result.second);
return result.first;
} else {
if (overwrite_existing)
(*i).second = tag_data_t(data, false);
return i;
}
} }
void item_t::parse_tags(const char * p, void item_t::parse_tags(const char * p, bool overwrite_existing,
optional<date_t::year_type> current_year) optional<date_t::year_type> current_year)
{ {
if (const char * b = std::strchr(p, '[')) { if (const char * b = std::strchr(p, '[')) {
@ -157,17 +165,16 @@ void item_t::parse_tags(const char * p,
q = std::strtok(NULL, " \t")) { q = std::strtok(NULL, " \t")) {
const string::size_type len = std::strlen(q); const string::size_type len = std::strlen(q);
if (! tag.empty()) { if (! tag.empty()) {
if (! has_tag(tag)) { string_map::iterator i = set_tag(tag, string(p + (q - buf.get())),
string_map::iterator i = set_tag(tag, string(p + (q - buf.get()))); overwrite_existing);
(*i).second.second = true; (*i).second.second = true;
}
break; break;
} }
else if (q[0] == ':' && q[len - 1] == ':') { // a series of tags else if (q[0] == ':' && q[len - 1] == ':') { // a series of tags
for (char * r = std::strtok(q + 1, ":"); for (char * r = std::strtok(q + 1, ":");
r; r;
r = std::strtok(NULL, ":")) { r = std::strtok(NULL, ":")) {
string_map::iterator i = set_tag(r); string_map::iterator i = set_tag(r, none, overwrite_existing);
(*i).second.second = true; (*i).second.second = true;
} }
} }
@ -177,7 +184,7 @@ void item_t::parse_tags(const char * p,
} }
} }
void item_t::append_note(const char * p, void item_t::append_note(const char * p, bool overwrite_existing,
optional<date_t::year_type> current_year) optional<date_t::year_type> current_year)
{ {
if (note) { if (note) {
@ -187,7 +194,7 @@ void item_t::append_note(const char * p,
note = p; note = p;
} }
parse_tags(p, current_year); parse_tags(p, overwrite_existing, current_year);
} }
namespace { namespace {

View file

@ -157,12 +157,14 @@ public:
virtual optional<string> get_tag(const mask_t& tag_mask, virtual optional<string> get_tag(const mask_t& tag_mask,
const optional<mask_t>& value_mask = none) const; const optional<mask_t>& value_mask = none) const;
virtual string_map::iterator set_tag(const string& tag, virtual string_map::iterator
const optional<string>& value = none); set_tag(const string& tag,
const optional<string>& value = none,
const bool overwrite_existing = true);
virtual void parse_tags(const char * p, virtual void parse_tags(const char * p, bool overwrite_existing = true,
optional<date_t::year_type> current_year = none); optional<date_t::year_type> current_year = none);
virtual void append_note(const char * p, virtual void append_note(const char * p, bool overwrite_existing = true,
optional<date_t::year_type> current_year = none); optional<date_t::year_type> current_year = none);
static bool use_effective_date; static bool use_effective_date;

View file

@ -1199,7 +1199,7 @@ post_t * instance_t::parse_post(char * line,
// Parse the optional note // Parse the optional note
if (next && *next == ';') { if (next && *next == ';') {
post->append_note(++next, current_year); post->append_note(++next, true, current_year);
next = line + len; next = line + len;
DEBUG("textual.parse", "line " << linenum << ": " DEBUG("textual.parse", "line " << linenum << ": "
<< "Parsed a posting note"); << "Parsed a posting note");
@ -1218,7 +1218,7 @@ post_t * instance_t::parse_post(char * line,
if (! context.state_stack.empty()) { if (! context.state_stack.empty()) {
foreach (const state_t& state, context.state_stack) foreach (const state_t& state, context.state_stack)
if (state.type() == typeid(string)) if (state.type() == typeid(string))
post->parse_tags(boost::get<string>(state).c_str()); post->parse_tags(boost::get<string>(state).c_str(), true);
} }
TRACE_STOP(post_details, 1); TRACE_STOP(post_details, 1);
@ -1355,7 +1355,7 @@ xact_t * instance_t::parse_xact(char * line,
item = xact.get(); item = xact.get();
// This is a trailing note, and possibly a metadata info tag // This is a trailing note, and possibly a metadata info tag
item->append_note(p + 1, current_year); item->append_note(p + 1, true, current_year);
item->pos->end_pos = curr_pos; item->pos->end_pos = curr_pos;
item->pos->end_line++; item->pos->end_line++;
} else { } else {
@ -1389,7 +1389,7 @@ xact_t * instance_t::parse_xact(char * line,
if (! context.state_stack.empty()) { if (! context.state_stack.empty()) {
foreach (const state_t& state, context.state_stack) foreach (const state_t& state, context.state_stack)
if (state.type() == typeid(string)) if (state.type() == typeid(string))
xact->parse_tags(boost::get<string>(state).c_str()); xact->parse_tags(boost::get<string>(state).c_str(), false);
} }
TRACE_STOP(xact_details, 1); TRACE_STOP(xact_details, 1);