Logo
ApraPipes 1.0
Loading...
Searching...
No Matches
PathUtils.h
1// ============================================================
2// File: declarative/PathUtils.h
3// Path validation and normalization utilities for declarative pipelines
4// ============================================================
5
6#pragma once
7
8#include <string>
9#include <vector>
10#include "Metadata.h"
11
12namespace apra {
13namespace path_utils {
14
15// ============================================================
16// Path Validation Result
17// ============================================================
19 bool valid = false;
20 std::string error; // Error message if not valid
21 std::string warning; // Warning message (e.g., no files match pattern)
22 std::string normalized_path; // Platform-normalized path
23 bool directory_created = false; // True if directory was created
24};
25
26// ============================================================
27// Path Normalization
28// ============================================================
29
30// Normalize path separators to platform-native format
31// On Windows: converts / to \\
32// On Linux/macOS: converts \\ to /
33std::string normalizePath(const std::string& path);
34
35// Get the parent directory of a path
36// e.g., "/path/to/file.txt" -> "/path/to"
37std::string parentPath(const std::string& path);
38
39// Get the filename component of a path
40// e.g., "/path/to/file.txt" -> "file.txt"
41std::string filename(const std::string& path);
42
43// ============================================================
44// Path Existence Checks
45// ============================================================
46
47// Check if a path exists (file or directory)
48bool pathExists(const std::string& path);
49
50// Check if path is a regular file
51bool isFile(const std::string& path);
52
53// Check if path is a directory
54bool isDirectory(const std::string& path);
55
56// Check if path is writable (can create/write files)
57bool isWritable(const std::string& path);
58
59// ============================================================
60// Directory Operations
61// ============================================================
62
63// Create directory and all parent directories if needed
64// Returns true if directory exists or was created successfully
65bool createDirectories(const std::string& path);
66
67// ============================================================
68// Pattern Matching
69// ============================================================
70
71// Check if any files match a pattern with ???? wildcards
72// e.g., "/path/frame_????.jpg" checks for frame_0000.jpg, frame_0001.jpg, etc.
73bool patternHasMatches(const std::string& pattern);
74
75// Count how many files match a pattern
76size_t countPatternMatches(const std::string& pattern);
77
78// Get first matching file for a pattern (for existence check)
79std::string firstPatternMatch(const std::string& pattern);
80
81// ============================================================
82// Comprehensive Path Validation
83// ============================================================
84
85// Validate a path based on its type and requirement
86// This is the main entry point for path validation
87//
88// Validation rules:
89// - MustExist: Path must exist (error if not, warn for patterns with no matches)
90// - MayExist: No existence check needed
91// - MustNotExist: Path must not exist (warn if exists)
92// - ParentMustExist: Parent directory must exist (error if not)
93// - WillBeCreated: Attempt to create parent directory (error if fails)
94//
95// Additional checks:
96// - For writers (WillBeCreated/ParentMustExist): Check write permissions
97// - For patterns: Check if at least one file matches
98// - Normalize path separators for cross-platform compatibility
99//
101 const std::string& path,
102 PathType type,
103 PathRequirement requirement
104);
105
106// ============================================================
107// Utility Functions
108// ============================================================
109
110// Convert PathType enum to string for error messages
111std::string pathTypeToString(PathType type);
112
113// Convert PathRequirement enum to string for error messages
114std::string pathRequirementToString(PathRequirement requirement);
115
116// Check if a path contains wildcard characters (? or *)
117bool hasWildcards(const std::string& path);
118
119// Extract the directory part from a file pattern
120// e.g., "./data/testOutput/bmp_????.bmp" -> "./data/testOutput"
121std::string patternDirectory(const std::string& pattern);
122
123} // namespace path_utils
124} // namespace apra
std::string filename(const std::string &path)
Definition PathUtils.cpp:42
std::string normalizePath(const std::string &path)
Definition PathUtils.cpp:28
std::string parentPath(const std::string &path)
Definition PathUtils.cpp:36
size_t countPatternMatches(const std::string &pattern)
Definition PathUtils.cpp:166
std::string patternDirectory(const std::string &pattern)
Definition PathUtils.cpp:126
bool patternHasMatches(const std::string &pattern)
Definition PathUtils.cpp:162
std::string firstPatternMatch(const std::string &pattern)
Definition PathUtils.cpp:196
PathValidationResult validatePath(const std::string &path, PathType type, PathRequirement requirement)
Definition PathUtils.cpp:228
bool isFile(const std::string &path)
Definition PathUtils.cpp:61
bool pathExists(const std::string &path)
Definition PathUtils.cpp:52
std::string pathRequirementToString(PathRequirement requirement)
Definition PathUtils.cpp:388
bool hasWildcards(const std::string &path)
Definition PathUtils.cpp:121
std::string pathTypeToString(PathType type)
Definition PathUtils.cpp:375
bool createDirectories(const std::string &path)
Definition PathUtils.cpp:105
bool isDirectory(const std::string &path)
Definition PathUtils.cpp:70
bool isWritable(const std::string &path)
Definition PathUtils.cpp:79
Definition FrameTypeRegistrations.h:10
PathType
Definition Metadata.h:39
PathRequirement
Definition Metadata.h:52
bool valid
Definition PathUtils.h:19
std::string warning
Definition PathUtils.h:21
std::string normalized_path
Definition PathUtils.h:22
bool directory_created
Definition PathUtils.h:23
std::string error
Definition PathUtils.h:20