GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/unrrdu/cksum.c Lines: 13 38 34.2 %
Date: 2017-05-26 Branches: 1 16 6.2 %

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 "Compute 32-bit CRC of nrrd data (same as via \"cksum\")"
28
static const char *_unrrdu_cksumInfoL =
29
(INFO ". Unlike other commands, this doesn't produce a nrrd.  It only "
30
 "prints to standard out the CRC and byte counts for the input nrrd(s), "
31
 "seeking to emulate the formatting of cksum output.\n "
32
 "* Uses nrrdCRC32");
33
34
int
35
unrrdu_cksumDoit(const char *me, char *inS, int endian,
36
                 int printendian, FILE *fout) {
37
  Nrrd *nrrd;
38
  airArray *mop;
39
  unsigned int crc;
40
  char stmp[AIR_STRLEN_SMALL], ends[AIR_STRLEN_SMALL];
41
  size_t nn;
42
43
  mop = airMopNew();
44
  airMopAdd(mop, nrrd=nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
45
  if (nrrdLoad(nrrd, inS, NULL)) {
46
    biffMovef(me, NRRD, "%s: trouble loading \"%s\"", me, inS);
47
    airMopError(mop); return 1;
48
  }
49
  crc = nrrdCRC32(nrrd, endian);
50
  nn = nrrdElementNumber(nrrd)*nrrdElementSize(nrrd);
51
  sprintf(ends, "(%s)", airEnumStr(airEndian, endian));
52
  fprintf(fout, "%u%s %s%s%s\n", crc,
53
          printendian ? ends : "",
54
          airSprintSize_t(stmp, nn),
55
          strcmp("-", inS) ? " " : "",
56
          strcmp("-", inS) ? inS : "");
57
58
  airMopOkay(mop);
59
  return 0;
60
}
61
62
int
63
unrrdu_cksumMain(int argc, const char **argv, const char *me,
64
                 hestParm *hparm) {
65
2
  hestOpt *opt = NULL;
66
1
  char *err, **inS;
67
  airArray *mop;
68
1
  int pret, endian, printend;
69
1
  unsigned int ni, ninLen;
70
71
1
  mop = airMopNew();
72
1
  hestOptAdd(&opt, "en,endian", "end", airTypeEnum, 1, 1, &endian,
73
1
             airEnumStr(airEndian, airMyEndian()),
74
             "Endianness in which to compute CRC; \"little\" for Intel and "
75
             "friends; \"big\" for everyone else. "
76
             "Defaults to endianness of this machine",
77
1
             NULL, airEndian);
78
1
  hestOptAdd(&opt, "pen,printendian", "bool", airTypeBool, 1, 1, &printend,
79
             "false",
80
             "whether or not to indicate after the CRC value the endianness "
81
             "with which the CRC was computed; doing so clarifies "
82
             "that the CRC result depends on endianness and may remove "
83
             "confusion in comparing results on platforms of different "
84
             "endianness");
85
1
  hestOptAdd(&opt, NULL, "nin1", airTypeString, 1, -1, &inS, NULL,
86
             "input nrrd(s)", &ninLen);
87
1
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
88
89
2
  USAGE(_unrrdu_cksumInfoL);
90
  PARSE();
91
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
92
93
  for (ni=0; ni<ninLen; ni++) {
94
    if (unrrdu_cksumDoit(me, inS[ni], endian, printend, stdout)) {
95
      airMopAdd(mop, err = biffGetDone(me), airFree, airMopAlways);
96
      fprintf(stderr, "%s: trouble with \"%s\":\n%s",
97
              me, inS[ni], err);
98
      /* continue working on the remaining files */
99
    }
100
  }
101
102
  airMopOkay(mop);
103
  return 0;
104
1
}
105
106
UNRRDU_CMD(cksum, INFO);