From: Amos Jeffries Date: Tue, 6 Nov 2012 22:32:56 +0000 (+1300) Subject: Update Notes API to support key=pair additions and lookup X-Git-Tag: SQUID_3_4_0_1~471^2~15 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bc6003497aac5b97c47754092fe2d9c356f02bfe;p=thirdparty%2Fsquid.git Update Notes API to support key=pair additions and lookup * Adds a Notes::const_iterator definition to match Notes::iterator * Adds a Notes::findByName(note-name) method to locate notes given their textual key / name string. * Adds a Notes::add(key, value) method to insert key and value as a single operation. No more need for callers to be using Note::Pointer or be aware of the Note:: API just to insert the value. TODO: * add "key=value" syntax parser to Notes. The current parse() method onlyl accepts MiME header syntax (line- and whitespace- oriented). * add ostream operator to dump the notes list content to a stream. --- diff --git a/src/Notes.cc b/src/Notes.cc index c0389f2f66..783f42fbf1 100644 --- a/src/Notes.cc +++ b/src/Notes.cc @@ -73,16 +73,32 @@ Note::match(HttpRequest *request, HttpReply *reply) } Note::Pointer -Notes::add(const String ¬eKey) +Notes::findByName(const String ¬eKey) const { - typedef Notes::NotesList::iterator AMLI; + typedef Notes::NotesList::const_iterator AMLI; for (AMLI i = notes.begin(); i != notes.end(); ++i) { if ((*i)->key == noteKey) return (*i); } - Note::Pointer note = new Note(noteKey); - notes.push_back(note); + return Note::Pointer(); +} + +void +Notes::add(const String ¬eKey, const String ¬eValue) +{ + Note::Pointer key = add(noteKey); + key->addValue(noteValue); +} + +Note::Pointer +Notes::add(const String ¬eKey) +{ + Note::Pointer note = findByName(noteKey); + if (note == NULL) { + note = new Note(noteKey); + notes.push_back(note); + } return note; } diff --git a/src/Notes.h b/src/Notes.h index fab5922d86..c93ce4ac94 100644 --- a/src/Notes.h +++ b/src/Notes.h @@ -61,6 +61,7 @@ class Notes public: typedef Vector NotesList; typedef NotesList::iterator iterator; ///< iterates over the notes list + typedef NotesList::const_iterator const_iterator; ///< iterates over the notes list Notes(const char *aDescr, const char **metasBlacklist): descr(aDescr), blacklisted(metasBlacklist) {} Notes(): descr(NULL), blacklisted(NULL) {} @@ -93,6 +94,18 @@ private: * returns a pointer to the existing object. */ Note::Pointer add(const String ¬eKey); + +public: + /** + * Adds a note key and value to the notes list. + * If the key name already exists in list, add the given value to its set of values. + */ + void add(const String ¬eKey, const String ¬eValue); + + /** + * Looks up a note by key name and returns a Note::Pointer to it + */ + Note::Pointer findByName(const String ¬eKey) const; }; class NotePairs : public HttpHeader