Browse Source

Move system::getAbsolutePath() to string::absolutePath(). Implement on

Windows.
tags/v1.1.0
Andrew Belt 5 years ago
parent
commit
60a1a47f00
5 changed files with 35 additions and 16 deletions
  1. +4
    -0
      include/string.hpp
  2. +0
    -4
      include/system.hpp
  3. +0
    -1
      src/main.cpp
  4. +31
    -0
      src/string.cpp
  5. +0
    -11
      src/system.cpp

+ 4
- 0
include/string.hpp View File

@@ -48,6 +48,10 @@ Example: filenameExtension("file.txt") // "txt"
Note: Only works on filenames. Call filename(path) to get the filename of the path. Note: Only works on filenames. Call filename(path) to get the filename of the path.
*/ */
std::string filenameExtension(const std::string &filename); std::string filenameExtension(const std::string &filename);
/** Returns the canonicalized absolute path pointed to by `path`, following symlinks.
Returns "" if the symbol is not found.
*/
std::string absolutePath(const std::string &path);
/** Scores how well a query matches a string. /** Scores how well a query matches a string.
A score of 0 means no match. A score of 0 means no match.
The score is arbitrary and is only meaningful for sorting. The score is arbitrary and is only meaningful for sorting.


+ 0
- 4
include/system.hpp View File

@@ -25,10 +25,6 @@ void copyFile(const std::string &srcPath, const std::string &destPath);
The parent directory must exist. The parent directory must exist.
*/ */
void createDirectory(const std::string &path); void createDirectory(const std::string &path);
/** Returns the canonicalized absolute path pointed to by `path`.
Returns "" if the symbol is not found.
*/
std::string getAbsolutePath(const std::string &path);
/** Returns the number of logical simultaneous multithreading (SMT) (e.g. Intel Hyperthreaded) threads on the CPU. */ /** Returns the number of logical simultaneous multithreading (SMT) (e.g. Intel Hyperthreaded) threads on the CPU. */
int getLogicalCoreCount(); int getLogicalCoreCount();
/** Sets a name of the current thread for debuggers and OS-specific process viewers. */ /** Sets a name of the current thread for debuggers and OS-specific process viewers. */


+ 0
- 1
src/main.cpp View File

@@ -95,7 +95,6 @@ int main(int argc, char *argv[]) {


asset::init(); asset::init();
logger::init(); logger::init();
DEBUG("abspath: %s", system::getAbsolutePath("今日は/a").c_str());


// We can now install a signal handler and log the output // We can now install a signal handler and log the output
// Mac has its own decent crash handler // Mac has its own decent crash handler


+ 31
- 0
src/string.cpp View File

@@ -15,11 +15,13 @@ std::string fromWstring(const std::wstring &s) {
return converter.to_bytes(s); return converter.to_bytes(s);
} }



std::wstring toWstring(const std::string &s) { std::wstring toWstring(const std::string &s) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.from_bytes(s); return converter.from_bytes(s);
} }



