swg_utils.cpp
Go to the documentation of this file.
1 /** @file swg_utils.cpp utility functions for bindings*/
2 
3 /* FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2008-2025 Thomas Moor
6  Exclusive copyright is granted to Klaus Schmidt
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 */
23 
24 #include "swg_utils.h"
25 
26 
27 namespace faudes {
28 
29 // programatically throw exceptions
30 void faudes_throw_exception(const std::string& msg) {
31  throw Exception("faudes script", msg, 49);
32 }
33 
34 
35 // global switches
36 bool faudes_statenames = true;
37 std::string faudes_dotpath = "dot";
38 
39 // access functions
42 void faudes_dotexecpath(const std::string& filename) { faudes_dotpath=filename; }
43 std::string faudes_dotexecpath() { return faudes_dotpath; }
44 std::string faudes_version(void) { return VersionString()+" "+PluginsString(); }
45 std::string faudes_build(void) { return BuildString(); }
46 
47 // helper: run dot for test
48 bool faudes_dotready(void) {
49  // cache value
50  static bool ready=false;
51  static bool known=false;
52  if(known) return ready;
53  // test for dot binary
54  std::string testdot = faudes_dotpath + " -V";
55  ready = (system(testdot.c_str()) == 0);
56  known = true;
57  return ready;
58 }
59 
60 //global help dictionary topic -> key -> text
61 std::map< std::string, std::string > faudes_dictionary_topics;
62 std::map< std::string, std::map<std::string, std::vector<std::string> > > faudes_dictionary_entries;
63 
64 // insert topic to dictionary
65 void faudes_dict_insert_topic(const std::string& topic, const std::string& text) {
66  faudes_dictionary_topics[topic]=text;
67 }
68 
69 // insert entry to dictionary
70 void faudes_dict_insert_entry(const std::string& topic, const std::string& key, const std::string& entry) {
71  std::string k=key;
72  if(k.length()>0)
73  k.at(0)=toupper(k.at(0));
74  faudes_dictionary_entries[topic][k].push_back(entry);
75  if(topic!="")
76  if(faudes_dictionary_topics[topic]=="")
77  faudes_dictionary_topics[topic]=topic;
78 }
79 
80 
81 // main help text
82 void faudes_help(void) {
83  std::stringstream sstr;
84 
85  // section: intro
86  sstr << std::endl
87  << "libFAUDES bindings provide access to libFAUDES data structures and functions. " << std::endl
88  << "For detailed information, please consult the libFAUDES documentation."<< std::endl
89  << std::endl
90  << "All libFAUDES bindings are in the module 'faudes', ie access is via 'faudes.*'." << std::endl
91  << std::endl
92  << "Interface:" << std::endl
93  << std::endl;
94 
95  // section: topics
96  std::map< std::string, std::string >::iterator tit;
97  for(tit=faudes_dictionary_topics.begin(); tit!=faudes_dictionary_topics.end();tit++) {
98  std::string left = " faudes.Help(\"" + tit->first +"\")";
99  std::string right = tit->second;
100  while(left.length()<37) left+=" ";
101  sstr << left << right << std::endl;
102  }
103  sstr << std::endl;
104 
105  // section: behavioural
106  sstr
107  << "Configuration:" << std::endl
108  << std::endl
109  << " faudes.StateNamesOn() enable automatic state names" << std::endl
110  << " faudes.StateNamesOff() disable automatic state names" << std::endl
111  << " faudes.DotExecPath(\"filename\") path of dot executable" << std::endl
112  << " faudes.Version() return libfaudes version string" << std::endl
113  << std::endl
114  << "Console Commands:" << std::endl
115  << std::endl
116  << " faudes.Print(\"message\") print as faudes debugging message" << std::endl
117  << " faudes.Error(\"message\") abort script with error message" << std::endl
118  << " faudes.Verbosity(int) set verbositry level " << std::endl;
119 
120  // do print to stderr
121  Print(0,sstr.str()); // verb 0 <> always
122 }
123 
124 // section text
125 void faudes_help(const std::string& topic) {
126  std::stringstream sstr;
127 
128  // section: intro
129  sstr
130  << std::endl
131  << "libFAUDES help topic: \"" << topic << "\"" << std::endl
132  << std::endl;
133 
134  // section: list entries
135  std::map< std::string, std::map< std::string, std::vector<std::string> > >::iterator tit;
136  tit = faudes_dictionary_entries.find(topic);
137  if(tit!=faudes_dictionary_entries.end()) {
138  std::map< std::string, std::vector<std::string> >::iterator kit;
139  for(kit = tit->second.begin(); kit != tit->second.end(); kit++) {
140  if(kit != tit->second.begin()) sstr << std::endl;
141  const std::string& line = kit->first;
142  sstr << " *** " << line << " ***" << std::endl;
143  for(unsigned int i=0; i< kit->second.size(); i++) {
144  const std::string& line = kit->second[i];
145  std::size_t sep = line.find_first_of(' ');
146  if(sep==std::string::npos) sep=0;
147  while(sep<20) { sstr << " "; sep++; };
148  sstr << line << std::endl;
149  }
150  }
151  } else {
152  sstr << " (no entries) " << std::endl;
153  }
154 
155  // do print to stderr
156  Print(0,sstr.str()); // verb 0 <> always
157 }
158 
159 // API wrappers
160 void faudes_gen_version(const Generator& rGen, const std::string& ver, Generator& rRes) {
161  rGen.Version(ver,rRes);
162 }
163 void faudes_gen_version(const Generator& rGen, const std::string& pat, const std::string& rep, Generator& rRes) {
164  rGen.Version(pat,rep,rRes);
165 }
166 void faudes_set_union(const EventSet& rAlph1, const EventSet& rAlph2, EventSet& rRes)
167  { rRes = rAlph1; rRes.InsertSet(rAlph2); }
168 void faudes_set_intersection(const EventSet& rAlph1, const EventSet& rAlph2, EventSet& rRes)
169  { rRes = rAlph1; rRes.RestrictSet(rAlph2); }
170 void faudes_set_difference(const EventSet& rAlph1, const EventSet& rAlph2, EventSet& rRes)
171  { rRes = rAlph1; rRes.EraseSet(rAlph2); }
172 
173 
174 
175 }//namespace
176 
virtual void InsertSet(const NameSet &rOtherSet)
void EraseSet(const NameSet &rOtherSet)
void RestrictSet(const NameSet &rOtherSet)
static void StateNamesEnabledDefault(bool flag)
virtual void Version(const std::string &rVersion, vGenerator &rResGen) const
std::string VersionString()
Definition: cfl_utils.cpp:141
void faudes_gen_version(const Generator &rGen, const std::string &ver, Generator &rRes)
Definition: swg_utils.cpp:160
void faudes_help(void)
Definition: swg_utils.cpp:82
bool faudes_statenames
Definition: swg_utils.cpp:36
void faudes_set_union(const EventSet &rAlph1, const EventSet &rAlph2, EventSet &rRes)
Definition: swg_utils.cpp:166
std::string faudes_version(void)
Definition: swg_utils.cpp:44
std::map< std::string, std::map< std::string, std::vector< std::string > > > faudes_dictionary_entries
Definition: swg_utils.cpp:62
void faudes_set_intersection(const EventSet &rAlph1, const EventSet &rAlph2, EventSet &rRes)
Definition: swg_utils.cpp:168
void faudes_dotexecpath(const std::string &filename)
Definition: swg_utils.cpp:42
std::string PluginsString()
Definition: cfl_utils.cpp:146
void faudes_statenames_on(void)
Definition: swg_utils.cpp:40
std::string BuildString()
Definition: cfl_utils.cpp:160
void faudes_dict_insert_topic(const std::string &topic, const std::string &text)
Definition: swg_utils.cpp:65
std::string faudes_build(void)
Definition: swg_utils.cpp:45
void faudes_statenames_off(void)
Definition: swg_utils.cpp:41
std::map< std::string, std::string > faudes_dictionary_topics
Definition: swg_utils.cpp:61
void faudes_dict_insert_entry(const std::string &topic, const std::string &key, const std::string &entry)
Definition: swg_utils.cpp:70
bool faudes_dotready(void)
Definition: swg_utils.cpp:48
void Print(int v, const std::string &message)
Definition: cfl_utils.cpp:477
void faudes_throw_exception(const std::string &msg)
Definition: swg_utils.cpp:30
void faudes_set_difference(const EventSet &rAlph1, const EventSet &rAlph2, EventSet &rRes)
Definition: swg_utils.cpp:170
std::string faudes_dotpath
Definition: swg_utils.cpp:37

libFAUDES 2.33h --- 2025.06.18 --- c++ api documentaion by doxygen