Visual Computing Library
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/concepts/ranges/range.h>
27#include <vclib/misc/string.h>
28
29#include <algorithm>
30#include <string>
31#include <vector>
32
33namespace vcl {
34
41{
42 std::vector<std::string> mExtensions;
43 std::string mDescription;
44
45public:
46 constexpr FileFormat(const char* extension, std::string description = "") :
47 mExtensions {extension}, mDescription(description)
48 {
49 clearExtension(mExtensions[0]);
50 }
51
52 constexpr FileFormat(
53 const std::string& extension,
54 std::string description = "") :
55 FileFormat(extension.c_str(), description)
56 {
57 }
58
59 constexpr FileFormat(Range auto extensions, std::string description = "") :
60 mExtensions(extensions), mDescription(description)
61 {
62 for (auto& ext : mExtensions) {
63 clearExtension(ext);
64 }
65
66 // make sure extensions are sorted - this is important for the
67 // comparison operator
68 std::sort(mExtensions.begin(), mExtensions.end());
69 }
70
71 const std::string& description() const { return mDescription; }
72
73 const std::vector<std::string>& extensions() const { return mExtensions; }
74
75 bool matchExtension(std::string extension) const
76 {
77 clearExtension(extension);
78 for (const auto& ext : mExtensions) {
79 if (ext == extension) {
80 return true;
81 }
82 }
83 return false;
84 }
85
97 auto operator<=>(const FileFormat& other) const
98 {
99 for (const auto& ext : mExtensions) {
100 if (other.matchExtension(ext)) {
101 return std::strong_ordering::equal;
102 }
103 }
104
105 return mExtensions[0] <=> other.mExtensions[0];
106 }
107
108 bool operator==(const FileFormat& other) const
109 {
110 return *this <=> other == std::strong_ordering::equal;
111 }
112
113private:
114 static constexpr void clearExtension(std::string& extension)
115 {
116 if (!extension.empty() && extension.front() == '.') {
117 extension.erase(0, 1);
118 }
119 extension = toLower(extension);
120 }
121};
122
123} // namespace vcl
124
125#endif // VCL_IO_FILE_FORMAT_H
The FileFormat class represents a file format.
Definition file_format.h:41
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:97
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
Utility concept that is evaluated true if T is a range, e.g. if has a begin and an end.
Definition range.h:39