~ubuntu-branches/ubuntu/wily/ecasound/wily-proposed

« back to all changes in this revision

Viewing changes to libecasound/eca-preset-map.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghedini
  • Date: 2011-05-12 17:58:03 UTC
  • Revision ID: james.westby@ubuntu.com-20110512175803-zy3lodjecabt9r3v
Tags: upstream-2.8.0
ImportĀ upstreamĀ versionĀ 2.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ------------------------------------------------------------------------
 
2
// eca-preset-map: Dynamic register for storing effect presets
 
3
// Copyright (C) 2000-2003 Kai Vehmanen
 
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
18
// ------------------------------------------------------------------------
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
#include <config.h>
 
22
#endif
 
23
 
 
24
#include <algorithm>
 
25
#include <list>
 
26
#include <vector>
 
27
#include <string>
 
28
 
 
29
#include <kvu_dbc.h>
 
30
 
 
31
#include "eca-object.h"
 
32
#include "eca-resources.h"
 
33
#include "eca-preset-map.h"
 
34
#include "global-preset.h"
 
35
 
 
36
using std::find;
 
37
using std::list;
 
38
using std::map;
 
39
using std::string;
 
40
using std::vector;
 
41
 
 
42
ECA_PRESET_MAP::ECA_PRESET_MAP(void)
 
43
{
 
44
#ifndef ECA_DISABLE_EFFECTS
 
45
  ECA_RESOURCES ecarc;
 
46
 
 
47
  string filename =
 
48
    ecarc.resource("user-resource-directory") + "/" + ecarc.resource("resource-file-effect-presets");
 
49
 
 
50
  string global_filename =
 
51
    ecarc.resource("resource-directory") + "/" + ecarc.resource("resource-file-effect-presets");
 
52
 
 
53
  load_preset_file(global_filename);
 
54
  load_preset_file(filename);
 
55
#endif
 
56
}
 
57
 
 
58
ECA_PRESET_MAP::~ECA_PRESET_MAP(void)
 
59
{
 
60
}
 
61
 
 
62
void ECA_PRESET_MAP::load_preset_file(const string& fname)
 
63
{
 
64
  RESOURCE_FILE preset_file;
 
65
  preset_file.resource_file(fname);
 
66
  preset_file.load();
 
67
  const vector<string>& pmap = preset_file.keywords();
 
68
  vector<string>::const_iterator p = pmap.begin();
 
69
  while(p != pmap.end()) {
 
70
    if (*p != "") preset_keywords_rep.push_back(*p);
 
71
    ++p;
 
72
  }
 
73
}
 
74
 
 
75
void ECA_PRESET_MAP::register_object(const string& keyword, const string& matchstr, ECA_OBJECT* object)
 
76
{
 
77
  if (find(preset_keywords_rep.begin(), preset_keywords_rep.end(), keyword) == preset_keywords_rep.end())
 
78
    preset_keywords_rep.push_back(keyword);
 
79
 
 
80
  ECA_OBJECT_MAP::register_object(keyword, matchstr, object);
 
81
}
 
82
 
 
83
void ECA_PRESET_MAP::unregister_object(const string& keyword)
 
84
{
 
85
  preset_keywords_rep.remove(keyword);
 
86
  ECA_OBJECT_MAP::unregister_object(keyword);
 
87
}
 
88
 
 
89
const list<string>& ECA_PRESET_MAP::registered_objects(void) const
 
90
{
 
91
  return(preset_keywords_rep);
 
92
}
 
93
 
 
94
bool ECA_PRESET_MAP::has_keyword(const std::string& keyword) const
 
95
{
 
96
  if (find(preset_keywords_rep.begin(), preset_keywords_rep.end(), keyword) == preset_keywords_rep.end())
 
97
    return(false);
 
98
 
 
99
  return (true);
 
100
}
 
101
 
 
102
const ECA_OBJECT* ECA_PRESET_MAP::object_expr(const string& expr) const
 
103
{
 
104
  if (find(preset_keywords_rep.begin(), preset_keywords_rep.end(), expr) != preset_keywords_rep.end()) {
 
105
    return(object(expr));
 
106
  }
 
107
  return(0);
 
108
}
 
109
 
 
110
const ECA_OBJECT* ECA_PRESET_MAP::object(const string& keyword) const
 
111
{
 
112
  const PRESET* retobj = 0;
 
113
 
 
114
#ifndef ECA_DISABLE_EFFECTS
 
115
  if (find(preset_keywords_rep.begin(), preset_keywords_rep.end(), keyword) != preset_keywords_rep.end()) {
 
116
    const list<string>& objlist = ECA_OBJECT_MAP::registered_objects();
 
117
 
 
118
    if (find(objlist.begin(), objlist.end(), keyword) == objlist.end()) {
 
119
      try {
 
120
        PRESET* obj = dynamic_cast<PRESET*>(new GLOBAL_PRESET(keyword));
 
121
        if (obj != 0) {
 
122
          const_cast<ECA_PRESET_MAP*>(this)->register_object(keyword, "^" + keyword + "$", obj);
 
123
          retobj = obj;
 
124
          //  std::cerr << "(eca-preset-map) registering obj; " << keyword << ".\n";
 
125
        }
 
126
        //  else std::cerr << "(eca-preset-map) fail (3); " << keyword << ".\n";
 
127
 
 
128
      }
 
129
      catch(...) { retobj = 0; }
 
130
 
 
131
      DBC_CHECK(find(objlist.begin(), objlist.end(), keyword) != objlist.end() || retobj == 0);
 
132
    }
 
133
    else {
 
134
      retobj = dynamic_cast<const PRESET*>(ECA_OBJECT_MAP::object(keyword));
 
135
      //  if (retobj == 0) std::cerr << "(eca-preset-map) fail (2); " << keyword << ".\n";
 
136
    }
 
137
  }
 
138
  //  else std::cerr << "(eca-preset-map) fail (1); " << keyword << ".\n";
 
139
#endif
 
140
 
 
141
  return(retobj);
 
142
}