Pol  Revision:cb584c9
scrdef.cpp
Go to the documentation of this file.
1 
7 #include "scrdef.h"
8 
9 #include "../bscript/escrutil.h"
10 #include "../clib/fileutil.h"
11 #include "../clib/logfacility.h"
12 #include "../plib/pkg.h"
13 
14 namespace Pol
15 {
16 namespace Core
17 {
18 std::string full_scriptname( const std::string& spec, const Plib::Package* pkg,
19  const char* mainpfx )
20 {
21  if ( spec.empty() )
22  return spec;
23 
24  if ( pkg != nullptr )
25  return Bscript::normalize_ecl_filename( pkg->dir() + spec );
26 
27  if ( spec.find( '/' ) == std::string::npos )
28  return Bscript::normalize_ecl_filename( mainpfx + spec );
29  else
30  return Bscript::normalize_ecl_filename( "scripts/" + spec );
31 }
32 
33 ScriptDef::ScriptDef( const std::string& iname, const Plib::Package* ipkg, const char* mainpfx )
34 {
35  config( iname, ipkg, mainpfx, true );
36 }
37 
38 ScriptDef::ScriptDef( const std::string& iname, const Plib::Package* ipkg )
39 {
40  config( iname, ipkg, "", true );
41 }
42 
43 bool ScriptDef::operator==( const ScriptDef& other ) const
44 {
45  if ( empty() && other.empty() )
46  return true;
47 
48  return pkg_ == other.pkg_ && name_ == other.name_;
49 }
50 bool ScriptDef::operator!=( const ScriptDef& other ) const
51 {
52  return !( *this == other );
53 }
54 
55 void ScriptDef::config( const std::string& iname, const Plib::Package* ipkg, const char* mainpfx,
56  bool warn_if_not_found )
57 {
58  std::string path;
59  const Plib::Package* npkg;
60  if ( !Plib::pkgdef_split( iname, ipkg, &npkg, &path ) )
61  {
62  ERROR_PRINT << "Error reading script descriptor '" << iname << "'\n";
63  throw std::runtime_error( "Error reading script descriptor" );
64  }
65 
66  localname_ = path;
67  name_ = full_scriptname( path, npkg, mainpfx );
68  pkg_ = npkg;
69 
70  if ( warn_if_not_found )
71  {
72  if ( !empty() && !exists() )
73  {
74  ERROR_PRINT << "Warning! " << name_.get() << " does not exist!\n";
75  }
76  }
77 }
78 
79 void ScriptDef::config( const std::string& iname, const Plib::Package* ipkg )
80 {
81  config( iname, ipkg, "", true );
82 }
83 
84 bool ScriptDef::config_nodie( const std::string& iname, const Plib::Package* ipkg,
85  const char* mainpfx )
86 {
87  std::string path;
88  const Plib::Package* npkg;
89  if ( !Plib::pkgdef_split( iname, ipkg, &npkg, &path ) )
90  {
91  ERROR_PRINT << "Error reading script descriptor '" << iname << "'\n";
92  return false;
93  }
94 
95  localname_ = iname;
96  name_ = full_scriptname( path, npkg, mainpfx );
97  pkg_ = npkg;
98 
99  return true;
100 }
101 
102 std::string ScriptDef::relativename( const Plib::Package* pkg ) const
103 {
104  if ( name_ == "" )
105  return "";
106  else if ( pkg == pkg_ )
107  return localname_;
108  else
109  return ":" + ( pkg_ ? pkg_->name() : "" ) + ":" + localname_.get();
110 }
111 
112 void ScriptDef::quickconfig( const Plib::Package* pkg, const std::string& name_ecl )
113 {
114  localname_ = "unknown";
115  name_ = pkg->dir() + name_ecl;
116  pkg_ = pkg;
117 }
118 
119 void ScriptDef::quickconfig( const std::string& name_ecl )
120 {
121  localname_ = "unknown";
122  name_ = name_ecl;
123  pkg_ = nullptr;
124 }
125 
126 bool ScriptDef::exists() const
127 {
128  return !empty() && Clib::FileExists( c_str() );
129  // ref_ptr<EScriptProgram> prog = find_script2( *this, false, true );
130  // return (prog.get() != nullptr);
131 }
132 
134 {
135  localname_ = "";
136  name_ = "";
137  pkg_ = nullptr;
138 }
139 
141 {
142  return sizeof( ScriptDef );
143 }
144 }
145 }
bool empty() const
Definition: scrdef.h:41
const Plib::Package * pkg() const
Definition: scrdef.h:46
void quickconfig(const Plib::Package *pkg, const std::string &name_ecl)
Definition: scrdef.cpp:112
std::string full_scriptname(const std::string &spec, const Plib::Package *pkg, const char *mainpfx)
Definition: scrdef.cpp:18
bool config_nodie(const std::string &name, const Plib::Package *pkg, const char *mainpfx)
Definition: scrdef.cpp:84
size_t estimatedSize() const
Definition: scrdef.cpp:140
std::string relativename(const Plib::Package *pkg=nullptr) const
Definition: scrdef.cpp:102
const std::string & name() const
Definition: pkg.h:83
bool operator==(const ScriptDef &other) const
Definition: scrdef.cpp:43
bool pkgdef_split(const std::string &spec, const Package *inpkg, const Package **outpkg, std::string *path)
Definition: pkg.cpp:410
bool exists() const
Definition: scrdef.cpp:126
bool operator!=(const ScriptDef &other) const
Definition: scrdef.cpp:50
void config(const std::string &name, const Plib::Package *pkg, const char *mainpfx, bool warn_if_not_found=true)
Definition: scrdef.cpp:55
const Plib::Package * pkg_
Definition: scrdef.h:55
boost_utils::script_name_flystring name_
Definition: scrdef.h:54
const char * c_str() const
Definition: scrdef.h:42
boost_utils::script_name_flystring localname_
Definition: scrdef.h:53
std::string normalize_ecl_filename(const std::string &filename)
Definition: escrutil.cpp:135
#define ERROR_PRINT
Definition: logfacility.h:230
bool FileExists(const char *filename)
Definition: fileutil.cpp:118
Definition: berror.cpp:12
const std::string & dir() const
Definition: pkg.h:79