public software.psfex

[/] [branches/] [rhl/] [src/] [fits/] [fitsmisc.c] - Blame information for rev 195

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 157 bertin
/*
2
*                               fitsmisc.c
3 2 bertin
*
4 157 bertin
* Miscellaneous functions.
5 2 bertin
*
6 157 bertin
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 2 bertin
*
8 157 bertin
*       This file part of:      AstrOmatic FITS/LDAC library
9 2 bertin
*
10 160 bertin
*       Copyright:              (C) 1995-2010 Emmanuel Bertin -- IAP/CNRS/UPMC
11 2 bertin
*
12 157 bertin
*       License:                GNU General Public License
13
*
14
*       AstrOmatic software is free software: you can redistribute it and/or
15
*       modify it under the terms of the GNU General Public License as
16
*       published by the Free Software Foundation, either version 3 of the
17
*       License, or (at your option) any later version.
18
*       AstrOmatic software is distributed in the hope that it will be useful,
19
*       but WITHOUT ANY WARRANTY; without even the implied warranty of
20
*       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
*       GNU General Public License for more details.
22
*       You should have received a copy of the GNU General Public License
23
*       along with AstrOmatic software.
24
*       If not, see <http://www.gnu.org/licenses/>.
25
*
26
*       Last modified:          09/10/2010
27
*
28
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
29 2 bertin
 
30
#ifdef HAVE_CONFIG_H
31
#include        "config.h"
32
#endif
33
 
34
#include        <ctype.h>
35
#include        <stdio.h>
36
#include        <stdlib.h>
37
#include        <string.h>
38 4 bertin
#include        <time.h>
39 2 bertin
 
40
#include        "fitscat_defs.h"
41
#include        "fitscat.h"
42
 
43 195 rhl
static void     (*errorfunc)(const char *msg1, const char *msg2) = NULL;
44 4 bertin
static char     warning_historystr[WARNING_NMAX][192]={""};
45
static int      nwarning = 0, nwarning_history = 0, nerror = 0;
46
 
47 2 bertin
/********************************* error ************************************/
48
/*
49
I hope it will never be used!
50
*/
51 195 rhl
void    error(int num, const char *msg1, const char *msg2)
52 2 bertin
  {
53
  fprintf(stderr, "\n> %s%s\n\n",msg1,msg2);
54 4 bertin
  if (num && errorfunc && !nerror)
55
    {
56
    nerror = 1;
57
    errorfunc(msg1, msg2);
58
    }
59 2 bertin
  exit(num);
60
  }
61
 
62
 
63 4 bertin
/**************************** error_installfunc *****************************/
64
/*
65
I hope it will never be used!
66
*/
67 195 rhl
void    error_installfunc(void (*func)(const char *msg1, const char *msg2))
68 4 bertin
  {
69
  if (func)
70
    errorfunc = func;
71
 
72
  return;
73
  }
74
 
75
 
76 2 bertin
/********************************* warning **********************************/
77
/*
78
Print a warning message on screen.
79
*/
80
void    warning(char *msg1, char *msg2)
81
  {
82 4 bertin
   time_t       warntime;
83
   struct tm    *tm;
84
 
85
  warntime = time(NULL);
86
  tm = localtime(&warntime);
87
 
88
  fprintf(stderr, "\n> WARNING: %s%s\n\n",msg1,msg2);
89
  sprintf(warning_historystr[(nwarning++)%WARNING_NMAX],
90
        "%04d-%02d-%02d %02d:%02d:%02d : %.80s%.80s",
91
        tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
92
        tm->tm_hour, tm->tm_min, tm->tm_sec,
93
        msg1, msg2);
94
 
95
 
96 2 bertin
  return;
97
  }
98
 
99
 
100 4 bertin
/****************************** warning_history ******************************/
101
/*
102
Return warning.
103
*/
104
char    *warning_history(void)
105
  {
106
   char         *str;
107
 
108
  if (nwarning_history >= WARNING_NMAX)
109
    {
110
    nwarning_history = 0;        /* So it can be accessed later on */
111
    return "";
112
    }
113
 
114
  str = warning_historystr[((nwarning>WARNING_NMAX? (nwarning%WARNING_NMAX):0)
115
        + nwarning_history++)%WARNING_NMAX];
116
  if (!*str)
117
    nwarning_history = 0;        /* So it can be accessed later on */
118
 
119
  return str;
120
  }
121
 
122
 
