00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "virtualFile.h"
00016 #include "virtualFileSystem.h"
00017 #include "virtualFileList.h"
00018 #include "config_express.h"
00019 #include "pvector.h"
00020
00021 TypeHandle VirtualFile::_type_handle;
00022
00023
00024
00025
00026
00027
00028 bool VirtualFile::
00029 has_file() const {
00030 return false;
00031 }
00032
00033
00034
00035
00036
00037
00038
00039 bool VirtualFile::
00040 is_directory() const {
00041 return false;
00042 }
00043
00044
00045
00046
00047
00048
00049
00050 bool VirtualFile::
00051 is_regular_file() const {
00052 return false;
00053 }
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 PT(VirtualFileList) VirtualFile::
00065 scan_directory() const {
00066
00067
00068 VirtualFileSystem *file_system = get_file_system();
00069 Filename this_filename = get_filename();
00070 vector_string mount_points_flat;
00071 file_system->scan_mount_points(mount_points_flat, this_filename);
00072
00073
00074
00075 ov_set<string> mount_points;
00076 copy(mount_points_flat.begin(), mount_points_flat.end(),
00077 back_inserter(mount_points));
00078 mount_points.sort();
00079
00080
00081 PT(VirtualFileList) file_list = new VirtualFileList;
00082
00083
00084
00085 ov_set<string>::const_iterator mi;
00086 for (mi = mount_points.begin(); mi != mount_points.end(); ++mi) {
00087 const string &basename = (*mi);
00088 Filename filename(this_filename, basename);
00089 PT(VirtualFile) file = file_system->get_file(filename);
00090 file_list->add_file(file);
00091 }
00092
00093
00094 vector_string names;
00095 if (!scan_local_directory(file_list, mount_points)) {
00096
00097 if (file_list->get_num_files() == 0) {
00098 return NULL;
00099 }
00100
00101
00102
00103 return file_list;
00104 }
00105
00106 return file_list;
00107 }
00108
00109
00110
00111
00112
00113
00114 void VirtualFile::
00115 output(ostream &out) const {
00116 out << get_filename();
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 void VirtualFile::
00126 ls(ostream &out) const {
00127 CPT(VirtualFileList) contents = scan_directory();
00128 if (contents == (VirtualFileList *)NULL) {
00129 if (!is_directory()) {
00130 out << get_filename() << "\n";
00131 } else {
00132 out << get_filename() << " cannot be read.\n";
00133 }
00134 return;
00135 }
00136
00137 int num_files = contents->get_num_files();
00138 for (int i = 0; i < num_files; i++) {
00139 VirtualFile *file = contents->get_file(i);
00140 out << file->get_filename().get_basename() << "\n";
00141 }
00142 }
00143
00144
00145
00146
00147
00148
00149
00150 void VirtualFile::
00151 ls_all(ostream &out) const {
00152 if (!is_directory()) {
00153 out << get_filename() << " is not a directory.\n";
00154 } else {
00155 r_ls_all(out, get_filename());
00156 }
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 istream *VirtualFile::
00168 open_read_file(bool auto_unwrap) const {
00169 return NULL;
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 off_t VirtualFile::
00182 get_file_size(istream *stream) const {
00183 return get_file_size();
00184 }
00185
00186
00187
00188
00189
00190
00191
00192 off_t VirtualFile::
00193 get_file_size() const {
00194 return 0;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 time_t VirtualFile::
00212 get_timestamp() const {
00213 return 0;
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 void VirtualFile::
00226 close_read_file(istream *stream) const {
00227 if (stream != (istream *)NULL) {
00228
00229
00230
00231
00232 #if !defined(USE_MEMORY_NOWRAPPERS) && defined(REDEFINE_GLOBAL_OPERATOR_NEW)
00233 stream->~istream();
00234 (*global_operator_delete)(stream);
00235 #else
00236 delete stream;
00237 #endif
00238 }
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251 bool VirtualFile::
00252 was_read_successful() const {
00253 return true;
00254 }
00255
00256
00257
00258
00259
00260
00261
00262
00263 bool VirtualFile::
00264 read_file(string &result, bool auto_unwrap) const {
00265 result = string();
00266
00267 pvector<unsigned char> pv;
00268 if (!read_file(pv, auto_unwrap)) {
00269 return false;
00270 }
00271
00272 if (!pv.empty()) {
00273 result.append((const char *)&pv[0], pv.size());
00274 }
00275
00276 return true;
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286 bool VirtualFile::
00287 read_file(pvector<unsigned char> &result, bool auto_unwrap) const {
00288 return false;
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 bool VirtualFile::
00300 simple_read_file(istream *in, pvector<unsigned char> &result) {
00301 static const size_t buffer_size = 4096;
00302 char buffer[buffer_size];
00303
00304 in->read(buffer, buffer_size);
00305 size_t count = in->gcount();
00306 while (count != 0) {
00307 thread_consider_yield();
00308 result.insert(result.end(), buffer, buffer + count);
00309 in->read(buffer, buffer_size);
00310 count = in->gcount();
00311 }
00312
00313 return (!in->fail() || in->eof());
00314 }
00315
00316
00317
00318
00319
00320
00321
00322 bool VirtualFile::
00323 simple_read_file(istream *in, pvector<unsigned char> &result, size_t max_bytes) {
00324 static const size_t buffer_size = 4096;
00325 char buffer[buffer_size];
00326
00327 in->read(buffer, min(buffer_size, max_bytes));
00328 size_t count = in->gcount();
00329 while (count != 0) {
00330 thread_consider_yield();
00331 nassertr(count <= max_bytes, false);
00332 result.insert(result.end(), buffer, buffer + count);
00333 max_bytes -= count;
00334 in->read(buffer, min(buffer_size, max_bytes));
00335 count = in->gcount();
00336 }
00337
00338 return (!in->fail() || in->eof());
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 bool VirtualFile::
00351 scan_local_directory(VirtualFileList *, const ov_set<string> &) const {
00352 return false;
00353 }
00354
00355
00356
00357
00358
00359
00360 void VirtualFile::
00361 r_ls_all(ostream &out, const Filename &root) const {
00362 CPT(VirtualFileList) contents = scan_directory();
00363 if (contents == (VirtualFileList *)NULL) {
00364 return;
00365 }
00366
00367 int num_files = contents->get_num_files();
00368 for (int i = 0; i < num_files; i++) {
00369 VirtualFile *file = contents->get_file(i);
00370 Filename filename = file->get_filename();
00371 filename.make_relative_to(root);
00372 out << filename << "\n";
00373 if (file->is_directory()) {
00374 file->r_ls_all(out, root);
00375 }
00376 }
00377 }