Jamoma API  0.6.0.a19
j.folder.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup implementationMaxExternals
4  *
5  * @brief j.folder - Perform operations on folders in the filesystem.
6  *
7  * @details
8  *
9  * @authors Tim Place, Trond Lossius
10  *
11  * @copyright Copyright © 2009 by Tim Place @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 
17 #include "JamomaForMax.h"
18 
19 typedef struct _folder {
20  t_object obj;
21 } t_folder;
22 
23 // Prototypes
24 void *folder_new(t_symbol *msg, short argc, t_atom *argv);
25 void folder_assist(t_folder *x, void *b, long msg, long arg, char *dst);
26 void folder_makedir(t_folder *x, t_symbol *path);
27 void folder_copy(t_folder *x, t_symbol *srcpath, t_symbol *dstpath);
28 
29 
30 
31 // Class Globals
32 t_class *folder_class;
33 
34 
35 /************************************************************************************/
36 // Main() Function
37 int JAMOMA_EXPORT_MAXOBJ main(void)
38 
39 {
40  common_symbols_init();
41 
42  t_class *c = class_new("j.folder",(method)folder_new, (method)0L, sizeof(t_folder), (method)0L, A_GIMME, 0);
43 
44  class_addmethod(c, (method)folder_makedir, "make", A_SYM, 0);
45  class_addmethod(c, (method)folder_assist, "assist", A_CANT, 0);
46  class_addmethod(c, (method)stdinletinfo, "inletinfo",A_CANT, 0);
47  class_addmethod(c, (method)folder_copy, "copy", A_SYM, A_SYM, 0);
48 
49  // Finalize our class
50  class_register(CLASS_BOX, c);
51  folder_class = c;
52 
53  return 0;
54 }
55 
56 
57 /************************************************************************************/
58 // Object Creation Method
59 
60 void *folder_new(t_symbol *msg, short argc, t_atom *argv)
61 {
62  t_folder *x;
63 
64  x = (t_folder *)object_alloc(folder_class);
65  if (x) {
66  object_obex_store((void *)x, _sym_dumpout, (object *)outlet_new(x,NULL));
67 
68  attr_args_process(x,argc,argv);
69  }
70  return (x);
71 }
72 
73 
74 
75 /************************************************************************************/
76 // Methods bound to input/inlets
77 
78 // Method for Assistance Messages
79 void folder_assist(t_folder *x, void *b, long msg, long arg, char *dst)
80 {
81  if (msg==1) // Inlets
82  strcpy(dst, "(int/symbol/list) Input");
83  else if (msg==2) { // Outlets
84  switch(arg) {
85  case 0: strcpy(dst, "dumpout"); break;
86  }
87  }
88 }
89 
90 
91 
92 void folder_domakedir(t_folder *x, t_symbol *s, long argc, t_atom *argv)
93 {
94  short path = 0; // parent folder, which we supply
95  short createdPath = 0; // the new folder after it is created
96  char *folderName; // the name of the new folder
97  char fullpath[4096];
98  short err = 0;
99  char temp[256];
100 
101  path_nameconform(s->s_name, fullpath, PATH_STYLE_MAX, PATH_TYPE_ABSOLUTE);
102  folderName = strrchr(fullpath, '/');
103 
104  if (folderName) {
105  *folderName = 0;
106  folderName++;
107 
108  err = path_frompathname(fullpath, &path, temp);
109  if (!err)
110  err = path_createfolder(path, folderName, &createdPath);
111  if (err)
112  object_error((t_object*)x, "error %hd trying to create folder", err);
113  }
114  object_obex_dumpout(x, _sym_bang, 0, NULL);
115 }
116 
117 void folder_makedir(t_folder *x, t_symbol *path)
118 {
119  defer(x, (method)folder_domakedir, path, 0, 0);
120 }
121 
122 
123 void folder_docopy(t_folder *x, t_symbol *srcin, long argc, t_atom *argv)
124 {
125  t_symbol* dstin = atom_getsym(argv);
126  char srcname[MAX_PATH_CHARS];
127  short srcpath = 0;
128  char srcfilename[MAX_FILENAME_CHARS];
129  char dstname[MAX_PATH_CHARS];
130  short dstpath = 0;
131  char dstfilename[MAX_FILENAME_CHARS];
132  short newpath = 0;
133  char* tempstr = NULL;
134 
135  strncpy_zero(srcname, srcin->s_name, MAX_PATH_CHARS);
136  path_frompathname(srcname, &srcpath, srcfilename);
137 
138  strncpy_zero(dstname, dstin->s_name, MAX_PATH_CHARS);
139  tempstr = strrchr(dstname, '/');
140  *tempstr = 0;
141  tempstr++;
142  path_frompathname(dstname, &dstpath, dstfilename);
143  if (tempstr)
144  strncpy_zero(dstfilename, tempstr, MAX_FILENAME_CHARS);
145 
146  if (!srcfilename[0])
147  path_copyfolder(srcpath, dstpath, dstfilename, true, &newpath);
148  else
149  path_copyfile(srcpath, srcfilename, dstpath, dstfilename);
150  object_obex_dumpout(x, _sym_bang, 0, NULL);
151 }
152 
153 void folder_copy(t_folder *x, t_symbol *srcpath, t_symbol *dstpath)
154 {
155  t_atom a;
156 
157  atom_setsym(&a, dstpath);
158  defer(x, (method)folder_docopy, srcpath, 1, &a);
159 }
160 
161 
162 
int JAMOMA_EXPORT_MAXOBJ main(void)
Set up this class as a Max external the first time an object of this kind is instantiated.
Definition: j.folder.cpp:37
Various utilities for interfacing with Max that are not specific to JamomaModular as such...