Loading...
Searching...
No Matches
lib/score/tools/File.hpp
1#pragma once
2#include <score/document/DocumentContext.hpp>
3#include <score/tools/FilePath.hpp>
4
5#include <QFileInfo>
6
7#include <string_view>
8
9namespace score
10{
11
12// Used instead of QFileInfo
13// as it does a stat which can be super expensive
14// when scanning large libraries ; this class only extracts
15// path info from the string.
16// Note: it works on string_view, that is, it should only
17// be used for transient computations as it won't allocate memory for the
18// path string it is created with.
19struct SCORE_LIB_BASE_EXPORT PathInfo
20{
21public:
22 explicit PathInfo(std::string_view v) noexcept;
23
24 // Absolute path to the file, passed as input.
25 // ex. /home/user/foo.tar.gz
26 const std::string_view absoluteFilePath;
27
28 // foo.tar.gz
29 std::string_view fileName;
30
31 // foo.tar
32 std::string_view completeBaseName;
33
34 // foo
35 std::string_view baseName;
36
37 // /home/user
38 std::string_view absolutePath;
39
40 // user
41 std::string_view parentDirName;
42};
43
44inline QByteArray mapAsByteArray(QFile& f) noexcept
45{
46 const auto sz = f.size();
47 if(auto data = f.map(0, sz))
48 {
49 return QByteArray::fromRawData(reinterpret_cast<const char*>(data), sz);
50 }
51 else
52 {
53 return {};
54 }
55}
56
57inline std::string_view mapAsStringView(QFile& f) noexcept
58{
59 const auto sz = f.size();
60 if(auto data = f.map(0, sz))
61 {
62 return std::string_view(reinterpret_cast<const char*>(data), sz);
63 }
64 else
65 {
66 return {};
67 }
68}
69
70inline QString readFileAsQString(QFile& f) noexcept
71{
72 const auto sz = f.size();
73 if(auto data = f.map(0, sz))
74 {
75 auto str = QString::fromUtf8(reinterpret_cast<const char*>(data), sz);
76 f.unmap(data);
77 return str;
78 }
79 else
80 {
81 return {};
82 }
83}
84
85}
Base toolkit upon which the software is built.
Definition Application.cpp:90
Definition lib/score/tools/File.hpp:20