Cogs.Core
CommandParseHelpers.h
1#pragma once
2
3#include "Utilities/Parsing.h"
4
5#include <ctime>
6#include <iomanip>
7#include <sstream>
8#include <string>
9#include <vector>
10
12{
13 class Entity;
14}
15
16namespace Cogs::Core
17{
19 inline StringView getOption(const std::vector<ParsedValue> & options, const StringView & key, const StringView & defaultValue = "")
20 {
21 for (auto & o : options) {
22 if (key == o.key) {
23 return o.value;
24 }
25 }
26
27 return defaultValue;
28 }
29
31 inline size_t getOption(const std::vector<ParsedValue> & options, const StringView & key, const size_t defaultValue)
32 {
33 for (auto & o : options) {
34 if (key == o.key) {
35 int value;
36 if (!o.asInt(value)) return defaultValue;
37 return size_t(value);
38 }
39 }
40
41 return defaultValue;
42 }
43
45 inline float getOption(const std::vector<ParsedValue> & options, const StringView & key, const float defaultValue)
46 {
47 for (auto & o : options) {
48 if (key == o.key) {
49 float value;
50 if (!o.asFloat(value)) value = defaultValue;
51 return value;
52 }
53 }
54
55 return defaultValue;
56 }
57
59 inline bool replaceToken(std::string & str, const std::vector<ParsedValue> & options, const std::string token, const StringView & option, const StringView & direct)
60 {
61 auto fPos = str.find(token);
62
63 if (fPos != std::string::npos) {
64 auto replacement = option.empty() ? direct : getOption(options, option);
65
66 if (replacement.empty()) {
67 return false;
68 }
69
70 str.replace(fPos, token.length(), replacement.to_string());
71 }
72
73 return true;
74 }
75
76 inline std::string makeDateString(std::time_t & t, const StringView & fmt)
77 {
78#ifdef WIN32
79 struct tm tm;
80 localtime_s(&tm, &t);
81#else
82 auto & tm = *localtime(&t);
83#endif
84
85 std::ostringstream oss;
86
87 oss << std::put_time(&tm, fmt.data());
88 auto year = oss.str();
89
90 return oss.str();
91 }
92
94 inline std::string replaceFileMacros(std::string outputName, const std::vector<ParsedValue> & options)
95 {
96 auto t = std::time(nullptr);
97
98 auto year = makeDateString(t, "%Y");
99 auto month = makeDateString(t, "%m");
100 auto day = makeDateString(t, "%d");
101 auto date = makeDateString(t, "%Y-%m-%d");
102
103 struct TokenReplacementPair
104 {
105 const char * token;
106 const char * optionName;
107 const char * direct;
108 } tokens[] = {
109 { "$FileName", "stem", nullptr}, // Value of the "stem" option.
110 { "$Extension", "extension", nullptr }, // Value of the "extension" option.
111 { "$Year", "", year.c_str() }, // Value of Current date year.
112 { "$Month", "", month.c_str() },
113 { "$Day", "", day.c_str() },
114 { "$Date", "", date.c_str() },
115 };
116
117 for (auto & p : tokens) {
118 replaceToken(outputName, options, p.token, p.optionName, p.direct);
119 }
120
121 return outputName;
122 }
123
125 inline std::string fixInvalidPathChars(const std::string & filename)
126 {
127 std::string output;
128 for (auto c : filename) {
129 switch (c) {
130 case '<':
131 case '>':
132 case ':':
133 case '"':
134 case '/':
135 case '\\':
136 case '|':
137 case '?':
138 case '*':
139 break;
140 default:
141 output.push_back(c);
142 }
143 }
144
145 return output;
146 }
147}
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
constexpr const char * data() const noexcept
Get the sequence of characters referenced by the string view.
Definition: StringView.h:171
Contains code for composing and managing entities built from components.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
bool replaceToken(std::string &str, const std::vector< ParsedValue > &options, const std::string token, const StringView &option, const StringView &direct)
Check string for the given token. Replace with option string is set else direct value.
std::string replaceFileMacros(std::string outputName, const std::vector< ParsedValue > &options)
Replace common macroes for file name generation. I.e.: "A/$Filename" etc.
std::string fixInvalidPathChars(const std::string &filename)
Remove characters that are not valid for filenames lik "<>:/\?" etc.
StringView getOption(const std::vector< ParsedValue > &options, const StringView &key, const StringView &defaultValue="")
Find and get value of option in vector as a string. Return default if not found.