ACAV f0ba4b7c9529
Abstract Syntax Tree (AST) visualization tool for C, C++, and Objective-C
Loading...
Searching...
No Matches
FileManager.h
Go to the documentation of this file.
1/*$!{
2* Aurora Clang AST Viewer (ACAV)
3*
4* Copyright (c) 2026 Min Liu
5* Copyright (c) 2026 Michael David Adams
6*
7* SPDX-License-Identifier: GPL-2.0-or-later
8*
9* This program is free software; you can redistribute it and/or modify
10* it under the terms of the GNU General Public License as published by
11* the Free Software Foundation; either version 2 of the License, or
12* (at your option) any later version.
13*
14* This program is distributed in the hope that it will be useful,
15* but WITHOUT ANY WARRANTY; without even the implied warranty of
16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17* GNU General Public License for more details.
18*
19* You should have received a copy of the GNU General Public License along
20* with this program; if not, see <https://www.gnu.org/licenses/>.
21}$!*/
22
25#pragma once
26
27#include <cstddef>
28#include <mutex>
29#include <optional>
30#include <string>
31#include <string_view>
32#include <unordered_map>
33#include <vector>
34
35namespace acav {
36
38using FileID = std::size_t;
39
45class FileManager {
46public:
47 static constexpr FileID InvalidFileID = 0;
48
49 FileManager() = default;
50 ~FileManager() = default;
51
52 FileManager(const FileManager &) = delete;
53 FileManager &operator=(const FileManager &) = delete;
54 FileManager(FileManager &&) = delete;
55 FileManager &operator=(FileManager &&) = delete;
56
60 FileID registerFile(std::string_view filePath);
61
65 std::optional<FileID> tryGetFileId(std::string_view filePath) const;
66
70 std::string_view getFilePath(FileID id) const;
71
75 std::size_t getRefCount(FileID id) const;
76
78 std::size_t getRegisteredFileCount() const;
79
81 bool isValidFileId(FileID id) const;
82
83private:
84 // Internal file record
85 struct FileRecord {
86 std::string path; // Normalized canonical path
87 std::size_t refCount = 0; // Number of times registered
88 };
89
90 // Normalize file path to canonical form
91 // Handles: relative paths, symbolic links, ".." and "." components
92 // Returns: Canonical absolute path as string
93 std::string normalizePath(std::string_view filePath) const;
94
95 mutable std::mutex mutex_; // Thread-safety lock
96 std::vector<FileRecord> files_; // Indexed by FileID - 1
97 std::unordered_map<std::string, FileID> pathToId_; // Fast lookup cache
98};
99
100} // namespace acav
std::size_t FileID
Type-safe identifier for registered files. 0 is reserved for invalid.
Definition FileManager.h:38
static constexpr FileID InvalidFileID
Reserved invalid FileID.
Definition FileManager.h:47
FileID registerFile(std::string_view filePath)
Register a file and return its FileID.
std::size_t getRefCount(FileID id) const
Get reference count for a file.
std::string_view getFilePath(FileID id) const
Get the canonical path for a FileID.
std::size_t getRegisteredFileCount() const
Get total number of registered files.
bool isValidFileId(FileID id) const
Check if a FileID is valid.
std::optional< FileID > tryGetFileId(std::string_view filePath) const
Look up FileID for a path without registering.