1 /*
2  * Schism Tracker - a cross-platform Impulse Tracker clone
3  * copyright (c) 2003-2005 Storlek <storlek@rigelseven.com>
4  * copyright (c) 2005-2008 Mrs. Brisby <mrs.brisby@nimh.org>
5  * copyright (c) 2009 Storlek & Mrs. Brisby
6  * copyright (c) 2010-2012 Storlek
7  * URL: http://schismtracker.org/
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23 
24 #ifndef UTIL_H
25 #define UTIL_H
26 
27 #include <sys/stat.h> /* roundabout way to get time_t */
28 #include <sys/types.h>
29 
30 /* --------------------------------------------------------------------- */
31 
32 #define ARRAY_SIZE(a) ((signed)(sizeof(a)/sizeof(*(a))))
33 
34 
35 /* macros stolen from glib */
36 #ifndef MAX
37 # define MAX(X,Y) (((X)>(Y))?(X):(Y))
38 #endif
39 #ifndef MIN
40 # define MIN(X,Y) (((X)<(Y))?(X):(Y))
41 #endif
42 #ifndef CLAMP
43 # define CLAMP(N,L,H) (((N)>(H))?(H):(((N)<(L))?(L):(N)))
44 #endif
45 
46 #ifdef __GNUC__
47 # ifndef LIKELY
48 #  define LIKELY(x) __builtin_expect(!!(x),1)
49 # endif
50 # ifndef UNLIKELY
51 #  define UNLIKELY(x) __builtin_expect(!!(x),0)
52 # endif
rest_user_get_me(\\Slim\\Http\\Request $p_request, \\Slim\\Http\\Response $p_response, array $p_args)53 # ifndef UNUSED
54 #  define UNUSED __attribute__((unused))
55 # endif
56 # ifndef NORETURN
57 #  define NORETURN __attribute__((noreturn))
58 # endif
59 # ifndef PACKED
60 #  define PACKED __attribute__((packed))
61 # endif
62 # ifndef MALLOC
63 #  define MALLOC __attribute__ ((malloc))
64 # endif
65 #else
66 # ifndef UNUSED
67 #  define UNUSED
68 # endif
69 # ifndef PACKED
70 #  define PACKED
71 # endif
72 # ifndef NORETURN
73 #  define NORETURN
74 # endif
75 # ifndef LIKELY
76 #  define LIKELY(x)
77 # endif
78 # ifndef UNLIKELY
79 #  define UNLIKELY(x)
80 # endif
81 # ifndef MALLOC
82 #  define MALLOC
83 # endif
84 #endif
85 
86 /* Path stuff that differs by platform */
87 #ifdef WIN32
88 # define DIR_SEPARATOR '\\'
89 # define DIR_SEPARATOR_STR "\\"
90 # define IS_DIR_SEPARATOR(c) ((c) == '/' || (c) == '\\')
91 #else
92 # define DIR_SEPARATOR '/'
93 # define DIR_SEPARATOR_STR "/"
94 # define IS_DIR_SEPARATOR(c) ((c) == '/')
95 #endif
96 
97 /* --------------------------------------------------------------------- */
98 /* functions returning const char * use a static buffer; ones returning char * malloc their return value
99 (thus it needs free'd)... except numtostr, get_time_string, and get_date_string, which return the buffer
100 passed to them in the 'buf' parameter. */
101 
102 /* memory */
103 extern MALLOC void *mem_alloc(size_t);
104 extern MALLOC void *mem_calloc(size_t, size_t);
105 extern MALLOC char *str_dup(const char *);
106 extern MALLOC char *strn_dup(const char *, size_t);
107 extern void *mem_realloc(void *,size_t);
108 extern void mem_free(void *);
109 
110 /*Conversion*/
111 /* linear -> deciBell*/
112 /* amplitude normalized to 1.0f.*/
113 extern float dB(float amplitude);
114 
115 /// deciBell -> linear*/
116 extern float dB2_amp(float db);
117 
118 /* linear -> deciBell*/
rest_user_reset_password(\\Slim\\Http\\Request $p_request, \\Slim\\Http\\Response $p_response, array $p_args)119 /* power normalized to 1.0f.*/
120 extern float pdB(float power);
121 
122 /* deciBell -> linear*/
123 extern float dB2_power(float db);
124 
125 /* linear -> deciBell*/
126 /* amplitude normalized to 1.0f.*/
127 /* Output scaled (and clipped) to 128 lines with noisefloor range.*/
128 /* ([0..128] = [-noisefloor..0dB])*/
129 /* correction_dBs corrects the dB after converted, but before scaling.*/
130 extern short dB_s(int noisefloor, float amplitude, float correction_dBs);
131 
132 /* deciBell -> linear*/
133 /* Input scaled to 128 lines with noisefloor range.*/
134 /* ([0..128] = [-noisefloor..0dB])*/
135 /* amplitude normalized to 1.0f.*/
136 /* correction_dBs corrects the dB after converted, but before scaling.*/
137 extern short dB2_amp_s(int noisefloor, int db, float correction_dBs);
138 
139 /* linear -> deciBell*/
140 /* power normalized to 1.0f.*/
141 /* Output scaled (and clipped) to 128 lines with noisefloor range.*/
142 /* ([0..128] = [-noisefloor..0dB])*/
143 /* correction_dBs corrects the dB after converted, but before scaling.*/
144 extern short pdB_s(int noisefloor, float power, float correction_dBs);
145 
146 /* deciBell -> linear*/
147 /* Input scaled to 128 lines with noisefloor range.*/
148 /* ([0..128] = [-noisefloor..0dB])*/
149 /* power normalized to 1.0f.*/
150 /* correction_dBs corrects the dB after converted, but before scaling.*/
151 extern short dB2_power_s(int noisefloor, int db, float correction_dBs);
152 
153 /* formatting */
154 /* for get_{time,date}_string, buf should be (at least) 27 chars; anything past that isn't used. */
155 char *get_date_string(time_t when, char *buf);
156 char *get_time_string(time_t when, char *buf);
157 char *numtostr(int digits, unsigned int n, char *buf);
158 char *numtostr_signed(int digits, int n, char *buf);
159 char *num99tostr(int n, char *buf);
160 
161 /* string handling */
162 const char *get_basename(const char *filename);
163 const char *get_extension(const char *filename); // including dot; "" if no extension (note: used to strip dot)
164 char *get_parent_directory(const char *dirname);
165 int ltrim_string(char *s); // return: length of string after trimming
166 int rtrim_string(char *s);
167 int trim_string(char *s);
168 int str_break(const char *s, char c, char **first, char **second);
169 char *str_escape(const char *source, int space_hack);
170 char *str_unescape(const char *source);
171 char *pretty_name(const char *filename);
172 int get_num_lines(const char *text);
173 char *str_concat(const char *s, ...);
174 
175 
176 /* filesystem */
177 int make_backup_file(const char *filename, int numbered);
178 long file_size(const char *filename);
179 int is_directory(const char *filename);
180 /* following functions should free() the resulting strings */
181 char *get_home_directory(void); /* "default" directory for user files, i.e. $HOME, My Documents, etc. */
182 char *get_dot_directory(void); /* where settings files go (%AppData% on Windows, same as $HOME elsewhere) */
183 char *get_current_directory(void); /* just a getcwd() wrapper */
184 
185 void put_env_var(const char *key, const char *value);
186 void unset_env_var(const char *key);
187 
188 /* integer sqrt (very fast; 32 bits limited) */
189 unsigned int i_sqrt(unsigned int r);
190 
191 /* sleep in msec */
192 void ms_sleep(unsigned int m);
193 
194 /* run a hook */
195 int run_hook(const char *dir, const char *name, const char *maybe_arg);
196 
197 /* Mostly a glorified rename(), with fixes for certain dumb OSes.
198 If 'overwrite' is zero, attempts to rename over an existing file will fail with EEXIST. */
199 int rename_file(const char *old, const char *newf, int overwrite);
200 
201 #endif /* ! UTIL_H */
202 
203