Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Clark Lin
StudyCpp
Commits
f469e4fe
Commit
f469e4fe
authored
Dec 02, 2022
by
Administrator
Browse files
add setup loading from file
parent
48df3400
Changes
7
Hide whitespace changes
Inline
Side-by-side
.gitignore
View file @
f469e4fe
...
@@ -10,5 +10,8 @@
...
@@ -10,5 +10,8 @@
# Include all h file under all folder
# Include all h file under all folder
!**/*.h
!**/*.h
# Include all configure file under all folder
!**/*.conf
# Include .gitignore itself
# Include .gitignore itself
!.gitignore
!.gitignore
04.HandsOn/common/Constant.cpp
0 → 100644
View file @
f469e4fe
#include <iostream>
#include <fstream>
#include <map>
#include "Constant.h"
using
namespace
std
;
map
<
string
,
string
>
loadSetup
(
string
setupFileName
)
{
// 定义文件流,用于打开文件
fstream
fs
;
fs
.
open
(
setupFileName
,
ios
::
in
);
// 定义变量,用于存放行
string
line
;
// 定义map
map
<
string
,
string
>
mapSetup
;
// 逐行读取
if
(
!
fs
)
{
cout
<<
"Cannot open file.
\n
"
;
return
mapSetup
;
}
else
{
// 逐行读取
while
(
getline
(
fs
,
line
))
{
// 移除空格
line
.
erase
(
std
::
remove
(
line
.
begin
(),
line
.
end
(),
' '
),
line
.
end
());
// 是否以#开头
if
(
line
[
0
]
==
'#'
)
{
continue
;
}
// 定义分隔符
char
delimiter
=
'='
;
// 取分隔符的位置
size_t
pos
=
line
.
find
(
delimiter
);
// 如果分隔符找不到
if
(
pos
==
string
::
npos
)
{
cout
<<
"Not found!"
<<
endl
;
return
mapSetup
;
}
// 取key
string
key
=
line
.
substr
(
0
,
pos
);
// 取value
string
value
=
line
.
substr
(
pos
+
1
);
mapSetup
.
insert
(
std
::
pair
<
string
,
string
>
(
key
,
value
));
}
}
return
mapSetup
;
};
\ No newline at end of file
04.HandsOn/common/Constant.h
0 → 100644
View file @
f469e4fe
#pragma once // 防止头文件被重复包含
#include <iostream>
#include <map>
using
namespace
std
;
static
string
SETUP_FILE_NAME
=
"common/Setup.conf"
;
static
string
RESOLUTION_WIDTH
=
"resolution_width"
;
static
string
RESOLUTION_HEIGHT
=
"resolution_height"
;
map
<
string
,
string
>
loadSetup
(
string
setupFileName
);
\ No newline at end of file
04.HandsOn/common/Setup.conf
0 → 100644
View file @
f469e4fe
#This is setup file
resolution_width
=
1920
resolution_height
=
1080
mode
=
1
multiuser_allowed
=
false
\ No newline at end of file
04.HandsOn/common/Setup.cpp
View file @
f469e4fe
#include <iostream>
#include <iostream>
#include "Setup.h"
#include "Setup.h"
#include "Constant.h"
using
namespace
std
;
using
namespace
std
;
map
<
string
,
string
>
setup
();
// 构造函数
Setup
::
Setup
(
string
setupFileName
)
{
this
->
mapSetup
=
loadSetup
(
setupFileName
);
this
->
setResolution
();
};
// 打印所有设定项
void
Setup
::
printSetup
()
{
cout
<<
"----- Print all setup -----"
<<
endl
;
for
(
auto
&
x
:
this
->
mapSetup
)
{
std
::
cout
<<
"["
<<
x
.
first
<<
"]:["
<<
x
.
second
<<
"]"
<<
endl
;
}
};
// 设定分辨率
// 设定分辨率
void
Setup
::
setResolution
()
{
void
Setup
::
setResolution
()
{
this
->
resolution_width
=
1920
;
// 从设定处取得
this
->
resolution_width
=
stoi
(
mapSetup
.
at
(
RESOLUTION_WIDTH
));
// 从设定处取得
this
->
resolution_height
=
1080
;
// 从设定处取得
this
->
resolution_height
=
stoi
(
mapSetup
.
at
(
RESOLUTION_HEIGHT
))
;
// 从设定处取得
};
};
...
...
04.HandsOn/common/Setup.h
View file @
f469e4fe
#pragma once // 防止头文件被重复包含
#pragma once // 防止头文件被重复包含
#include <iostream>
#include <iostream>
#include <map>
using
namespace
std
;
using
namespace
std
;
...
@@ -13,9 +14,17 @@ class Setup {
...
@@ -13,9 +14,17 @@ class Setup {
int
resolution_width
;
int
resolution_width
;
// 垂直分辨率
// 垂直分辨率
int
resolution_height
;
int
resolution_height
;
// 定义存放设定值的map
map
<
string
,
string
>
mapSetup
;
public:
public:
// 构造函数
Setup
(
string
setupFileName
);
// 打印所有设定项
void
printSetup
();
// 设定分辨率
// 设定分辨率
void
setResolution
();
void
setResolution
();
...
...
04.HandsOn/main.cpp
View file @
f469e4fe
#include <iostream>
#include <iostream>
#include "common/Setup.h"
#include "common/Setup.h"
#include "common/Constant.h"
int
main
()
{
int
main
()
{
Setup
s
;
// 读取文件,装载设定项
s
.
setResolution
();
Setup
s
(
SETUP_FILE_NAME
);
// 打印所有装载的设定项
s
.
printSetup
();
// 打印分辨率设定
cout
<<
"----- Resolution setup -----"
<<
endl
;
cout
<<
"Resolution: "
<<
s
.
getResolution
()
<<
endl
;
cout
<<
"Resolution: "
<<
s
.
getResolution
()
<<
endl
;
}
}
\ No newline at end of file
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