-
Notifications
You must be signed in to change notification settings - Fork 19
/
misc_utils.c
345 lines (294 loc) · 8.35 KB
/
misc_utils.c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
/* like strcpy, except guaranteed to work with overlapping strings */
#define strMove(d,s) memmove(d,s,strlen(s)+1)
char *rmtrail(char *str)
/* Removes trailing space from a string */
{
int i;
if (str && 0 != (i = strlen(str))) {
while (--i >= 0) {
if (!isspace(str[i]))
break;
}
str[++i] = '\0';
}
return str;
}
char *rmlead(char *str)
/* Removes leading space from a string */
{
char *obuf;
if (str) {
for (obuf = str; *obuf && isspace(*obuf); ++obuf);
if (str != obuf) strMove(str, obuf);
}
return str;
}
char *remove_whitespace(char *str)
/* Remove leading and trailing space from a string */
{
return rmlead(rmtrail(str));
}
char *strlower(char *str)
/* Convert a string to lower case */
{
char *ss;
if (str) {
for (ss = str; *ss; ++ss)
*ss = tolower(*ss);
}
return str;
}
void split_path_file(char *input, char **path, char **file)
/* This routine splits an input string into a path and */
/* a filename. Since it allocates the memory for the */
/* path and filename dynamically, the calling program */
/* must free both "path" and "file". */
{
char *sptr = NULL, stmp[200];
unsigned int len, pathlen = 0, filelen = 0;
len = strlen(input);
sptr = strrchr(input, '/');
if (sptr == NULL) {
getcwd(stmp, 200);
if (stmp == NULL) {
printf("\nCurrent directory name is too long.\n");
printf("Exiting\n\n");
exit(1);
}
pathlen = strlen(stmp);
*path = (char *) calloc(pathlen + 1, sizeof(char));
*file = (char *) calloc(len + 1, sizeof(char));
strcpy(*path, stmp);
strncpy(*file, input, len);
} else {
pathlen = sptr - input;
filelen = len - pathlen - 1;
*path = (char *) calloc(pathlen + 1, sizeof(char));
*file = (char *) calloc(filelen + 1, sizeof(char));
strncpy(*path, input, pathlen);
strncpy(*file, sptr + 1, filelen);
}
}
int split_root_suffix(char *input, char **root, char **suffix)
/* This routine splits an input string into a root name */
/* + suffix. Since it allocates the memory for the */
/* root and suffix dynamically, the calling program */
/* must free both "root" and "suffix". */
/* If the routine finds a suffix, it returns 1, else 0. */
{
char *sptr = NULL;
unsigned int len, rootlen = 0, suffixlen = 0;
len = strlen(input);
sptr = strrchr(input, '.');
if (sptr == NULL) {
*root = (char *) calloc(len + 1, sizeof(char));
strncpy(*root, input, len);
return 0;
} else {
rootlen = sptr - input;
*root = (char *) calloc(rootlen + 1, sizeof(char));
strncpy(*root, input, rootlen);
suffixlen = len - rootlen - 1;
*suffix = (char *) calloc(suffixlen + 1, sizeof(char));
strncpy(*suffix, sptr + 1, suffixlen);
return 1;
}
}
void strtofilename(char *string)
/* Trim spaces off the end of *input and convert */
/* all other spaces into underscores. */
{
int ii;
ii = strlen(string) - 1;
do {
if (string[ii] == ' ')
string[ii] = '\0';
else
break;
} while (ii--);
do {
if (string[ii] == ' ')
string[ii] = '_';
} while (ii--);
}
double delay_from_dm(double dm, double freq_emitted)
/* Return the delay in seconds caused by dispersion, given */
/* a Dispersion Measure (dm) in cm-3 pc, and the emitted */
/* frequency (freq_emitted) of the pulsar in MHz. */
{
return dm / (0.000241 * freq_emitted * freq_emitted);
}
long long next2_to_n(long long x)
/* Return the first value of 2^n >= x */
{
long long i = 1;
while (i < x)
i <<= 1;
return i;
}
void avg_std(float *x, int n, double *mean, double *std, int stride)
/* For a float vector, *x, of length n*stride, this */
/* routine returns the mean and variance of the n values */
/* separated in memory by stride bytes (contiguous is stride=1) */
{
int ii;
double an = 0.0, an1 = 0.0, dx, var;
/* Modified (29 June 98) C version of the following: */
/* ALGORITHM AS 52 APPL. STATIST. (1972) VOL.21, P.226 */
/* Returned values were checked with Mathematica 3.01 */
if (n < 1) {
printf("\vVector length must be > 0 in avg_var(). Exiting\n");
exit(1);
} else {
*mean = (double) x[0];
var = 0.0;
}
for (ii = 1; ii < n; ii++) {
an = (double) (ii + 1);
an1 = (double) (ii);
dx = ((double) x[ii*stride] - *mean) / an;
var += an * an1 * dx * dx;
*mean += dx;
}
if (n > 1) {
var /= an1;
*std = sqrt(var);
} else {
*std = 0.0;
}
return;
}
static int TOMS_gcd(int a, int b)
/* Return the greatest common denominator of 'a' and 'b' */
{
int r;
do {
r = a % b;
a = b;
b = r;
} while (r != 0);
return a;
}
short transpose_bytes(unsigned char *a, int nx, int ny, unsigned char *move,
int move_size)
/*
* TOMS Transpose. Revised version of algorithm 380.
*
* These routines do in-place transposes of arrays.
*
* [ Cate, E.G. and Twigg, D.W., ACM Transactions on Mathematical Software,
* vol. 3, no. 1, 104-110 (1977) ]
*
* C version by Steven G. Johnson. February 1997.
*
* "a" is a 1D array of length ny*nx which contains the nx x ny matrix to be
* transposed. "a" is stored in C order (last index varies fastest). move
* is a 1D array of length move_size used to store information to speed up
* the process. The value move_size=(ny+nx)/2 is recommended.
*
* The return value indicates the success or failure of the routine. Returns 0
* if okay, -1 if ny or nx < 0, and -2 if move_size < 1. The return value
* should never be positive, but it it is, it is set to the final position in
* a when the search is completed but some elements have not been moved.
*
* Note: move[i] will stay zero for fixed points.
*/
{
int i, j, im, mn;
unsigned char b, c, d;
int ncount;
int k;
/* check arguments and initialize: */
if (ny < 0 || nx < 0)
return -1;
if (ny < 2 || nx < 2)
return 0;
if (move_size < 1)
return -2;
if (ny == nx) {
/*
* if matrix is square, exchange elements a(i,j) and a(j,i):
*/
for (i = 0; i < nx; ++i)
for (j = i + 1; j < nx; ++j) {
b = a[i + j * nx];
a[i + j * nx] = a[j + i * nx];
a[j + i * nx] = b;
}
return 0;
}
ncount = 2; /* always at least 2 fixed points */
k = (mn = ny * nx) - 1;
for (i = 0; i < move_size; ++i)
move[i] = 0;
if (ny >= 3 && nx >= 3)
ncount += TOMS_gcd(ny - 1, nx - 1) - 1; /* # fixed points */
i = 1;
im = ny;
while (1) {
int i1, i2, i1c, i2c;
int kmi;
/** Rearrange the elements of a loop
and its companion loop: **/
i1 = i;
kmi = k - i;
b = a[i1];
i1c = kmi;
c = a[i1c];
while (1) {
i2 = ny * i1 - k * (i1 / nx);
i2c = k - i2;
if (i1 < move_size)
move[i1] = 1;
if (i1c < move_size)
move[i1c] = 1;
ncount += 2;
if (i2 == i)
break;
if (i2 == kmi) {
d = b;
b = c;
c = d;
break;
}
a[i1] = a[i2];
a[i1c] = a[i2c];
i1 = i2;
i1c = i2c;
}
a[i1] = b;
a[i1c] = c;
if (ncount >= mn)
break; /* we've moved all elements */
/** Search for loops to rearrange: **/
while (1) {
int max;
max = k - i;
++i;
if (i > max)
return i;
im += ny;
if (im > k)
im -= k;
i2 = im;
if (i == i2)
continue;
if (i >= move_size) {
while (i2 > i && i2 < max) {
i1 = i2;
i2 = ny * i1 - k * (i1 / nx);
}
if (i2 == i)
break;
} else if (!move[i])
break;
}
}
return 0;
}