ONE - On-device Neural Engine
Loading...
Searching...
No Matches
crew Namespace Reference

Data Structures

class  JsonExport
 
struct  Part
 
struct  PConfig
 
struct  Section
 

Typedefs

using Parts = std::vector< Part >
 
using Source = Part
 
using KeyValues = std::unordered_map< std::string, std::string >
 
using Sections = std::vector< Section >
 

Functions

bool read_ini (const std::string &path, PConfig &config)
 Read config as ini file, return false if failed.
 
bool write_ini (std::ostream &os, const PConfig &config)
 Write config as ini file, return false if failed.
 
bool write_json (std::ostream &os, const PConfig &config)
 Write config as json file, return false if failed.
 
Sections read_ini (const char *data, size_t length)
 Reads Config INI from null terminated string and return Sections.
 
Sections read_ini (const std::string &path)
 Reads Config INI from file and return Sections.
 
void write_ini (std::ostream &os, const Sections &sections)
 Write Config INI with Sections to ostream.
 
void write_ini (const std::string &path, const Sections &sections)
 Write Config INI with Sections to file, throw if failed.
 
Section find (const Sections &sections, const std::string &name)
 Find a section with name, empty section if not found.
 
std::string find (const Section &section, const std::string &key)
 Find a key-value pair from key and return value, empty string if not found.
 
void dump (std::ostream &os, const Sections &sections)
 Dump content of sections.
 

Typedef Documentation

◆ KeyValues

using crew::KeyValues = typedef std::unordered_map<std::string, std::string>

Definition at line 28 of file PConfigIni.h.

◆ Parts

using crew::Parts = typedef std::vector<Part>

Definition at line 34 of file PConfig.h.

◆ Sections

using crew::Sections = typedef std::vector<Section>

Definition at line 36 of file PConfigIni.h.

◆ Source

using crew::Source = typedef Part

Definition at line 35 of file PConfig.h.

Function Documentation

◆ dump()

void crew::dump ( std::ostream &  os,
const Sections sections 
)

Dump content of sections.

Definition at line 25 of file PConfigIniDump.cpp.

26{
27 for (auto &section : sections)
28 {
29 os << "[" << section.name << "]" << std::endl;
30 for (auto &item : section.items)
31 {
32 os << item.first << "=" << item.second << std::endl;
33 }
34 os << std::endl;
35 }
36}

Referenced by operator<<().

◆ find() [1/2]

std::string crew::find ( const Section section,
const std::string &  key 
)

Find a key-value pair from key and return value, empty string if not found.

Definition at line 215 of file PConfigIni.cpp.

216{
217 for (auto &item : section.items)
218 {
219 if (item.first == key)
220 return item.second;
221 }
222 return "";
223}

References crew::Section::items.

◆ find() [2/2]

Section crew::find ( const Sections sections,
const std::string &  name 
)

Find a section with name, empty section if not found.

Definition at line 204 of file PConfigIni.cpp.

205{
206 for (auto &section : sections)
207 {
208 if (section.name == name)
209 return section;
210 }
211 Section not_found;
212 return not_found;
213}

Referenced by read_ini().

◆ read_ini() [1/3]

Sections crew::read_ini ( const char *  data,
size_t  length 
)

Reads Config INI from null terminated string and return Sections.

Definition at line 59 of file PConfigIni.cpp.

60{
61 assert(data != nullptr);
62 assert(length > 0);
63
64 auto buffer = std::vector<char>();
65 buffer.reserve(length + 1);
66 char *pbuffer = buffer.data();
67 memcpy(pbuffer, data, length);
68 // add null at end to be sure
69 *(pbuffer + length) = 0;
70
71 Sections sections;
72 Section section;
73
74 std::string string_line;
75
76 const char *delim = "\r\n";
77 const char *one_line = std::strtok(pbuffer, delim);
78 while (one_line != nullptr)
79 {
80 if (*one_line == '[')
81 {
82 if (!section.name.empty())
83 {
84 sections.push_back(section);
85 }
86 section.name.clear();
87 section.items.clear();
88
89 string_line = one_line + 1;
90 auto pos = string_line.find(']');
91 assert(pos != std::string::npos);
92 if (pos != std::string::npos)
93 {
94 section.name = string_line.substr(0, pos);
95 }
96 }
97 else if (*one_line == '#' || *one_line == ';')
98 {
99 // Comment line, do nothing
100 }
101 else if (*one_line) // string legnth is not 0
102 {
103 if (section.name.empty())
104 throw std::runtime_error("Invalid INI file");
105
106 string_line = one_line;
107 auto pos = string_line.find('=');
108 assert(pos != std::string::npos);
109 if (pos != std::string::npos)
110 {
111 auto key = string_line.substr(0, pos);
112 auto val = string_line.substr(pos + 1);
113 key = filter_escape(key);
114 section.items.emplace(key, val);
115 }
116 }
117
118 one_line = std::strtok(nullptr, delim);
119 }
120 if (!section.name.empty())
121 {
122 sections.push_back(section);
123 }
124
125 return sections;
126}
std::vector< Section > Sections
Definition PConfigIni.h:36
std::string name
Definition PConfigIni.h:32
KeyValues items
Definition PConfigIni.h:33

References crew::Section::items, and crew::Section::name.

