CASM  1.1.0
A Clusters Approach to Statistical Mechanics
api.cc
Go to the documentation of this file.
1 #include "ccasm/api.hh"
2 
3 #include <wordexp.h>
4 
5 #include <boost/filesystem.hpp>
6 
9 #include "casm/app/errors.hh"
10 #include "casm/casm_io/Log.hh"
13 #include "casm/clex/PrimClex.hh"
14 
15 using namespace CASM;
16 
17 extern "C" {
18 
19 costream *casm_STDOUT() { return reinterpret_cast<costream *>(&cout_log()); }
20 
21 costream *casm_STDERR() { return reinterpret_cast<costream *>(&cerr_log()); }
22 
24  return reinterpret_cast<costream *>(&null_log());
25 }
26 
28  return reinterpret_cast<costream *>(new OStringStreamLog());
29 }
30 
32  delete reinterpret_cast<OStringStreamLog *>(ptr);
33 }
34 
35 unsigned long casm_ostringstream_size(costream *ptr) {
36  typedef std::char_traits<char>::pos_type pos_type;
37  return reinterpret_cast<OStringStreamLog *>(ptr)->ss().tellp() + pos_type(1);
38 }
39 
40 char *casm_ostringstream_strcpy(costream *ptr, char *c_str) {
41  auto str = reinterpret_cast<OStringStreamLog *>(ptr)->ss().str();
42  std::strcpy(c_str, str.c_str());
43  return c_str;
44 }
45 
46 cPrimClex *casm_primclex_null() { return nullptr; }
47 
49  Log &_log(*reinterpret_cast<Log *>(log));
50  Log &_err_log(*reinterpret_cast<Log *>(err_log));
51  ScopedLogging logging{_log, _err_log};
52  PrimClex *ptr = new PrimClex(fs::path(path));
53  return reinterpret_cast<cPrimClex *>(ptr);
54 }
55 
57  delete reinterpret_cast<PrimClex *>(ptr);
58 }
59 
61  bool read_settings, bool read_composition,
62  bool read_chem_ref, bool read_configs,
63  bool clear_clex) {
64  Log &_log(*reinterpret_cast<Log *>(log));
65  Log &_err_log(*reinterpret_cast<Log *>(err_log));
66  ScopedLogging logging{_log, _err_log};
67  PrimClex *_primclex = reinterpret_cast<PrimClex *>(ptr);
68  _primclex->refresh(read_settings, read_composition, read_chem_ref,
69  read_configs, clear_clex);
70 }
71 
73 void casm_command_list(costream *ostringstream_log) {
74  Log &_log(*reinterpret_cast<OStringStreamLog *>(ostringstream_log));
75  std::vector<std::string> command_list;
76  for (const auto &val : command_map()) {
77  command_list.push_back(val.first);
78  }
79  jsonParser json;
80  json = command_list;
81  _log << json;
82 }
83 
88 int casm_capi(char *args, cPrimClex *primclex, char *root, costream *log,
89  costream *err_log) {
90  PrimClex *_primclex = reinterpret_cast<PrimClex *>(primclex);
91  Log &_log(*reinterpret_cast<Log *>(log));
92  Log &_err_log(*reinterpret_cast<Log *>(err_log));
93  ScopedLogging logging{_log, _err_log};
94 
95  std::string s("casm ");
96  s += std::string(args);
97 
98  fs::path _root(root);
99 
100  CommandArgs command_args(s, _primclex, _root);
101  if (command_args.parse_result()) {
102  return command_args.parse_result();
103  }
104 
105  try {
106  int code = casm_api(command_args);
107  return code;
108  } catch (std::exception const &e) {
109  _log << "CASM: Uncaught exception: \n" << e.what();
110  }
111  return ERR_UNKNOWN;
112 }
113 }
std::set< std::string > & s
costream * casm_ostringstream_new()
Definition: api.cc:27
char * casm_ostringstream_strcpy(costream *ptr, char *c_str)
Definition: api.cc:40
int casm_capi(char *args, cPrimClex *primclex, char *root, costream *log, costream *err_log)
Definition: api.cc:88
cPrimClex * casm_primclex_new(char *path, costream *log, costream *err_log)
Definition: api.cc:48
void casm_primclex_refresh(cPrimClex *ptr, costream *log, costream *err_log, bool read_settings, bool read_composition, bool read_chem_ref, bool read_configs, bool clear_clex)
Definition: api.cc:60
unsigned long casm_ostringstream_size(costream *ptr)
Definition: api.cc:35
void casm_primclex_delete(cPrimClex *ptr)
Definition: api.cc:56
void casm_command_list(costream *ostringstream_log)
Print to log the recognized casm commands as a JSON array.
Definition: api.cc:73
cPrimClex * casm_primclex_null()
Definition: api.cc:46
void casm_ostringstream_delete(costream *ptr)
Definition: api.cc:31
costream * casm_nullstream()
Definition: api.cc:23
costream * casm_STDOUT()
Definition: api.cc:19
costream * casm_STDERR()
Definition: api.cc:21
#define ERR_UNKNOWN
Definition: errors.hh:10
int parse_result() const
Definition: CLIParse.hh:24
Definition: Log.hh:48
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
CommandMap & command_map()
Return static CommandMap containing all CASM API commands.
struct costream costream
For std::ostream*.
Definition: api.hh:12
struct cPrimClex cPrimClex
For CASM::PrimClex*.
Definition: api.hh:9
int casm_api(const CommandArgs &args)
Executes CASM commands specified by args.
void refresh(bool read_settings=false, bool read_composition=false, bool read_chem_ref=false, bool read_configs=false, bool clear_clex=false)
Reload PrimClex data from settings.
Definition: PrimClex.cc:156
Main CASM namespace.
Definition: APICommand.hh:8
Log & cerr_log()
Definition: Log.hh:433
Log & log()
Definition: Log.hh:424
Log & null_log()
Definition: Log.hh:438
Log & cout_log()
Definition: Log.hh:428
Log & err_log()
Definition: Log.hh:426
PrimClex * primclex
Definition: settings.cc:135
Data structure holding basic CASM command info.