Newer
Older
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
* This file part of: AstrOmatic FITS/LDAC library
* Copyright: (C) 1995-2010 Emmanuel Bertin -- IAP/CNRS/UPMC
*
* License: GNU General Public License
*
* AstrOmatic software 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 3 of the
* License, or (at your option) any later version.
* AstrOmatic software 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 AstrOmatic software.
* If not, see <http://www.gnu.org/licenses/>.
*
* Last modified: 09/10/2010
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "fitscat_defs.h"
#include "fitscat.h"
static void (*errorfunc)(char *msg1, char *msg2) = NULL;
static char warning_historystr[WARNING_NMAX][192]={""};
static int nwarning = 0, nwarning_history = 0, nerror = 0;
/********************************* error ************************************/
/*
I hope it will never be used!
*/
void error(int num, char *msg1, char *msg2)
{
fprintf(stderr, "\n> %s%s\n\n",msg1,msg2);
if (num && errorfunc && !nerror)
{
nerror = 1;
errorfunc(msg1, msg2);
}
exit(num);
}
/**************************** error_installfunc *****************************/
/*
I hope it will never be used!
*/
void error_installfunc(void (*func)(char *msg1, char *msg2))
{
if (func)
errorfunc = func;
return;
}
/********************************* warning **********************************/
/*
Print a warning message on screen.
*/
void warning(char *msg1, char *msg2)
{
time_t warntime;
struct tm *tm;
warntime = time(NULL);
tm = localtime(&warntime);
fprintf(stderr, "\n> WARNING: %s%s\n\n",msg1,msg2);
sprintf(warning_historystr[(nwarning++)%WARNING_NMAX],
"%04d-%02d-%02d %02d:%02d:%02d : %.80s%.80s",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec,
msg1, msg2);
return;
}
/****************************** warning_history ******************************/
/*
Return warning.
*/
char *warning_history(void)
{
char *str;
if (nwarning_history >= WARNING_NMAX)
{
nwarning_history = 0; /* So it can be accessed later on */
return "";
}
str = warning_historystr[((nwarning>WARNING_NMAX? (nwarning%WARNING_NMAX):0)
+ nwarning_history++)%WARNING_NMAX];
if (!*str)
nwarning_history = 0; /* So it can be accessed later on */
return str;
}
/******************************* swapbytes **********************************/
/*
Swap bytes for doubles, longs and shorts (for DEC machines or PC for inst.).
*/
void swapbytes(void *ptr, int nb, int n)
{
cp = (char *)ptr;
if (nb&4)
{
for (j=n; j--; cp+=4)
{
c = cp[3];
cp[3] = cp[0];
cp[0] = c;
c = cp[2];
cp[2] = cp[1];
cp[1] = c;
}
return;
}
if (nb&2)
{
for (j=n; j--; cp+=2)
{
c = cp[1];
cp[1] = cp[0];
cp[0] = c;
}
return;
}
if (nb&1)
return;
if (nb&8)
{
for (j=n; j--; cp+=8)
{
c = cp[7];
cp[7] = cp[0];
cp[0] = c;
c = cp[6];
cp[6] = cp[1];
cp[1] = c;
c = cp[5];
cp[5] = cp[2];
cp[2] = c;
c = cp[4];
cp[4] = cp[3];
cp[3] = c;
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
}
return;
}
error(EXIT_FAILURE, "*Internal Error*: Unknown size in ", "swapbytes()");
return;
}
/****** wstrncmp ***************************************************************
PROTO int wstrncmp(char *cs, char *ct, int n)
PURPOSE simple wildcard strcmp.
INPUT character string 1,
character string 2,
maximum number of characters to be compared.
OUTPUT comparison integer (same meaning as strcmp).
NOTES -.
AUTHOR E. Bertin (IAP & Leiden observatory)
VERSION 15/02/96
***/
int wstrncmp(char *cs, char *ct, int n)
{
int diff,i;
i = n;
diff = 0;
do
{
diff = ((*cs=='?'&&*ct)||(*ct=='?'&&*cs))?0:*cs-*ct;
} while (!diff && --i && *(cs++) && *(ct++));
return diff;
}
/****** findkey ****************************************************************
PROTO int findkey(char *str, char *key, int size)
PURPOSE Find an item within a list of keywords.
INPUT character string,
an array of character strings containing the list of keywords,
offset (in char) between each keyword.
OUTPUT position in the list (0 = first) if keyword matched,
RETURN_ERROR otherwise.
NOTES the matching is case-sensitive.
AUTHOR E. Bertin (IAP & Leiden observatory)
VERSION 15/02/96
***/
int findkey(char *str, char *key, int size)
{
int i;
for (i=0; key[0]; i++, key += size)
if (!strcmp(str, key))
return i;
return RETURN_ERROR;
}
/********************************* findnkey **********************************
PROTO int findnkey(char *str, char *key, int size, int nkey)
PURPOSE Find an item within a list of nkey keywords.
INPUT character string,
an array of character strings containing the list of keywords,
offset (in char) between each keyword.
number of keywords.
OUTPUT position in the list (0 = first) if keyword matched,
RETURN_ERROR otherwise.
NOTES the matching is case-sensitive.
AUTHOR E. Bertin (IAP & Leiden observatory)
VERSION 15/02/96
***/
int findnkey(char *str, char *key, int size, int nkey)
{
int i;
for (i=0; i<nkey; i++, key += size)
if (!strcmp(str, key))
return i;
return RETURN_ERROR;
}