123 2 bertin
/******************************* swapbytes **********************************/
124
/*
125
Swap bytes for doubles, longs and shorts (for DEC machines or PC for inst.).
126
*/
127
void    swapbytes(void *ptr, int nb, int n)
128
  {
129 90 bertin
   char *cp,
130
        c;
131
   int  j;
132 2 bertin
 
133
  cp = (char *)ptr;
134
 
135
  if (nb&4)
136
    {
137
    for (j=n; j--; cp+=4)
138
      {
139 90 bertin
      c = cp[3];
140
      cp[3] = cp[0];
141
      cp[0] = c;
142
      c = cp[2];
143
      cp[2] = cp[1];
144
      cp[1] = c;
145 2 bertin
      }
146
    return;
147
    }
148
 
149
  if (nb&2)
150
    {
151
    for (j=n; j--; cp+=2)
152 90 bertin
      {
153
      c = cp[1];
154
      cp[1] = cp[0];
155
      cp[0] = c;
156
      }
157 2 bertin
    return;
158
    }
159
 
160
  if (nb&1)
161
    return;
162
 
163
  if (nb&8)
164
    {
165
    for (j=n; j--; cp+=8)
166
      {
167 90 bertin
      c = cp[7];
168
      cp[7] = cp[0];
169
      cp[0] = c;
170
      c = cp[6];
171
      cp[6] = cp[1];
172
      cp[1] = c;
173
      c = cp[5];
174
      cp[5] = cp[2];
175
      cp[2] = c;
176
      c = cp[4];
177
      cp[4] = cp[3];
178
      cp[3] = c;
179 2 bertin
      }
180
    return;
181
    }
182
 
183
  error(EXIT_FAILURE, "*Internal Error*: Unknown size in ", "swapbytes()");
184
 
185
  return;
186
  }
187
 
188
 
189
/****** wstrncmp ***************************************************************
190
PROTO   int wstrncmp(char *cs, char *ct, int n)
191
PURPOSE simple wildcard strcmp.
192
INPUT   character string 1,
193
        character string 2,
194
        maximum number of characters to be compared.
195
OUTPUT  comparison integer (same meaning as strcmp).
196
NOTES   -.
197
AUTHOR  E. Bertin (IAP & Leiden observatory)
198
VERSION 15/02/96
199
 ***/
200
int     wstrncmp(char *cs, char *ct, int n)
201
 
202
  {
203
   int  diff,i;
204
 
205
  i = n;
206
  diff = 0;
207
  do
208
    {
209
    diff = ((*cs=='?'&&*ct)||(*ct=='?'&&*cs))?0:*cs-*ct;
210
    } while (!diff && --i && *(cs++) && *(ct++));
211
 
212
  return diff;
213
  }
214
 
215
 
216
/****** findkey ****************************************************************
217
PROTO   int findkey(char *str, char *key, int size)
218
PURPOSE Find an item within a list of keywords.
219
INPUT   character string,
220
        an array of character strings containing the list of keywords,
221
        offset (in char) between each keyword.
222
OUTPUT  position in the list (0 = first) if keyword matched,
223
        RETURN_ERROR otherwise.
224
NOTES   the matching is case-sensitive.
225
AUTHOR  E. Bertin (IAP & Leiden observatory)
226
VERSION 15/02/96
227
 ***/
228
int     findkey(char *str, char *key, int size)
229
 
230
  {
231
  int i;
232
 
233
  for (i=0; key[0]; i++, key += size)
234
    if (!strcmp(str, key))
235
      return i;
236
 
237
  return RETURN_ERROR;
238
  }
239
 
240
 
241
/********************************* findnkey **********************************
242
PROTO   int findnkey(char *str, char *key, int size, int nkey)
243
PURPOSE Find an item within a list of nkey keywords.
244
INPUT   character string,
245
        an array of character strings containing the list of keywords,
246
        offset (in char) between each keyword.
247
        number of keywords.
248
OUTPUT  position in the list (0 = first) if keyword matched,
249
        RETURN_ERROR otherwise.
250
NOTES   the matching is case-sensitive.
251
AUTHOR  E. Bertin (IAP & Leiden observatory)
252
VERSION 15/02/96
253
 ***/
254
int     findnkey(char *str, char *key, int size, int nkey)
255
 
256
  {
257
  int i;
258
 
259
  for (i=0; i<nkey; i++, key += size)
260
    if (!strcmp(str, key))
261
      return i;
262
 
263
  return RETURN_ERROR;
264
  }
265
 
266