summaryrefslogtreecommitdiff
path: root/src/include/util.h
blob: 9a4024bf4991e5b9cd8dd7e4c0d33a19647fab4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Schism Tracker - a cross-platform Impulse Tracker clone
 * copyright (c) 2003-2005 Storlek <storlek@rigelseven.com>
 * copyright (c) 2005-2008 Mrs. Brisby <mrs.brisby@nimh.org>
 * copyright (c) 2009 Storlek & Mrs. Brisby
 * copyright (c) 2010-2012 Storlek
 * URL: http://schismtracker.org/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef UTIL_H
#define UTIL_H

#include <sys/stat.h> /* roundabout way to get time_t */
#include <sys/types.h>

/* --------------------------------------------------------------------- */

#define ARRAY_SIZE(a) ((signed)(sizeof(a)/sizeof(*(a))))


/* macros stolen from glib */
#ifndef MAX
# define MAX(X,Y) (((X)>(Y))?(X):(Y))
#endif
#ifndef MIN
# define MIN(X,Y) (((X)<(Y))?(X):(Y))
#endif
#ifndef CLAMP
# define CLAMP(N,L,H) (((N)>(H))?(H):(((N)<(L))?(L):(N)))
#endif

#ifdef __GNUC__
# ifndef LIKELY
#  define LIKELY(x) __builtin_expect(!!(x),1)
# endif
# ifndef UNLIKELY
#  define UNLIKELY(x) __builtin_expect(!!(x),0)
# endif
# ifndef UNUSED
#  define UNUSED __attribute__((unused))
# endif
# ifndef NORETURN
#  define NORETURN __attribute__((noreturn))
# endif
# ifndef PACKED
#  define PACKED __attribute__((packed))
# endif
# ifndef MALLOC
#  define MALLOC __attribute__ ((malloc))
# endif
#else
# ifndef UNUSED
#  define UNUSED
# endif
# ifndef PACKED
#  define PACKED
# endif
# ifndef NORETURN
#  define NORETURN
# endif
# ifndef LIKELY
#  define LIKELY(x)
# endif
# ifndef UNLIKELY
#  define UNLIKELY(x)
# endif
# ifndef MALLOC
#  define MALLOC
# endif
#endif

/* Path stuff that differs by platform */
#ifdef WIN32
# define DIR_SEPARATOR '\\'
# define DIR_SEPARATOR_STR "\\"
# define IS_DIR_SEPARATOR(c) ((c) == '/' || (c) == '\\')
#else
# define DIR_SEPARATOR '/'
# define DIR_SEPARATOR_STR "/"
# define IS_DIR_SEPARATOR(c) ((c) == '/')
#endif

/* --------------------------------------------------------------------- */
/* functions returning const char * use a static buffer; ones returning char * malloc their return value
(thus it needs free'd)... except numtostr, get_time_string, and get_date_string, which return the buffer
passed to them in the 'buf' parameter. */

/* memory */
extern MALLOC void *mem_alloc(size_t);
extern MALLOC char *str_dup(const char *);
extern void *mem_realloc(void *,size_t);
extern void mem_free(void *);

/*Conversion*/
/* linear -> deciBell*/
/* amplitude normalized to 1.0f.*/
extern float dB(float amplitude);

/// deciBell -> linear*/
extern float dB2_amp(float db);

/* linear -> deciBell*/
/* power normalized to 1.0f.*/
extern float pdB(float power);

/* deciBell -> linear*/
extern float dB2_power(float db);

/* linear -> deciBell*/
/* amplitude normalized to 1.0f.*/
/* Output scaled (and clipped) to 128 lines with noisefloor range.*/
/* ([0..128] = [-noisefloor..0dB])*/
/* correction_dBs corrects the dB after converted, but before scaling.*/
extern short dB_s(int noisefloor, float amplitude, float correction_dBs);

/* deciBell -> linear*/
/* Input scaled to 128 lines with noisefloor range.*/
/* ([0..128] = [-noisefloor..0dB])*/
/* amplitude normalized to 1.0f.*/
/* correction_dBs corrects the dB after converted, but before scaling.*/
extern short dB2_amp_s(int noisefloor, int db, float correction_dBs);

/* linear -> deciBell*/
/* power normalized to 1.0f.*/
/* Output scaled (and clipped) to 128 lines with noisefloor range.*/
/* ([0..128] = [-noisefloor..0dB])*/
/* correction_dBs corrects the dB after converted, but before scaling.*/
extern short pdB_s(int noisefloor, float power, float correction_dBs);

/* deciBell -> linear*/
/* Input scaled to 128 lines with noisefloor range.*/
/* ([0..128] = [-noisefloor..0dB])*/
/* power normalized to 1.0f.*/
/* correction_dBs corrects the dB after converted, but before scaling.*/
extern short dB2_power_s(int noisefloor, int db, float correction_dBs);

/* formatting */
/* for get_{time,date}_string, buf should be (at least) 27 chars; anything past that isn't used. */
char *get_date_string(time_t when, char *buf);
char *get_time_string(time_t when, char *buf);
char *numtostr(int digits, unsigned int n, char *buf);
char *numtostr_signed(int digits, int n, char *buf);
char *num99tostr(int n, char *buf);

/* string handling */
const char *get_basename(const char *filename);
const char *get_extension(const char *filename); // including dot; "" if no extension (note: used to strip dot)
char *get_parent_directory(const char *dirname);
int ltrim_string(char *s); // return: length of string after trimming
int rtrim_string(char *s);
int trim_string(char *s);
int str_break(const char *s, char c, char **first, char **second);
char *str_escape(const char *source, int space_hack);
char *str_unescape(const char *source);
char *pretty_name(const char *filename);
int get_num_lines(const char *text);
char *str_concat(const char *s, ...);


/* filesystem */
int make_backup_file(const char *filename, int numbered);
long file_size(const char *filename);
int is_directory(const char *filename);
/* following functions should free() the resulting strings */
char *get_home_directory(void); /* "default" directory for user files, i.e. $HOME, My Documents, etc. */
char *get_dot_directory(void); /* where settings files go (%AppData% on Windows, same as $HOME elsewhere) */
char *get_current_directory(void); /* just a getcwd() wrapper */

void put_env_var(const char *key, const char *value);
void unset_env_var(const char *key);

/* integer sqrt (very fast; 32 bits limited) */
unsigned int i_sqrt(unsigned int r);

/* sleep in msec */
void ms_sleep(unsigned int m);

/* run a hook */
int run_hook(const char *dir, const char *name, const char *maybe_arg);

/* Mostly a glorified rename(), with fixes for certain dumb OSes.
If 'overwrite' is zero, attempts to rename over an existing file will fail with EEXIST. */
int rename_file(const char *old, const char *newf, int overwrite);

#endif /* ! UTIL_H */
© All Rights Reserved