Visual Computing Library  devel
Loading...
Searching...
No Matches
file_format.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_IO_FILE_FORMAT_H
24#define VCL_IO_FILE_FORMAT_H
25
26#include <vclib/base.h>
27
28#include <algorithm>
29#include <string>
30#include <vector>
31
32namespace vcl {
33
40{
41 std::vector<std::string> mExtensions;
42 std::string mDescription;
43
44public:
45 constexpr FileFormat(const char* extension, std::string description = "") :
46 mExtensions {extension}, mDescription(description)
47 {
48 clearExtension(mExtensions[0]);
49 }
50
51 constexpr FileFormat(
52 const std::string& extension,
53 std::string description = "") :
54 FileFormat(extension.c_str(), description)
55 {
56 }
57
58 constexpr FileFormat(Range auto extensions, std::string description = "") :
59 mExtensions(extensions), mDescription(description)
60 {
61 for (auto& ext : mExtensions) {
62 clearExtension(ext);
63 }
64
65 // make sure extensions are sorted - this is important for the
66 // comparison operator
67 std::sort(mExtensions.begin(), mExtensions.end());
68 }
69
70 const std::string& description() const { return mDescription; }
71
72 const std::vector<std::string>& extensions() const { return mExtensions; }
73
74 bool matchExtension(std::string extension) const
75 {
76 clearExtension(extension);
77 for (const auto& ext : mExtensions) {
78 if (ext == extension) {
79 return true;
80 }
81 }
82 return false;
83 }
84
96 auto operator<=>(const FileFormat& other) const
97 {
98 for (const auto& ext : mExtensions) {
99 if (other.matchExtension(ext)) {
100 return std::strong_ordering::equal;
101 }
102 }
103
104 return mExtensions[0] <=> other.mExtensions[0];
105 }
106
107 bool operator==(const FileFormat& other) const
108 {
109 return *this <=> other == std::strong_ordering::equal;
110 }
111
112private:
113 static constexpr void clearExtension(std::string& extension)
114 {
115 if (!extension.empty() && extension.front() == '.') {
116 extension.erase(0, 1);
117 }
118 extension = toLower(extension);
119 }
120};
121
122} // namespace vcl
123
124#endif // VCL_IO_FILE_FORMAT_H
A class representing a box in N-dimensional space.
Definition box.h:46
The FileFormat class represents a file format.
Definition file_format.h:40
auto operator<=>(const FileFormat &other) const
A FileFormat is equal to another if at least one extension is equal. The description is not considere...
Definition file_format.h:96
Utility concept that is evaluated true if T is a range, e.g. if has a begin and an end.
Definition range.h:39