Create a branch for cmake bringup.
[reactos.git] / base / applications / network / telnet / src / tkeydef.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 //Telnet Win32 : an ANSI telnet client.
3 //Copyright (C) 1998-2000 Paul Brannan
4 //Copyright (C) 1998 I.Ioannou
5 //Copyright (C) 1997 Brad Johnson
6 //
7 //This program is free software; you can redistribute it and/or
8 //modify it under the terms of the GNU General Public License
9 //as published by the Free Software Foundation; either version 2
10 //of the License, or (at your option) any later version.
11 //
12 //This program is distributed in the hope that it will be useful,
13 //but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 //GNU General Public License for more details.
16 //
17 //You should have received a copy of the GNU General Public License
18 //along with this program; if not, write to the Free Software
19 //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 //I.Ioannou
22 //roryt@hol.gr
23 //
24 ///////////////////////////////////////////////////////////////////////////
25
26 /////////////////////////////////////////////////////////
27 // Class TkeyDef - Key Definitions //
28 // - kept in an array container //
29 // originally part of KeyTrans.cpp //
30 /////////////////////////////////////////////////////////
31
32 #include "tkeydef.h"
33 #include <string.h>
34
35 // This class did not properly release memory before, and a buffer overrun
36 // was apparent in operator=(char*). Fixed. (Paul Brannan Feb. 4, 1999)
37
38 TKeyDef::TKeyDef() {
39 uKeyDef.szKeyDef = 0;
40 vk_code = dwState = 0;
41 }
42
43 TKeyDef::TKeyDef(char *def, DWORD state, DWORD code) {
44 uKeyDef.szKeyDef = 0;
45 if (def != NULL && *def != 0) {
46 // szKeyDef = (char *) GlobalAlloc(GPTR, strlen(def) +1);
47 uKeyDef.szKeyDef = new char[strlen(def)+1];
48 strcpy(uKeyDef.szKeyDef, def);
49 }
50 dwState = state;
51 vk_code = code;
52 }
53
54 TKeyDef::TKeyDef(optype op, DWORD state, DWORD code) {
55 uKeyDef.op = new optype;
56 uKeyDef.op->sendstr = 0;
57 uKeyDef.op->the_op = op.the_op;
58 dwState = state;
59 vk_code = code;
60 }
61
62 TKeyDef::TKeyDef(const TKeyDef &t) {
63 if(t.uKeyDef.szKeyDef == NULL) {
64 uKeyDef.szKeyDef = (char *)NULL;
65 } else if(t.uKeyDef.op->sendstr == 0) {
66 uKeyDef.op = new optype;
67 uKeyDef.op->sendstr = 0;
68 uKeyDef.op->the_op = t.uKeyDef.op->the_op;
69 } else {
70 uKeyDef.szKeyDef = new char[strlen(t.uKeyDef.szKeyDef)+1];
71 strcpy(uKeyDef.szKeyDef, t.uKeyDef.szKeyDef);
72 }
73 dwState = t.dwState;
74 vk_code = t.vk_code;
75 }
76
77 TKeyDef::~TKeyDef() {
78 if(uKeyDef.szKeyDef) delete[] uKeyDef.szKeyDef;
79 }
80
81 char * TKeyDef::operator=(char *def) {
82 if(def != NULL && *def != 0) {
83 if(uKeyDef.szKeyDef) delete[] uKeyDef.szKeyDef;
84 uKeyDef.szKeyDef = new char[strlen(def)+1];
85 strcpy(uKeyDef.szKeyDef, def);
86 }
87 return uKeyDef.szKeyDef;
88 }
89
90 DWORD TKeyDef::operator=(DWORD code) {
91 return vk_code = code;
92 }
93
94 TKeyDef& TKeyDef::operator=(const TKeyDef &t) {
95 if(t.uKeyDef.szKeyDef) {
96 if(uKeyDef.szKeyDef) delete[] uKeyDef.szKeyDef;
97 if(t.uKeyDef.op->sendstr) {
98 uKeyDef.szKeyDef = new char[strlen(t.uKeyDef.szKeyDef)+1];
99 strcpy(uKeyDef.szKeyDef, t.uKeyDef.szKeyDef);
100 } else {
101 uKeyDef.op = new optype;
102 uKeyDef.op->sendstr = 0;
103 uKeyDef.op->the_op = t.uKeyDef.op->the_op;
104 }
105 } else {
106 uKeyDef.szKeyDef = (char *)NULL;
107 }
108 dwState = t.dwState;
109 vk_code = t.vk_code;
110 return *this;
111 }
112
113 const optype& TKeyDef::operator=(optype op) {
114 uKeyDef.op = new optype;
115 uKeyDef.op->sendstr = 0;
116 uKeyDef.op->the_op = op.the_op;
117 return *uKeyDef.op;
118 }
119
120 // STL requires that operators be friends rather than member functions
121 // (Paul Brannan 5/25/98)
122 #ifndef __BORLANDC__
123 bool operator==(const TKeyDef & t1, const TKeyDef & t2) {
124 return ((t1.vk_code == t2.vk_code) && (t1.dwState == t2.dwState));
125 }
126 // We need this function for compatibility with STL (Paul Brannan 5/25/98)
127 bool operator< (const TKeyDef& t1, const TKeyDef& t2) {
128 if (t1.vk_code == t2.vk_code) return t1.dwState < t2.dwState;
129 return t1.vk_code < t2.vk_code;
130 }
131 #else
132 int TKeyDef::operator==(TKeyDef & t) {
133 return ((vk_code == t.vk_code) && (dwState == t.dwState));
134 }
135 #endif
136