std::string f(const char *format, ...) { std::string f(const char *format, ...) {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
@@ -37,18 +39,21 @@ std::string f(const char *format, ...) {
return s; return s;
} }



std::string lowercase(const std::string &s) { std::string lowercase(const std::string &s) {
std::string r = s; std::string r = s;
std::transform(r.begin(), r.end(), r.begin(), [](unsigned char c) { return std::tolower(c); }); std::transform(r.begin(), r.end(), r.begin(), [](unsigned char c) { return std::tolower(c); });
return r; return r;
} }



std::string uppercase(const std::string &s) { std::string uppercase(const std::string &s) {
std::string r = s; std::string r = s;
std::transform(r.begin(), r.end(), r.begin(), [](unsigned char c) { return std::toupper(c); }); std::transform(r.begin(), r.end(), r.begin(), [](unsigned char c) { return std::toupper(c); });
return r; return r;
} }



std::string trim(const std::string &s) { std::string trim(const std::string &s) {
const std::string whitespace = " \n\r\t"; const std::string whitespace = " \n\r\t";
size_t first = s.find_first_not_of(whitespace); size_t first = s.find_first_not_of(whitespace);
@@ -60,6 +65,7 @@ std::string trim(const std::string &s) {
return s.substr(first, last - first + 1); return s.substr(first, last - first + 1);
} }



std::string ellipsize(const std::string &s, size_t len) { std::string ellipsize(const std::string &s, size_t len) {
if (s.size() <= len) if (s.size() <= len)
return s; return s;
@@ -67,6 +73,7 @@ std::string ellipsize(const std::string &s, size_t len) {
return s.substr(0, len - 3) + "..."; return s.substr(0, len - 3) + "...";
} }



std::string ellipsizePrefix(const std::string &s, size_t len) { std::string ellipsizePrefix(const std::string &s, size_t len) {
if (s.size() <= len) if (s.size() <= len)
return s; return s;
@@ -74,14 +81,17 @@ std::string ellipsizePrefix(const std::string &s, size_t len) {
return "..." + s.substr(s.size() - (len - 3)); return "..." + s.substr(s.size() - (len - 3));
} }



bool startsWith(const std::string &str, const std::string &prefix) { bool startsWith(const std::string &str, const std::string &prefix) {
return str.substr(0, prefix.size()) == prefix; return str.substr(0, prefix.size()) == prefix;
} }



bool endsWith(const std::string &str, const std::string &suffix) { bool endsWith(const std::string &str, const std::string &suffix) {
return str.substr(str.size() - suffix.size(), suffix.size()) == suffix; return str.substr(str.size() - suffix.size(), suffix.size()) == suffix;
} }



std::string directory(const std::string &path) { std::string directory(const std::string &path) {
char *pathDup = strdup(path.c_str()); char *pathDup = strdup(path.c_str());
std::string directory = dirname(pathDup); std::string directory = dirname(pathDup);
@@ -89,6 +99,7 @@ std::string directory(const std::string &path) {
return directory; return directory;
} }



std::string filename(const std::string &path) { std::string filename(const std::string &path) {
char *pathDup = strdup(path.c_str()); char *pathDup = strdup(path.c_str());
std::string filename = basename(pathDup); std::string filename = basename(pathDup);
@@ -96,6 +107,7 @@ std::string filename(const std::string &path) {
return filename; return filename;
} }



std::string filenameBase(const std::string &filename) { std::string filenameBase(const std::string &filename) {
size_t pos = filename.rfind('.'); size_t pos = filename.rfind('.');
if (pos == std::string::npos) if (pos == std::string::npos)
@@ -103,6 +115,7 @@ std::string filenameBase(const std::string &filename) {
return std::string(filename, 0, pos); return std::string(filename, 0, pos);
} }



std::string filenameExtension(const std::string &filename) { std::string filenameExtension(const std::string &filename) {
size_t pos = filename.rfind('.'); size_t pos = filename.rfind('.');
if (pos == std::string::npos) if (pos == std::string::npos)
@@ -110,6 +123,24 @@ std::string filenameExtension(const std::string &filename) {
return std::string(filename, pos + 1); return std::string(filename, pos + 1);
} }



std::string absolutePath(const std::string &path) {
#if defined ARCH_LIN || defined ARCH_MAC
char buf[PATH_MAX];
char *absPathC = realpath(path.c_str(), buf);
if (absPathC)
return absPathC;
#elif defined ARCH_WIN
std::wstring pathW = toWstring(path);
wchar_t buf[PATH_MAX];
wchar_t *absPathC = _wfullpath(buf, pathW.c_str(), PATH_MAX);
if (absPathC)
return fromWstring(absPathC);
#endif
return "";
}


float fuzzyScore(const std::string &s, const std::string &query) { float fuzzyScore(const std::string &s, const std::string &query) {
size_t pos = s.find(query); size_t pos = s.find(query);
if (pos == std::string::npos) if (pos == std::string::npos)


+ 0
- 11
src/system.cpp View File

@@ -107,17 +107,6 @@ void createDirectory(const std::string &path) {
} }




std::string getAbsolutePath(const std::string &path) {
#if defined ARCH_LIN || defined ARCH_MAC
char buf[PATH_MAX];
char *pathC = realpath(path.c_str(), buf);
if (pathC)
return pathC;
#endif
return "";
}


int getLogicalCoreCount() { int getLogicalCoreCount() {
return std::thread::hardware_concurrency(); return std::thread::hardware_concurrency();
} }


Loading…
Cancel
Save