yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
2.7 KiB101 linesraw
1#include "slang-implicit-directory-collector.h"
2
3#include "slang-io.h"
4
5namespace Slang
6{
7
8// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ImplicitDirectoryCollector !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
9
10ImplicitDirectoryCollector::ImplicitDirectoryCollector(
11    const String& canonicalPath,
12    bool directoryExists)
13    : m_directoryExists(directoryExists)
14{
15    if (!isRootPath(canonicalPath.getUnownedSlice()))
16    {
17        StringBuilder buffer;
18        buffer << canonicalPath;
19        buffer.append('/');
20        m_prefix = buffer.produceString();
21    }
22}
23
24/* static */ bool ImplicitDirectoryCollector::isRootPath(const UnownedStringSlice& path)
25{
26    const auto length = path.getLength();
27    if (length == 0)
28    {
29        return true;
30    }
31    else if (length == 1)
32    {
33        const auto c = path[0];
34        return c == '.' || Path::isDelimiter(c);
35    }
36    return false;
37}
38
39void ImplicitDirectoryCollector::addRemainingPath(
40    SlangPathType pathType,
41    const UnownedStringSlice& inPathRemainder)
42{
43    // If it's zero length we probably don't want to add it
44    if (inPathRemainder.getLength() == 0)
45    {
46        // It's empty so don't add normal way - implies the directory exists
47        m_directoryExists = true;
48        return;
49    }
50
51    UnownedStringSlice pathRemainder(inPathRemainder);
52    const Index slashIndex = pathRemainder.indexOf('/');
53
54    // If we have a following / that means it's an implicit directory.
55    if (slashIndex >= 0)
56    {
57        pathType = SLANG_PATH_TYPE_DIRECTORY;
58        pathRemainder =
59            UnownedStringSlice(pathRemainder.begin(), pathRemainder.begin() + slashIndex);
60    }
61
62    const Index countIndex = m_map.findOrAdd(pathRemainder, pathType);
63    SLANG_UNUSED(countIndex);
64    // Make sure they are the same type
65    SLANG_ASSERT(SlangPathType(m_map.getValueAt(countIndex)) == pathType);
66}
67
68void ImplicitDirectoryCollector::addPath(
69    SlangPathType pathType,
70    const UnownedStringSlice& canonicalPath)
71{
72    if (canonicalPath != toSlice(".") && hasPrefix(canonicalPath))
73    {
74        UnownedStringSlice remainder = getRemainder(canonicalPath);
75        addRemainingPath(pathType, remainder);
76    }
77}
78
79SlangResult ImplicitDirectoryCollector::enumerate(
80    FileSystemContentsCallBack callback,
81    void* userData)
82{
83    const Int count = m_map.getCount();
84
85    for (Index i = 0; i < count; ++i)
86    {
87        const auto& pair = m_map.getAt(i);
88
89        UnownedStringSlice path = pair.key;
90        SlangPathType pathType = SlangPathType(pair.value);
91
92        // Note *is* 0 terminated in the pool
93        // Let's check tho
94        SLANG_ASSERT(path.begin()[path.getLength()] == 0);
95        callback(pathType, path.begin(), userData);
96    }
97
98    return getDirectoryExists() ? SLANG_OK : SLANG_E_NOT_FOUND;
99}
100
101} // namespace Slang