-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.cpp
More file actions
256 lines (220 loc) · 5.25 KB
/
config.cpp
File metadata and controls
256 lines (220 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
===========================================================================
SSHConnector
Copyright (C) 2024 spalter
This file is part of sshconnector, distributed under the GNU GPL v2
For full terms see the included COPYING file.
===========================================================================
*/
#include "config.h"
#include "sshconnector.h"
/***********************************************************************
main
***********************************************************************/
/*
=====================
Config::Config
=====================
*/
Config::Config()
{
}
/*
=====================
Config::~Config
=====================
*/
Config::~Config()
{
SSHConnector::Log( ( char * ) "Destroy configuration" );
// Todo: Clean up hosts properly
}
/*
=====================
Config::Initialize
=====================
*/
void Config::Initialize( char *file )
{
SSHConnector::Log( ( char * ) "Initialize host configuration" );
filename = file;
ReadConfig();
}
/*
=====================
Config::AppendList
=====================
*/
void Config::AppendList( char *file )
{
SSHConnector::Log( ( char * ) "Append host configuration" );
filename = file;
ReadConfig();
}
/*
=====================
Config::GetHosts
=====================
*/
vector<t_host> Config::GetHosts()
{
return hosts;
}
/*
=====================
Config::ReadConfig
=====================
*/
void Config::ReadConfig()
{
SSHConnector::Log( ( char * ) "Read configuration" );
ifstream stream( filename );
char line[255];
if ( stream )
{
while ( stream )
{
stream.getline( line, 255 );
if ( line[0] != '#' && strlen( line ) > 0 )
{
SplitLine( line );
}
}
stream.close();
}
else
{
SSHConnector::Log( ( char * ) "File not found!" );
}
}
/*
=====================
Config::SplitLine
=====================
*/
void Config::SplitLine( char *line )
{
const char delimeter = ';';
char *check = ( char * ) memchr( line, delimeter, strlen( line ) );
if ( check != NULL )
{
string string_line;
string_line.assign( line );
vector<string> result = Split( string_line, delimeter );
if ( result.size() == 4 )
{
t_host host = { result[0], result[1], result[2], result[3] };
hosts.push_back( host );
}
}
}
/*
=====================
Config::Split
=====================
*/
vector<string> &Config::Split( const string &str, char delimeter, vector<string> &items )
{
stringstream str_stream( str );
string item;
while ( getline( str_stream, item, delimeter ) )
{
items.push_back( item );
}
return items;
}
/*
=====================
Config::Split
=====================
*/
vector<string> Config::Split( const std::string &str, char delimeter )
{
vector<string> items;
return Split( str, delimeter, items );
}
/*
=====================
Config::CopyToVectorList
=====================
*/
void Config::CopyToVectorList( vector<char *> &result )
{
auto list = GetHosts();
unsigned int i;
for ( i = 0; i < list.size(); i++ )
{
char *item = ( char * ) "";
BuildHostLine( item, list[i].name, list[i].host, list[i].port, list[i].user );
result.push_back( item );
}
}
/*
=====================
Config::CopyToCharArray
=====================
*/
int Config::CopyToCharArray( char **&result )
{
auto list = GetHosts();
result = new char *[list.size()];
unsigned int i;
for ( i = 0; i < list.size(); i++ )
{
result[i] = new char[255];
char *item = new char[255];
BuildHostLine( item, list[i].name, list[i].host, list[i].port, list[i].user );
strncpy( result[i], item, strlen( item ) + 1 );
}
return i;
}
/*
=====================
Config::ToCharArray
=====================
*/
char *Config::ToCharArray( string &value )
{
char *out = new char[value.length() + 1];
strncpy( out, value.c_str(), value.length() + 1 );
return out;
}
/*
=====================
Config::GetSshCommandById
=====================
*/
void Config::GetSshCommandById( char *cmd, int index )
{
char *item_host = new char[hosts[index].host.length() + 1];
strncpy( item_host, hosts[index].host.c_str(), hosts[index].host.length() + 1 );
char *item_port = new char[hosts[index].port.length() + 1];
strncpy( item_port, hosts[index].port.c_str(), hosts[index].port.length() + 1 );
char *item_user = new char[hosts[index].user.length() + 1];
strncpy( item_user, hosts[index].user.c_str(), hosts[index].user.length() + 1 );
strcpy( cmd, "ssh " );
strcat( cmd, item_host );
strcat( cmd, " -p " );
strcat( cmd, item_port );
strcat( cmd, " -l " );
strcat( cmd, item_user );
}
/*
=====================
Config::BuildHostLine
=====================
*/
void Config::BuildHostLine( char *cmd, string &name, string &host, string &port, string &user )
{
auto cName = ToCharArray( name );
auto cHost = ToCharArray( host );
auto cPort = ToCharArray( port );
auto cUser = ToCharArray( user );
strcpy( cmd, cName );
strcat( cmd, " -> " );
strcat( cmd, cUser );
strcat( cmd, "@" );
strcat( cmd, cHost );
strcat( cmd, ":" );
strcat( cmd, cPort );
}