]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Update Notes API to support key=pair additions and lookup
authorAmos Jeffries <squid3@treenet.co.nz>
Tue, 6 Nov 2012 22:32:56 +0000 (11:32 +1300)
committerAmos Jeffries <squid3@treenet.co.nz>
Tue, 6 Nov 2012 22:32:56 +0000 (11:32 +1300)
* 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.

src/Notes.cc
src/Notes.h

index c0389f2f6653b530b5322d50a1d76421cac065b2..783f42fbf1357bf73593722b47e53458234f3f1b 100644 (file)
@@ -73,16 +73,32 @@ Note::match(HttpRequest *request, HttpReply *reply)
 }
 
 Note::Pointer
-Notes::add(const String &noteKey)
+Notes::findByName(const String &noteKey) 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 &noteKey, const String &noteValue)
+{
+    Note::Pointer key = add(noteKey);
+    key->addValue(noteValue);
+}
+
+Note::Pointer
+Notes::add(const String &noteKey)
+{
+    Note::Pointer note = findByName(noteKey);
+    if (note == NULL) {
+        note = new Note(noteKey);
+        notes.push_back(note);
+    }
     return note;
 }
 
index fab5922d86c19751241e5ee293af03d1ec8dec87..c93ce4ac94d2b963a57643cc5cb065047de8b851 100644 (file)
@@ -61,6 +61,7 @@ class Notes
 public:
     typedef Vector<Note::Pointer> 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 &noteKey);
+
+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 &noteKey, const String &noteValue);
+
+    /**
+     * Looks up a note by key name and returns a Note::Pointer to it
+     */
+    Note::Pointer findByName(const String &noteKey) const;
 };
 
 class NotePairs : public HttpHeader