Skip to content

Commit 0fe94e6

Browse files
committed
line_edit_control: small refactoring and reformatting
1 parent 85cf953 commit 0fe94e6

File tree

2 files changed

+63
-70
lines changed

2 files changed

+63
-70
lines changed

src/xrEngine/line_edit_control.cpp

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,16 @@ static bool terminate_char(char c, bool check_space = false)
6464

6565
line_edit_control::line_edit_control(u32 str_buffer_size)
6666
{
67-
m_edit_str = NULL;
68-
m_inserted = NULL;
69-
m_undo_buf = NULL;
70-
m_buf0 = NULL;
71-
m_buf1 = NULL;
72-
m_buf2 = NULL;
73-
m_buf3 = NULL;
67+
m_edit_str = nullptr;
68+
m_inserted = nullptr;
69+
m_undo_buf = nullptr;
70+
m_buf0 = nullptr;
71+
m_buf1 = nullptr;
72+
m_buf2 = nullptr;
73+
m_buf3 = nullptr;
7474

7575
for (u32 i = 0; i < DIK_COUNT; ++i)
76-
{
77-
m_actions[i] = NULL;
78-
}
76+
m_actions[i] = nullptr;
7977

8078
init(str_buffer_size);
8179

@@ -175,27 +173,27 @@ void line_edit_control::init(u32 str_buffer_size, init_mode mode)
175173
clamp(m_buffer_size, (int)MIN_BUF_SIZE, (int)MAX_BUF_SIZE);
176174

177175
xr_free(m_edit_str);
178-
m_edit_str = (LPSTR)xr_malloc(m_buffer_size * sizeof(char));
176+
m_edit_str = (pstr)xr_malloc(m_buffer_size * sizeof(char));
179177
xr_free(m_inserted);
180-
m_inserted = (LPSTR)xr_malloc(m_buffer_size * sizeof(char));
178+
m_inserted = (pstr)xr_malloc(m_buffer_size * sizeof(char));
181179
xr_free(m_undo_buf);
182-
m_undo_buf = (LPSTR)xr_malloc(m_buffer_size * sizeof(char));
180+
m_undo_buf = (pstr)xr_malloc(m_buffer_size * sizeof(char));
183181

184182
xr_free(m_buf0);
185-
m_buf0 = (LPSTR)xr_malloc(m_buffer_size * sizeof(char));
183+
m_buf0 = (pstr)xr_malloc(m_buffer_size * sizeof(char));
186184
xr_free(m_buf1);
187-
m_buf1 = (LPSTR)xr_malloc(m_buffer_size * sizeof(char));
185+
m_buf1 = (pstr)xr_malloc(m_buffer_size * sizeof(char));
188186
xr_free(m_buf2);
189-
m_buf2 = (LPSTR)xr_malloc(m_buffer_size * sizeof(char));
187+
m_buf2 = (pstr)xr_malloc(m_buffer_size * sizeof(char));
190188
xr_free(m_buf3);
191-
m_buf3 = (LPSTR)xr_malloc(m_buffer_size * sizeof(char));
189+
m_buf3 = (pstr)xr_malloc(m_buffer_size * sizeof(char));
192190

193191
clear_states();
194192

195193
for (u32 i = 0; i < DIK_COUNT; ++i)
196194
{
197195
xr_delete(m_actions[i]);
198-
m_actions[i] = NULL;
196+
m_actions[i] = nullptr;
199197
}
200198

201199
if (mode == im_read_only)
@@ -391,7 +389,7 @@ void line_edit_control::assign_callback(u32 const dik, key_state state, Callback
391389
void line_edit_control::insert_character(char c) { m_inserted[0] = c; }
392390
void line_edit_control::clear_inserted() { m_inserted[0] = m_inserted[1] = 0; }
393391
bool line_edit_control::empty_inserted() { return (m_inserted[0] == 0); }
394-
void line_edit_control::set_edit(LPCSTR str)
392+
void line_edit_control::set_edit(pcstr str)
395393
{
396394
u32 str_size = xr_strlen(str);
397395
clamp(str_size, (u32)0, (u32)(m_buffer_size - 1));
@@ -569,7 +567,7 @@ void line_edit_control::add_inserted_text()
569567
}
570568
}
571569

572-
PSTR buf = (PSTR)_alloca((m_buffer_size + 1) * sizeof(char));
570+
auto buf = (pstr)_alloca((m_buffer_size + 1) * sizeof(char));
573571

574572
strncpy_s(buf, m_buffer_size, m_edit_str, m_p1); // part 1
575573
strncpy_s(m_undo_buf, m_buffer_size, m_edit_str + m_p1, m_p2 - m_p1);
@@ -606,7 +604,7 @@ void line_edit_control::copy_to_clipboard()
606604
return;
607605
}
608606
u32 edit_len = xr_strlen(m_edit_str);
609-
PSTR buf = (PSTR)_alloca((edit_len + 1) * sizeof(char));
607+
auto buf = (pstr)_alloca((edit_len + 1) * sizeof(char));
610608
strncpy_s(buf, edit_len + 1, m_edit_str + m_p1, m_p2 - m_p1);
611609
buf[edit_len] = 0;
612610
os_clipboard::copy_to_clipboard(buf);
@@ -693,124 +691,119 @@ void line_edit_control::move_pos_right() { ++m_cur_pos; }
693691
void line_edit_control::move_pos_left_word()
694692
{
695693
int i = m_cur_pos - 1;
694+
696695
while (i >= 0 && m_edit_str[i] == ' ')
697-
{
698696
--i;
699-
}
697+
700698
if (!terminate_char(m_edit_str[i]))
701699
{
702700
while (i >= 0 && !terminate_char(m_edit_str[i], true))
703-
{
704701
--i;
705-
}
702+
706703
++i;
707704
}
705+
708706
m_cur_pos = i;
709707
}
710708

