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