Code

* Lots of documentation for the Inkscape::XML namespace
[inkscape.git] / src / xml / node-fns.cpp
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
5 #include <map>
6 #include <cstring>
7 #include <string>
8 #include <glib.h> // g_assert()
10 #include "xml/node-iterators.h"
11 #include "algorithms/find-if-before.h"
13 namespace Inkscape {
14 namespace XML {
16 /* the id_permitted stuff is a temporary-ish hack */
18 namespace {
20 bool id_permitted_internal(GQuark qname) {
21     char const *qname_s=g_quark_to_string(qname);
22     return !strncmp("svg:", qname_s, 4) || !strncmp("sodipodi:", qname_s, 9) ||
23            !strncmp("inkscape:", qname_s, 9);
24 }
27 bool id_permitted_internal_memoized(GQuark qname) {
28     typedef std::map<GQuark, bool> IdPermittedMap;
29     static IdPermittedMap id_permitted_names;
31     IdPermittedMap::iterator found;
32     found = id_permitted_names.find(qname);
33     if ( found != id_permitted_names.end() ) {
34         return found->second;
35     } else {
36         bool permitted=id_permitted_internal(qname);
37         id_permitted_names[qname] = permitted;
38         return permitted;
39     }
40 }
42 }
44 bool id_permitted(Node const *node) {
45     g_return_val_if_fail(node != NULL, false);
47     if ( node->type() != ELEMENT_NODE ) {
48         return false;
49     }
51     return id_permitted_internal_memoized((GQuark)node->code());
52 }
54 struct node_matches {
55     node_matches(Node const &n) : node(n) {}
56     bool operator()(Node const &other) { return &other == &node; }
57     Node const &node;
58 };
60 // documentation moved to header
61 Node *previous_node(Node *node) {
62     using Inkscape::Algorithms::find_if_before;
64     if ( !node || !node->parent() ) {
65         return NULL;
66     }
68     Node *previous=find_if_before<NodeSiblingIterator>(
69         node->parent()->firstChild(), NULL, node_matches(*node)
70     );
72     g_assert(previous == NULL
73              ? node->parent()->firstChild() == node
74              : previous->next() == node);
76     return previous;
77 }
79 }
80 }
82 /*
83   Local Variables:
84   mode:c++
85   c-file-style:"stroustrup"
86   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
87   indent-tabs-mode:nil
88   fill-column:99
89   End:
90 */
91 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :