aboutsummaryrefslogtreecommitdiff
path: root/src/path.c
blob: d75e6bd884a9bccff0d06d462a48584aaa864c7f (plain)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272

#include "tof.h"
#define SLASH '/'

static char path_buf_a[MAXPDSTRING];
static char path_buf_b[MAXPDSTRING];

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;



static int path_is_absolute(char *dir, int length)
{
    if ((length && dir[0] == '/') || (dir[0] == '~')
#ifdef MSW
        || dir[0] == '%' || (length > 2 && (dir[1] == ':' && dir[2] == '/'))
#endif
        )
    {
        return 1;
    } else {
        return 0;            
    }
}


// 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_symbol(t_path *x, t_symbol *s) {
	
	
	strcpy(path_buf_a,s->s_name);
	int length = strlen(path_buf_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 ( path_is_absolute(path_buf_a,length  )) {
			getRelativeFilename(path_buf_b,x->dir->s_name,path_buf_a);
			result = gensym(path_buf_b);
		} else {
			result = gensym(path_buf_a);
		}
		
	} else {
		
		// TRANFORM RELATIVE PATHS TO ABSOLUTE PATHS
		
		// Is this a relative path?
		if ( path_is_absolute(path_buf_a,length)  ) {
			result = gensym(path_buf_a);
		} else {
			// remove extra ../
			strcpy(path_buf_b,x->dir->s_name);
			char* dir = path_buf_b;
			char* p;
			int l = strlen(dir);
			if (dir[l-1] == '/') dir[l-1] = '\0';
			char* file = path_buf_a;
			while( strncmp(file,"../",3) == 0 ) {
				file = file + 3;
				p = strrchr(dir,'/');
				if (p) *p = '\0';
			}
			
			strcat(dir,"/");
			strcat(dir,file);
			result = gensym(dir);
		}
	}	

	outlet_symbol(x->outlet1, result);
}






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(path_buf_a,x->dir->s_name);
	
	//strcat(path_buf_a,"/");
	x->dir = gensym(path_buf_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_addsymbol(path_class,path_symbol);
  
  //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);
  
}