Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Geithner, Thomas
desk-control
Commits
d0853790
Commit
d0853790
authored
Oct 11, 2017
by
Geithner, Thomas
Browse files
added config class
parent
0d735a37
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/Makefile
View file @
d0853790
...
...
@@ -18,10 +18,12 @@ OBJS=\
cmd-frontend.o
\
coap-frontend.o
\
avahi-service.o
\
config.o
\
TARGETS
=
\
desk-main
\
avahi-test
\
config-test
\
#avahi-test
\
all
:
$(TARGETS)
...
...
src/config-test.cpp
0 → 100644
View file @
d0853790
/*
* config-test.cpp
*
* Created on: 11.10.2017
* Author: geith
*/
#include "config.h"
#include <iostream>
#include <unistd.h>
using
namespace
std
;
using
namespace
desk
;
int
main
(
int
argc
,
char
**
argv
){
Config
conf
(
"test.conf"
);
try
{
cout
<<
"key_1 -> "
<<
conf
.
get
(
"key_1"
)
<<
endl
;
}
catch
(
ConfigException
&
e
){
cout
<<
e
.
what
()
<<
endl
;
}
cout
<<
"key_2 -> "
<<
conf
.
get
(
"key_2"
,
"default_value"
)
<<
endl
;
cout
<<
"key_3 -> "
<<
conf
.
get
(
"key_3"
,
"default_value"
)
<<
endl
;
cout
<<
"key_4 -> "
<<
conf
.
get
(
"key_4"
,
"default_value"
)
<<
endl
;
conf
.
set
(
"key_4"
,
"val_4"
);
return
0
;
}
src/config.cpp
0 → 100644
View file @
d0853790
/*
* config.cpp
*
* Created on: 11.10.2017
* Author: geith
*/
#include "config.h"
#include <fstream>
#include <iostream>
#include <sstream>
using
namespace
std
;
using
namespace
desk
;
Config
::
Config
(
const
string
file
)
:
m_configFile
(
file
)
{
readFile
();
}
void
Config
::
readFile
()
{
ifstream
in
(
m_configFile
);
if
(
in
.
fail
()){
// could not open config file
return
;
}
string
line
;
while
(
!
in
.
eof
()
&&
!
in
.
fail
()){
getline
(
in
,
line
);
cout
<<
__func__
<<
": got line
\"
"
<<
line
<<
"
\"
"
<<
endl
;
if
(
line
.
empty
()){
continue
;
}
istringstream
l_in
(
line
);
string
key
;
string
value
;
l_in
>>
key
>>
value
;
cout
<<
__func__
<<
": key="
<<
key
<<
", value="
<<
value
<<
endl
;
m_config
[
key
]
=
value
;
}
in
.
close
();
}
void
Config
::
writeFile
()
throw
(
ConfigException
)
{
ofstream
out
(
m_configFile
);
if
(
out
.
fail
()){
// could not open config file
throw
ConfigException
(
"could not write file"
);
}
for
(
auto
c
:
m_config
){
out
<<
c
.
first
<<
" "
<<
c
.
second
<<
endl
;
}
out
.
close
();
}
std
::
string
Config
::
get
(
const
std
::
string
&
key
)
throw
(
ConfigException
)
{
try
{
return
m_config
.
at
(
key
);
}
catch
(
out_of_range
&
e
){
throw
ConfigException
(
"key
\"
"
+
key
+
"
\"
not found"
);
}
}
std
::
string
Config
::
get
(
const
std
::
string
&
key
,
const
std
::
string
&
default_value
)
{
try
{
return
m_config
.
at
(
key
);
}
catch
(
out_of_range
&
e
){
return
default_value
;
}
}
void
Config
::
set
(
const
std
::
string
&
key
,
const
std
::
string
&
value
)
{
m_config
[
key
]
=
value
;
writeFile
();
}
src/config.h
0 → 100644
View file @
d0853790
/*
* config.h
*
* Created on: 11.10.2017
* Author: geith
*/
#ifndef CONFIG_H_
#define CONFIG_H_
#include <string>
#include <map>
namespace
desk
{
class
ConfigException
:
public
std
::
exception
{
public:
ConfigException
(
std
::
string
msg
)
_GLIBCXX_USE_NOEXCEPT
:
m_msg
(
msg
){
}
virtual
const
char
*
what
()
const
_GLIBCXX_USE_NOEXCEPT
{
return
m_msg
.
c_str
();
}
protected:
std
::
string
m_msg
;
};
class
Config
{
public:
Config
(
const
std
::
string
file
);
std
::
string
get
(
const
std
::
string
&
key
)
throw
(
ConfigException
);
std
::
string
get
(
const
std
::
string
&
key
,
const
std
::
string
&
default_value
);
void
set
(
const
std
::
string
&
key
,
const
std
::
string
&
value
);
protected:
void
readFile
();
void
writeFile
()
throw
(
ConfigException
);
const
std
::
string
m_configFile
;
std
::
map
<
std
::
string
,
std
::
string
>
m_config
;
};
}
#endif
/* CONFIG_H_ */
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment