-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdar.cpp
More file actions
266 lines (209 loc) · 7.29 KB
/
dar.cpp
File metadata and controls
266 lines (209 loc) · 7.29 KB
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
#include <cstdio>
#include <cerrno>
extern "C" {
#include <fcntl.h>
}
#include <ios>
#include <dar/libdar.hpp>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
vector<string> files_in_dir;
void dar_check_version();
extern "C" const char * cplusplus_hello_str();
extern "C" int darfs_exists(const char * path);
extern "C" mode_t darfs_mode(const char * path);
extern "C" off_t darfs_size(const char * path);
extern "C" int open_dar_archive(const char * basepath);
static libdar::archive * archive_object_ptr = NULL;
// our own callback functions.
// for the illustration of what theses 'context' arguments
// can be used for we will imagine the situation where
// multiple windows or multiple threads may each one use
// libdar, but all share the same callback functions.
//typedef t_window_type int;
// this is an arbitrary type that here we will say
// points to a graphical window object wrapped in a C++
// class.
// Note that the method show() wait_for_click() and so on
// attributed to the t_win class are absolutely
// imaginary. Any link to an existing class is a pure
// coincidence...
void warning_callback(const std::string &x, void *context)
{
//(t_win *)(context)->show(x);
}
bool answer_callback(const std::string &x, void *context)
{
return true;
/*
click_type ret;
(t_win *)(context)->show(x);
ret = (t_win *)(context)->wait_for_click();
return ret == click_OK;
*/
}
std::string string_callback(const std::string &x, bool echo, void *context)
{
return "true";
/*
(t_win *)(context)->show(x);
if(!echo)
(t_win *)(context)->set_hide_typed_char();
(t_win *)(context)->wait_for_click();
return (t_win *)(context)->read_text();
*/
}
int get_current_windows_id()
{
return 0;
}
// So now each window can have its user_interaction object based on the same
// user_interaction_callback object pointing to the same functions.
// user_interaction_callback objects can be shared among different window objects
libdar::user_interaction_callback dialog =
libdar::user_interaction_callback(&warning_callback, &answer_callback, &string_callback,
(void *)get_current_windows_id());
// just the "context" argument changes, and will be passed as is from the constructor to the callback
// functions
// here follows the definition of our own implementation of
// of a user_interaction class
/*
class my_user_interaction : public libdar::user_interaction
{
public :
// the inherited pure virtual methods we must define
// as seen at the beginning of this tutorial:
void pause(const std::string & message);
void warning(const std::string & message);
std::string get_string(const std::string & message, bool echo);
user_interaction *clone() const;
// we can overwrite this method to have splitted fields for listing:
void listing(const std::string & flag,
const std::string & perm,
const std::string & uid,
const std::string & gid,
const std::string & size,
const std::string & date,
const std::string & filename,
bool is_dir,
bool has_children);
// but it will not get used by libdar unless we call the protected method set_use_listing()
// for example this can be done in the class constructor :
my_user_interaction() { set_use_listing(true); };
};
void my_user_interaction::listing(const std::string & flag,
const std::string & perm,
const std::string & uid,
const std::string & gid,
const std::string & size,
const std::string & date,
const std::string & filename,
bool is_dir,
bool has_children)
{
std::cout << filename << "\n";
}
*/
void listing_callback(const std::string & flag,
const std::string & perm,
const std::string & uid,
const std::string & gid,
const std::string & size,
const std::string & date,
const std::string & filename,
bool is_dir,
bool has_children,
void *context)
{
// std::cout << filename << " (listing_calback) \n";
files_in_dir.push_back(filename);
}
extern "C" void darfs_dir_listing_flush()
{
files_in_dir.clear();
}
extern "C" void get_files_in_dir(const char * path, char *** list_of_files, unsigned long * n_files)
{
try {
dar_check_version(); // initializes too (mandatory!)
darfs_dir_listing_flush();
archive_object_ptr->get_children_of(dialog, path);
*n_files = files_in_dir.size();
//http://bytes.com/topic/c/answers/127614-best-way-copy-vector-string-char
//
// allocate memory for an array of character strings
char ** cstr = new char*[files_in_dir.size()];
// for each string, allocate memory in the character array and copy
for (unsigned long i=0; i<files_in_dir.size(); i++) {
cstr[i] = new char[files_in_dir[i].size()+1];
strncpy(cstr[i], files_in_dir[i].c_str(), files_in_dir[i].size()+1);
cout << "dar.cpp: " << "cstr:" << cstr[i] << " " << "files_in_dir:" << files_in_dir[i] << "|" << "\n";
}
*list_of_files = cstr;
}
catch(libdar::Egeneric & e)
{
std::cerr << e.get_message() << std::endl;
}
}
extern "C" int open_dar_archive(const char * basepath)
{
dar_check_version(); // initializes too (mandatory!)
try {
archive_object_ptr = new libdar::archive(dialog,
"/tmp", // where is the archive
"etc", // slice name
"dar", // dar's archive extensions
libdar::crypto_none,
"",
20480, // theses three previous are for encryptions
"", // not used as we didn't gave "-" as
"", // slice name
"", // no command executed for now
true); // no verbose output
dialog.set_listing_callback(&listing_callback);
// now libdar will call the listing_callback function when we
// use this dialog object for listing the archive contents.
archive_object_ptr->get_children_of(dialog, "");
}
catch(libdar::Egeneric & e)
{
std::cerr << e.get_message() << std::endl;
}
return 0;
}
void dar_check_version()
{
try
{
libdar::U_I maj, med, min;
// first we MUST call get_version()
libdar::get_version(maj, med, min);
if(maj != libdar::LIBDAR_COMPILE_TIME_MAJOR ||
med < libdar::LIBDAR_COMPILE_TIME_MEDIUM)
throw libdar::Erange("initialization",
"we are linking against a wrong libdar");
}
catch(libdar::Egeneric & e)
{
std::cerr << e.get_message() << std::endl;
}
}
extern "C" const char * cplusplus_hello_str()
{
return "Hello from C++\n";
}
extern "C" int darfs_exists(const char * path)
{
return true;
}
extern "C" mode_t darfs_mode(const char * path)
{
return S_IFREG | 0777;
}
extern "C" off_t darfs_size(const char * path)
{
return 123L;
}