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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
#include "tof.h"
#define SLASH '/'
static t_class *path_class;
typedef struct _path {
t_object x_obj;
//char buffer[MAXPDSTRING];
t_outlet* outlet1;
//t_canvas* canvas;
t_symbol* dir;
t_symbol* mode;
t_symbol* dirmode;
t_symbol* s_current;
t_symbol* s_relative;
} t_path;
// Code from http://www.codeguru.com/cpp/misc/misc/fileanddirectorynaming/article.php/c263#more
static void getRelativeFilename(char* relativeFilename, char *currentDirectory, char *absoluteFilename)
{
// declarations - put here so this should work in a C compiler
int afMarker = 0, rfMarker = 0;
int cdLen = 0, afLen = 0;
int i = 0;
int levels = 0;
//static char relativeFilename[MAX_FILENAME_LEN+1];
cdLen = strlen(currentDirectory);
afLen = strlen(absoluteFilename);
// Handle DOS names that are on different drives:
if(currentDirectory[0] != absoluteFilename[0])
{
// not on the same drive, so only absolute filename will do
strcpy(relativeFilename, absoluteFilename);
return;
}
// they are on the same drive, find out how much of the current directory
// is in the absolute filename
i = 1;//ABSOLUTE_NAME_START;
while(i < afLen && i < cdLen && currentDirectory[i] == absoluteFilename[i])
{
i++;
}
if(i == cdLen && (absoluteFilename[i] == SLASH || absoluteFilename[i-1] == SLASH))
{
// the whole current directory name is in the file name,
// so we just trim off the current directory name to get the
// current file name.
if(absoluteFilename[i] == SLASH)
{
// a directory name might have a trailing slash but a relative
// file name should not have a leading one...
i++;
}
strcpy(relativeFilename, &absoluteFilename[i]);
return;
}
// The file is not in a child directory of the current directory, so we
// need to step back the appropriate number of parent directories by
// using "..\"s. First find out how many levels deeper we are than the
// common directory
afMarker = i;
levels = 1;
// count the number of directory levels we have to go up to get to the
// common directory
while(i < cdLen)
{
i++;
if(currentDirectory[i] == SLASH)
{
// make sure it's not a trailing slash
i++;
if(currentDirectory[i] != '\0')
{
levels++;
}
}
}
// move the absolute filename marker back to the start of the directory name
// that it has stopped in.
while(afMarker > 0 && absoluteFilename[afMarker-1] != SLASH)
{
afMarker--;
}
// add the appropriate number of "..\"s.
rfMarker = 0;
for(i = 0; i < levels; i++)
{
relativeFilename[rfMarker++] = '.';
relativeFilename[rfMarker++] = '.';
relativeFilename[rfMarker++] = SLASH;
}
// copy the rest of the filename into the result string
strcpy(&relativeFilename[rfMarker], &absoluteFilename[afMarker]);
//return relativeFilename;
}
// BANG: output the current root path
static void path_bang(t_path *x)
{
outlet_symbol(x->outlet1, x->dir );
}
static void path_anything(t_path *x, t_symbol *s, int ac, t_atom* av) {
int length = tof_anything_to_string(s,ac,av,tof_buf_temp_a);
t_symbol* result;
if ( x->mode == x->s_relative ) {
// TRANSFORM ABSOLUTE PATHS TO RELATIVE PATHS
// Is this a relative path?
// Checks for a starting / or a : as second character
if ( tof_path_is_absolute(tof_buf_temp_a,length )) {
getRelativeFilename(tof_buf_temp_b,x->dir->s_name,tof_buf_temp_a);
result = gensym(tof_buf_temp_b);
} else {
result = gensym(tof_buf_temp_a);
}
} else {
// TRANFORM RELATIVE PATHS TO ABSOLUTE PATHS
// Is this a relative path?
if ( tof_path_is_absolute(tof_buf_temp_a,length) ) {
result = gensym(tof_buf_temp_a);
} else {
strcpy(tof_buf_temp_b,x->dir->s_name);
strcat(tof_buf_temp_b,tof_buf_temp_a);
result = gensym(tof_buf_temp_b);
}
}
outlet_symbol(x->outlet1, result);
}
/*
void path_list(t_path *x,t_symbol *s, int argc, t_atom *argv)
{
if ( argc >= 2) {
x->value = atom_getfloat(argv);
x->inc = atom_getfloat(argv+1);
}
}
*/
static void path_free(t_path *x)
{
//binbuf_free(x->binbuf);
}
void *path_new(t_symbol *s, int argc, t_atom *argv)
{
t_path *x = (t_path *)pd_new(path_class);
x->s_current = gensym("current");
x->s_relative = gensym("relative");
int i;
t_symbol * mode_temp;
for ( i = 0; i < argc; i++) {
if ( IS_A_SYMBOL(argv,i) ) {
mode_temp = atom_getsymbol(argv+i);
if ( mode_temp == x->s_current ) x->dirmode = x->s_current;
if ( mode_temp == x->s_relative) x->mode = x->s_relative;
}
}
if ( x->dirmode == x->s_current) {
x->dir = tof_get_dir(tof_get_canvas());
} else {
x->dir = tof_get_dir(tof_get_root_canvas(tof_get_canvas()));
}
strcpy(tof_buf_temp_a,x->dir->s_name);
strcat(tof_buf_temp_a,"/");
x->dir = gensym(tof_buf_temp_a);
/*
inlet_new(&x->x_obj, &x->x_obj.ob_pd,
gensym("float"), gensym("set"));
floatinlet_new(&x->x_obj, &x->inc);
*/
x->outlet1 = outlet_new(&x->x_obj, &s_float);
return (void *)x;
}
void path_setup(void) {
path_class = class_new(gensym("path"),
(t_newmethod)path_new,
(t_method)path_free, sizeof(t_path),
CLASS_DEFAULT,
A_GIMME, 0);
class_addbang(path_class, path_bang);
class_addanything(path_class,path_anything);
//class_addmethod(path_class,
// (t_method)path_append, gensym("append"),
// A_GIMME, 0);
//class_addfloat (path_class, path_float);
/*class_addmethod(path_class,
(t_method)path_set, gensym("set"),
A_DEFFLOAT, 0);
*/
//class_addlist (path_class, path_list);
}
|