• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <limits.h>
8 #include <unistd.h>
9 
10 #include "flutter/fml/logging.h"
11 
12 namespace fml {
13 namespace paths {
14 
15 namespace {
16 
17 constexpr char kFileURLPrefix[] = "file://";
18 constexpr size_t kFileURLPrefixLength = sizeof(kFileURLPrefix) - 1;
19 
GetCurrentDirectory()20 std::string GetCurrentDirectory() {
21   char buffer[PATH_MAX];
22   FML_CHECK(getcwd(buffer, sizeof(buffer)));
23   return std::string(buffer);
24 }
25 
26 }  // namespace
27 
AbsolutePath(const std::string & path)28 std::string AbsolutePath(const std::string& path) {
29   if (path.size() > 0) {
30     if (path[0] == '/') {
31       // Path is already absolute.
32       return path;
33     }
34     return GetCurrentDirectory() + "/" + path;
35   } else {
36     // Path is empty.
37     return GetCurrentDirectory();
38   }
39 }
40 
GetDirectoryName(const std::string & path)41 std::string GetDirectoryName(const std::string& path) {
42   size_t separator = path.rfind('/');
43   if (separator == 0u)
44     return "/";
45   if (separator == std::string::npos)
46     return std::string();
47   return path.substr(0, separator);
48 }
49 
FromURI(const std::string & uri)50 std::string FromURI(const std::string& uri) {
51   if (uri.substr(0, kFileURLPrefixLength) != kFileURLPrefix)
52     return uri;
53 
54   std::string file_path = uri.substr(kFileURLPrefixLength);
55   return SanitizeURIEscapedCharacters(file_path);
56 }
57 
58 }  // namespace paths
59 }  // namespace fml
60