1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/*
Copyright (C) 2001 Stefan Westerfeld
[email protected]
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <dirent.h>
#include "soundserver.h"
#include "debug.h"
using namespace Arts;
using namespace std;
namespace Arts {
class SampleStorageEntry_impl : public SampleStorageEntry_skel {
protected:
string _name, _filename;
bool _completed;
FILE *file;
public:
SampleStorageEntry_impl(const string& name, const string& filename)
{
_name = name;
_filename = filename;
_completed = false;
file = fopen(_filename.c_str(),"w");
}
~SampleStorageEntry_impl()
{
if(file)
{
fclose(file);
file = 0;
}
unlink(_filename.c_str());
}
string name() { return _name; }
string filename() { return _filename; }
bool completed() { return _completed; }
void write(const vector<mcopbyte>& data)
{
arts_return_if_fail(file != 0);
fwrite(&data[0],1,data.size(),file);
}
void finish()
{
arts_return_if_fail(file != 0);
fclose(file);
file = 0;
_completed = true;
}
};
class SampleStorage_impl : public SampleStorage_skel {
protected:
string directory;
long uniqueID;
vector<SampleStorageEntry> entries;
public:
SampleStorage_impl()
{
uniqueID = 1;
}
void constructor(const string& newDirectory, bool clearOnInit)
{
arts_return_if_fail(directory.empty());
directory = newDirectory;
mkdir(directory.c_str(),0700);
if(clearOnInit) clear();
}
void clear()
{
DIR *dir = opendir(directory.c_str());
if(!dir) return;
struct dirent *de;
while((de = readdir(dir)) != 0)
{
string currentEntry = directory + "/" + de->d_name;
if(de->d_name[0] != '.')
{
unlink(currentEntry.c_str());
}
}
closedir(dir);
}
string encodeName(const string& name)
{
string::const_iterator i;
string result;
for(i = name.begin(); i != name.end(); i++)
{
unsigned char c = *i;
if ((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '_' || c == '-')
{
result += c;
}
else
{
char hex[17] = "0123456789ABCDEF";
result += '=';
result += hex[(c >> 4) & 0xf];
result += hex[c & 0xf];
}
}
return result;
}
SampleStorageEntry createEntry(const string& name)
{
char uniqueprefix[32];
sprintf(uniqueprefix,"s%ld-",uniqueID++);
string filename = directory + string("/")
+ uniqueprefix + encodeName(name);
return SampleStorageEntry::_from_base(
new SampleStorageEntry_impl(name, filename)
);
}
SampleStorageEntry findEntry(const string& name)
{
vector<SampleStorageEntry>::iterator i;
for(i = entries.begin(); i != entries.end(); i++)
if(i->name() == name) return *i;
return SampleStorageEntry::null();
}
void addEntry(SampleStorageEntry entry)
{
arts_return_if_fail(!entry.isNull());
/* remove the old entry for that name, if any */
SampleStorageEntry oldEntry = findEntry(entry.name());
if(!oldEntry.isNull())
removeEntry(oldEntry);
/* add the new entry */
entries.push_back(entry);
}
void removeEntry(SampleStorageEntry entry)
{
arts_return_if_fail(!entry.isNull());
vector<SampleStorageEntry>::iterator i;
for(i = entries.begin(); i != entries.end(); i++)
{
if(i->name() == entry.name())
{
entries.erase(i);
return;
}
}
arts_warning("SampleStorage::removeEntry(%s) failed\n",
entry.name().c_str());
}
};
REGISTER_IMPLEMENTATION(SampleStorage_impl);
}
|