CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/File/AndroidContentURI.h
Views: 1401
1
#pragma once
2
3
#include <string>
4
5
#include "Common/StringUtils.h"
6
#include "Common/Net/URL.h"
7
#include "Common/Log.h"
8
9
// Utility to deal with Android storage URIs of the forms:
10
// content://com.android.externalstorage.documents/tree/primary%3APSP%20ISO
11
// content://com.android.externalstorage.documents/tree/primary%3APSP%20ISO/document/primary%3APSP%20ISO
12
13
// This file compiles on all platforms, to reduce the need for ifdefs.
14
15
// I am not 100% sure it's OK to rely on the internal format of file content URIs.
16
// On the other hand, I'm sure tons of apps would break if these changed, so I think we can
17
// consider them pretty stable. Additionally, the official Document library just manipulates the URIs
18
// in similar ways...
19
class AndroidContentURI {
20
private:
21
std::string provider;
22
std::string root;
23
std::string file;
24
public:
25
AndroidContentURI() {}
26
explicit AndroidContentURI(std::string_view path) {
27
Parse(path);
28
}
29
30
bool Parse(std::string_view path);
31
32
AndroidContentURI WithRootFilePath(const std::string &filePath);
33
AndroidContentURI WithComponent(std::string_view filePath);
34
AndroidContentURI WithExtraExtension(std::string_view extension); // The ext string contains the dot.
35
AndroidContentURI WithReplacedExtension(const std::string &oldExtension, const std::string &newExtension) const;
36
AndroidContentURI WithReplacedExtension(const std::string &newExtension) const;
37
38
bool CanNavigateUp() const;
39
40
// Only goes downwards in hierarchies. No ".." will ever be generated.
41
bool ComputePathTo(const AndroidContentURI &other, std::string &path) const;
42
43
std::string GetFileExtension() const;
44
std::string GetLastPart() const;
45
46
bool NavigateUp();
47
48
bool TreeContains(const AndroidContentURI &fileURI) {
49
if (root.empty()) {
50
return false;
51
}
52
return startsWith(fileURI.file, root);
53
}
54
55
std::string ToString() const;
56
57
// Never store the output of this, only show it to the user.
58
std::string ToVisualString() const {
59
return file;
60
}
61
62
const std::string &FilePath() const {
63
return file;
64
}
65
66
const std::string &RootPath() const {
67
return root.empty() ? file : root;
68
}
69
70
const std::string &Provider() const {
71
return provider;
72
}
73
74
bool IsTreeURI() const {
75
return !root.empty();
76
}
77
};
78
79