Skip to content

Commit

Permalink
partial shared_str replacement (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Im-dex committed Nov 6, 2017
1 parent 8d2f28e commit ee987c8
Show file tree
Hide file tree
Showing 57 changed files with 404 additions and 392 deletions.
5 changes: 5 additions & 0 deletions code/engine/xrCore/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ void IReader::r_stringZ(xr_string& dest) {
Pos += int(dest.size() + 1);
}

void IReader::r_stringZ(std::string& dest) {
dest = data + Pos;
Pos += int(dest.size() + 1);
}

void IReader::skip_stringZ() {
char* src = data;
while ((src[Pos] != 0) && !eof())
Expand Down
9 changes: 8 additions & 1 deletion code/engine/xrCore/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,15 @@ class XRCORE_API IWriter {
}

void w_stringZ(const xr_string& p) {
w(p.c_str() ? p.c_str() : "", p.size());
w(p.c_str(), p.size());
w_u8(0);
}

void w_stringZ(const std::string& p) {
w(p.c_str(), p.size());
w_u8(0);
}

void w_fcolor(const Fcolor& v) { w(&v, sizeof(Fcolor)); }
void w_fvector4(const Fvector4& v) { w(&v, sizeof(Fvector4)); }
void w_fvector3(const Fvector3& v) { w(&v, sizeof(Fvector3)); }
Expand Down Expand Up @@ -380,6 +386,7 @@ class XRCORE_API IReader : public IReaderBase<IReader> {
void r_stringZ(char* dest, const size_t tgt_sz);
void r_stringZ(shared_str& dest);
void r_stringZ(xr_string& dest);
void r_stringZ(std::string& dest);

void close();

Expand Down
15 changes: 11 additions & 4 deletions code/engine/xrCore/xrstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,18 @@ IC int xr_strcmp(const shared_str& a, const shared_str& b) {
else
return xr_strcmp(*a, *b);
}
IC void xr_strlwr(xr_string& src) {
for (xr_string::iterator it = src.begin(); it != src.end(); it++)
*it = xr_string::value_type(tolower(*it));

[[deprecated]]
inline void xr_strlwr(xr_string& src) {
_strlwr(src.data());
}

[[deprecated]]
inline void xr_strlwr(std::string& src) {
_strlwr(src.data());
}
IC void xr_strlwr(shared_str& src) {

inline void xr_strlwr(shared_str& src) {
if (*src) {
LPSTR lp = xr_strdup(*src);
xr_strlwr(lp);
Expand Down
3 changes: 2 additions & 1 deletion code/engine/xrEngine/Environment_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,10 @@ void CEnvironment::mods_load() {
void CEnvironment::mods_unload() { Modifiers.clear(); }

void CEnvironment::load_level_specific_ambients() {
const shared_str level_name = g_pGameLevel->name();
const auto& level_name = g_pGameLevel->name();

string_path path;
// TODO: [imdex] use string_view
strconcat(sizeof(path), path, "environment\\ambients\\", level_name.c_str(), ".ltx");

string_path full_path;
Expand Down
6 changes: 4 additions & 2 deletions code/engine/xrEngine/FDemoRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,11 @@ void CDemoRecord::MakeLevelMapProcess() {

string_path tmp;
if (m_iLMScreenshotFragment == -1)
xr_sprintf(tmp, sizeof(tmp), "map_%s", *g_pGameLevel->name());
// TODO: [imdex] use string_view
xr_sprintf(tmp, sizeof(tmp), "map_%s", g_pGameLevel->name().c_str());
else
xr_sprintf(tmp, sizeof(tmp), "map_%s#%d", *g_pGameLevel->name(),
// TODO: [imdex] use string_view
xr_sprintf(tmp, sizeof(tmp), "map_%s#%d", g_pGameLevel->name().c_str(),
m_iLMScreenshotFragment);

if (m_iLMScreenshotFragment != -1) {
Expand Down
2 changes: 1 addition & 1 deletion code/engine/xrEngine/IGame_Level.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class ENGINE_API IGame_Level : public DLL_Pure,
IGame_Level();
virtual ~IGame_Level();

virtual shared_str name() const = 0;
virtual const std::string& name() const = 0;
virtual void GetLevelInfo(CServerInfo* si) = 0;

virtual BOOL net_Start(LPCSTR op_server, LPCSTR op_client) = 0;
Expand Down
68 changes: 31 additions & 37 deletions code/engine/xrEngine/XR_IOConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ CConsole::CConsole() : m_hShader_back(NULL) {
void CConsole::Initialize() {
scroll_delta = 0;
bVisible = false;
pFont = NULL;
pFont2 = NULL;
pFont = nullptr;
pFont2 = nullptr;

m_mouse_pos.x = 0;
m_mouse_pos.y = 0;
m_last_cmd = NULL;
m_last_cmd.clear();

m_cmd_history.reserve(m_cmd_history_max + 2);
m_cmd_history.clear();
Expand All @@ -145,7 +145,7 @@ void CConsole::Initialize() {

m_tips_mode = 0;
m_prev_length_str = 0;
m_cur_cmd = NULL;
m_cur_cmd.clear();
reset_selected_tip();

// Commands
Expand Down Expand Up @@ -529,7 +529,7 @@ void CConsole::ExecuteCommand(LPCSTR cmd_str, bool record_cmd) {
c[0] = mark2;
c[1] = 0;

if (m_last_cmd.c_str() == 0 || xr_strcmp(m_last_cmd, edt) != 0) {
if (m_last_cmd.empty() || m_last_cmd != edt) {
Log(c, edt);
add_cmd_history(edt);
m_last_cmd = edt;
Expand Down Expand Up @@ -637,7 +637,7 @@ void CConsole::ExecuteScript(LPCSTR str) {

// -------------------------------------------------------------------------------------------------

IConsole_Command* CConsole::find_next_cmd(LPCSTR in_str, shared_str& out_str) {
IConsole_Command* CConsole::find_next_cmd(LPCSTR in_str, std::string& out_str) {
LPCSTR radmin_cmd_name = "ra ";
bool b_ra = (in_str == strstr(in_str, radmin_cmd_name));
u32 offset = (b_ra) ? xr_strlen(radmin_cmd_name) : 0;
Expand All @@ -655,10 +655,10 @@ IConsole_Command* CConsole::find_next_cmd(LPCSTR in_str, shared_str& out_str) {
xr_strcpy(new_str, offset + name_cmd_size + 2, (b_ra) ? radmin_cmd_name : "");
xr_strcat(new_str, offset + name_cmd_size + 2, name_cmd);

out_str._set((LPCSTR)new_str);
out_str = (LPCSTR)new_str;
return cc;
}
return NULL;
return nullptr;
}

bool CConsole::add_next_cmds(LPCSTR in_str, vecTipsEx& out_v) {
Expand All @@ -670,7 +670,7 @@ bool CConsole::add_next_cmds(LPCSTR in_str, vecTipsEx& out_v) {
LPSTR t2;
STRCONCAT(t2, in_str, " ");

shared_str temp;
std::string temp;
IConsole_Command* cc = find_next_cmd(t2, temp);
if (!cc || temp.size() == 0) {
return false;
Expand All @@ -679,11 +679,10 @@ bool CConsole::add_next_cmds(LPCSTR in_str, vecTipsEx& out_v) {
bool res = false;
for (u32 i = cur_count; i < MAX_TIPS_COUNT * 2; ++i) // fake=protect
{
temp._set(cc->Name());
bool dup = (std::find(out_v.begin(), out_v.end(), temp) != out_v.end());
temp = cc->Name();
const bool dup = (std::find(out_v.begin(), out_v.end(), temp) != out_v.end());
if (!dup) {
TipString ts(temp);
out_v.push_back(ts);
out_v.push_back(TipString(temp));
res = true;
}
if (out_v.size() >= MAX_TIPS_COUNT) {
Expand Down Expand Up @@ -720,11 +719,10 @@ bool CConsole::add_internal_cmds(LPCSTR in_str, vecTipsEx& out_v) {
name2[in_sz] = 0;

if (!stricmp(name2, in_str)) {
shared_str temp;
temp._set(name);
bool dup = (std::find(out_v.begin(), out_v.end(), temp) != out_v.end());
std::string temp(name);
const bool dup = std::find(out_v.begin(), out_v.end(), temp) != out_v.end();
if (!dup) {
out_v.push_back(TipString(temp, 0, in_sz));
out_v.push_back(TipString(std::move(temp), 0, in_sz));
res = true;
}
}
Expand All @@ -742,13 +740,12 @@ bool CConsole::add_internal_cmds(LPCSTR in_str, vecTipsEx& out_v) {
LPCSTR name = itb->first;
LPCSTR fd_str = strstr(name, in_str);
if (fd_str) {
shared_str temp;
temp._set(name);
std::string temp(name);
bool dup = (std::find(out_v.begin(), out_v.end(), temp) != out_v.end());
if (!dup) {
u32 name_sz = xr_strlen(name);
int fd_sz = name_sz - xr_strlen(fd_str);
out_v.push_back(TipString(temp, fd_sz, fd_sz + in_sz));
const u32 name_sz = xr_strlen(name);
const int fd_sz = name_sz - xr_strlen(fd_str);
out_v.push_back(TipString(std::move(temp), fd_sz, fd_sz + in_sz));
res = true;
}
}
Expand All @@ -764,7 +761,7 @@ void CConsole::update_tips() {
m_temp_tips.clear();
m_tips.clear();

m_cur_cmd = NULL;
m_cur_cmd.clear();
if (!bVisible) {
return;
}
Expand Down Expand Up @@ -808,7 +805,7 @@ void CConsole::update_tips() {

cc->fill_tips(m_temp_tips, mode);
m_tips_mode = 2;
m_cur_cmd._set(first);
m_cur_cmd = first;
select_for_filter(last, m_temp_tips, m_tips);

if (m_tips.size() == 0) {
Expand Down Expand Up @@ -838,27 +835,24 @@ void CConsole::update_tips() {
}
}

void CConsole::select_for_filter(LPCSTR filter_str, vecTips& in_v, vecTipsEx& out_v) {
void CConsole::select_for_filter(const char* filter_str, vecTips& in_v, vecTipsEx& out_v) const {
out_v.clear();
u32 in_count = in_v.size();
if (in_count == 0 || !filter_str) {
if (in_v.empty() || !filter_str) {
return;
}

bool all = (xr_strlen(filter_str) == 0);
const std::string_view filter = filter_str;
const bool all = filter.empty();

vecTips::iterator itb = in_v.begin();
vecTips::iterator ite = in_v.end();
for (; itb != ite; ++itb) {
shared_str const& str = (*itb);
for (const auto& str : in_v) {
if (all) {
out_v.push_back(TipString(str));
} else {
LPCSTR fd_str = strstr(str.c_str(), filter_str);
if (fd_str) {
int fd_sz = str.size() - xr_strlen(fd_str);
TipString ts(str, fd_sz, fd_sz + xr_strlen(filter_str));
out_v.push_back(ts);
const auto pos = str.find(filter);
if (pos != std::string::npos) {
const int fd_sz = str.size() - pos;
TipString ts(str, fd_sz, fd_sz + filter.size());
out_v.push_back(std::move(ts));
}
}
} // for
Expand Down
50 changes: 21 additions & 29 deletions code/engine/xrEngine/XR_IOConsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,23 @@ class line_edit_control;
}; // namespace text_editor

struct TipString {
shared_str text;
std::string text;
int HL_start; // Highlight
int HL_finish;

TipString() {
text._set("");
HL_start = 0;
HL_finish = 0;
}
TipString(shared_str const& tips_text, int start_pos, int finish_pos) {
text._set(tips_text);
HL_start = start_pos;
HL_finish = finish_pos;
}
TipString(LPCSTR tips_text, int start_pos, int finish_pos) {
text._set(tips_text);
HL_start = start_pos;
HL_finish = finish_pos;
}
TipString(shared_str const& tips_text) {
text._set(tips_text);
HL_start = 0;
HL_finish = 0;
}
IC bool operator==(shared_str const& tips_text) { return (text == tips_text); }
TipString() = default;

TipString(std::string tips_text, const int start_pos = 0, const int finish_pos = 0)
: text(std::move(tips_text)),
HL_start(start_pos),
HL_finish(finish_pos)
{}

TipString(const std::string_view tips_text, const int start_pos, const int finish_pos)
: TipString(std::string(tips_text), start_pos, finish_pos)
{}

bool operator==(const std::string_view tips_text) { return text == tips_text; }
};

class ENGINE_API CConsole : public pureRender,
Expand All @@ -55,8 +47,8 @@ class ENGINE_API CConsole : public pureRender,
typedef vecCMD::iterator vecCMD_IT;
typedef vecCMD::const_iterator vecCMD_CIT;
typedef fastdelegate::FastDelegate0<void> Callback;
typedef xr_vector<shared_str> vecHistory;
typedef xr_vector<shared_str> vecTips;
typedef xr_vector<std::string> vecHistory;
typedef xr_vector<std::string> vecTips;
typedef xr_vector<TipString> vecTipsEx;

enum { CONSOLE_BUF_SIZE = 1024 };
Expand All @@ -77,13 +69,13 @@ class ENGINE_API CConsole : public pureRender,
vecHistory m_cmd_history;
u32 m_cmd_history_max;
int m_cmd_history_idx;
shared_str m_last_cmd;
std::string m_last_cmd;
BENCH_SEC_SCRAMBLEMEMBER1

vecTips m_temp_tips;
vecTipsEx m_tips;
u32 m_tips_mode;
shared_str m_cur_cmd;
std::string m_cur_cmd;
int m_select_tip;
int m_start_tip;
u32 m_prev_length_str;
Expand Down Expand Up @@ -178,7 +170,7 @@ class ENGINE_API CConsole : public pureRender,
void xr_stdcall GamePause();

protected:
void add_cmd_history(shared_str const& str);
void add_cmd_history(std::string str);
void next_cmd_history_idx();
void prev_cmd_history_idx();
void reset_cmd_history_idx();
Expand All @@ -189,12 +181,12 @@ class ENGINE_API CConsole : public pureRender,
void check_prev_selected_tip();
void reset_selected_tip();

IConsole_Command* find_next_cmd(LPCSTR in_str, shared_str& out_str);
IConsole_Command* find_next_cmd(LPCSTR in_str, std::string& out_str);
bool add_next_cmds(LPCSTR in_str, vecTipsEx& out_v);
bool add_internal_cmds(LPCSTR in_str, vecTipsEx& out_v);

void update_tips();
void select_for_filter(LPCSTR filter_str, vecTips& in_v, vecTipsEx& out_v);
void select_for_filter(const char* filter_str, vecTips& in_v, vecTipsEx& out_v) const;

}; // class CConsole

Expand Down
12 changes: 6 additions & 6 deletions code/engine/xrEngine/XR_IOConsole_callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ void CConsole::End_log() // PAGE_DOWN+Ctrl

void CConsole::Find_cmd() // DIK_TAB
{
shared_str out_str;
std::string out_str;

IConsole_Command* cc = find_next_cmd(ec().str_edit(), out_str);
if (cc && out_str.size()) {
ec().set_edit(out_str.c_str());
if (cc && !out_str.empty()) {
ec().set_edit(out_str.c_str()); // TODO: [imdex] use string_view
}
}

Expand Down Expand Up @@ -153,14 +153,14 @@ void CConsole::PageDown_tips() {
void CConsole::Execute_cmd() // DIK_RETURN, DIK_NUMPADENTER
{
if (0 <= m_select_tip && m_select_tip < (int)m_tips.size()) {
shared_str const& str = m_tips[m_select_tip].text;
const auto& str = m_tips[m_select_tip].text;
if (m_tips_mode == 1) {
LPSTR buf;
STRCONCAT(buf, str.c_str(), " ");
STRCONCAT(buf, str.c_str(), " "); // TODO: [imdex] use string_view
ec().set_edit(buf);
} else if (m_tips_mode == 2) {
LPSTR buf;
STRCONCAT(buf, m_cur_cmd.c_str(), " ", str.c_str());
STRCONCAT(buf, m_cur_cmd.c_str(), " ", str.c_str()); // TODO: [imdex] use string_view
ec().set_edit(buf);
}
reset_selected_tip();
Expand Down

0 comments on commit ee987c8

Please sign in to comment.