GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/unrrdu/head.c Lines: 8 49 16.3 %
Date: 2017-05-26 Branches: 1 34 2.9 %

Line Branch Exec Source
1
/*
2
  Teem: Tools to process and visualize scientific data and images             .
3
  Copyright (C) 2013, 2012, 2011, 2010, 2009  University of Chicago
4
  Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
5
  Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah
6
7
  This library is free software; you can redistribute it and/or
8
  modify it under the terms of the GNU Lesser General Public License
9
  (LGPL) as published by the Free Software Foundation; either
10
  version 2.1 of the License, or (at your option) any later version.
11
  The terms of redistributing and/or modifying this software also
12
  include exceptions to the LGPL that facilitate static linking.
13
14
  This library is distributed in the hope that it will be useful,
15
  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
  Lesser General Public License for more details.
18
19
  You should have received a copy of the GNU Lesser General Public License
20
  along with this library; if not, write to Free Software Foundation, Inc.,
21
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22
*/
23
24
#include "unrrdu.h"
25
#include "privateUnrrdu.h"
26
27
#define INFO "Print header of one or more nrrd files"
28
static const char *_unrrdu_headInfoL =
29
(INFO  ".  The value of this is simply to print the contents of nrrd "
30
 "headers.  This avoids the use of \"head -N\", where N has to be "
31
 "determined manually, which always risks printing raw binary data "
32
 "(following the header) to screen, which tends to clobber terminal "
33
 "settings, make pointless beeps, and be annoying.\n "
34
 "* Uses _nrrdOneLine");
35
36
int
37
unrrdu_headDoit(const char *me, NrrdIoState *nio, char *inS, FILE *fout) {
38
  airArray *mop;
39
  unsigned int len;
40
  FILE *fin;
41
42
  mop = airMopNew();
43
  if (!( fin = airFopen(inS, stdin, "rb") )) {
44
    biffAddf(me, "%s: couldn't fopen(\"%s\",\"rb\"): %s\n",
45
             me, inS, strerror(errno));
46
    airMopError(mop); return 1;
47
  }
48
  airMopAdd(mop, fin, (airMopper)airFclose, airMopAlways);
49
50
  if (_nrrdOneLine(&len, nio, fin)) {
51
    biffAddf(me, "%s: error getting first line of file \"%s\"", me, inS);
52
    airMopError(mop); return 1;
53
  }
54
  if (!len) {
55
    biffAddf(me, "%s: immediately hit EOF\n", me);
56
    airMopError(mop); return 1;
57
  }
58
  if (!( nrrdFormatNRRD->contentStartsLike(nio) )) {
59
    biffAddf(me, "%s: first line (\"%s\") isn't a nrrd magic\n",
60
             me, nio->line);
61
    airMopError(mop); return 1;
62
  }
63
  while (len > 1) {
64
    fprintf(fout, "%s\n", nio->line);
65
    _nrrdOneLine(&len, nio, fin);
66
  };
67
68
  /* experience has shown that on at least windows and darwin, the writing
69
     process's fwrite() to stdout will fail if we exit without consuming
70
     everything from stdin */
71
  if (stdin == fin) {
72
    int c = getc(fin);
73
    while (EOF != c) {
74
      c = getc(fin);
75
    }
76
  }
77
78
  airMopOkay(mop);
79
  return 0;
80
}
81
82
int
83
unrrdu_headMain(int argc, const char **argv, const char *me,
84
                hestParm *hparm) {
85
2
  hestOpt *opt = NULL;
86
1
  char *err, **inS;
87
  NrrdIoState *nio;
88
  airArray *mop;
89
  int pret;
90
1
  unsigned int ni, ninLen;
91
92
1
  mop = airMopNew();
93
1
  hestOptAdd(&opt, NULL, "nin1", airTypeString, 1, -1, &inS, NULL,
94
             "input nrrd(s)", &ninLen);
95
1
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
96
97
2
  USAGE(_unrrdu_headInfoL);
98
  PARSE();
99
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
100
101
  nio = nrrdIoStateNew();
102
  airMopAdd(mop, nio, (airMopper)nrrdIoStateNix, airMopAlways);
103
104
  for (ni=0; ni<ninLen; ni++) {
105
    if (ninLen > 1) {
106
      fprintf(stdout, "==> %s <==\n", inS[ni]);
107
    }
108
    if (unrrdu_headDoit(me, nio, inS[ni], stdout)) {
109
      airMopAdd(mop, err = biffGetDone(me), airFree, airMopAlways);
110
      fprintf(stderr, "%s: trouble reading from \"%s\":\n%s",
111
              me, inS[ni], err);
112
      /* continue working on the remaining files */
113
    }
114
    if (ninLen > 1 && ni < ninLen-1) {
115
      fprintf(stdout, "\n");
116
    }
117
  }
118
119
  airMopOkay(mop);
120
  return 0;
121
1
}
122
123
UNRRDU_CMD(head, INFO);