23 "Bad command line: \"" << what <<
'\"',
30 "Failed to parse query \"" << query <<
"\" in class \"" << class_name <<
'\"',
32 ((
const char*)class_name)
38 "Cannot find class \"" << class_name <<
'\"',
39 ((
const char*)class_name)
47 "Caught " << what <<
" exception: \'" << text <<
'\'',
56void printUsage(
const char *appName, std::ostream& s)
59 "Usage: " << appName <<
"\n"
60 " [--class name-of-class [--query query [--print-references recursion-depth [class-name*] [--]]]\n"
63 " [--out-data-file data_file] [--out-data-file schema_file] inputfiles*\n"
66 " -o | --out-data-file datafilename the out filename to store oks objects from input files\n"
67 " -s | --out-schema-file schemafilename the out filename to store oks classes from input files\n"
68 " -c | --class class_name dump given class (all objects or matching some query)\n"
69 " -q | --query query print objects matching query (can only be used with class)\n"
70 " -r | --print-references N C1 C2 ... CX print objects referenced by found objects (can only be used with query), where:\n"
71 " * the parameter N defines recursion depth for referenced objects (> 0)\n"
72 " * the optional set of names {C1 .. CX} defines [sub-]classes for above objects\n"
73 " -v | --version print version\n"
74 " -h | --help print this text\n"
77 " Merges several oks files into single schema and data files.\n"
81enum __OksMergeExitStatus__ {
83 __NoDataFilesLoaded__,
84 __NoSchemaFilesLoaded__,
86 __NoOutputSchemaFile__,
96 Oks::error_msg(
"oks_merge") <<
"no parameter(s) for command line argument \'" <<
s <<
"\' provided\n\n";
101main(
int argc,
char *argv[])
103 appTitle =
"OKS merge. OKS kernel version ";
106 const char * appName =
"oks_merge";
108 std::string out_data_file;
109 std::string out_schema_file;
111 const char * class_name =
nullptr;
112 const char * query =
nullptr;
113 long recursion_depth = 0;
114 std::vector<std::string> ref_classes;
127 for(
int i = 1; i < argc; i++) {
128 const char * cp = argv[i];
130 if(!strcmp(cp,
"-h") || !strcmp(cp,
"--help")) {
134 else if(!strcmp(cp,
"-v") || !strcmp(cp,
"--version")) {
138 else if(!strcmp(cp,
"-o") || !strcmp(cp,
"--out-data-file")) {
139 if(++i == argc) {
no_param(cp); }
else { out_data_file = argv[i]; }
141 else if(!strcmp(cp,
"-s") || !strcmp(cp,
"--out-schema-file")) {
142 if(++i == argc) {
no_param(cp); }
else { out_schema_file = argv[i]; }
144 else if(!strcmp(cp,
"-c") || !strcmp(cp,
"--class")) {
145 if(++i == argc) {
no_param(cp); }
else { class_name = argv[i]; }
147 else if(!strcmp(cp,
"-q") || !strcmp(cp,
"--query")) {
148 if(++i == argc) {
no_param(cp); }
else { query = argv[i]; }
150 else if(!strcmp(cp,
"-r") || !strcmp(cp,
"--print-references")) {
151 if(++i == argc) {
no_param(cp); }
else { recursion_depth = atol(argv[i]); }
153 for(; j < argc - i - 1; ++j) {
154 if(argv[i+1+j][0] !=
'-') { ref_classes.push_back(argv[i+1+j]); }
else {
break; }
158 else if(strcmp(cp,
"--")) {
163 if(!out_data_file.empty() && kernel.
data_files().empty()) {
165 return __NoDataFilesLoaded__;
168 if(!out_schema_file.empty() && kernel.
schema_files().empty()) {
170 return __NoSchemaFilesLoaded__;
173 if(query && !class_name) {
174 ers::fatal(oks_merge::BadCommandLine(
ERS_HERE,
"Query can only be executed when class name is provided (use -c option)"));
182 if(!out_data_file.empty()) {
183 data_file_h = kernel.
new_data(out_data_file);
187 if(!out_schema_file.empty()) {
188 schema_file_h = kernel.
new_schema(out_schema_file);
192 if(!data_file_h && !schema_file_h) {
193 ers::fatal(oks_merge::BadCommandLine(
ERS_HERE,
"There is no out schema file name defined"));
194 return __NoOutputSchemaFile__;
198 for(OksClass::Map::const_iterator j = kernel.
classes().begin(); j != kernel.
classes().end(); ++j) {
199 j->second->set_file(schema_file_h,
false);
210 if(class_name && *class_name) {
212 if(query && *query) {
216 size_t num = (objs ? objs->size() : 0);
217 std::cout <<
"Found " << num <<
" matching query \"" << query <<
"\" in class \"" << class_name <<
"\"";
220 std::cout <<
':' << std::endl;
221 while(!objs->empty()) {
224 if(recursion_depth > 0) {
227 o->
references(refs, recursion_depth,
true, &all_ref_classes);
228 std::cout <<
" - " << o <<
" references " << refs.size() <<
" objects" << std::endl;
235 for(OksObject::FSet::iterator i = refs.begin(); i != refs.end(); ++i) {
240 std::cout << std::endl;
250 std::cout << *
c << std::endl;
259 for(OksObject::Set::const_iterator j = kernel.
objects().begin(); j != kernel.
objects().end(); ++j) {
260 (*j)->set_file(data_file_h,
false);
274 catch (std::exception & ex) {
#define ERS_DECLARE_ISSUE(namespace_name, class_name, message, attributes)
Provides interface to the OKS XML schema and data files.
void add_include_file(const std::string &name)
Add include file.
Provides interface to the OKS kernel.
OksFile * load_file(const std::string &name, bool bind=true)
Load OKS database file.
const OksFile::Map & data_files() const
Get all data files.
OksFile * new_data(const std::string &name, const std::string &logical_name="", const std::string &type="")
Create OKS data file.
void set_active_schema(OksFile *file_h)
Set active OKS schema file.
const OksObject::Set & objects() const
Get objects.
static void set_use_strict_repository_paths(bool flag)
Set flag to use strict-repository-paths check.
const OksClass::Map & classes() const
Get classes.
void save_schema(OksFile *file_h, bool force=false, OksFile *true_file_h=0)
Save OKS schema file.
void save_data(OksFile *file_h, bool ignore_bad_objects=false, OksFile *true_file_h=nullptr, bool force_defaults=false)
Save OKS data file.
OksFile * new_schema(const std::string &name)
Create OKS schema file.
static const char * GetVersion()
Get OKS version. The method returns string containing CVS tag and date of OKS build.
OksClass * find_class(const std::string &class_name) const
Find class by name (C++ string).
void set_active_data(OksFile *file_h)
Set active OKS data file.
const OksFile::Map & schema_files() const
Get all schema files.
void get_all_classes(const std::vector< std::string > &names_in, ClassSet &classes_out) const
The method searches all classes including subclasses for given names.
OksObject describes instance of OksClass.
std::list< OksObject * > List
std::unordered_set< OksObject *, oks::hash_obj_ptr, oks::equal_obj_ptr > FSet
void references(OksObject::FSet &refs, unsigned long recursion_depth, bool add_self=false, oks::ClassSet *classes=0) const
void set_file(OksFile *file, bool update_owner=true)
Move object to different file.
static std::ostream & error_msg(const char *)
virtual const char * what() const noexcept
int main(int argc, char **argv)
std::unordered_set< const OksClass *, oks::hash_class_ptr, oks::equal_class_ptr > ClassSet
void fatal(const Issue &issue)
static void printUsage(std::ostream &s)
static void no_param(const char *s)