-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStorageObject.cpp
214 lines (203 loc) · 5.55 KB
/
StorageObject.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
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
#include "StorageObject.h"
#include "KRunnable.h"
struct equal_wchar_ignorecase {
bool operator()(wchar_t x, wchar_t y) const {
return std::tolower(x) == std::tolower(y);
}
};
bool icomp(const std::wstring& x, const std::wstring& y) {
return x.size() == y.size() && std::equal(x.begin(), x.end(), y.begin(), equal_wchar_ignorecase());
}
ttstr TVPSearchCD(const ttstr& name)
{
// search CD which has specified volume label name.
// return drive letter ( such as 'A' or 'B' )
// return empty string if not found.
std::wstring narrow_name = name.c_str();
wchar_t dr[4];
for (dr[0] = L'A', dr[1] = L':', dr[2] = L'\\', dr[3] = 0; dr[0] <= L'Z'; dr[0]++)
{
if (::GetDriveType(dr) == DRIVE_CDROM)
{
wchar_t vlabel[256];
wchar_t fs[256];
DWORD mcl = 0, sfs = 0;
GetVolumeInformation(dr, vlabel, 255, NULL, &mcl, &sfs, fs, 255);
if (icomp(std::wstring(vlabel), narrow_name))
//if(std::string(vlabel).AnsiCompareIC(narrow_name)==0)
return ttstr((tjs_char)dr[0]);
}
}
return ttstr();
}
StorageObject::StorageObject() {
// put concurrent objects
putFunc(L"addAutoPath", []TJSNATIVEARG{
if (n < 1)
return TJS_E_BADPARAMCOUNT;
ttstr stor;
stor = p[0]->AsStringNoAddRef();
stor.Independ();
(new KRunnable([stor] {TVPAddAutoPath(stor); }))->runTask();
return TJS_S_OK;
});
putFunc(L"removeAutoPath", []TJSNATIVEARG{
if (n < 1)
return TJS_E_BADPARAMCOUNT;
ttstr stor;
stor = p[0]->AsStringNoAddRef();
stor.Independ();
(new KRunnable([stor] {TVPRemoveAutoPath(stor); }))->runTask();
return TJS_S_OK;
});
//put async objects
putFunc(L"chopStorageExt", []TJSNATIVEARG{
if (n < 1)
return TJS_E_BADPARAMCOUNT;
// chop storage's extension and return it.
ttstr name = p[0]->AsStringNoAddRef();
const tjs_char* s = name.c_str();
tjs_int slen = name.GetLen();
const tjs_char* pc = s + slen;
pc--;
while (pc >= s)
{
if (*pc == TJS_W('\\')) break;
if (*pc == TJS_W('/')) break;
if (*pc == TJS_W('>')) break;
if (*pc == TJS_W('.'))
{
// found extension delimiter
*r = ttstr(s, (int)(pc - s));
return TJS_S_OK;
}
pc--;
}
// not found
*r = name;
return TJS_S_OK;
});
putFunc(L"extractStorageExt", []TJSNATIVEARG{
// extract an extension from name.
// returned string will contain extension delimiter ( '.' ), except for
// missing extension of the input string.
// ( returns null string when input string does not have an extension )
ttstr name = p[0]->AsStringNoAddRef();
const tjs_char * s = name.c_str();
tjs_int slen = name.GetLen();
const tjs_char * px = s + slen;
px--;
while (px >= s)
{
if (*px == TJS_W('\\')) break;
if (*px == TJS_W('/')) break;
if (*px == '>') break;
if (*px == TJS_W('.'))
{
// found extension delimiter
tjs_int extlen = (tjs_int)(slen - (px - s));
*r = ttstr(px, extlen);
}
px--;
}
// not found
*r = ttstr();
return TJS_S_OK;
});
putFunc(L"extractStoragePath", []TJSNATIVEARG{
if (n < 1)
return TJS_E_BADPARAMCOUNT;
// extract "name"'s path ( including last delimiter ) and return it.
ttstr name = p[0]->AsStringNoAddRef();
const tjs_char* s = name.c_str();
tjs_int slen = name.GetLen();
const tjs_char* pc = s + slen;
pc--;
while (pc >= s)
{
if (*pc == TJS_W('\\')) break;
if (*pc == TJS_W('/')) break;
if (*pc == L'>') break;
pc--;
}
pc++;
*r = ttstr(s, (int)(pc - s));
return TJS_S_OK;
});
putFunc(L"extractStorageName", []TJSNATIVEARG{
if (n < 1)
return TJS_E_BADPARAMCOUNT;
// extract "name"'s storage name ( excluding path ) and return it.
ttstr name = p[0]->AsStringNoAddRef();
const tjs_char* s = name.c_str();
tjs_int slen = name.GetLen();
const tjs_char* pc = s + slen;
pc--;
while (pc >= s)
{
if (*pc == TJS_W('\\')) break;
if (*pc == TJS_W('/')) break;
if (*pc == L'>') break;
pc--;
}
pc++;
if (pc == s)
*r = name;
else
*r = ttstr(pc, (int)(slen - (pc - s)));
return TJS_S_OK;
});
putFunc(L"getFullPath", []TJSNATIVEARG{
if (n < 1)
return TJS_E_BADPARAMCOUNT;
// extract "name"'s path ( including last delimiter ) and return it.
ttstr name = p[0]->AsStringNoAddRef();
auto ro = new KRunnable([&] {name = TVPNormalizeStorageName(name); }, new KFuture());
ro->runTask();
ro->getFuture()->wait();
*r = name;
return TJS_S_OK;
});
putFunc(L"getLocalName", []TJSNATIVEARG{
if (n < 1)
return TJS_E_BADPARAMCOUNT;
// extract "name"'s path ( including last delimiter ) and return it.
ttstr name = p[0]->AsStringNoAddRef();
auto ro = new KRunnable([&] {TVPGetLocalName(name); }, new KFuture());
ro->runTask();
ro->getFuture()->wait();
*r = name;
return TJS_S_OK;
});
putFunc(L"getPlacedPath", []TJSNATIVEARG{
if (n < 1)
return TJS_E_BADPARAMCOUNT;
// extract "name"'s path ( including last delimiter ) and return it.
ttstr name = p[0]->AsStringNoAddRef();
auto ro = new KRunnable([&] {*r = name = TVPGetPlacedPath(name); }, new KFuture());
ro->runTask();
ro->getFuture()->wait();
return TJS_S_OK;
});
putFunc(L"isExistentStorage", []TJSNATIVEARG{
if (n < 1)
return TJS_E_BADPARAMCOUNT;
// extract "name"'s path ( including last delimiter ) and return it.
ttstr name = p[0]->AsStringNoAddRef();
auto ro = new KRunnable([&] {*r = TVPIsExistentStorage(name); }, new KFuture());
ro->runTask();
ro->getFuture()->wait();
return TJS_S_OK;
});
putFunc(L"searchCD", []TJSNATIVEARG{
if (n < 1)
return TJS_E_BADPARAMCOUNT;
// extract "name"'s path ( including last delimiter ) and return it.
ttstr name = p[0]->AsStringNoAddRef();
*r = TVPSearchCD(name);
return TJS_S_OK;
});
putFunc(L"selectFile", []TJSNATIVEARG{
return E_NOTIMPL;
});
}