paths_win.cc 2.1 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/fml/paths.h"

#include <windows.h>

9
#include "flutter/fml/paths.h"
10 11 12 13

namespace fml {
namespace paths {

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
namespace {

size_t RootLength(const std::string& path) {
  if (path.size() == 0)
    return 0;
  if (path[0] == '/')
    return 1;
  if (path[0] == '\\') {
    if (path.size() < 2 || path[1] != '\\')
      return 1;
    // The path is a network share. Search for up to two '\'s, as they are
    // the server and share - and part of the root part.
    size_t index = path.find('\\', 2);
    if (index > 0) {
      index = path.find('\\', index + 1);
      if (index > 0)
        return index;
    }
    return path.size();
  }
  // If the path is of the form 'C:/' or 'C:\', with C being any letter, it's
  // a root part.
  if (path.length() >= 2 && path[1] == ':' &&
      (path[2] == '/' || path[2] == '\\') &&
      ((path[0] >= 'A' && path[0] <= 'Z') ||
       (path[0] >= 'a' && path[0] <= 'z'))) {
    return 3;
  }
  return 0;
}

size_t LastSeparator(const std::string& path) {
  return path.find_last_of("/\\");
}

}  // namespace

51 52 53 54 55 56 57 58 59 60
std::pair<bool, std::string> GetExecutableDirectoryPath() {
  HMODULE module = GetModuleHandle(NULL);
  if (module == NULL) {
    return {false, ""};
  }
  char path[MAX_PATH];
  DWORD read_size = GetModuleFileNameA(module, path, MAX_PATH);
  if (read_size == 0 || read_size == MAX_PATH) {
    return {false, ""};
  }
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  return {true, GetDirectoryName(std::string{path, read_size})};
}

std::string AbsolutePath(const std::string& path) {
  char absPath[MAX_PATH];
  _fullpath(absPath, path.c_str(), MAX_PATH);
  return std::string(absPath);
}

std::string GetDirectoryName(const std::string& path) {
  size_t rootLength = RootLength(path);
  size_t separator = LastSeparator(path);
  if (separator < rootLength)
    separator = rootLength;
  if (separator == std::string::npos)
    return std::string();
  return path.substr(0, separator);
78 79 80 81
}

}  // namespace paths
}  // namespace fml