25#include <boost/date_time/posix_time/posix_time_types.hpp>
26#include <boost/date_time/posix_time/time_formatters.hpp>
27#include <boost/date_time/posix_time/time_parsers.hpp>
28#include <boost/interprocess/sync/file_lock.hpp>
51 "<!DOCTYPE oks-schema [\n"
53 " <!ELEMENT oks-schema (info, (include)?, (comments)?, (class)+)>\n"
55 " <!ELEMENT info EMPTY>\n"
57 " name CDATA #IMPLIED\n"
58 " type CDATA #IMPLIED\n"
59 " num-of-items CDATA #REQUIRED\n"
60 " oks-format CDATA #FIXED \"schema\"\n"
61 " oks-version CDATA #REQUIRED\n"
62 " created-by CDATA #IMPLIED\n"
63 " created-on CDATA #IMPLIED\n"
64 " creation-time CDATA #IMPLIED\n"
65 " last-modified-by CDATA #IMPLIED\n"
66 " last-modified-on CDATA #IMPLIED\n"
67 " last-modification-time CDATA #IMPLIED\n"
70 " <!ELEMENT include (file)+>\n"
71 " <!ELEMENT file EMPTY>\n"
73 " path CDATA #REQUIRED\n"
76 " <!ELEMENT comments (comment)+>\n"
77 " <!ELEMENT comment EMPTY>\n"
78 " <!ATTLIST comment\n"
79 " creation-time CDATA #REQUIRED\n"
80 " created-by CDATA #REQUIRED\n"
81 " created-on CDATA #REQUIRED\n"
82 " author CDATA #REQUIRED\n"
83 " text CDATA #REQUIRED\n"
86 " <!ELEMENT class (superclass | attribute | relationship | method)*>\n"
88 " name CDATA #REQUIRED\n"
89 " description CDATA \"\"\n"
90 " is-abstract (yes|no) \"no\"\n"
93 " <!ELEMENT superclass EMPTY>\n"
94 " <!ATTLIST superclass name CDATA #REQUIRED>\n"
96 " <!ELEMENT attribute EMPTY>\n"
97 " <!ATTLIST attribute\n"
98 " name CDATA #REQUIRED\n"
99 " description CDATA \"\"\n"
100 " type (bool|s8|u8|s16|u16|s32|u32|s64|u64|float|double|date|time|string|uid|enum|class) #REQUIRED\n"
101 " range CDATA \"\"\n"
102 " format (dec|hex|oct) \"dec\"\n"
103 " is-multi-value (yes|no) \"no\"\n"
104 " init-value CDATA \"\"\n"
105 " is-not-null (yes|no) \"no\"\n"
106 " ordered (yes|no) \"no\"\n"
109 " <!ELEMENT relationship EMPTY>\n"
110 " <!ATTLIST relationship\n"
111 " name CDATA #REQUIRED\n"
112 " description CDATA \"\"\n"
113 " class-type CDATA #REQUIRED\n"
114 " low-cc (zero|one) #REQUIRED\n"
115 " high-cc (one|many) #REQUIRED\n"
116 " is-composite (yes|no) #REQUIRED\n"
117 " is-exclusive (yes|no) #REQUIRED\n"
118 " is-dependent (yes|no) #REQUIRED\n"
119 " ordered (yes|no) \"no\"\n"
122 " <!ELEMENT method (method-implementation*)>\n"
123 " <!ATTLIST method\n"
124 " name CDATA #REQUIRED\n"
125 " description CDATA \"\"\n"
128 " <!ELEMENT method-implementation EMPTY>\n"
129 " <!ATTLIST method-implementation\n"
130 " language CDATA #REQUIRED\n"
131 " prototype CDATA #REQUIRED\n"
138 "<!DOCTYPE oks-data [\n"
140 " <!ELEMENT oks-data (info, (include)?, (comments)?, (obj)+)>\n"
142 " <!ELEMENT info EMPTY>\n"
144 " name CDATA #IMPLIED\n"
145 " type CDATA #IMPLIED\n"
146 " num-of-items CDATA #REQUIRED\n"
147 " oks-format CDATA #FIXED \"data\"\n"
148 " oks-version CDATA #REQUIRED\n"
149 " created-by CDATA #IMPLIED\n"
150 " created-on CDATA #IMPLIED\n"
151 " creation-time CDATA #IMPLIED\n"
152 " last-modified-by CDATA #IMPLIED\n"
153 " last-modified-on CDATA #IMPLIED\n"
154 " last-modification-time CDATA #IMPLIED\n"
157 " <!ELEMENT include (file)*>\n"
158 " <!ELEMENT file EMPTY>\n"
160 " path CDATA #REQUIRED\n"
163 " <!ELEMENT comments (comment)*>\n"
164 " <!ELEMENT comment EMPTY>\n"
165 " <!ATTLIST comment\n"
166 " creation-time CDATA #REQUIRED\n"
167 " created-by CDATA #REQUIRED\n"
168 " created-on CDATA #REQUIRED\n"
169 " author CDATA #REQUIRED\n"
170 " text CDATA #REQUIRED\n"
173 " <!ELEMENT obj (attr | rel)*>\n"
175 " class CDATA #REQUIRED\n"
176 " id CDATA #REQUIRED\n"
178 " <!ELEMENT attr (data)*>\n"
180 " name CDATA #REQUIRED\n"
181 " type (bool|s8|u8|s16|u16|s32|u32|s64|u64|float|double|date|time|string|uid|enum|class|-) \"-\"\n"
184 " <!ELEMENT data EMPTY>\n"
186 " val CDATA #REQUIRED\n"
188 " <!ELEMENT rel (ref)*>\n"
190 " name CDATA #REQUIRED\n"
191 " class CDATA \"\"\n"
194 " <!ELEMENT ref EMPTY>\n"
196 " class CDATA #REQUIRED\n"
197 " id CDATA #REQUIRED\n"
205 return ( std::string(
"Failed to add include \'") + include_name +
"\' to file \'" + file.get_full_file_name() +
"\' because:\n" + reason);
211 return ( std::string(
"Failed to remove include \'") + include_name +
"\' from file \'" + file.get_full_file_name() +
"\' because:\n" + reason);
217 return ( std::string(
"Failed to rename include of \'") + file.get_full_file_name() +
"\' from \'" + from +
"\' to \'" +
to +
"\' because:\n" + reason);
223 return ( std::string(
"Failed to add new comment to file \'") + file.get_full_file_name() +
"\' because:\n" + reason);
229 return ( std::string(
"Failed to remove comment created at \'") + creation_time +
"\' from file \'" + file.get_full_file_name() +
"\' because:\n" + reason);
235 return ( std::string(
"Failed to change comment created at \'") + creation_time +
"\' from file \'" + file.get_full_file_name() +
"\' because:\n" + reason);
241 return ( std::string(
"Failed to ") + (lock_action ?
"" :
"un") +
"lock file \'" + file.get_full_file_name() +
"\' because:\n" + reason);
247 return ( std::string(
"Failed to ") + action +
" of file \'" + file.get_full_file_name() +
"\' because:\n" + reason);
253 return ( std::string(
"Failed to compare file \'") + src +
"\' with \'" + dest +
"\' because:\n" + reason);
287 for(std::map<std::string, oks::Comment *>::iterator i =
p_comments.begin(); i !=
p_comments.end(); ++i) {
335 for (
const auto& i : f.p_comments)
364 const char * fname =
"OksFile::OksFile()";
377 const char * tag_start = xmls->get_tag_start();
380 xmls->throw_unexpected_tag(tag_start,
"?xml");
401 Oks::warning_msg(fname) <<
" Unexpected attribute \'" << attr.
name() <<
"\' in the xml file header. Either \'version\' or \'encoding\' is expected.\n";
410 catch (std::exception & e) {
420 const char * dtd = xmls->get_tag();
421 const char dtd_start[] =
"<!DOCTYPE ";
422 if(!strncmp(dtd_start, dtd,
sizeof(dtd_start) - 1)) {
429 catch (std::exception & e) {
439 xmls->store_position();
441 const char * a_tag = xmls->get_tag();
443 const char xml_stylesheet_start[] =
"<?xml-stylesheet";
444 if(strncmp(xml_stylesheet_start, a_tag,
sizeof(xml_stylesheet_start) - 1)) {
445 xmls->restore_position();
451 catch (std::exception & e) {
458 xmls->get_tag_start();
463 catch (std::exception & e) {
471 const char * tag_start = xmls->get_tag_start();
480 catch (std::exception & e) {
485 std::string oks_version;
514 xmls->throw_unexpected_tag(attr.
name(),
"info\'s tags");
521 catch (std::exception & e) {
527 xmls->store_position();
529 bool read_include =
false;
532 const char * tag_start = xmls->get_tag_start();
535 else { xmls->restore_position();
return; }
540 catch (std::exception & e) {
549 const char * tag_start = xmls->get_tag_start();
566 xmls->throw_unexpected_tag(attr.
name(),
"path");
574 catch (std::exception & e) {
584 const char * tag_start = xmls->get_tag_start();
591 std::unique_ptr<oks::Comment> comment(
new oks::Comment() );
592 std::string creation_time;
608 xmls->throw_unexpected_tag(attr.
name(),
"comment\'s tags");
612 comment->validate(creation_time);
615 std::ostringstream text;
616 text <<
"The comment created at \'" << creation_time <<
"\' was already defined in this file";
617 throw std::runtime_error(text.str().c_str());
620 p_comments[creation_time] = comment.release();
626 catch (std::exception & e) {
639 if( creation_time.size() != 15 || creation_time[8] !=
'T' ) {
640 std::ostringstream msg;
641 msg <<
"the creation time \'" << creation_time <<
"\' is not an ISO-8601 date-time";
642 throw std::runtime_error(msg.str().c_str());
652 throw std::runtime_error(
"text is empty");
683 boost::posix_time::ptime
now = boost::posix_time::second_clock::universal_time();
684 std::string creation_time = boost::posix_time::to_iso_string(
now);
687 std::unique_ptr<oks::Comment> comment(
new oks::Comment(text, author) );
694 x = comment.release();
702 catch(std::exception & ex) {
712 std::map<std::string, oks::Comment *>::const_iterator i =
p_comments.find(creation_time);
713 return (i ==
p_comments.end()) ? 0: i->second;
727 std::string saved_author(comment->p_author);
728 std::string saved_text(comment->p_text);
730 comment->p_author = author;
731 comment->p_text = text;
736 catch(std::exception& ex) {
737 comment->p_author = saved_author;
738 comment->p_text = saved_text;
774 const static std::string __data(
"data");
775 const static std::string __schema(
"schema");
779 const char __oks_data[] =
"oks-data";
780 const char __oks_schema[] =
"oks-schema";
782 const char * id(__oks_data);
783 long id_len(
sizeof(__oks_data) - 1);
792 id_len =
sizeof(__oks_schema) - 1;
798 throw std::runtime_error(
"bad oks format");
803 const char __hdr_start[] =
"\n\n<!-- ";
804 const char __hdr_end[] =
" version 2.2 -->\n\n\n";
807 header.append(__hdr_start,
sizeof(__hdr_start) - 1);
808 header.append(
id, id_len);
809 header.append(__hdr_end,
sizeof(__hdr_end) - 1);
810 header.append(dtd, dtd_len);
812 xmls.
put_raw(header.c_str(), header.size());
825 std::string created_c_str;
850 if (!created_c_str.empty())
851 xmls.
put_attribute(
"creation-time",
sizeof(
"creation-time") - 1, created_c_str.c_str());
856 xmls.
put_attribute(
"last-modification-time",
sizeof(
"last-modification-time") - 1, last_modified_c_str.c_str());
891 xmls.
put_attribute(
"creation-time",
sizeof(
"creation-time") - 1, i.first.c_str());
892 xmls.
put_attribute(
"created-by",
sizeof(
"created-by") - 1, i.second->p_created_by.c_str());
893 xmls.
put_attribute(
"created-on",
sizeof(
"created-on") - 1, i.second->p_created_on.c_str());
894 xmls.
put_attribute(
"author",
sizeof(
"author") - 1, i.second->p_author.c_str());
895 xmls.
put_attribute(
"text",
sizeof(
"text") - 1, i.second->p_text.c_str());
905 catch (std::exception& ex)
916 return (!
p_kernel->get_user_repository_root().empty());
931 const std::string& s(
p_kernel->get_user_repository_root());
932 if(
size_t len = s.size()) {
967 const char _prefix_str[] =
".oks-lock-";
972 if(idx == std::string::npos) {
993 lock_file_contents.clear();
998 if(stat(lock_name, &buf) == 0) {
999 std::ifstream f(lock_name);
1002 std::getline(f, lock_file_contents);
1006 lock_file_contents =
"unknown [cannot read lock file \'";
1008 lock_file_contents +=
"\']";
1051 std::string lock_file_contents;
1059 if (
lock.try_lock() ==
false)
1061 std::ostringstream text;
1062 text <<
"file is already locked by \"" << lock_file_contents <<
'\"';
1071 catch (
const boost::interprocess::interprocess_exception& ex)
1073 std::ostringstream text;
1074 text <<
"boost::interprocess::unlock() failed: \"" << ex.what() <<
'\"';
1080 Oks::warning_msg(
"OksFile::lock()") <<
"Remove obsolete lock of file \'" <<
p_full_name <<
"\' created by \n\"" << lock_file_contents <<
"\"\n";
1085 std::ostringstream text;
1091 catch (
const boost::interprocess::interprocess_exception& ex)
1093 std::ostringstream text;
1094 text <<
"boost::interprocess::try_lock() failed: \"" << ex.what() <<
'\"';
1108 std::ostringstream text;
1115 boost::posix_time::ptime
now(boost::posix_time::second_clock::universal_time());
1120 catch (std::exception& ex)
1122 std::ostringstream text;
1123 text <<
"failed to write lock file \'" <<
p_lock_file_name <<
"\': " << ex.what();
1133 if (
p_lock->try_lock() ==
false)
1135 throw std::runtime_error(
"file is locked by another process");
1138 catch (
const std::exception& ex)
1140 std::ostringstream text;
1141 text <<
"boost::interprocess::try_lock() failed: \"" << ex.what() <<
'\"';
1157 catch (
const boost::interprocess::interprocess_exception& ex)
1159 std::ostringstream text;
1160 text <<
"boost::interprocess::unlock() failed: \"" << ex.what() <<
'\"';
1169 std::ostringstream text;
1176std::list<std::string>::iterator
1214 std::string path =
p_kernel->get_file_path(s,
this);
1215 p_kernel->k_load_file(path,
true,
this, 0);
1217 catch(std::exception& ex) {
1251 p_kernel->k_close_dangling_includes();
1258 if(old_s == new_s)
return;
1318 s <<
"user \"" << c.get_created_by() <<
"\" (" << c.get_author() <<
")"
1319 <<
" on \"" << c.get_created_on() <<
"\" wrote: \"" << c.get_text() <<
"\"";
1329 " short file name: \'" << f.get_short_file_name() <<
"\'\n"
1330 " full file name: \'" << f.get_full_file_name() <<
"\'\n"
1331 " lock file name: \'" << f.get_lock_file() <<
"\'\n"
1332 " logical name: \'" << f.get_logical_name() <<
"\'\n"
1333 " type: \'" << f.get_type() <<
"\'\n"
1334 " oks format: \'" << f.get_oks_format() <<
"\'\n"
1335 " number of items: " << f.get_number_of_items() <<
"\n"
1336 " created by: " << f.get_created_by() <<
"\n"
1337 " creation time: " << boost::posix_time::to_simple_string(f.get_creation_time()) <<
"\n"
1338 " created on: " << f.get_created_on() <<
"\n"
1339 " last modified by: \'" << f.get_last_modified_by() <<
"\'\n"
1340 " last modification time: " << boost::posix_time::to_simple_string(f.get_last_modification_time()) <<
"\'\n"
1341 " last modified on: " << f.get_last_modified_on() <<
"\'\n"
1342 " is " << (f.is_read_only() ?
"read-only" :
"read-write") <<
"\n"
1343 " is " << (f.is_locked() ?
"" :
"not ") <<
"locked\n"
1344 " is " << (f.is_updated() ?
"" :
"not ") <<
"updated\n";
1347 size_t num_of_includes = f.get_include_files().size();
1349 if (num_of_includes)
1351 s <<
" contains " << num_of_includes <<
" include file(s)";
1352 for (
const auto& i : f.get_include_files())
1353 s <<
" - \'" << i <<
"\'\n";
1357 s <<
" contains no include files\n";
1362 size_t num_of_comments = f.get_comments().size();
1364 if (num_of_comments)
1366 s <<
" contains " << num_of_comments <<
" comment(s)";
1367 for (
const auto& i : f.get_comments())
1368 s <<
" - " << boost::posix_time::to_simple_string(boost::posix_time::from_iso_string(i.first)) <<
": \'" << *i.second <<
"\'\n";
1372 s <<
" contains no comments\n";
1376 s <<
"END of oks file\n";
1407 if(update_repository ==
true ) {
1410 if(!full_repository_name.empty() && stat(full_repository_name.c_str(), &buf) == 0) {
1479 OksFile::Map::const_iterator j = data_files.find(&s);
1480 if(j != data_files.end()) {
1481 if(
out.find(j->second) !=
out.end()) {
continue; }
1482 out.insert(j->second);
1485 j = schema_files.find(&s);
1486 if(j != schema_files.end()) {
1487 if(
out.find(j->second) !=
out.end()) {
continue; }
1488 out.insert(j->second);
1495 j->second->get_all_include_files(kernel,
out);
1506 static const char start_tag[] =
"<info ";
1507 static const char last_tag[] =
"last-modification-time=\"yyyymmddThhmmss\"/>";
1509 const std::string::size_type len = line.length();
1512 ( len < (
sizeof(start_tag) +
sizeof(last_tag)) ) ||
1513 ( line.compare(0,
sizeof(start_tag)-1, start_tag,
sizeof(start_tag)-1) != 0 ) ||
1514 ( line.compare(len -
sizeof(last_tag) + 1, 24, last_tag, 24) != 0 )
1521 std::ifstream f1(file1_name);
1522 std::ifstream f2(file2_name);
1539 if (f1.eof() && f2.eof())
1544 std::getline(f1, line1);
1545 std::getline(f2, line2);
Cannot add include file. Such exception is thrown when OKS cannot add include file.
static std::string fill(const OksFile &file, const std::string &include_name, const std::string &reason) noexcept
Cannot remove include file. Such exception is thrown when OKS cannot remove include file.
static std::string fill(const OksFile &file, const std::string &include_name, const std::string &reason) noexcept
Cannot rename include file. Such exception is thrown when OKS cannot rename include file.
static std::string fill(const OksFile &file, const std::string &from, const std::string &to, const std::string &reason) noexcept
static std::string fill(const OksFile &file, const std::string &action, const std::string &reason) noexcept
static std::string fill(const std::string &src, const std::string &dest, const std::string &reason) noexcept
Cannot add include file. Such exception is thrown when OKS cannot add include file.
static std::string fill(const OksFile &file, bool lock_action, const std::string &reason) noexcept
Provides interface to the OKS XML schema and data files.
static bool compare(const char *file1_name, const char *file2_name)
Compare two files.
static const char xml_schema_file_dtd[]
std::shared_ptr< boost::interprocess::file_lock > p_lock
std::string p_lock_file_name
std::list< std::string >::iterator find_include_file(const std::string &)
static const char xml_comments_tag[]
void unlock()
Unlock OKS file.
void add_include_file(const std::string &name)
Add include file.
time_t p_repository_last_modified
OksFile(const std::string &, const std::string &, const std::string &, const std::string &, OksKernel *)
oks::Comment * get_comment(const std::string &creation_time) noexcept
void get_all_include_files(const OksKernel *kernel, std::set< OksFile * > &out)
Get all include files.
void lock()
Lock OKS file.
void write(OksXmlOutputStream &)
void set_type(const std::string &type)
Set file type.
void init_lock_name() const
bool get_lock_string(std::string &info) const
Return lock status of OKS file and if the file is locked, get information string about process which ...
static const char xml_data_file_dtd[]
std::string make_repository_name() const
static bool p_nolock_mode
std::map< std::string, oks::Comment * > p_comments
boost::posix_time::ptime p_last_modification_time
std::list< std::string > p_list_of_include_files
boost::posix_time::ptime p_creation_time
std::string p_logical_name
const std::string & get_repository_name() const
Get name of file inside repository.
void remove_comment(const std::string &creation_time)
Modify existing comment.
void set_logical_name(const std::string &name)
Set logical name of file.
std::string p_repository_name
std::string p_last_modified_by
void add_comment(const std::string &text, const std::string &author)
Add new comment to file.
void update_status_of_file(bool update_local=true, bool update_repository=true)
Update status of file.
std::map< const std::string *, OksFile *, SortByName > Map
bool is_repository_file() const
Get information about repository, the file belongs to.
void clear_comments() noexcept
static const char xml_file_tag[]
OksFile * check_parent(const OksFile *parent_h)
Set given parent, if this file is not yet included.
const OksFile * p_included_by
static const char xml_comment_tag[]
std::string p_last_modified_on
OksFile & operator=(const OksFile &)
void modify_comment(const std::string &creation_time, const std::string &text, const std::string &author)
void rename_include_file(const std::string &from, const std::string &to)
Rename include file.
static const char xml_file_header[]
FileStatus get_status_of_file() const
Return update status of file.
static const char xml_include_tag[]
void rename(const std::string &short_name, const std::string &full_name)
void remove_include_file(const std::string &name)
Remove include file.
static const char xml_info_tag[]
Provides interface to the OKS kernel.
static std::string & get_host_name()
Get hostname of given process.
static std::string & get_user_name()
Get username of given process.
static bool check_read_only(OksFile *f)
Check if the OKS file is read-only.
const OksFile::Map & data_files() const
Get all data files.
std::string get_file_path(const std::string &path, const OksFile *parent_file=0, bool strict_paths=true) const
Calculates full path to file.
static const std::string & get_repository_root()
Get OKS repository root.
static const char * GetVersion()
Get OKS version. The method returns string containing CVS tag and date of OKS build.
const OksFile::Map & schema_files() const
Get all schema files.
void put_last_tag(const char *, size_t len)
void put_attribute(const char *, size_t len, const char *)
void put_start_tag(const char *, size_t len)
static std::ostream & warning_msg(const char *)
virtual const char * what() const noexcept
#define TLOG_DEBUG(lvl,...)
bool cmp_str8(const char *s1, const char s2[9])
const std::string strerror(int error)
Convert C error number to string.
bool cmp_str1(const char *s1, const char s2[2])
bool cmp_str4(const char *s1, const char s2[5])
bool cmp_str6(const char *s1, const char s2[7])
bool cmp_str10(const char *s1, const char s2[11])
static bool is_not_info(const std::string &line)
bool cmp_str9(const char *s1, const char s2[10])
boost::posix_time::ptime str2time(const char *value, size_t len, const char *file_name=nullptr)
std::ostream & operator<<(std::ostream &s, const oks::exception &ex)
bool cmp_str13(const char *s1, const char s2[14])
bool cmp_str16(const char *s1, const char s2[17])
bool cmp_str15(const char *s1, const char s2[16])
bool cmp_str11(const char *s1, const char s2[12])
bool cmp_str12(const char *s1, const char s2[13])
bool cmp_str7(const char *s1, const char s2[8])
FELIX Initialization std::string initerror FELIX queue timed out
Both frame_count_limit and tp_count_limit were set to(disabled) in the TPCRawDataProcessor config. TPs will not send."