dotnet-installer/src/corehost/common/utils.cpp

90 lines
2.3 KiB
C++
Raw Normal View History

// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include "utils.h"
2015-11-19 13:06:14 -08:00
#include "trace.h"
bool coreclr_exists_in_dir(const pal::string_t& candidate)
{
pal::string_t test(candidate);
2015-12-10 19:06:55 -08:00
append_path(&test, LIBCORECLR_NAME);
trace::verbose(_X("Checking if CoreCLR path exists=[%s]"), test.c_str());
return pal::file_exists(test);
}
2016-03-03 01:46:06 -08:00
bool ends_with(const pal::string_t& value, const pal::string_t& suffix, bool match_case)
{
2016-03-03 01:46:06 -08:00
auto cmp = match_case ? pal::strcmp : pal::strcasecmp;
return (value.size() >= suffix.size()) &&
cmp(value.c_str() + value.size() - suffix.size(), suffix.c_str()) == 0;
}
2016-03-03 01:46:06 -08:00
bool starts_with(const pal::string_t& value, const pal::string_t& prefix, bool match_case)
{
2016-03-03 01:46:06 -08:00
auto cmp = match_case ? pal::strncmp : pal::strncasecmp;
return (value.size() >= prefix.size()) &&
cmp(value.c_str(), prefix.c_str(), prefix.size()) == 0;
}
2015-12-10 19:06:55 -08:00
void append_path(pal::string_t* path1, const pal::char_t* path2)
2015-11-01 16:21:10 -08:00
{
2015-11-04 09:00:17 -08:00
if (pal::is_path_rooted(path2))
{
2015-12-10 19:06:55 -08:00
path1->assign(path2);
2015-11-04 09:00:17 -08:00
}
else
{
2015-12-10 19:06:55 -08:00
if (path1->empty() || path1->back() != DIR_SEPARATOR)
2015-11-04 09:00:17 -08:00
{
2015-12-10 19:06:55 -08:00
path1->push_back(DIR_SEPARATOR);
2015-11-04 09:00:17 -08:00
}
2015-12-10 19:06:55 -08:00
path1->append(path2);
2015-11-04 09:00:17 -08:00
}
2015-11-01 16:21:10 -08:00
}
pal::string_t get_executable(const pal::string_t& filename)
2015-11-01 03:39:56 -08:00
{
2015-11-04 09:00:17 -08:00
pal::string_t result(filename);
2015-11-01 16:21:10 -08:00
2016-03-03 01:46:06 -08:00
if (ends_with(result, _X(".exe"), false))
2015-11-04 09:00:17 -08:00
{
// We need to strip off the old extension
2016-03-03 01:46:06 -08:00
result.erase(result.size() - 4);
2015-11-04 09:00:17 -08:00
}
2015-11-01 16:21:10 -08:00
2015-11-04 09:00:17 -08:00
return result;
2015-11-01 03:39:56 -08:00
}
pal::string_t get_filename(const pal::string_t& path)
{
2015-11-04 09:00:17 -08:00
// Find the last dir separator
auto path_sep = path.find_last_of(DIR_SEPARATOR);
if (path_sep == pal::string_t::npos)
{
return pal::string_t(path);
}
2015-11-01 16:21:10 -08:00
2015-11-04 09:00:17 -08:00
return path.substr(path_sep + 1);
}
pal::string_t get_directory(const pal::string_t& path)
{
2015-11-04 09:00:17 -08:00
// Find the last dir separator
auto path_sep = path.find_last_of(DIR_SEPARATOR);
if (path_sep == pal::string_t::npos)
{
return pal::string_t(path);
}
2015-11-01 16:21:10 -08:00
2015-11-04 09:00:17 -08:00
return path.substr(0, path_sep);
}
2015-12-10 19:06:55 -08:00
void replace_char(pal::string_t* path, pal::char_t match, pal::char_t repl)
{
int pos = 0;
while ((pos = path->find(match, pos)) != pal::string_t::npos)
{
(*path)[pos] = repl;
}
}