SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
sddscheck.c
Go to the documentation of this file.
1/**
2 * @file sddscheck.c
3 * @brief Validates and checks an SDDS file for corruption or issues.
4 *
5 * @details
6 * This program reads an SDDS (Self Describing Data Set) file and determines its validity.
7 * It processes the file by verifying its structure, pages, and data, and outputs the status:
8 * - `"ok"` if the file is valid.
9 * - `"nonexistent"` if the file does not exist.
10 * - `"badHeader"` if the file has an invalid header.
11 * - `"corrupted"` if the file contains errors.
12 *
13 * @section Usage
14 * ```
15 * sddscheck <filename> [-printErrors]
16 * ```
17 *
18 * @section Options
19 * | Option | Description |
20 * |---------------------------------------|---------------------------------------------------------------------------------------|
21 * | `-printErrors` | Outputs detailed error messages to stderr. |
22 *
23 * @copyright
24 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
25 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
26 *
27 * @license
28 * This file is distributed under the terms of the Software License Agreement
29 * found in the file LICENSE included with this distribution.
30 *
31 * @author
32 * M. Borland, C. Saunders, R. Soliday
33 */
34
35#include "mdb.h"
36#include "SDDS.h"
37#include "scan.h"
38
39typedef enum {
40 CLO_PRINTERRORS = 0
41} OptionType;
42
43#define N_OPTIONS 1
44
45static char *option[N_OPTIONS] = {
46 "printErrors"
47};
48
49char *usage =
50 "sddscheck <filename> [-printErrors]\n\n"
51 "This program allows you to determine whether an SDDS file has been\n"
52 "corrupted. It reads the entire file and prints a message to stdout.\n"
53 "\n"
54 "If the file is ok, \"ok\" is printed.\n"
55 "If the file has a problem, one of the following will be printed:\n"
56 " - \"nonexistent\": The file does not exist.\n"
57 " - \"badHeader\": The file header is invalid.\n"
58 " - \"corrupted\": The file contains errors.\n"
59 "\n"
60 "Options:\n"
61 " -printErrors: Deliver error messages to stderr.\n"
62 "\n"
63 "Program by Michael Borland. (" __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION ")\n";
64
65int main(int argc, char **argv) {
66 SDDS_DATASET SDDS_input;
67 char *input;
68 long i_arg, retval, print_errors;
69 SCANNED_ARG *s_arg;
70
71 /* Register the program name for error reporting. */
73
74 /* Parse command-line arguments. */
75 argc = scanargs(&s_arg, argc, argv);
76 if (!s_arg || argc < 2) {
77 bomb(NULL, usage); /* Display usage and exit if arguments are insufficient. */
78 }
79
80 input = NULL;
81 print_errors = 0;
82
83 /* Process each command-line argument. */
84 for (i_arg = 1; i_arg < argc; i_arg++) {
85 if (s_arg[i_arg].arg_type == OPTION) {
86 /* Match recognized options. */
87 switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
88 case CLO_PRINTERRORS:
89 print_errors = 1;
90 break;
91 default:
92 SDDS_Bomb("unknown option given"); /* Handle unrecognized options. */
93 break;
94 }
95 } else {
96 /* Assign the first non-option argument as the input file name. */
97 if (input == NULL)
98 input = s_arg[i_arg].list[0];
99 else
100 SDDS_Bomb("too many filenames"); /* Ensure only one input file is specified. */
101 }
102 }
103
104 /* Check if the input file exists. */
105 if (!fexists(input)) {
106 puts("nonexistent"); /* Indicate file does not exist. */
107 exit(0);
108 }
109
110 /* Initialize the SDDS input file. */
111 if (!SDDS_InitializeInput(&SDDS_input, input)) {
112 puts("badHeader"); /* Indicate the file header is invalid. */
113 if (print_errors)
114 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); /* Print detailed errors if enabled. */
115 exit(0);
116 }
117
118 /* Read and process each page of the SDDS file. */
119 while ((retval = SDDS_ReadPage(&SDDS_input)) > 0) {
120 /* Loop continues until EOF or an error occurs. */
121 }
122
123 if (retval == -1) {
124 /* EOF reached successfully. */
125 puts("ok");
126 } else {
127 /* Handle file corruption or errors during processing. */
128 if (print_errors)
129 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
130 puts("corrupted");
131 }
132
133 return (0); /* Exit successfully. */
134}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_InitializeInput(SDDS_DATASET *SDDS_dataset, char *filename)
Definition SDDS_input.c:49
int32_t SDDS_ReadPage(SDDS_DATASET *SDDS_dataset)
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
Definition SDDS_utils.c:432
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
Definition SDDS_utils.c:288
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
Definition SDDS_utils.c:342
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
long fexists(const char *filename)
Checks if a file exists.
Definition fexists.c:27
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
Definition scanargs.c:36