Flutter Engine
paths_posix.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 <unistd.h>
8 
9 #include <climits>
10 
11 #include "flutter/fml/logging.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 std::string GetCurrentDirectory() {
22  char buffer[PATH_MAX];
23  FML_CHECK(getcwd(buffer, sizeof(buffer)));
24  return std::string(buffer);
25 }
26 
27 } // namespace
28 
29 std::string AbsolutePath(const std::string& path) {
30  if (path.size() > 0) {
31  if (path[0] == '/') {
32  // Path is already absolute.
33  return path;
34  }
35  return GetCurrentDirectory() + "/" + path;
36  } else {
37  // Path is empty.
38  return GetCurrentDirectory();
39  }
40 }
41 
42 std::string GetDirectoryName(const std::string& path) {
43  size_t separator = path.rfind('/');
44  if (separator == 0u) {
45  return "/";
46  }
47  if (separator == std::string::npos) {
48  return std::string();
49  }
50  return path.substr(0, separator);
51 }
52 
53 std::string FromURI(const std::string& uri) {
54  if (uri.substr(0, kFileURLPrefixLength) != kFileURLPrefix) {
55  return uri;
56  }
57 
58  std::string file_path = uri.substr(kFileURLPrefixLength);
59  return SanitizeURIEscapedCharacters(file_path);
60 }
61 
62 } // namespace paths
63 } // namespace fml
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
std::string SanitizeURIEscapedCharacters(const std::string &str)
Definition: paths.cc:32
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::string FromURI(const std::string &uri)
Definition: paths_posix.cc:53
#define FML_CHECK(condition)
Definition: logging.h:68