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 
13 namespace fml {
14 namespace paths {
15 
16 namespace {
17 
18 constexpr char kFileURLPrefix[] = "file:///";
19 constexpr size_t kFileURLPrefixLength = sizeof(kFileURLPrefix) - 1;
20 
21 size_t RootLength(const std::string& path) {
22  if (path.size() == 0)
23  return 0;
24  if (path[0] == '/')
25  return 1;
26  if (path[0] == '\\') {
27  if (path.size() < 2 || path[1] != '\\')
28  return 1;
29  // The path is a network share. Search for up to two '\'s, as they are
30  // the server and share - and part of the root part.
31  size_t index = path.find('\\', 2);
32  if (index > 0) {
33  index = path.find('\\', index + 1);
34  if (index > 0)
35  return index;
36  }
37  return path.size();
38  }
39  // If the path is of the form 'C:/' or 'C:\', with C being any letter, it's
40  // a root part.
41  if (path.length() >= 2 && path[1] == ':' &&
42  (path[2] == '/' || path[2] == '\\') &&
43  ((path[0] >= 'A' && path[0] <= 'Z') ||
44  (path[0] >= 'a' && path[0] <= 'z'))) {
45  return 3;
46  }
47  return 0;
48 }
49 
50 size_t LastSeparator(const std::string& path) {
51  return path.find_last_of("/\\");
52 }
53 
54 } // namespace
55 
56 std::pair<bool, std::string> GetExecutableDirectoryPath() {
57  HMODULE module = GetModuleHandle(NULL);
58  if (module == NULL) {
59  return {false, ""};
60  }
61  char path[MAX_PATH];
62  DWORD read_size = GetModuleFileNameA(module, path, MAX_PATH);
63  if (read_size == 0 || read_size == MAX_PATH) {
64  return {false, ""};
65  }
66  return {true, GetDirectoryName(std::string{path, read_size})};
67 }
68 
69 std::string AbsolutePath(const std::string& path) {
70  char absPath[MAX_PATH];
71  _fullpath(absPath, path.c_str(), MAX_PATH);
72  return std::string(absPath);
73 }
74 
75 std::string GetDirectoryName(const std::string& path) {
76  size_t rootLength = RootLength(path);
77  size_t separator = LastSeparator(path);
78  if (separator < rootLength)
79  separator = rootLength;
80  if (separator == std::string::npos)
81  return std::string();
82  return path.substr(0, separator);
83 }
84 
85 std::string FromURI(const std::string& uri) {
86  if (uri.substr(0, kFileURLPrefixLength) != kFileURLPrefix)
87  return uri;
88 
89  std::string file_path = uri.substr(kFileURLPrefixLength);
90  std::replace(file_path.begin(), file_path.end(), '/', '\\');
91  return SanitizeURIEscapedCharacters(file_path);
92 }
93 
95  // Unsupported on this platform.
96  return {};
97 }
98 
99 } // namespace paths
100 } // namespace fml
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
std::string SanitizeURIEscapedCharacters(const std::string &str)
Definition: paths.cc:32
fml::UniqueFD GetCachesDirectory()
Definition: ascii_trie.cc:9
std::string AbsolutePath(const std::string &path)
Definition: paths_posix.cc:29
std::string GetDirectoryName(const std::string &path)
Definition: paths_posix.cc:42
std::pair< bool, std::string > GetExecutableDirectoryPath()
std::string FromURI(const std::string &uri)
Definition: paths_posix.cc:53