-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsdpfs.cpp
executable file
·175 lines (153 loc) · 5.88 KB
/
msdpfs.cpp
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
/**
msdpfs.cpp, by Dirk Leas
Copyright (C) 2017 Dirk Leas
This program is free software; you may redistribute and/or modify
it under the terms of the GNU General Public Licence as published by
the Free Software Foundation, either version 3 of said Licence, or
(at your option) any later version.
--
msdpfs - filesystem exists, rm, mkdir, cp and semi-related env
max-devkit api: quasi (https://tinyurl.com/khygtdf)
macos:
cmake ..
cmake --build .
windows:
cmake -G "Visual Studio 15 2017 Win64" -DWIN64:Bool=True ..
cmake --build . --config Release
*/
#ifdef _WIN32
#include <direct.h>
#endif
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include "c74_max.h"
using namespace c74::max;
struct t_msdpfs {
t_object ob;
void *status; // status from cp, exists, mkdir, rm
void *env_val; // value of environment variable from env
};
static t_class* this_class = nullptr;
const char* real_path(const char* f) {
static char name[MAX_PATH_CHARS];
path_nameconform(f, name, PATH_STYLE_SLASH, PATH_TYPE_BOOT);
return name;
}
void* msdpfs_new(t_symbol* name, long argc, t_atom* argv) {
t_msdpfs* self = (t_msdpfs*) object_alloc(this_class);
self->env_val = outlet_new(self, "environment variable value"); // odd, had to add backwards order?!
self->status = outlet_new(self, "status, 1=success, 0=failure");
return self;
}
void msdpfs_free(t_msdpfs* self) {}
void msdpfs_docp(t_msdpfs* self, t_symbol* s, long argc, t_atom* argv) {
if ((argc != 2) || (atom_gettype(&argv[0]) != A_SYM) || (atom_gettype(&argv[0]) != A_SYM)) {
object_error((t_object*)self, "source/destination missing or incorrect type, specify valid files%s", "");
outlet_int(self->status, 0);
return;
}
try {
std::ifstream src(real_path(atom_getsym(&argv[0])->s_name), std::ios::binary);
std::ofstream dst(real_path(atom_getsym(&argv[1])->s_name), std::ios::binary);
dst << src.rdbuf();
outlet_int(self->status, 1);
return;
} catch (const std::exception& e) {
object_error((t_object*)self, "can't copy from %s to %s, invalid source or existing destination",
atom_getsym(&argv[0])->s_name, atom_getsym(&argv[1])->s_name);
outlet_int(self->status, 0);
return;
}
}
void msdpfs_doenv(t_msdpfs* self, t_symbol* s, long argc, t_atom* argv) {
static char* value;
value = getenv(s->s_name);
object_error((t_object*)self, "environment variable: %s, value: %s", s->s_name, value);
if (value != NULL) outlet_anything(self->status, gensym(value), 0, NULL);
else outlet_anything(self->status, gensym(""), 0, NULL);
}
void msdpfs_doexists(t_msdpfs* self, t_symbol* s, long argc, t_atom* argv) {
struct stat f;
if (stat(real_path(s->s_name), &f) == 0) outlet_int(self->status, 1);
else {
object_error((t_object*)self, "no such file or directory %s", s->s_name);
outlet_int(self->status, 0);
}
}
void msdpfs_domkdir(t_msdpfs* self, t_symbol* s, long argc, t_atom* argv) {
int status = -1;
struct stat f;
if (stat(real_path(s->s_name), &f) == 0) {
object_error((t_object*)self, "directory %s already exists", s->s_name);
outlet_int(self->status, 0);
return;
}
#ifdef _WIN32
status = _mkdir(real_path(s->s_name));
#else
status = mkdir(real_path(s->s_name), 0777);
#endif
if (status == 0) {
outlet_int(self->status, 1);
return;
}
else {
object_error((t_object*)self, "can't make directory %s", s->s_name);
outlet_int(self->status, 0);
return;
}
}
void msdpfs_dorm(t_msdpfs* self, t_symbol* s, long argc, t_atom* argv) {
struct stat f;
if (stat(real_path(s->s_name), &f) != 0) {
object_error((t_object*)self, "no such file or directory %s", s->s_name);
outlet_int(self->status, 0);
return;
}
if (remove(real_path(s->s_name)) == 0) {
outlet_int(self->status, 1);
return;
}
else {
object_error((t_object*)self, "can't remove file or directory %s, error %d", s->s_name, errno);
outlet_int(self->status, 0);
return;
}
}
void msdpfs_cp(t_object* self, t_symbol* s, long argc, t_atom* argv) {
defer(self, (method)msdpfs_docp, s, argc, argv);
}
void msdpfs_env(t_object* self, t_symbol* s) { defer(self, (method)msdpfs_doenv, s, 0, NULL); }
void msdpfs_exists(t_object* self, t_symbol* s) { defer(self, (method)msdpfs_doexists, s, 0, NULL); }
void msdpfs_mkdir(t_object* self, t_symbol* s) { defer(self, (method)msdpfs_domkdir, s, 0, NULL); }
void msdpfs_rm(t_object* self, t_symbol* s) { defer(self, (method)msdpfs_dorm, s, 0, NULL); }
void msdpfs_assist(t_msdpfs* self, void* unused, t_assist_function io, long index, char* string_dest) {
if (io == ASSIST_INLET) {
switch (index) {
case 0:
strncpy(string_dest,"[cp|exists|mkdir|rm SOURCE (DESTINATION)]", ASSIST_STRING_MAXSIZE);
break;
}
}
else if (io == ASSIST_OUTLET) {
switch (index) {
case 0:
strncpy(string_dest,"status, 1=success, 0=failure", ASSIST_STRING_MAXSIZE);
break;
case 1:
strncpy(string_dest,"environment variable value", ASSIST_STRING_MAXSIZE);
break;
}
}
}
void ext_main(void* r) {
this_class = class_new("msdpfs", (method)msdpfs_new, (method)msdpfs_free, sizeof(t_msdpfs), 0L, A_GIMME, 0);
class_addmethod(this_class, (method)msdpfs_cp, "cp", A_GIMME, 0);
class_addmethod(this_class, (method)msdpfs_env, "env", A_DEFSYM, 0);
class_addmethod(this_class, (method)msdpfs_exists, "exists", A_DEFSYM, 0);
class_addmethod(this_class, (method)msdpfs_mkdir, "mkdir", A_DEFSYM, 0);
class_addmethod(this_class, (method)msdpfs_rm, "rm", A_DEFSYM, 0);
class_register(CLASS_BOX, this_class);
}