Code

Merge from trunk.
[inkscape.git] / src / util / ege-tags.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is EGE Tagging Support.
15  *
16  * The Initial Developer of the Original Code is
17  * Jon A. Cruz.
18  * Portions created by the Initial Developer are Copyright (C) 2009
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */
37 #if HAVE_CONFIG_H
38 #include "config.h"
39 #endif // HAVE_CONFIG_H
41 #if HAVE_LIBINTL_H
42 #include <libintl.h>
43 #endif // HAVE_LIBINTL_H
45 #if !defined(_)
46 #define _(s) gettext(s)
47 #endif // !defined(_)
49 #include <set>
50 #include <algorithm>
51 #include <functional>
53 #include "ege-tags.h"
55 #include <glib.h>
57 namespace ege
58 {
60 Label::Label(std::string const& lang, std::string const& value) :
61     lang(lang),
62     value(value)
63 {
64 }
66 Label::~Label()
67 {
68 }
70 // =========================================================================
72 Tag::~Tag()
73 {
74 }
76 Tag::Tag(std::string const& key) :
77     key(key)
78 {
79 }
81 // =========================================================================
83 TagSet::TagSet() :
84     lang(),
85     tags(),
86     counts()
87 {
88 }
90 TagSet::~TagSet()
91 {
92 }
94 void TagSet::setLang(std::string const& lang)
95 {
96     if (lang != this->lang) {
97         this->lang = lang;
98     }
99 }
102 struct sameLang : public std::binary_function<Label, Label, bool> {
103     bool operator()(Label const& x, Label const& y) const { return (x.lang == y.lang); }
104 };
107 bool TagSet::addTag(Tag const& tag)
109     bool present = false;
111     for ( std::vector<Tag>::iterator it = tags.begin(); (it != tags.end()) && !present; ++it ) {
112         if (tag.key == it->key) {
113             present = true;
115             for ( std::vector<Label>::const_iterator it2 = tag.labels.begin(); it2 != tag.labels.end(); ++it2 ) {
116                 std::vector<Label>::iterator itOld = std::find_if( it->labels.begin(), it->labels.end(), std::bind2nd(sameLang(), *it2) );
117                 if (itOld != it->labels.end()) {
118                     itOld->value = it2->value;
119                 } else {
120                     it->labels.push_back(*it2);
121                 }
122             }
123         }
124     }
126     if (!present) {
127         tags.push_back(tag);
128         counts[tag.key] = 0;
129     }
131     return present;
135 std::vector<Tag> const& TagSet::getTags()
137     return tags;
140 int TagSet::getCount( std::string const& key )
142     int count = 0;
143     if ( counts.find(key) != counts.end() ) {
144         count = counts[key];
145     }
146     return count;
149 void TagSet::increment( std::string const& key )
151     if ( counts.find(key) != counts.end() ) {
152         counts[key]++;
153     } else {
154         Tag tag(key);
155         tags.push_back(tag);
156         counts[key] = 1;
157     }
160 void TagSet::decrement( std::string const& key )
162     if ( counts.find(key) != counts.end() ) {
163         counts[key]--;
164     }
167 } // namespace ege
169 /*
170   Local Variables:
171   mode:c++
172   c-file-style:"stroustrup"
173   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
174   indent-tabs-mode:nil
175   fill-column:99
176   End:
177 */
178 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :