Debian Codegraph @

plugins/script/interfaces/FileSystemInterface.cpp

raw
1 #include "FileSystemInterface.h"
2
3 #include "generic/callback.h"
4 #include "iarchive.h"
5 #include "itextstream.h"
6
7 namespace script
8 {
9
10 void FileSystemInterface::forEachFile(const std::string& basedir,
11 const std::string& extension,
12 VirtualFileSystemVisitor& visitor,
13 std::size_t depth)
14 {
15 GlobalFileSystem().forEachFile(
16 basedir, extension,
17 [&](const vfs::FileInfo& fileInfo) { visitor.visit(fileInfo.name); },
18 depth
19 );
20 }
21
22 std::string FileSystemInterface::readTextFile(const std::string& filename)
23 {
24 ArchiveTextFilePtr file = GlobalFileSystem().openTextFile(filename);
25
26 if (file == NULL) return "";
27
28 TextInputStream& istream = file->getInputStream();
29
30 const std::size_t READSIZE = 16384;
31
32 std::string text;
33 char buffer[READSIZE];
34 std::size_t bytesRead = READSIZE;
35
36 do
37 {
38 bytesRead = istream.read(buffer, READSIZE);
39
40 // Copy the stuff to the string
41 text.append(buffer, bytesRead);
42
43 } while (bytesRead == READSIZE);
44
45 return text;
46 }
47
48 int FileSystemInterface::getFileCount(const std::string& filename)
49 {
50 return GlobalFileSystem().getFileCount(filename);
51 }
52
53 std::string FileSystemInterface::findFile(const std::string& name)
54 {
55 return GlobalFileSystem().findFile(name);
56 }
57
58 std::string FileSystemInterface::findRoot(const std::string& name)
59 {
60 return GlobalFileSystem().findRoot(name);
61 }
62
63 void FileSystemInterface::registerInterface(py::module& scope, py::dict& globals)
64 {
65 // Expose the FileVisitor interface
66 py::class_<VirtualFileSystemVisitor, FileVisitorWrapper> visitor(scope, "FileVisitor");
67 visitor.def(py::init<>());
68 visitor.def("visit", &VirtualFileSystemVisitor::visit);
69
70 // Add the VFS module declaration to the given python namespace
71 py::class_<FileSystemInterface> filesystem(scope, "FileSystem");
72 filesystem.def("forEachFile", &FileSystemInterface::forEachFile);
73 filesystem.def("findFile", &FileSystemInterface::findFile);
74 filesystem.def("findRoot", &FileSystemInterface::findRoot);
75 filesystem.def("readTextFile", &FileSystemInterface::readTextFile);
76 filesystem.def("getFileCount", &FileSystemInterface::getFileCount);
77
78 // Now point the Python variable "GlobalFileSystem" to this instance
79 globals["GlobalFileSystem"] = this;
80 }
81
82 } // namespace script