zohann commited on
Commit
9789ea9
·
verified ·
1 Parent(s): 21519cf

Upload Audio_Effects_SDK/samples/utils/ConfigReader.cpp with huggingface_hub

Browse files
Audio_Effects_SDK/samples/utils/ConfigReader.cpp ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
3
+
4
+ NVIDIA CORPORATION and its licensors retain all intellectual property
5
+ and proprietary rights in and to this software, related documentation
6
+ and any modifications thereto. Any use, reproduction, disclosure or
7
+ distribution of this software and related documentation without an express
8
+ license agreement from NVIDIA CORPORATION is strictly prohibited.
9
+ */
10
+
11
+ #include "ConfigReader.hpp"
12
+
13
+ #include <fstream>
14
+ #include <iostream>
15
+ #include <sstream>
16
+
17
+ bool ConfigReader::Load(const std::string & config_filename)
18
+ {
19
+ std::ifstream configFile;
20
+ configFile.open(config_filename, std::ios_base::in);
21
+ int line_number = 0;
22
+ if (configFile.is_open()) {
23
+ // read stream line by line
24
+ for (std::string line; std::getline(configFile, line);) {
25
+ line_number++;
26
+
27
+ // Strip whitespace
28
+ std::size_t start = line.find_first_not_of(" \n\r\t");
29
+ if (start == std::string::npos) {
30
+ continue;
31
+ }
32
+ line = line.substr(start);
33
+ // Ignore comments
34
+ if (line[0] == '#') {
35
+ continue;
36
+ }
37
+
38
+ // Get the name
39
+ std::size_t end = line.find_first_of(" \t");
40
+ if (end == std::string::npos) {
41
+ std::cerr << "Invalid config file at line " << line_number << std::endl;
42
+ return false;
43
+ }
44
+ std::string name = line.substr(start, end - start);
45
+ line = line.substr(end);
46
+
47
+ // Strip whitespace between name value pair
48
+ start = line.find_first_not_of(" \t");
49
+ if (start == std::string::npos) {
50
+ std::cerr << "Invalid config file at line " << line_number << std::endl;
51
+ return false;
52
+ }
53
+ std::string value = line.substr(start);
54
+ #ifdef __linux__
55
+ // Remove trailing \r s
56
+ if (value.size() && value[value.size() -1] == '\r')
57
+ {
58
+ value.pop_back();
59
+ }
60
+ #endif
61
+ config_dict_.insert({ name, value });
62
+ }
63
+ configFile.close();
64
+ }
65
+ else {
66
+ std::cerr << "Unable to open file: " << config_filename << std::endl;
67
+ return false;
68
+ }
69
+
70
+ loaded_ = true;
71
+ return true;
72
+ }
73
+
74
+ bool ConfigReader::IsConfigValueAvailable(const std::string& name) const {
75
+ if (!loaded_) {
76
+ std::cout << "Not loaded" << std::endl;
77
+ return false;
78
+ }
79
+
80
+ return config_dict_.find(name) != config_dict_.end();
81
+ }
82
+
83
+ bool ConfigReader::GetConfigValue(const std::string& name, std::string* value) const {
84
+ if (!loaded_) {
85
+ std::cout << "Not loaded" << std::endl;
86
+ return false;
87
+ }
88
+
89
+ auto iter = config_dict_.find(name);
90
+ if (iter == config_dict_.end()) {
91
+ std::cerr << "Unable to find \"" << name << " effect\" configuration" << std::endl;
92
+ return false;
93
+ }
94
+
95
+ *value = iter->second;
96
+ return true;
97
+ }
98
+
99
+ std::string ConfigReader::GetConfigValue(const std::string & name) const
100
+ {
101
+ std::string value;
102
+ if (GetConfigValue(name, &value) == false) {
103
+ std::cerr << "Config value not found" << std::endl;
104
+ return value;
105
+ }
106
+
107
+ return value;
108
+ }
109
+
110
+ std::vector<std::string> ConfigReader::GetConfigValueList(const std::string & name) const
111
+ {
112
+ std::string value_list;
113
+ if (GetConfigValue(name, &value_list) == false) {
114
+ std::cerr << "Config value list not found" << std::endl;
115
+ return std::vector<std::string>();
116
+ }
117
+
118
+ std::vector<std::string> list;
119
+ // Split string around spaces.
120
+ std::istringstream ss(value_list);
121
+ do {
122
+ std::string value;
123
+ ss >> value;
124
+ if (!value.empty())
125
+ list.push_back(value);
126
+ } while (ss);
127
+
128
+ return list;
129
+ }
130
+