711709
void line_edit_control::move_pos_right_word()
712710
{
713711
int edit_len = (int)xr_strlen(m_edit_str);
714712
int i = m_cur_pos + 1;
713+
715714
while (i < edit_len && !terminate_char(m_edit_str[i], true))
716-
{
717715
++i;
718-
}
719-
// while( i < edit_len && terminate_char( m_edit_str[i] ) ) { ++i; }
716+
717+
//while (i < edit_len && terminate_char(m_edit_str[i]))
718+
// ++i;
719+
720720
while (i < edit_len && m_edit_str[i] == ' ')
721-
{
722721
++i;
723-
}
722+
724723
m_cur_pos = i;
725724
}
726725

727726
void line_edit_control::compute_positions()
728727
{
729728
m_p1 = m_cur_pos;
730729
m_p2 = m_cur_pos;
730+
731731
if (m_unselected_mode)
732-
{
733732
return;
734-
}
735733

736734
if (m_cur_pos > m_select_start)
737-
{
738735
m_p1 = m_select_start;
739-
}
736+
740737
else if (m_cur_pos < m_select_start)
741-
{
742738
m_p2 = m_select_start;
743-
}
744739
}
745740

746741
void line_edit_control::clamp_cur_pos() { clamp(m_cur_pos, 0, (int)xr_strlen(m_edit_str)); }
747742
void line_edit_control::SwitchKL() { ActivateKeyboardLayout((HKL)HKL_NEXT, 0); }
748743
// -------------------------------------------------------------------------------------------------
749744

750-
void remove_spaces(PSTR str) // in & out
745+
void remove_spaces(pstr str)
751746
{
752747
u32 str_size = xr_strlen(str);
753748
if (str_size < 1)
754749
{
755750
return;
756751
}
757-
PSTR new_str = (PSTR)_alloca((str_size + 1) * sizeof(char));
752+
auto new_str = (pstr)_alloca((str_size + 1) * sizeof(char));
758753
new_str[0] = 0;
759754

760755
u32 a = 0, b = 0, i = 0;
761756
while (b < str_size)
762757
{
763758
a = b;
759+
764760
while (a < str_size && str[a] == ' ')
765-
{
766761
++a;
767-
}
762+
768763
b = a;
764+
769765
while (b < str_size && str[b] != ' ')
770-
{
771766
++b;
772-
}
767+
773768
strncpy_s(new_str + i, str_size + 1, str + a, b - a);
774769
i += (b - a);
770+
775771
if (i < str_size)
776-
{
777772
new_str[i] = ' ';
778-
}
773+
779774
++b;
780775
++i;
781776
}
782777
--i;
778+
783779
if (i < str_size)
784-
{
780+
785781
strncpy_s(str, str_size, new_str, i);
786-
}
787782
}
788783

