00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "nstream.h"
00036
00037 #include "common/wave_ex.h"
00038 #include "perf/perf.h"
00039 #include "util/file.h"
00040
00041
00042
00043 namespace nstream {
00044
00045
00046 Stream::~Stream(void) throw() { }
00047 Entry::~Entry(void) throw() { }
00048 File::~File(void) throw() { }
00049 Folder::~Folder(void) throw() { }
00050 Manager::~Manager(void) throw() { }
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 static eIterationFlag
00061 maybeVisit
00062 (
00063 IN Entry * entry,
00064 IN visit_entry_fn callback,
00065 IN void * context,
00066 IN const SetString * extensions,
00067 IN const char * filter,
00068 IN bool visitHidden
00069 )
00070 {
00071 ASSERT(entry, "null");
00072 ASSERT(callback, "null");
00073
00074
00075
00076
00077 const char * name = entry->getName();
00078 ASSERT(name, "null");
00079
00080
00081 if (extensions) {
00082 const char * ext = GetExtension(name);
00083 if (!ext ||
00084 extensions->end() == extensions->find(ext)) {
00085
00086
00087 return eIterate_Continue;
00088 }
00089 }
00090
00091
00092 if (filter) {
00093 if (!strstr(name, filter)) {
00094
00095
00096 return eIterate_Continue;
00097 }
00098 }
00099
00100
00101 return callback(entry, context);
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 void
00113 Stream::writeDiagnostics
00114 (
00115 IO std::ostream& out
00116 )
00117 {
00118
00119
00120
00121
00122 smart_ptr<File> file = this->getFile();
00123 if (file) {
00124 const char * filename = file->getName();
00125 out << "read-only stream from file: '" << filename << "'";
00126 } else {
00127 out << "(stream from unknown file)";
00128 }
00129 }
00130
00131
00132
00133 smart_ptr<Stream>
00134 getStreamRelativeTo
00135 (
00136 IN Stream * startStream,
00137 IN const char * relativePath
00138 )
00139 {
00140 perf::Timer timer("getStreamRelativeTo");
00141 ASSERT(startStream, "null");
00142 ASSERT(relativePath, "null");
00143
00144
00145
00146
00147 smart_ptr<File> file = startStream->getFile();
00148 THROW(file, "failed to get File for Stream");
00149
00150 const char * name = file->getName();
00151 ASSERT(name, "File contains null name");
00152
00153 std::string relName = getPathRelativeTo(name, relativePath);
00154
00155
00156 smart_ptr<Manager> mgr = file->getManager();
00157 THROW(mgr, "File returned null manager");
00158
00159 smart_ptr<File> newFile = mgr->getEntry(relName.c_str());
00160 if (!newFile) {
00161 DPRINTF("No such entry, or not a file: %s", relName.c_str());
00162 return NULL;
00163 }
00164
00165
00166 return newFile->openStream();
00167 }
00168
00169
00170
00171 smart_ptr<Stream>
00172 openNamedStream
00173 (
00174 IN Manager * mgr,
00175 IN const char * name
00176 )
00177 {
00178 ASSERT(mgr, "null");
00179 ASSERT(name, "null");
00180
00181 smart_ptr<File> file = mgr->getEntry(name);
00182 THROW(file,
00183 "Could not get File at path (does not exist, or is folder): "
00184 << name);
00185
00186 smart_ptr<Stream> stream = file->openStream();
00187 THROW(stream, "Failed to open stream for File: " << name);
00188
00189 return stream;
00190 }
00191
00192
00193
00194 std::string
00195 getStreamName
00196 (
00197 IN Stream * stream
00198 )
00199 {
00200 ASSERT(stream, "null");
00201
00202 smart_ptr<File> file = stream->getFile();
00203 THROW(file, "failed to retrieve File for Stream");
00204
00205 return file->getName();
00206 }
00207
00208
00209
00210 eIterationFlag
00211 walkChildFolders
00212 (
00213 IN Folder * root,
00214 IN visit_entry_fn callback,
00215 IN void * context,
00216 IN const SetString * extensions,
00217 IN const char * filter,
00218 IN bool visitHidden
00219 )
00220 {
00221 ASSERT(root, "null");
00222 ASSERT(callback, "null");
00223
00224
00225
00226
00227 root->resetIteration();
00228 while (smart_ptr<Entry> entry = root->getNextChild()) {
00229 const char * name = entry->getName();
00230 ASSERT(name, "null");
00231 name = GetFilename(name);
00232
00233
00234
00235
00236
00237 if ('.' == name[0] && !visitHidden) {
00238
00239
00240 continue;
00241 }
00242
00243
00244 if (eIterate_Stop ==
00245 maybeVisit(entry, callback, context, extensions, filter,
00246 visitHidden)) {
00247 return eIterate_Stop;
00248 }
00249
00250
00251 Folder * child = dynamic_cast<Folder *>((Entry *) entry);
00252 if (!child)
00253 continue;
00254 if (eIterate_Stop == walkChildFolders(child, callback,
00255 context, extensions, filter,
00256 visitHidden))
00257 return eIterate_Stop;
00258 }
00259
00260 return eIterate_Continue;
00261 }
00262
00263
00264
00265 };
00266