Visual Computing Library  devel
Loading...
Searching...
No Matches
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 "file_format.h"
27
28#include <vclib/io/exceptions.h>
29
30#include <vclib/base.h>
31
32#include <filesystem>
33#include <fstream>
34#include <string>
35#include <vector>
36
37namespace vcl {
38
40{
41 std::string mFilename;
42
43public:
44 FileInfo() = default;
45
46 FileInfo(const std::string& filename) : mFilename(filename) {}
47
48 bool exists() const { return exists(mFilename); }
49
50 std::size_t fileSize() const { return fileSize(mFilename); }
51
52 bool isFileBinary() const { return isFileBinary(mFilename); }
53
54 /* Static member functions */
55
56 // file stat
57
64 static bool exists(const std::string& filename)
65 {
66 return std::filesystem::exists(filename);
67 }
68
75 static std::size_t fileSize(const std::string& filename)
76 {
77 std::ifstream file(filename, std::ios::binary | std::ios::ate);
78 return file.tellg();
79 }
80
92 static bool isFileBinary(const std::string& filename)
93 {
94 const std::size_t CHECK_BUFF_SIZE = 1000;
95
96 std::ifstream file(filename, std::ios::binary | std::ios::ate);
97 std::size_t fsize = file.tellg();
98 std::size_t size = std::min(fsize, CHECK_BUFF_SIZE);
99 file.seekg(0, std::ios::beg);
100
101 std::vector<unsigned char> buffer(size);
102 if (file.read((char*) buffer.data(), size)) {
103 for (uint i = 0; i < buffer.size(); ++i) {
104 if (buffer[i] > 127)
105 return true;
106 }
107 }
108 else {
109 throw MalformedFileException("Cannot read data from file.");
110 }
111 return false;
112 }
113
114 // name and extension management
115
138 const std::string& fullname,
139 std::string& rawName,
140 std::string& extension)
141 {
142 // https://stackoverflow.com/questions/6417817
143
144 size_t lastIndex = fullname.find_last_of(".");
145 if (lastIndex != std::string::npos) {
146 rawName = fullname.substr(0, lastIndex);
148 }
149 else {
151 }
152 }
153
173 const std::string& fullpath,
174 std::string& path,
175 std::string& filename)
176 {
177 size_t lastIndex = fullpath.find_last_of("/");
178 if (lastIndex != std::string::npos) {
179 path = fullpath.substr(0, lastIndex + 1);
180 filename = fullpath.substr(lastIndex + 1, fullpath.size());
181 }
182 else {
184 }
185 }
186
200 static std::string pathWithoutFileName(const std::string& fullpath)
201 {
202 std::string path, filename;
204 return path;
205 }
206
220 static std::string fileNameWithoutExtension(const std::string& fullpath)
221 {
224 return filename;
225 }
226
240 static std::string fileNameWithExtension(const std::string& fullpath)
241 {
242 std::string filename, path, ext;
244 return filename;
245 }
246
260 static std::string extension(const std::string& filename)
261 {
262 std::string fn, ext;
264 return ext;
265 }
266
280 static FileFormat fileFormat(const std::string& filename)
281 {
283 }
284
292 static std::string addExtensionIfNeeded(
293 const std::string& filename,
294 const std::string& ext)
295 {
296 std::string actualFileName;
297 size_t lastIndex = filename.find_last_of(".");
298 if (lastIndex != std::string::npos) {
299 std::string e = filename.substr(lastIndex + 1, filename.size());
300 if (e == ext)
302 else
303 actualFileName = filename + "." + ext;
304 }
305 else
306 actualFileName = filename + "." + ext;
307 return actualFileName;
308 }
309};
310
311} // namespace vcl
312
313#endif // VCL_IO_FILE_INFO_H
A class representing a box in N-dimensional space.
Definition box.h:46
PointT size() const
Computes the size of the box.
Definition box.h:267
The FileFormat class represents a file format.
Definition file_format.h:40
Definition file_info.h:40
static std::size_t fileSize(const std::string &filename)
Get the size of a file.
Definition file_info.h:75
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:292
static std::string fileNameWithExtension(const std::string &fullpath)
Get the filename with extension of a file.
Definition file_info.h:240
static std::string fileNameWithoutExtension(const std::string &fullpath)
Get the file name without extension of a file.
Definition file_info.h:220
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:172
static bool exists(const std::string &filename)
Check if a file exists.
Definition file_info.h:64
static std::string extension(const std::string &filename)
Get the extension of a file.
Definition file_info.h:260
static bool isFileBinary(const std::string &filename)
Check if a file is binary.
Definition file_info.h:92
static FileFormat fileFormat(const std::string &filename)
Get the file format of a file from its filename.
Definition file_info.h:280
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:137
static std::string pathWithoutFileName(const std::string &fullpath)
Get the path of a file.
Definition file_info.h:200
Exception thrown when the file is malformed.
Definition exceptions.h:76