Flutter Engine
The Flutter Engine
paths_win.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/fml/paths.h"
6
7#include <windows.h>
8
9#include <algorithm>
10
11#include "flutter/fml/paths.h"
12#include "flutter/fml/platform/win/wstring_conversion.h"
13
14namespace fml {
15namespace paths {
16
17namespace {
18
19constexpr char kFileURLPrefix[] = "file:///";
20constexpr size_t kFileURLPrefixLength = sizeof(kFileURLPrefix) - 1;
21
22size_t RootLength(const std::string& path) {
23 if (path.empty())
24 return 0;
25 if (path[0] == '/')
26 return 1;
27 if (path[0] == '\\') {
28 if (path.size() < 2 || path[1] != '\\')
29 return 1;
30 // The path is a network share. Search for up to two '\'s, as they are
31 // the server and share - and part of the root part.
32 size_t index = path.find('\\', 2);
33 if (index > 0) {
34 index = path.find('\\', index + 1);
35 if (index > 0)
36 return index;
37 }
38 return path.size();
39 }
40 // If the path is of the form 'C:/' or 'C:\', with C being any letter, it's
41 // a root part.
42 if (path.length() >= 2 && path[1] == ':' &&
43 (path[2] == '/' || path[2] == '\\') &&
44 ((path[0] >= 'A' && path[0] <= 'Z') ||
45 (path[0] >= 'a' && path[0] <= 'z'))) {
46 return 3;
47 }
48 return 0;
49}
50
51size_t LastSeparator(const std::string& path) {
52 return path.find_last_of("/\\");
53}
54
55} // namespace
56
57std::pair<bool, std::string> GetExecutablePath() {
58 HMODULE module = GetModuleHandle(NULL);
59 if (module == NULL) {
60 return {false, ""};
61 }
62 wchar_t path[MAX_PATH];
63 DWORD read_size = GetModuleFileNameW(module, path, MAX_PATH);
64 if (read_size == 0 || read_size == MAX_PATH) {
65 return {false, ""};
66 }
67 return {true, WideStringToUtf8({path, read_size})};
68}
69
70std::string AbsolutePath(const std::string& path) {
71 char absPath[MAX_PATH];
72 _fullpath(absPath, path.c_str(), MAX_PATH);
73 return std::string(absPath);
74}
75
76std::string GetDirectoryName(const std::string& path) {
77 size_t rootLength = RootLength(path);
78 size_t separator = LastSeparator(path);
79 if (separator < rootLength)
80 separator = rootLength;
81 if (separator == std::string::npos)
82 return std::string();
83 return path.substr(0, separator);
84}
85
86std::string FromURI(const std::string& uri) {
87 if (uri.substr(0, kFileURLPrefixLength) != kFileURLPrefix)
88 return uri;
89
90 std::string file_path = uri.substr(kFileURLPrefixLength);
91 std::replace(file_path.begin(), file_path.end(), '/', '\\');
92 return SanitizeURIEscapedCharacters(file_path);
93}
94
96 // Unsupported on this platform.
97 return {};
98}
99
100} // namespace paths
101} // namespace fml
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
std::pair< bool, std::string > GetExecutablePath()
std::string AbsolutePath(const std::string &path)
Definition: paths_posix.cc:29
fml::UniqueFD GetCachesDirectory()
std::string SanitizeURIEscapedCharacters(const std::string &str)
Definition: paths.cc:32
std::string GetDirectoryName(const std::string &path)
Definition: paths_posix.cc:42
std::string FromURI(const std::string &uri)
Definition: paths_posix.cc:53
Definition: ascii_trie.cc:9
std::string WideStringToUtf8(const std::wstring_view str)
#define MAX_PATH
unsigned long DWORD
Definition: windows_types.h:22
HINSTANCE HMODULE
Definition: windows_types.h:96