789-
void split_cmd(PSTR first, PSTR second, LPCSTR str)
784+
void split_cmd(pstr first, pstr second, pcstr str)
790785
{
791786
first[0] = 0;
792787
second[0] = 0;
788+
793789
u32 str_size = xr_strlen(str);
794790
if (str_size < 1)
795-
{
796791
return;
797-
}
798792

799793
// split into =>>(cmd) (params)
800794
u32 a = 0;
795+
801796
while (a < str_size && str[a] != ' ')
802-
{
803797
++a;
804-
}
798+
805799
strncpy_s(first, str_size + 1, str, a);
800+
806801
if (a < str_size)
807-
{
808802
first[a] = 0;
809-
}
803+
810804
else
811-
{
812805
first[str_size] = 0;
813-
}
806+
814807
++a;
815808
if (a < str_size)
816809
{

src/xrEngine/line_edit_control.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
namespace text_editor
1212
{
13-
void remove_spaces(PSTR str); // in & out
14-
void split_cmd(PSTR first, PSTR second, LPCSTR str);
13+
void remove_spaces(pstr str); // in & out
14+
void split_cmd(pstr first, pstr second, pcstr str);
1515

1616
class base;
1717

@@ -46,9 +46,8 @@ enum init_mode
4646

4747
class ENGINE_API line_edit_control
4848
{
49-
private:
50-
typedef text_editor::base Base;
51-
typedef fastdelegate::FastDelegate0<void> Callback;
49+
using Base = text_editor::base;
50+
using Callback = fastdelegate::FastDelegate0<void>;
5251

5352
public:
5453
line_edit_control(u32 str_buffer_size);
@@ -65,18 +64,19 @@ class ENGINE_API line_edit_control
6564

6665
void insert_character(char c);
6766

68-
IC bool get_key_state(key_state mask) const { return (mask) ? !!(m_key_state.test(mask)) : true; }
69-
IC void set_key_state(key_state mask, bool value) { m_key_state.set(mask, value); }
70-
IC bool cursor_view() const { return m_cursor_view; }
71-
IC bool need_update() const { return m_need_update; }
72-
IC LPCSTR str_edit() const { return m_edit_str; }
73-
IC LPCSTR str_before_cursor() const { return m_buf0; }
74-
IC LPCSTR str_before_mark() const { return m_buf1; }
75-
IC LPCSTR str_mark() const { return m_buf2; }
76-
IC LPCSTR str_after_mark() const { return m_buf3; }
77-
void set_edit(LPCSTR str);
67+
bool get_key_state(key_state mask) const { return mask ? !!m_key_state.test(mask) : true; }
68+
void set_key_state(key_state mask, bool value) { m_key_state.set(mask, value); }
69+
bool cursor_view() const { return m_cursor_view; }
70+
bool need_update() const { return m_need_update; }
71+
pcstr str_edit() const { return m_edit_str; }
72+
pcstr str_before_cursor() const { return m_buf0; }
73+
pcstr str_before_mark() const { return m_buf1; }
74+
pcstr str_mark() const { return m_buf2; }
75+
pcstr str_after_mark() const { return m_buf3; }
76+
void set_edit(pcstr str);
7877
void set_selected_mode(bool status) { m_unselected_mode = !status; }
7978
bool get_selected_mode() const { return !m_unselected_mode; }
79+
8080
private:
8181
line_edit_control(line_edit_control const&);
8282
line_edit_control const& operator=(line_edit_control const&);

0 commit comments

Comments
 (0)