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 std::string sm_mac_application_support_dir();
81 #endif
82 
83 enum DocumentsDir
84 {
85  DOCUMENTS_DIR_INSTRUMENTS
86 };
87 
88 std::string sm_get_documents_dir (DocumentsDir p);
89 
90 class Error
91 {
92 public:
93  enum class Code {
94  NONE,
95  FILE_NOT_FOUND,
96  FORMAT_INVALID,
97  PARSE_ERROR,
98  STR
99  };
100 
101  Error (Code code) :
102  m_code (code)
103  {
104  switch (code)
105  {
106  case Code::NONE:
107  m_message = "OK";
108  break;
109 
110  case Code::FILE_NOT_FOUND:
111  m_message = "No such file, device or directory";
112  break;
113 
114  case Code::FORMAT_INVALID:
115  m_message = "Invalid format";
116  break;
117 
118  case Code::PARSE_ERROR:
119  m_message = "Parsing error";
120  break;
121 
122  default:
123  m_message = "Unknown error";
124  }
125  }
126  explicit
127  Error (const std::string& message) :
128  m_code (Code::STR),
129  m_message (message)
130  {
131  }
132 
133  Code
134  code()
135  {
136  return m_code;
137  }
138  const char *
139  message()
140  {
141  return m_message.c_str();
142  }
143  operator bool()
144  {
145  return m_code != Code::NONE;
146  }
147 private:
148  Code m_code;
149  std::string m_message;
150 };
151 
152 #ifdef SM_OS_WINDOWS
153 std::string sm_resolve_link (const std::string& link_file);
154 #endif
155 
156 std::string sha1_hash (const unsigned char *data, size_t len);
157 std::string sha1_hash (const std::string& str);
158 
159 double get_time();
160 
161 std::string to_utf8 (const std::u32string& str);
162 std::u32string to_utf32 (const std::string& utf8);
163 
164 Error read_dir (const std::string& dirname, std::vector<std::string>& files);
165 bool file_exists (const std::string& filename);
166 bool dir_exists (const std::string& dirname);
167 
168 } // namespace SpectMorph
169 
170 /* we want to be able to use sm_debug without extra includes */
171 #include "smdebug.hh"
172 
173 #endif
SpectMorph::Error
Definition: smutils.hh:90