◆ read_ini() [2/3]

Sections crew::read_ini ( const std::string &  path)

Reads Config INI from file and return Sections.

Definition at line 128 of file PConfigIni.cpp.

129{
130 foder::FileLoader file_loader{path};
131 // load will throw if error while opening
132 auto ini_data = file_loader.load();
133
134 return read_ini(ini_data.data(), ini_data.size());
135}
DataBuffer load(void) const
Definition FileLoader.h:39

References foder::FileLoader::load(), and read_ini().

◆ read_ini() [3/3]

bool crew::read_ini ( const std::string &  path,
PConfig config 
)

Read config as ini file, return false if failed.

Definition at line 121 of file PConfig.cpp.

122{
123 auto sections = crew::read_ini(path);
124
125 auto section_source = crew::find(sections, "source");
126 auto section_models = crew::find(sections, "models");
127 if (section_source.name != "source" || section_models.name != "models")
128 {
129 return false;
130 }
131
132 if (!read_part(section_source, pconfig.source))
133 {
134 return false;
135 }
136
137 // get models list
138 std::vector<std::string> models;
139 for (int32_t i = 1;; ++i)
140 {
141 std::string item = "m" + std::to_string(i);
142 std::string model = crew::find(section_models, item);
143 if (model.empty())
144 break;
145
146 models.push_back(model);
147 }
148
149 for (auto &model : models)
150 {
151 auto section_model = crew::find(sections, model);
152
153 Part part;
154 if (!read_part(section_model, part))
155 {
156 return false;
157 }
158 pconfig.parts.push_back(part);
159 }
160
161 return true;
162}
bool read_ini(const std::string &path, PConfig &config)
Read config as ini file, return false if failed.
Definition PConfig.cpp:121
Section find(const Sections &sections, const std::string &name)
Find a section with name, empty section if not found.

References find(), crew::PConfig::parts, read_ini(), and crew::PConfig::source.

Referenced by prunner::PModelsRunner::load_config(), partee::read(), read_ini(), and read_ini().

◆ write_ini() [1/3]

void crew::write_ini ( const std::string &  path,
const Sections sections 
)

Write Config INI with Sections to file, throw if failed.

Definition at line 190 of file PConfigIni.cpp.

191{
192 std::ofstream fs(filepath.c_str(), std::ofstream::binary | std::ofstream::trunc);
193 if (not fs.good())
194 {
195 std::string msg = "Failed to create file: " + filepath;
196 throw std::runtime_error(msg);
197 }
198
199 write_ini(fs, sections);
200
201 fs.close();
202}

References write_ini().

◆ write_ini() [2/3]

bool crew::write_ini ( std::ostream &  os,
const PConfig config 
)

Write config as ini file, return false if failed.

Definition at line 164 of file PConfig.cpp.

165{
166 crew::Sections sections;
167
168 // make [source]
169 crew::Section section_source;
170 section_source.name = "source";
171 section_source.items["file"] = pconfig.source.model_file;
172 part_to_section_io(pconfig.source, section_source);
173 sections.push_back(section_source);
174
175 // make [models]
176 crew::Section section_models;
177 section_models.name = "models";
178 uint32_t idx = 1;
179 for (auto &part : pconfig.parts)
180 {
181 std::string key = "m" + std::to_string(idx);
182 section_models.items[key] = part.model_file;
183 idx++;
184 }
185 sections.push_back(section_models);
186
187 for (auto &part : pconfig.parts)
188 {
189 // make circle model section
190 crew::Section section_model;
191 section_model.name = part.model_file;
192 section_model.items["file"] = part.model_file;
193 part_to_section_io(part, section_model);
194 sections.push_back(section_model);
195 }
196
197 write_ini(os, sections);
198
199 return true;
200}
bool write_ini(std::ostream &os, const PConfig &config)
Write config as ini file, return false if failed.
Definition PConfig.cpp:164

References crew::Section::items, crew::Part::model_file, crew::Section::name, crew::PConfig::parts, crew::PConfig::source, and write_ini().

Referenced by write_ini(), and write_ini().

◆ write_ini() [3/3]

void crew::write_ini ( std::ostream &  os,
const Sections sections 
)

Write Config INI with Sections to ostream.

Definition at line 177 of file PConfigIni.cpp.

178{
179 std::stringstream ss;
180
181 auto processed = insert_escape(sections);
182
183 ss << processed;
184
185 std::string strss = ss.str();
186
187 os.write(strss.c_str(), strss.length());
188}

◆ write_json()

bool crew::write_json ( std::ostream &  os,
const PConfig config 
)

Write config as json file, return false if failed.

Definition at line 202 of file PConfig.cpp.

203{
204 crew::JsonExport je(os);
205
206 je.open_brace();
207 {
208 je.open_brace("source");
209 write_part(je, pconfig.source);
210 je.close_brace(true);
211 }
212 {
213 je.open_bracket("parts");
214 write_parts(je, pconfig.parts);
215 je.close_bracket(false);
216 }
217 je.close_brace(false);
218
219 return true;
220}

References crew::JsonExport::close_brace(), crew::JsonExport::close_bracket(), crew::JsonExport::open_brace(), crew::JsonExport::open_bracket(), crew::PConfig::parts, and crew::PConfig::source.