SpectMorph
smutils.hh
1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html
2 
3 #ifndef SPECTMORPH_UTIL_HH
4 #define SPECTMORPH_UTIL_HH
5 
6 #include <string>
7 
8 // operating system: one of these three
9 #if WIN32
10  #define SM_OS_WINDOWS
11 #elif __APPLE__
12  #define SM_OS_MACOS
13 #elif __linux__
14  #define SM_OS_LINUX
15 #else
16  #error "unsupported platform"
17 #endif
18 
19 // detect compiler
20 #if __clang__
21  #define SM_COMP_CLANG
22 #elif __GNUC__ > 2
23  #define SM_COMP_GCC
24 #else
25  #error "unsupported compiler"
26 #endif
27 
28 namespace SpectMorph
29 {
30 
31 /* integer types */
32 typedef uint8_t uint8;
33 typedef uint32_t uint32;
34 typedef int64_t int64;
35 typedef uint64_t uint64;
36 typedef unsigned int uint;
37 
38 #define SPECTMORPH_CLASS_NON_COPYABLE(Class) private: Class (const Class&); Class& operator= (const Class&);
39 
40 #ifdef SM_COMP_GCC
41  #define SPECTMORPH_PRINTF(format_idx, arg_idx) __attribute__ ((__format__ (gnu_printf, format_idx, arg_idx)))
42 #else
43  #define SPECTMORPH_PRINTF(format_idx, arg_idx) __attribute__ ((__format__ (__printf__, format_idx, arg_idx)))
44 #endif
45 
46 std::string string_printf (const char *format, ...) SPECTMORPH_PRINTF (1, 2);
47 std::string string_vprintf (const char *format, va_list vargs);
48 
49 std::string string_locale_printf (const char *format, ...) SPECTMORPH_PRINTF (1, 2);
50 
51 void sm_printf (const char *format, ...) SPECTMORPH_PRINTF (1, 2);
52 
53 enum InstallDir
54 {
55  INSTALL_DIR_BIN,
56  INSTALL_DIR_TEMPLATES,
57  INSTALL_DIR_INSTRUMENTS
58 };
59 
60 std::string sm_get_install_dir (InstallDir p);
61 
62 // data directory is relocatable
63 void sm_set_pkg_data_dir (const std::string& data_dir);
64 
65 enum UserDir
66 {
67  USER_DIR_INSTRUMENTS,
68  USER_DIR_CACHE, /* FIXME: unify with sm_get_cache_dir */
69  USER_DIR_DATA
70 };
71 
72 std::string sm_get_user_dir (UserDir p);
73 std::string sm_get_default_plan();
74 std::string sm_get_cache_dir();
75 
76 #ifdef SM_OS_MACOS
77 std::string sm_mac_documents_dir();
78 #endif
79 
80 enum DocumentsDir
81 {
82  DOCUMENTS_DIR_INSTRUMENTS
83 };
84 
85 std::string sm_get_documents_dir (DocumentsDir p);
86 
87 class Error
88 {
89 public:
90  enum class Code {
91  NONE,
92  FILE_NOT_FOUND,
93  FORMAT_INVALID,
94  PARSE_ERROR,
95  STR
96  };
97 
98  Error (Code code) :
99  m_code (code)
100  {
101  switch (code)
102  {
103  case Code::NONE:
104  m_message = "OK";
105  break;
106 
107  case Code::FILE_NOT_FOUND:
108  m_message = "No such file, device or directory";
109  break;
110 
111  case Code::FORMAT_INVALID:
112  m_message = "Invalid format";
113  break;
114 
115  case Code::PARSE_ERROR:
116  m_message = "Parsing error";
117  break;
118 
119  default:
120  m_message = "Unknown error";
121  }
122  }
123  explicit
124  Error (const std::string& message) :
125  m_code (Code::STR),
126  m_message (message)
127  {
128  }
129 
130  Code
131  code()
132  {
133  return m_code;
134  }
135  const char *
136  message()
137  {
138  return m_message.c_str();
139  }
140  operator bool()
141  {
142  return m_code != Code::NONE;
143  }
144 private:
145  Code m_code;
146  std::string m_message;
147 };
148 
149 #ifdef SM_OS_WINDOWS
150 std::string sm_resolve_link (const std::string& link_file);
151 #endif
152 
153 std::string sha1_hash (const unsigned char *data, size_t len);
154 std::string sha1_hash (const std::string& str);
155 
156 double get_time();
157 
158 std::string to_utf8 (const std::u32string& str);
159 std::u32string to_utf32 (const std::string& utf8);
160 
161 } // namespace SpectMorph
162 
163 /* we want to be able to use sm_debug without extra includes */
164 #include "smdebug.hh"
165 
166 #endif
STL namespace.
Definition: smutils.hh:87
Definition: smadsrenvelope.hh:8