initial pass at session-renaming functionality
[ardour.git] / libs / ardour / uri_map.cc
1 /*
2     Copyright (C) 2008-2010 Paul Davis
3     Author: David Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <cassert>
22 #include <iostream>
23
24 #include <stdint.h>
25 #include <string.h>
26
27 #include <glib.h>
28
29 #include "pbd/error.h"
30
31 #include "ardour/uri_map.h"
32
33 using namespace std;
34
35 namespace ARDOUR {
36
37
38 URIMap::URIMap()
39 {
40         uri_map_feature_data.uri_to_id     = &URIMap::uri_map_uri_to_id;
41         uri_map_feature_data.callback_data = this;
42         uri_map_feature.URI                = LV2_URI_MAP_URI;
43         uri_map_feature.data               = &uri_map_feature_data;
44
45         uri_unmap_feature_data.id_to_uri     = &URIMap::uri_unmap_id_to_uri;
46         uri_unmap_feature_data.callback_data = this;
47         uri_unmap_feature.URI                = LV2_URI_UNMAP_URI;
48         uri_unmap_feature.data               = &uri_unmap_feature_data;
49 }
50
51
52 uint32_t
53 URIMap::uri_to_id(const char* map,
54                   const char* uri)
55 {
56         const uint32_t id = static_cast<uint32_t>(g_quark_from_string(uri));
57         if (map && !strcmp(map, "http://lv2plug.in/ns/ext/event")) {
58                 GlobalToEvent::iterator i = _global_to_event.find(id);
59                 if (i != _global_to_event.end()) {
60                         return i->second;
61                 } else {
62                         if (_global_to_event.size() + 1 > UINT16_MAX) {
63                                 PBD::error << "Event URI " << uri << " ID out of range." << endl;
64                                 return 0;
65                         }
66                         const uint16_t ev_id = _global_to_event.size() + 1;
67                         assert(_event_to_global.find(ev_id) == _event_to_global.end());
68                         _global_to_event.insert(make_pair(id, ev_id));
69                         _event_to_global.insert(make_pair(ev_id, id));
70                         return ev_id;
71                 }
72         } else {
73                 return id;
74         }
75 }
76
77
78 const char*
79 URIMap::id_to_uri(const char*    map,
80                   const uint32_t id)
81 {
82         if (map && !strcmp(map, "http://lv2plug.in/ns/ext/event")) {
83                 EventToGlobal::iterator i = _event_to_global.find(id);
84                 if (i == _event_to_global.end()) {
85                         PBD::error << "Failed to unmap event URI " << id << endl;
86                         return NULL;
87                 }
88                 return g_quark_to_string(i->second);
89         } else {
90                 return g_quark_to_string(id);
91         }
92
93 }
94
95
96 uint32_t
97 URIMap::uri_map_uri_to_id(LV2_URI_Map_Callback_Data callback_data,
98                           const char*               map,
99                           const char*               uri)
100 {
101         URIMap* me = (URIMap*)callback_data;
102         return me->uri_to_id(map, uri);
103
104 }
105
106
107 const char*
108 URIMap::uri_unmap_id_to_uri(LV2_URI_Map_Callback_Data callback_data,
109                             const char*               map,
110                             uint32_t                  id)
111 {
112         URIMap* me = (URIMap*)callback_data;
113         return me->id_to_uri(map, id);
114 }
115
116
117 } // namespace ARDOUR
118