IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 28246


Ignore:
Timestamp:
Jun 7, 2010, 2:24:29 PM (16 years ago)
Author:
eugene
Message:

add additional error checks to libfits, libdvo; fix error in F_scan,print,modify with OFF_T_FMT mis-used

Location:
trunk/Ohana/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libdvo/src/LoadPhotcodes.c

    r25757 r28246  
    1515  if (LoadPhotcodesText (master_file)) {
    1616    if (!check_file_access (catdir_file, TRUE, TRUE, TRUE)) return TRUE;
    17     SavePhotcodesFITS (catdir_file);
     17    if (!SavePhotcodesFITS (catdir_file)) return FALSE;
    1818    return TRUE;
    1919  }
  • trunk/Ohana/src/libdvo/src/SavePhotcodesFITS.c

    r27580 r28246  
    3232
    3333  /* convert FITS format data to internal format (byteswaps & EXTNAME) */
    34   gfits_db_create (&db);
    35   gfits_table_set_PhotCode_PS1_V2 (&db.ftable, photcode_output, table[0].Ncode);
    36   gfits_db_save (&db);
    37   gfits_db_close (&db);
     34  if (!gfits_db_create (&db)) return (FALSE);
     35  if (!gfits_table_set_PhotCode_PS1_V2 (&db.ftable, photcode_output, table[0].Ncode)) return (FALSE);
     36  if (!gfits_db_save (&db)) return (FALSE);
     37  if (!gfits_db_close (&db)) return (FALSE);
    3838
    3939  free (photcode_output);
  • trunk/Ohana/src/libdvo/src/dvo_image.c

    r28241 r28246  
    120120  /* adjust header */
    121121  Nimages = 0;
    122   gfits_scan (&db[0].header, "NIMAGES", OFF_T_FMT, 1,  &Nimages);
     122  if (!gfits_scan (&db[0].header, "NIMAGES", OFF_T_FMT, 1,  &Nimages)) return (FALSE);
    123123  Nimages += Nnew;
    124   gfits_modify (&db[0].header, "NIMAGES", OFF_T_FMT, 1,  Nimages);
    125 
    126   gfits_table_to_vtable (&db[0].ftable, &db[0].vtable, 0, 0);
    127   gfits_vadd_rows (&db[0].vtable, (char *) new, Nnew, sizeof(Image));
     124
     125  if (!gfits_modify (&db[0].header, "NIMAGES", OFF_T_FMT, 1,  Nimages)) return (FALSE);
     126
     127  if (!gfits_table_to_vtable (&db[0].ftable, &db[0].vtable, 0, 0)) return (FALSE);
     128
     129  if (!gfits_vadd_rows (&db[0].vtable, (char *) new, Nnew, sizeof(Image))) return (FALSE);
    128130
    129131  /* check that primary header and table header agree */
    130132  if (Nimages != db[0].theader.Naxis[1]) {
    131133    fprintf (stderr, "header / table length mismatch!\n");
     134    return (FALSE);
    132135  }
    133136  return (TRUE);
     
    157160      exit (2);
    158161  }
    159   return (TRUE);
     162  return (status);
    160163}
    161164
  • trunk/Ohana/src/libdvo/src/fits_db.c

    r27435 r28246  
    2020/* create an empty db */
    2121int gfits_db_create (FITS_DB *db) {
    22   gfits_init_header (&db[0].header);   
     22  if (!gfits_init_header (&db[0].header)) return (FALSE);
    2323  db[0].header.extend = TRUE;
    24   gfits_create_header (&db[0].header);
    25   gfits_create_matrix (&db[0].header, &db[0].matrix);
    26   gfits_print (&db[0].header, "NEXTEND", "%d", 1, 1);
     24  if (!gfits_create_header (&db[0].header))  return (FALSE);
     25  if (!gfits_create_matrix (&db[0].header, &db[0].matrix)) return (FALSE);
     26  if (!gfits_print (&db[0].header, "NEXTEND", "%d", 1, 1)) return (FALSE);
    2727  db[0].ftable.header = &db[0].theader;
    2828  return (TRUE);
  • trunk/Ohana/src/libfits/header/F_modify.c

    r28241 r28246  
    5151
    5252  /* write the numeric modes */
    53   if (!strcmp (mode, "%d"))   snprintf (string, 81, "%-8s= %20d / %-s ",    field, va_arg (argp, int),                comment);
    54   if (!strcmp (mode, "%ld"))  snprintf (string, 81, "%-8s= %20ld / %-s ",   field, va_arg (argp, long),               comment);
    55   if (!strcmp (mode, OFF_T_FMT)) snprintf (string, 81, "%-8s= %20lld / %-s ",  field, va_arg (argp, long long),               comment);
    56   if (!strcmp (mode, "%Ld"))  snprintf (string, 81, "%-8s= %20lld / %-s ",  field, va_arg (argp, long long),          comment);
    57   if (!strcmp (mode, "%u"))   snprintf (string, 81, "%-8s= %20u / %-s ",    field, va_arg (argp, unsigned),           comment);
    58   if (!strcmp (mode, "%lu"))  snprintf (string, 81, "%-8s= %20lu / %-s ",   field, va_arg (argp, unsigned long),      comment);
    59   if (!strcmp (mode, "%llu")) snprintf (string, 81, "%-8s= %20llu / %-s ",  field, va_arg (argp, unsigned long long), comment);
    60   if (!strcmp (mode, "%Lu"))  snprintf (string, 81, "%-8s= %20llu / %-s ",  field, va_arg (argp, unsigned long long), comment);
    61   if (!strcmp (mode, "%hd"))  snprintf (string, 81, "%-8s= %20d / %-s ",    field, va_arg (argp, int),                comment);
    62   if (!strcmp (mode, "%f"))   snprintf (string, 81, "%-8s= %20.10f / %-s ", field, va_arg (argp, double),             comment);
    63   if (!strcmp (mode, "%lf"))  snprintf (string, 81, "%-8s= %20.10f / %-s ", field, va_arg (argp, double),             comment);
    64   if (!strcmp (mode, "%e"))   snprintf (string, 81, "%-8s= %20.10E / %-s ", field, va_arg (argp, double),             comment);
    65   if (!strcmp (mode, "%le"))  snprintf (string, 81, "%-8s= %20.10E / %-s ", field, va_arg (argp, double),             comment);
    66   if (!strcmp (mode, "%g"))   snprintf (string, 81, "%-8s= %20.10G / %-s ", field, va_arg (argp, double),             comment);
    67   if (!strcmp (mode, "%lg"))  snprintf (string, 81, "%-8s= %20.10G / %-s ", field, va_arg (argp, double),             comment);
    68 
    69   if (!strcmp (mode, "%jd"))  snprintf (string, 81, "%-8s= %20jd / %-s ",   field, va_arg (argp, intmax_t),           comment);
     53  if (!strcmp (mode, "%d"))   { snprintf (string, 81, "%-8s= %20d / %-s ",    field, va_arg (argp, int),                comment); goto found_it; }
     54  if (!strcmp (mode, "%ld"))  { snprintf (string, 81, "%-8s= %20ld / %-s ",   field, va_arg (argp, long),               comment); goto found_it; }
     55  if (!strcmp (mode, "%lld")) { snprintf (string, 81, "%-8s= %20lld / %-s ",  field, va_arg (argp, long long),          comment); goto found_it; }
     56  if (!strcmp (mode, "%Ld"))  { snprintf (string, 81, "%-8s= %20lld / %-s ",  field, va_arg (argp, long long),          comment); goto found_it; }
     57  if (!strcmp (mode, "%u"))   { snprintf (string, 81, "%-8s= %20u / %-s ",    field, va_arg (argp, unsigned),           comment); goto found_it; }
     58  if (!strcmp (mode, "%lu"))  { snprintf (string, 81, "%-8s= %20lu / %-s ",   field, va_arg (argp, unsigned long),      comment); goto found_it; }
     59  if (!strcmp (mode, "%llu")) { snprintf (string, 81, "%-8s= %20llu / %-s ",  field, va_arg (argp, unsigned long long), comment); goto found_it; }
     60  if (!strcmp (mode, "%Lu"))  { snprintf (string, 81, "%-8s= %20llu / %-s ",  field, va_arg (argp, unsigned long long), comment); goto found_it; }
     61  if (!strcmp (mode, "%hd"))  { snprintf (string, 81, "%-8s= %20d / %-s ",    field, va_arg (argp, int),                comment); goto found_it; }
     62  if (!strcmp (mode, "%f"))   { snprintf (string, 81, "%-8s= %20.10f / %-s ", field, va_arg (argp, double),             comment); goto found_it; }
     63  if (!strcmp (mode, "%lf"))  { snprintf (string, 81, "%-8s= %20.10f / %-s ", field, va_arg (argp, double),             comment); goto found_it; }
     64  if (!strcmp (mode, "%e"))   { snprintf (string, 81, "%-8s= %20.10E / %-s ", field, va_arg (argp, double),             comment); goto found_it; }
     65  if (!strcmp (mode, "%le"))  { snprintf (string, 81, "%-8s= %20.10E / %-s ", field, va_arg (argp, double),             comment); goto found_it; }
     66  if (!strcmp (mode, "%g"))   { snprintf (string, 81, "%-8s= %20.10G / %-s ", field, va_arg (argp, double),             comment); goto found_it; }
     67  if (!strcmp (mode, "%lg"))  { snprintf (string, 81, "%-8s= %20.10G / %-s ", field, va_arg (argp, double),             comment); goto found_it; }
     68  if (!strcmp (mode, "%jd"))  { snprintf (string, 81, "%-8s= %20jd / %-s ",   field, va_arg (argp, intmax_t),           comment); goto found_it; }
    7069
    7170  /* string value.  Quotes must be at least 18 chars apart */
     
    7372    strncpy (data, va_arg (argp, char *), 68);
    7473    snprintf (string, 81, "%-8s= '%-18s' / %-s ", field, data, comment);
    75   }
    76 
     74    goto found_it;
     75  }
     76
     77  /* failed to find mode */
     78  return (FALSE);
     79
     80found_it:
    7781  strncpy (p, string, 80);
    7882  va_end (argp);
  • trunk/Ohana/src/libfits/header/F_print.c

    r28241 r28246  
    4646
    4747  /* write the numeric modes */
    48   if (!strcmp (mode, "%d"))  { snprintf (string, 81, "%-8s= %20d / %46s ",    field, va_arg (argp, int),                blank); }
    49   if (!strcmp (mode, "%ld")) { snprintf (string, 81, "%-8s= %20ld / %46s ",   field, va_arg (argp, long),               blank); }
    50   if (!strcmp (mode, OFF_T_FMT)){ snprintf (string, 81, "%-8s= %20lld / %46s ",  field, va_arg (argp, long long),          blank); }
    51   if (!strcmp (mode, "%Ld")) { snprintf (string, 81, "%-8s= %20lld / %46s ",  field, va_arg (argp, long long),          blank); }
    52   if (!strcmp (mode, "%u"))  { snprintf (string, 81, "%-8s= %20u / %46s ",    field, va_arg (argp, unsigned),           blank); }
    53   if (!strcmp (mode, "%lu")) { snprintf (string, 81, "%-8s= %20lu / %46s ",   field, va_arg (argp, unsigned long),      blank); }
    54   if (!strcmp (mode, "%llu")){ snprintf (string, 81, "%-8s= %20llu / %46s ",  field, va_arg (argp, unsigned long long), blank); }
    55   if (!strcmp (mode, "%Lu")) { snprintf (string, 81, "%-8s= %20llu / %46s ",  field, va_arg (argp, unsigned long long), blank); }
    56   if (!strcmp (mode, "%hd")) { snprintf (string, 81, "%-8s= %20d / %46s ",    field, va_arg (argp, int),                blank); }
    57   if (!strcmp (mode, "%f"))  { snprintf (string, 81, "%-8s= %20.10f / %46s ", field, va_arg (argp, double),             blank); }
    58   if (!strcmp (mode, "%lf")) { snprintf (string, 81, "%-8s= %20.10f / %46s ", field, va_arg (argp, double),             blank); }
    59   if (!strcmp (mode, "%e"))  { snprintf (string, 81, "%-8s= %20.10E / %46s ", field, va_arg (argp, double),             blank); }
    60   if (!strcmp (mode, "%le")) { snprintf (string, 81, "%-8s= %20.10E / %46s ", field, va_arg (argp, double),             blank); }
    61   if (!strcmp (mode, "%g"))  { snprintf (string, 81, "%-8s= %20.10G / %46s ", field, va_arg (argp, double),             blank); }
    62   if (!strcmp (mode, "%lg")) { snprintf (string, 81, "%-8s= %20.10G / %46s ", field, va_arg (argp, double),             blank); }
    63 
    64   if (!strcmp (mode, "%jd")) { snprintf (string, 81, "%-8s= %20jd / %46s ",   field, va_arg (argp, intmax_t),           blank); }
     48  if (!strcmp (mode, "%d"))  { snprintf (string, 81, "%-8s= %20d / %46s ",    field, va_arg (argp, int),                blank); goto found_it; }
     49  if (!strcmp (mode, "%ld")) { snprintf (string, 81, "%-8s= %20ld / %46s ",   field, va_arg (argp, long),               blank); goto found_it; }
     50  if (!strcmp (mode, "%lld")){ snprintf (string, 81, "%-8s= %20lld / %46s ",  field, va_arg (argp, long long),          blank); goto found_it; }
     51  if (!strcmp (mode, "%Ld")) { snprintf (string, 81, "%-8s= %20lld / %46s ",  field, va_arg (argp, long long),          blank); goto found_it; }
     52  if (!strcmp (mode, "%u"))  { snprintf (string, 81, "%-8s= %20u / %46s ",    field, va_arg (argp, unsigned),           blank); goto found_it; }
     53  if (!strcmp (mode, "%lu")) { snprintf (string, 81, "%-8s= %20lu / %46s ",   field, va_arg (argp, unsigned long),      blank); goto found_it; }
     54  if (!strcmp (mode, "%llu")){ snprintf (string, 81, "%-8s= %20llu / %46s ",  field, va_arg (argp, unsigned long long), blank); goto found_it; }
     55  if (!strcmp (mode, "%Lu")) { snprintf (string, 81, "%-8s= %20llu / %46s ",  field, va_arg (argp, unsigned long long), blank); goto found_it; }
     56  if (!strcmp (mode, "%hd")) { snprintf (string, 81, "%-8s= %20d / %46s ",    field, va_arg (argp, int),                blank); goto found_it; }
     57  if (!strcmp (mode, "%f"))  { snprintf (string, 81, "%-8s= %20.10f / %46s ", field, va_arg (argp, double),             blank); goto found_it; }
     58  if (!strcmp (mode, "%lf")) { snprintf (string, 81, "%-8s= %20.10f / %46s ", field, va_arg (argp, double),             blank); goto found_it; }
     59  if (!strcmp (mode, "%e"))  { snprintf (string, 81, "%-8s= %20.10E / %46s ", field, va_arg (argp, double),             blank); goto found_it; }
     60  if (!strcmp (mode, "%le")) { snprintf (string, 81, "%-8s= %20.10E / %46s ", field, va_arg (argp, double),             blank); goto found_it; }
     61  if (!strcmp (mode, "%g"))  { snprintf (string, 81, "%-8s= %20.10G / %46s ", field, va_arg (argp, double),             blank); goto found_it; }
     62  if (!strcmp (mode, "%lg")) { snprintf (string, 81, "%-8s= %20.10G / %46s ", field, va_arg (argp, double),             blank); goto found_it; }
     63  if (!strcmp (mode, "%jd")) { snprintf (string, 81, "%-8s= %20jd / %46s ",   field, va_arg (argp, intmax_t),           blank); goto found_it; }
    6564
    6665  /* string value.  Quotes must be at least 18 chars apart.  Longer lines will this should be fixed to allow arbitrary string lengths, up to 69 chars */
     
    6968    line[68] = 0;
    7069    snprintf (string, 81, "%-8s= '%-18s' / %46s ", field, line, blank);
     70    goto found_it;
    7171  }
     72  return (FALSE);
    7273
     74found_it:
    7375  strncpy (p, string, 80);
    74  
    7576  va_end (argp);
    7677  return (TRUE);
  • trunk/Ohana/src/libfits/header/F_scan.c

    r28241 r28246  
    6161  s = gfits_keyword_start (p); /* points at first char (not ') */
    6262  q = gfits_keyword_end (p); /* points at following space or ' */
     63
     64  if (!strcmp (mode, "%f") || !strcmp (mode, "%lf")) {
     65    fvalue = strtod (s, &q);
     66    if ((*q == 'd') || (*q == 'D'))
     67      fvalue *= pow (10.0, atof (q + 1));
     68
     69    if (!strcmp (mode, "%f"))   { *va_arg (argp, float *)  = fvalue; return (TRUE); }
     70    if (!strcmp (mode, "%lf"))  { *va_arg (argp, double *) = fvalue; return (TRUE); }
     71  }
     72
     73  value = strtoll (s, &q, 0);
     74  if ((*q == 'd') || (*q == 'D')) value *= pow (10.0, atof (q + 1));
     75
     76  if (!strcmp (mode, "%d"))   { *va_arg (argp, int *)                = value; return (TRUE); }
     77  if (!strcmp (mode, "%ld"))  { *va_arg (argp, long *)               = value; return (TRUE); }
     78  if (!strcmp (mode, "%lld")) { *va_arg (argp, long long *)          = value; return (TRUE); }
     79  if (!strcmp (mode, "%Ld"))  { *va_arg (argp, long long *)          = value; return (TRUE); }
     80  if (!strcmp (mode, "%u"))   { *va_arg (argp, unsigned *)           = value; return (TRUE); }
     81  if (!strcmp (mode, "%lu"))  { *va_arg (argp, unsigned long *)      = value; return (TRUE); }
     82  if (!strcmp (mode, "%llu")) { *va_arg (argp, unsigned long long *) = value; return (TRUE); }
     83  if (!strcmp (mode, "%Lu"))  { *va_arg (argp, unsigned long long *) = value; return (TRUE); }
     84  if (!strcmp (mode, "%hd"))  { *va_arg (argp, short *)              = value; return (TRUE); }
     85
     86  // XXX is this safe for 64bit off_t and 32bit off_t?
     87  if (!strcmp (mode, "%jd"))  { *va_arg (argp, intmax_t *)           = value; return (TRUE); }
     88
     89  /* no valid mode found */
     90  return (FALSE);
     91}
     92
     93// alternate version for the special types (boolean, comments, COMMENT)
     94int gfits_vscan_alt (Header *header, char *field, char *mode, int N, va_list argp) {
     95
     96  char *p, *q, *s, tmp[81];
     97  int Nchar, status;
     98
     99  /* find the correct line with field */
     100  p = gfits_header_field (header, field, N);
     101  if (p == NULL) {
     102    status = gfits_vscan_hierarch (header, field, mode, N, argp);
     103    return (status);
     104  }
     105
     106  /* non-data entry (COMMENT, HISTORY) */
     107  if (!strcmp (mode, "%S")) {
     108    strncpy (va_arg (argp, char *), p + 8, FT_HISTORY_LENGTH);
     109    return (TRUE);
     110  }
     111
     112  /* all others require '=' in column 8 */
     113  if (p[8] != '=') return (FALSE);
     114
     115  /* comment from data line */
     116  if (!strcmp (mode, "%C")) {
     117    q = gfits_keyword_end (p) + 3;
     118    q = MIN (p + 80, q);
     119    bzero (tmp, 81);
     120    Nchar = MIN (80, p + 80 - q);
     121    memcpy (tmp, q, Nchar);
     122    stripwhite (tmp);
     123    strcpy (va_arg (argp, char *), tmp);
     124    return (TRUE);
     125  }
     126
     127  /* boolean data, requires int target */
     128  if (!strcmp (mode, "%t")) {
     129    s = gfits_keyword_start (p);
     130    if (*s == 'T') {
     131      *va_arg (argp, int *) = TRUE;
     132      return (TRUE);
     133    }
     134    if (*s == 'F') {
     135      *va_arg (argp, int *) = FALSE;   
     136      return (TRUE);
     137    }
     138  }
     139
     140  /* no valid mode found */
     141  return (FALSE);
     142}
     143
     144# define HIERARCH_LENGTH 71
     145int gfits_vscan_hierarch (Header *header, char *field, char *mode, int N, va_list argp) {
     146
     147  char *p, *q, *s, tmp[81];
     148  int Nchar, Nfield;
     149  long long value;
     150  double fvalue;
     151 
     152  /* find the correct line with field */
     153  p = gfits_header_hierarch_field (header, field, N);
     154  if (p == NULL) return (FALSE);
     155
     156  /* non-data entries (COMMENT, HISTORY) are not allowed */
     157  if (!strcmp (mode, "%S")) {
     158    return (FALSE);
     159  }
     160
     161  /* all others require '=' in column p + 1 + strlen(field) */
     162  Nfield = strlen (field);
     163  if (p[Nfield + 1] != '=') return (FALSE);
     164
     165  /* comment from data line */
     166  if (!strcmp (mode, "%C")) {
     167    q = gfits_hierarch_keyword_end (p, field);
     168    if (!q) return (FALSE);
     169    q += 3;
     170    q = MIN (p + HIERARCH_LENGTH, q);
     171    bzero (tmp, 81);
     172    Nchar = MIN (HIERARCH_LENGTH, p + HIERARCH_LENGTH - q);
     173    memcpy (tmp, q, Nchar);
     174    stripwhite (tmp);
     175    strcpy (va_arg (argp, char *), tmp);
     176    return (TRUE);
     177  }
     178
     179  /* extract data into char array (exclude containing ' chars) */
     180  if (!strcmp (mode, "%s")) {
     181    s = gfits_hierarch_keyword_start (p, field); /* points at first char (not ') */
     182    q = gfits_hierarch_keyword_end (p, field); /* points at following space or ' */
     183
     184    Nchar = MIN (HIERARCH_LENGTH, MAX (0, (q - s)));
     185    bzero (tmp, 81);
     186    memcpy (tmp, s, Nchar);
     187    stripwhite (tmp);
     188    strcpy (va_arg (argp, char *), tmp);
     189    return (TRUE);
     190  }
     191 
     192  /* boolean data, requires int target */
     193  if (!strcmp (mode, "%t")) {
     194    s = gfits_hierarch_keyword_start (p, field);
     195    if (*s == 'T') {
     196      *va_arg (argp, int *) = TRUE;
     197      return (TRUE);
     198    }
     199    if (*s == 'F') {
     200      *va_arg (argp, int *) = FALSE;   
     201      return (TRUE);
     202    }
     203  }
     204
     205  /* remaining options are numerical data */
     206  /* need to interpret 1.0d5 as 1.0e5 */
     207  s = gfits_hierarch_keyword_start (p, field); /* points at first char (not ') */
     208  q = gfits_hierarch_keyword_end (p, field); /* points at following space or ' */
    63209
    64210  if (!strcmp (mode, "%f") || !strcmp (mode, "%lf")) {
     
    84230  if (!strcmp (mode, "%hd"))  { *va_arg (argp, short *)              = value; return (TRUE); }
    85231
    86   // XXX is this safe for 64bit off_t and 32bit off_t?
    87   if (!strcmp (mode, "%jd"))  { *va_arg (argp, intmax_t *)           = value; return (TRUE); }
    88 
    89232  /* no valid mode found */
    90233  return (FALSE);
    91234}
    92235
    93 // alternate version for the special types (boolean, comments, COMMENT)
    94 int gfits_vscan_alt (Header *header, char *field, char *mode, int N, va_list argp) {
    95 
    96   char *p, *q, *s, tmp[81];
    97   int Nchar, status;
    98 
    99   /* find the correct line with field */
    100   p = gfits_header_field (header, field, N);
    101   if (p == NULL) {
    102     status = gfits_vscan_hierarch (header, field, mode, N, argp);
    103     return (status);
    104   }
    105 
    106   /* non-data entry (COMMENT, HISTORY) */
    107   if (!strcmp (mode, "%S")) {
    108     strncpy (va_arg (argp, char *), p + 8, FT_HISTORY_LENGTH);
    109     return (TRUE);
    110   }
    111 
    112   /* all others require '=' in column 8 */
    113   if (p[8] != '=') return (FALSE);
    114 
    115   /* comment from data line */
    116   if (!strcmp (mode, "%C")) {
    117     q = gfits_keyword_end (p) + 3;
    118     q = MIN (p + 80, q);
    119     bzero (tmp, 81);
    120     Nchar = MIN (80, p + 80 - q);
    121     memcpy (tmp, q, Nchar);
    122     stripwhite (tmp);
    123     strcpy (va_arg (argp, char *), tmp);
    124     return (TRUE);
    125   }
    126 
    127   /* boolean data, requires int target */
    128   if (!strcmp (mode, "%t")) {
    129     s = gfits_keyword_start (p);
    130     if (*s == 'T') {
    131       *va_arg (argp, int *) = TRUE;
    132       return (TRUE);
    133     }
    134     if (*s == 'F') {
    135       *va_arg (argp, int *) = FALSE;   
    136       return (TRUE);
    137     }
    138   }
    139 
    140   /* no valid mode found */
    141   return (FALSE);
    142 }
    143 
    144 # define HIERARCH_LENGTH 71
    145 int gfits_vscan_hierarch (Header *header, char *field, char *mode, int N, va_list argp) {
    146 
    147   char *p, *q, *s, tmp[81];
    148   int Nchar, Nfield;
    149   long long value;
    150   double fvalue;
    151  
    152   /* find the correct line with field */
    153   p = gfits_header_hierarch_field (header, field, N);
    154   if (p == NULL) return (FALSE);
    155 
    156   /* non-data entries (COMMENT, HISTORY) are not allowed */
    157   if (!strcmp (mode, "%S")) {
    158     return (FALSE);
    159   }
    160 
    161   /* all others require '=' in column p + 1 + strlen(field) */
    162   Nfield = strlen (field);
    163   if (p[Nfield + 1] != '=') return (FALSE);
    164 
    165   /* comment from data line */
    166   if (!strcmp (mode, "%C")) {
    167     q = gfits_hierarch_keyword_end (p, field);
    168     if (!q) return (FALSE);
    169     q += 3;
    170     q = MIN (p + HIERARCH_LENGTH, q);
    171     bzero (tmp, 81);
    172     Nchar = MIN (HIERARCH_LENGTH, p + HIERARCH_LENGTH - q);
    173     memcpy (tmp, q, Nchar);
    174     stripwhite (tmp);
    175     strcpy (va_arg (argp, char *), tmp);
    176     return (TRUE);
    177   }
    178 
    179   /* extract data into char array (exclude containing ' chars) */
    180   if (!strcmp (mode, "%s")) {
    181     s = gfits_hierarch_keyword_start (p, field); /* points at first char (not ') */
    182     q = gfits_hierarch_keyword_end (p, field); /* points at following space or ' */
    183 
    184     Nchar = MIN (HIERARCH_LENGTH, MAX (0, (q - s)));
    185     bzero (tmp, 81);
    186     memcpy (tmp, s, Nchar);
    187     stripwhite (tmp);
    188     strcpy (va_arg (argp, char *), tmp);
    189     return (TRUE);
    190   }
    191  
    192   /* boolean data, requires int target */
    193   if (!strcmp (mode, "%t")) {
    194     s = gfits_hierarch_keyword_start (p, field);
    195     if (*s == 'T') {
    196       *va_arg (argp, int *) = TRUE;
    197       return (TRUE);
    198     }
    199     if (*s == 'F') {
    200       *va_arg (argp, int *) = FALSE;   
    201       return (TRUE);
    202     }
    203   }
    204 
    205   /* remaining options are numerical data */
    206   /* need to interpret 1.0d5 as 1.0e5 */
    207   s = gfits_hierarch_keyword_start (p, field); /* points at first char (not ') */
    208   q = gfits_hierarch_keyword_end (p, field); /* points at following space or ' */
    209 
    210   if (!strcmp (mode, "%f") || !strcmp (mode, "%lf")) {
    211     fvalue = strtod (s, &q);
    212     if ((*q == 'd') || (*q == 'D'))
    213       fvalue *= pow (10.0, atof (q + 1));
    214 
    215     if (!strcmp (mode, "%f"))   { *va_arg (argp, float *)  = fvalue; return (TRUE); }
    216     if (!strcmp (mode, "%lf"))  { *va_arg (argp, double *) = fvalue; return (TRUE); }
    217   }
    218 
    219   value = strtoll (s, &q, 0);
    220   if ((*q == 'd') || (*q == 'D')) value *= pow (10.0, atof (q + 1));
    221 
    222   if (!strcmp (mode, "%d"))   { *va_arg (argp, int *)                = value; return (TRUE); }
    223   if (!strcmp (mode, "%ld"))  { *va_arg (argp, long *)               = value; return (TRUE); }
    224   if (!strcmp (mode, OFF_T_FMT)) { *va_arg (argp, long long *)       = value; return (TRUE); }
    225   if (!strcmp (mode, "%Ld"))  { *va_arg (argp, long long *)          = value; return (TRUE); }
    226   if (!strcmp (mode, "%u"))   { *va_arg (argp, unsigned *)           = value; return (TRUE); }
    227   if (!strcmp (mode, "%lu"))  { *va_arg (argp, unsigned long *)      = value; return (TRUE); }
    228   if (!strcmp (mode, "%llu")) { *va_arg (argp, unsigned long long *) = value; return (TRUE); }
    229   if (!strcmp (mode, "%Lu"))  { *va_arg (argp, unsigned long long *) = value; return (TRUE); }
    230   if (!strcmp (mode, "%hd"))  { *va_arg (argp, short *)              = value; return (TRUE); }
    231 
    232   /* no valid mode found */
    233   return (FALSE);
    234 }
    235 
    236236/* if the variable argument stuff breaks on another system, look
    237237at F_modify.c as it has the old code */
Note: See TracChangeset for help on using the changeset viewer.