Changeset 1288 for cpp


Ignore:
Timestamp:
12/06/23 03:32:58 (11 months ago)
Author:
Maciej Komosinski
Message:

Added helper functions

Location:
cpp/common
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/util-stl.h

    r1130 r1288  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1313        for (unsigned int i = 0; i < N; i++)
    1414                v.push_back(d[i]);
     15}
     16
     17template<typename T> void push_back(vector<T>& to, const vector<T>& from)
     18{
     19        for (auto &e : from)
     20                to.push_back(e);
    1521}
    1622
     
    3743}
    3844
    39 template<typename Key,typename Value> Value mapValueOrDefault(const std::map<Key,Value> &map, const Key& key, const Value& default_value)
     45template<typename Key, typename Value> Value mapValueOrDefault(const std::map<Key, Value> &map, const Key& key, const Value& default_value)
    4046{
    4147        auto found = map.find(key);
  • cpp/common/util-string.cpp

    r1276 r1288  
    113113}
    114114
     115bool strip_prefix(string& modify_me, const char* prefix)
     116{
     117        if (starts_with(modify_me, prefix))
     118        {
     119                modify_me = modify_me.substr(strlen(prefix));
     120                return true;
     121        }
     122        return false;
     123}
    115124
    116125char* strmove(char *a, char *b) //strcpy that works well for overlapping strings ("Source and destination overlap")
     
    162171        size_t slash = filename.rfind(PATH_SEPARATOR_CHAR);
    163172        if (slash == string::npos) return string("");
    164         return filename.substr(0, slash);
     173        return (slash == 0) ? string(PATH_SEPARATOR_STRING) : filename.substr(0, slash);
     174}
     175
     176string concatPath(const string& path, const string& other_path)
     177{
     178        string result = path;
     179        if (!result.empty() && result.back() != PATH_SEPARATOR_CHAR && !other_path.empty())
     180                result += PATH_SEPARATOR_CHAR;
     181        result += other_path;
     182        return result;
    165183}
    166184
  • cpp/common/util-string.h

    r1276 r1288  
    1313char* strmove(char *a, char *b); //strcpy that works well for overlapping strings ("Source and destination overlap")
    1414bool str_starts_with(const char *str, const char *prefix);
    15 inline bool starts_with(const string& str, const char *prefix) { return str_starts_with(str.c_str(),prefix); } //std::string.starts_with(...) since c++20
     15inline bool starts_with(const string& str, const char *prefix) { return str_starts_with(str.c_str(), prefix); } //std::string.starts_with(...) since c++20
    1616bool ends_with(std::string_view str, std::string_view suffix);
     17bool strip_prefix(string& modify_me, const char* prefix); //@return true if the prefix is found and removed
    1718
    1819string ssprintf(const char* format, ...);
     
    2324string getFileDir(const string& filename); // get path component excluding filename ("" if no dir in file)
    2425string stripFileDir(const string& filename); // strip path component from filename
     26string concatPath(const string& path, const string& other_path); // concatenate paths, adding PATH_SEPARATOR_CHAR if necessary
    2527
    2628void ltrim_inplace(string &s);
Note: See TracChangeset for help on using the changeset viewer.