From 2e5e20e3e52b07cef2570245288c04d25863b90e Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 23 May 2009 18:51:37 -0400 Subject: [PATCH] Enhancement - Record accessors - Implemented a variety of get/set routines for Record. --- src/Record.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Record.h | 16 +++++++-------- src/T.cpp | 15 -------------- src/T.h | 1 - 4 files changed, 63 insertions(+), 24 deletions(-) diff --git a/src/Record.cpp b/src/Record.cpp index 4249ac7e0..513f7eb08 100644 --- a/src/Record.cpp +++ b/src/Record.cpp @@ -25,6 +25,8 @@ // //////////////////////////////////////////////////////////////////////////////// +#include +#include "util.h" #include "Record.h" //////////////////////////////////////////////////////////////////////////////// @@ -63,3 +65,56 @@ void Record::parse (const std::string& input) } //////////////////////////////////////////////////////////////////////////////// +std::vector Record::all () +{ + std::vector all; + foreach (a, mAtts) + all.push_back (a->second); + + return all; +} + +//////////////////////////////////////////////////////////////////////////////// +const std::string Record::get (const std::string& name) +{ + if (mAtts.find (name) != mAtts.end ()) + return mAtts[name].value (); + + return ""; +} + +//////////////////////////////////////////////////////////////////////////////// +int Record::getInt (const std::string& name) +{ + if (mAtts.find (name) != mAtts.end ()) + return ::atoi (mAtts[name].value ().c_str ()); + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// +void Record::set (const std::string& name, const std::string& value) +{ + mAtts[name] = Att (name, value); +} + +//////////////////////////////////////////////////////////////////////////////// +void Record::set (const std::string& name, int value) +{ + std::stringstream svalue; + svalue << value; + + mAtts[name] = Att (name, svalue.str ()); +} + +//////////////////////////////////////////////////////////////////////////////// +void Record::remove (const std::string& name) +{ + std::map copy = mAtts; + mAtts.clear (); + foreach (i, copy) + if (i->first != name) + mAtts[i->first] = i->second; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Record.h b/src/Record.h index 6483706dd..a23dee6db 100644 --- a/src/Record.h +++ b/src/Record.h @@ -28,6 +28,7 @@ #define INCLUDED_RECORD #include +#include #include "Att.h" class Record @@ -42,16 +43,15 @@ public: virtual std::string composeCSV () = 0; void parse (const std::string&); -/* - void getAttributes (std::map&); - const std::string getAttribute (const std::string&); - void setAttribute (const std::string&, const std::string&); - void setAttributes (const std::map &); - void removeAttribute (const std::string&); -*/ + std::vector all (); + const std::string get (const std::string&); + int getInt (const std::string&); + void set (const std::string&, const std::string&); + void set (const std::string&, int); + void remove (const std::string&); private: - std::vector mAtts; + std::map mAtts; }; #endif diff --git a/src/T.cpp b/src/T.cpp index 0ed0f9037..dff3fa9b1 100644 --- a/src/T.cpp +++ b/src/T.cpp @@ -215,21 +215,6 @@ void T::setAttribute (const std::string& name, const std::string& value) mAttributes[name] = value; } -//////////////////////////////////////////////////////////////////////////////// -void T::setAttributes (const std::map & attributes) -{ - foreach (i, attributes) - { - if (i->first.find (' ') != std::string::npos) - throw std::string ("An attribute name may not contain spaces"); - - if (i->second.find (' ') != std::string::npos) - throw std::string ("An attribute value may not contain spaces"); - - mAttributes[i->first] = i->second; - } -} - //////////////////////////////////////////////////////////////////////////////// void T::removeAttribute (const std::string& name) { diff --git a/src/T.h b/src/T.h index a17d5e7d5..3c4f62b7d 100644 --- a/src/T.h +++ b/src/T.h @@ -77,7 +77,6 @@ public: void getAttributes (std::map&); const std::string getAttribute (const std::string&); void setAttribute (const std::string&, const std::string&); - void setAttributes (const std::map &); void removeAttribute (const std::string&); void getAnnotations (std::map &) const;