Visual Computing Library
All Classes Functions Variables Typedefs Enumerations Friends Modules Pages Concepts
file_info.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_INFO_H
24#define VCL_IO_FILE_INFO_H
25
26#include <vclib/exceptions/io.h>
27#include <vclib/types.h>
28
29#include <filesystem>
30#include <fstream>
31#include <string>
32#include <vector>
33
34namespace vcl {
35
37{
38 std::string mFilename;
39
40public:
41 FileInfo() = default;
42
43 FileInfo(const std::string& filename) : mFilename(filename) {}
44
45 bool exists() const { return exists(mFilename); }
46
47 std::size_t fileSize() const { return fileSize(mFilename); }
48
49 bool isFileBinary() const { return isFileBinary(mFilename); }
50
51 /* Static member functions */
52
53 // file stat
54
61 static bool exists(const std::string& filename)
62 {
63 return std::filesystem::exists(filename);
64 }
65
72 static std::size_t fileSize(const std::string& filename)
73 {
74 std::ifstream file(filename, std::ios::binary | std::ios::ate);
75 return file.tellg();
76 }
77
89 static bool isFileBinary(const std::string& filename)
90 {
91 const std::size_t CHECK_BUFF_SIZE = 1000;
92
93 std::ifstream file(filename, std::ios::binary | std::ios::ate);
94 std::size_t fsize = file.tellg();
95 std::size_t size = std::min(fsize, CHECK_BUFF_SIZE);
96 file.seekg(0, std::ios::beg);
97
98 std::vector<unsigned char> buffer(size);
99 if (file.read((char*) buffer.data(), size)) {
100 for (uint i = 0; i < buffer.size(); ++i) {
101 if (buffer[i] > 127)
102 return true;
103 }
104 }
105 else {
106 throw MalformedFileException("Cannot read data from file.");
107 }
108 return false;
109 }
110
111 // name and extension management
112
135 const std::string& fullname,
136 std::string& rawName,
137 std::string& extension)
138 {
139 // https://stackoverflow.com/questions/6417817
140
141 size_t lastIndex = fullname.find_last_of(".");
142 if (lastIndex != std::string::npos) {
143 rawName = fullname.substr(0, lastIndex);
144 extension = fullname.substr(lastIndex, fullname.size());
145 }
146 else {
148 }
149 }
150
170 const std::string& fullpath,
171 std::string& path,
172 std::string& filename)
173 {
174 size_t lastIndex = fullpath.find_last_of("/");
175 if (lastIndex != std::string::npos) {
176 path = fullpath.substr(0, lastIndex + 1);
177 filename = fullpath.substr(lastIndex + 1, fullpath.size());
178 }
179 else {
181 }
182 }
183
197 static std::string pathWithoutFileName(const std::string& fullpath)
198 {
199 std::string path, filename;
201 return path;
202 }
203
217 static std::string fileNameWithoutExtension(const std::string& fullpath)
218 {
221 return filename;
222 }
223
237 static std::string fileNameWithExtension(const std::string& fullpath)
238 {
239 std::string filename, path, ext;
241 return filename;
242 }
243
257 static std::string extension(const std::string& filename)
258 {
259 std::string fn, ext;
261 return ext;
262 }
263
271 static std::string addExtensionIfNeeded(
272 const std::string& filename,
273 const std::string& ext)
274 {
275 std::string actualFileName;
276 size_t lastIndex = filename.find_last_of(".");
277 if (lastIndex != std::string::npos) {
278 std::string e = filename.substr(lastIndex + 1, filename.size());
279 if (e == ext)
281 else
282 actualFileName = filename + "." + ext;
283 }
284 else
285 actualFileName = filename + "." + ext;
286 return actualFileName;
287 }
288};
289
290} // namespace vcl
291
292#endif // VCL_IO_FILE_INFO_H
Definition file_info.h:37
static std::size_t fileSize(const std::string &filename)
Get the size of a file.
Definition file_info.h:72
static std::string addExtensionIfNeeded(const std::string &filename, const std::string &ext)
Adds an extension to a file name if it doesn't already have it.
Definition file_info.h:271
static std::string fileNameWithExtension(const std::string &fullpath)
Get the filename with extension of a file.
Definition file_info.h:237
static std::string fileNameWithoutExtension(const std::string &fullpath)
Get the file name without extension of a file.
Definition file_info.h:217
static void separateFileNameFromPath(const std::string &fullpath, std::string &path, std::string &filename)
Extracts the filename (extension included) of a string that contains a fullpath.
Definition file_info.h:169
static bool exists(const std::string &filename)
Check if a file exists.
Definition file_info.h:61
static std::string extension(const std::string &filename)
Get the extension of a file.
Definition file_info.h:257
static bool isFileBinary(const std::string &filename)
Check if a file is binary.
Definition file_info.h:89
static void separateExtensionFromFileName(const std::string &fullname, std::string &rawName, std::string &extension)
Extracts the extension of a string that contains a filename.
Definition file_info.h:134
static std::string pathWithoutFileName(const std::string &fullpath)
Get the path of a file.
Definition file_info.h:197
Exception thrown when the file is malformed.
Definition io.h:76
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43