GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/unrrdu/basinfo.c Lines: 17 61 27.9 %
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 "Modify whole-array attributes (not per-axis)"
28
static const char *_unrrdu_basinfoInfoL =
29
(INFO
30
 ", which is called \"basic info\" in Nrrd terminology. "
31
 "The only attributes which are set are those for which command-line "
32
 "options are given.\n "
33
 "* Uses no particular function; just sets fields in the Nrrd");
34
35
int
36
unrrdu_basinfoMain(int argc, const char **argv, const char *me,
37
                   hestParm *hparm) {
38
  /* these are stock for unrrdu */
39
2
  hestOpt *opt = NULL;
40
  airArray *mop;
41
  int pret;
42
1
  char *err;
43
  /* these are stock for things using the usual -i and -o */
44
1
  char *out;
45
1
  Nrrd *nin, *nout;
46
  /* these are specific to this command */
47
  NrrdIoState *nio;
48
1
  char *spcStr, *_origStr, *origStr, *content;
49
  int space;
50
1
  unsigned int spaceDim, cIdx;
51
52
1
  hestOptAdd(&opt, "spc,space", "space", airTypeString, 1, 1, &spcStr, "",
53
             "identify the space (e.g. \"RAS\", \"LPS\") in which the array "
54
             "conceptually lives, from the nrrdSpace airEnum, which in turn "
55
             "determines the dimension of the space.  Or, use an integer>0 to"
56
             "give the dimension of a space that nrrdSpace doesn't know about. "
57
             "By default (not using this option), the enclosing space is "
58
             "set as unknown.");
59
1
  hestOptAdd(&opt, "orig,origin", "origin", airTypeString, 1, 1, &_origStr, "",
60
             "(NOTE: must quote vector) the origin in space of the array: "
61
             "the location of the center "
62
             "of the first sample, of the form \"(x,y,z)\" (or however "
63
             "many coefficients are needed for the chosen space). Quoting the "
64
             "vector is needed to stop interpretation from the shell");
65
  cIdx =
66
1
  hestOptAdd(&opt, "c,content", "content", airTypeString, 1, 1, &content, "",
67
             "Specifies the content string of the nrrd, which is built upon "
68
             "by many nrrd function to record a history of operations");
69
1
  OPT_ADD_NIN(nin, "input nrrd");
70
1
  OPT_ADD_NOUT(out, "output nrrd");
71
72
1
  mop = airMopNew();
73
1
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
74
1
  nio = nrrdIoStateNew();
75
1
  airMopAdd(mop, nio, (airMopper)nrrdIoStateNix, airMopAlways);
76
77
2
  USAGE(_unrrdu_basinfoInfoL);
78
  PARSE();
79
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
80
81
  nout = nrrdNew();
82
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
83
  if (nrrdCopy(nout, nin)) {
84
    airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
85
    fprintf(stderr, "%s: error copying input:\n%s", me, err);
86
    airMopError(mop);
87
    return 1;
88
  }
89
90
  /* HEY: copy and paste from unrrdu/make.c */
91
  if (airStrlen(spcStr)) {
92
    space = airEnumVal(nrrdSpace, spcStr);
93
    if (!space) {
94
      /* couldn't parse it as space, perhaps its a uint */
95
      if (1 != sscanf(spcStr, "%u", &spaceDim)) {
96
        fprintf(stderr, "%s: couldn't parse \"%s\" as a nrrdSpace "
97
                "or as a uint", me, spcStr);
98
        airMopError(mop); return 1;
99
      }
100
      /* else we did parse it as a uint */
101
      nout->space = nrrdSpaceUnknown;
102
      nout->spaceDim = spaceDim;
103
    } else {
104
      /* we did parse a known space */
105
      nrrdSpaceSet(nout, space);
106
    }
107
  }
108
109
  /* HEY: copy and paste from unrrdu/make.c */
110
  if (airStrlen(content)) { /* must have come from user */
111
    if (nout->content) {
112
      free(nout->content);
113
    }
114
    nout->content = airStrdup(content);
115
  } else if (hestSourceUser == opt[cIdx].source) {
116
    /* else user actually said: -c "" */
117
    nout->content = (char *)airFree(nout->content);
118
  } /* else option not used */
119
120
  /* HEY: copy and paste from unrrdu/make.c */
121
  if (airStrlen(_origStr)) {
122
    /* why this is necessary is a bit confusing to me, both the check for
123
       enclosing quotes, and the need to use to a separate variable (isn't
124
       hest doing memory management of addresses, not variables?) */
125
    if ('\"' == _origStr[0] && '\"' == _origStr[strlen(_origStr)-1]) {
126
      _origStr[strlen(_origStr)-1] = 0;
127
      origStr = _origStr + 1;
128
    } else {
129
      origStr = _origStr;
130
    }
131
    /* same hack about using NrrdIoState->line as basis for parsing */
132
    nio->line = origStr;
133
    nio->pos = 0;
134
    if (nrrdFieldInfoParse[nrrdField_space_origin](NULL, nout,
135
                                                   nio, AIR_TRUE)) {
136
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
137
      fprintf(stderr, "%s: trouble with origin \"%s\":\n%s",
138
              me, origStr, err);
139
      nio->line = NULL; airMopError(mop); return 1;
140
    }
141
    nio->line = NULL;
142
  }
143
144
  SAVE(out, nout, NULL);
145
146
  airMopOkay(mop);
147
  return 0;
148
1
}
149
150
UNRRDU_CMD(basinfo, INFO);