68char *option[N_OPTIONS] = {
80 "Usage: sddsbreak [OPTIONS] [<inputfile>] [<outputfile>]\n"
83 " -pipe=[input][,output]\n"
84 " Use pipes for input and/or output.\n"
85 " -gapin=<column-name>[,{amount=<value> | factor=<value>}]\n"
86 " Break pages based on gaps in the specified column.\n"
87 " -increaseof=<column-name>[,amount=<value>[,cumulative][,reset]]\n"
88 " Break pages when the specified column increases by a certain amount.\n"
89 " -decreaseof=<column-name>[,amount=<value>[,cumulative][,reset]]\n"
90 " Break pages when the specified column decreases by a certain amount.\n"
91 " -changeof=<column-name>[,amount=<value>,base=<value>]\n"
92 " Break pages based on changes in the specified column relative to a base value.\n"
93 " -matchto=<column-name>,<pattern>[,after]\n"
94 " Break pages when a pattern is matched in the specified column.\n"
95 " -rowlimit=<integer>[,overlap=<integer>]\n"
96 " Limit the number of rows per subpage with an optional overlap.\n"
97 " -majorOrder=row|column\n"
98 " Specify the major order of data as row-major or column-major.\n"
100 "Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
102#define GAPIN_AMOUNT 0x0001U
103#define GAPIN_FACTOR 0x0002U
104char *gapinUsage =
"-gapin=<column-name>[,{amount=<value> | factor=<value>}]";
106#define CHANGEOF_AMOUNT 0x0001U
107#define CHANGEOF_BASE 0x0002U
108char *changeofUsage =
"-changeof=<column-name>[,amount=<value>,base=<value>]";
110#define INCREASEOF_AMOUNT 0x0001UL
111#define INCREASEOF_CUMULATIVE 0x0002UL
112#define INCREASEOF_RESET 0x0004UL
113char *increaseOfUsage =
"-increaseOf=<column-name>[,amount=<value>[,cumulative][,reset]]";
115#define DECREASEOF_AMOUNT 0x0001UL
116#define DECREASEOF_CUMULATIVE 0x0002UL
117#define DECREASEOF_RESET 0x0004UL
118char *decreaseOfUsage =
"-decreaseOf=<column-name>[,amount=<value>[,cumulative[,reset]]]";
120#define ROWLIMIT_OVERLAP 0x0001U
122int main(
int argc,
char **argv) {
126 char *input = NULL, *output = NULL, *columnName = NULL;
127 long mode = -1, matchCode, tmpfile_used;
128 int64_t i, j, rows, rowLimit;
130 double gapAmount = 0, gapFactor = 0, changeAmount, changeBase, *columnData = NULL;
131 char *matchPattern = NULL;
132 long matchPatternAfter = 0;
133 double increaseOfAmount = -1, decreaseOfAmount = -1;
137 unsigned long flags = 0, pipeFlags = 0, changeFlags = 0, decreaseOfFlags = 0, increaseOfFlags = 0;
138 unsigned long majorOrderFlag;
139 char **stringData = NULL;
140 short columnMajorOrder = -1;
143 argc =
scanargs(&scArg, argc, argv);
145 fprintf(stderr,
"%s", USAGE);
149 for (iArg = 1; iArg < argc; iArg++) {
150 if (scArg[iArg].arg_type == OPTION) {
151 switch (matchCode =
match_string(scArg[iArg].list[0], option, N_OPTIONS, 0)) {
152 case SET_MAJOR_ORDER:
154 scArg[iArg].n_items -= 1;
155 if (scArg[iArg].n_items > 0 &&
156 !
scanItemList(&majorOrderFlag, scArg[iArg].list + 1, &scArg[iArg].n_items, 0,
157 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
158 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)) {
159 SDDS_Bomb(
"invalid -majorOrder syntax/values");
161 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
162 columnMajorOrder = 1;
163 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
164 columnMajorOrder = 0;
168 if ((scArg[iArg].n_items -= 2) < 0 ||
169 !
scanItemList(&flags, scArg[iArg].list + 2, &scArg[iArg].n_items, 0,
170 "amount",
SDDS_DOUBLE, &gapAmount, 1, GAPIN_AMOUNT,
171 "factor",
SDDS_DOUBLE, &gapFactor, 1, GAPIN_FACTOR, NULL) ||
172 (flags & GAPIN_AMOUNT && gapAmount <= 0) ||
173 (flags & GAPIN_FACTOR && gapFactor <= 0)) {
174 fprintf(stderr,
"Error: invalid -gapin syntax/values\n");
177 columnName = scArg[iArg].list[1];
183 if (scArg[iArg].n_items < 2) {
184 fprintf(stderr,
"Error: invalid -increaseOf syntax\n");
187 scArg[iArg].n_items -= 2;
188 if (!
scanItemList(&increaseOfFlags, scArg[iArg].list + 2, &scArg[iArg].n_items, 0,
189 "amount",
SDDS_DOUBLE, &increaseOfAmount, 1, INCREASEOF_AMOUNT,
190 "cumulative", -1, NULL, 0, INCREASEOF_CUMULATIVE,
191 "reset", -1, NULL, 0, INCREASEOF_RESET, NULL) ||
192 ((flags & INCREASEOF_AMOUNT) && increaseOfAmount <= 0)) {
193 fprintf(stderr,
"Error: invalid -increaseOf syntax\n");
196 columnName = scArg[iArg].list[1];
202 if (scArg[iArg].n_items < 2) {
203 fprintf(stderr,
"Error: invalid -decreaseOf syntax\n");
206 scArg[iArg].n_items -= 2;
207 if (!
scanItemList(&decreaseOfFlags, scArg[iArg].list + 2, &scArg[iArg].n_items, 0,
208 "amount",
SDDS_DOUBLE, &decreaseOfAmount, 1, DECREASEOF_AMOUNT,
209 "cumulative", -1, NULL, 0, DECREASEOF_CUMULATIVE,
210 "reset", -1, NULL, 0, DECREASEOF_RESET, NULL) ||
211 ((flags & DECREASEOF_AMOUNT) && decreaseOfAmount <= 0)) {
212 fprintf(stderr,
"Error: invalid -decreaseOf syntax\n");
215 columnName = scArg[iArg].list[1];
220 if ((scArg[iArg].n_items -= 2) < 0 ||
221 !
scanItemList(&changeFlags, scArg[iArg].list + 2, &scArg[iArg].n_items, 0,
222 "amount",
SDDS_DOUBLE, &changeAmount, 1, CHANGEOF_AMOUNT,
223 "base",
SDDS_DOUBLE, &changeBase, 1, CHANGEOF_BASE, NULL) ||
224 (changeFlags & CHANGEOF_AMOUNT && changeAmount <= 0)) {
225 fprintf(stderr,
"Error: invalid -changeof syntax/values\n");
228 columnName = scArg[iArg].list[1];
233 if (scArg[iArg].n_items < 2) {
234 fprintf(stderr,
"Error: invalid -rowlimit syntax\n");
237 if (sscanf(scArg[iArg].list[1],
"%" SCNd64, &rowLimit) != 1 || rowLimit <= 0) {
238 fprintf(stderr,
"Error: invalid -rowlimit syntax\n");
241 if (scArg[iArg].n_items > 2) {
242 scArg[iArg].n_items -= 2;
243 if (!
scanItemList(&flags, scArg[iArg].list + 2, &scArg[iArg].n_items, 0,
244 "overlap",
SDDS_LONG, &overlap, 1, ROWLIMIT_OVERLAP, NULL) ||
246 fprintf(stderr,
"Error: invalid overlap given in -rowlimit syntax\n");
254 if (!
processPipeOption(scArg[iArg].list + 1, scArg[iArg].n_items - 1, &pipeFlags)) {
255 fprintf(stderr,
"Error: invalid -pipe syntax\n");
261 if ((scArg[iArg].n_items != 3 && scArg[iArg].n_items != 4) ||
262 strlen(columnName = scArg[iArg].list[1]) == 0 ||
263 strlen(matchPattern = scArg[iArg].list[2]) == 0) {
264 fprintf(stderr,
"Error: invalid -matchTo syntax\n");
267 if (scArg[iArg].n_items == 4) {
268 if (strncmp(scArg[iArg].list[3],
"after", strlen(scArg[iArg].list[3])) == 0)
269 matchPatternAfter = 1;
271 fprintf(stderr,
"Error: invalid -matchTo syntax\n");
279 fprintf(stderr,
"Error: unknown switch: %s\n", scArg[iArg].list[0]);
280 fprintf(stderr,
"%s", USAGE);
285 input = scArg[iArg].list[0];
286 else if (output == NULL)
287 output = scArg[iArg].list[0];
289 fprintf(stderr,
"Error: too many filenames given\n");
295 processFilenames(
"sddsbreak", &input, &output, pipeFlags, 0, &tmpfile_used);
298 fprintf(stderr,
"Error: no break mode specified\n");
308 SDDSnew.layout.data_mode.no_row_counts = 0;
309 if (columnMajorOrder != -1)
310 SDDSnew.layout.data_mode.column_major = columnMajorOrder;
312 SDDSnew.layout.data_mode.column_major = SDDSold.layout.data_mode.column_major;
319 if (mode != SET_ROWLIMIT) {
321 SDDS_SetError(
"Problem getting type information on given column");
325 if (mode == SET_MATCHTO) {
327 fprintf(stderr,
"Error: given column does not contain string data\n");
331 if (!(mode == SET_CHANGEOF && !(changeFlags & CHANGEOF_AMOUNT) && !(changeFlags & CHANGEOF_BASE))) {
332 fprintf(stderr,
"Error: given column does not contain numeric data\n");
340 SDDS_SetError(
"Problem getting number of rows of tabular data");
366 if (!gapAmount && rows > 1) {
367 double *gap =
tmalloc(
sizeof(*gap) * rows);
368 for (i = 1; i < rows; i++)
369 gap[i - 1] = fabs(columnData[i] - columnData[i - 1]);
371 fprintf(stderr,
"Error: unable to determine default gap amount--couldn't find median gap\n");
375 gapAmount *= (gapFactor ? gapFactor : 2);
379 int64_t newStart = 0;
380 for (i = 1; i <= rows; i++) {
381 if (i != rows && fabs(columnData[i] - columnData[i - 1]) < gapAmount)
403 int64_t newStart = 0;
404 if (increaseOfAmount <= 0) {
405 for (i = 1; i <= rows; i++) {
406 if (i != rows && columnData[i] <= columnData[i - 1])
417 if (increaseOfFlags & INCREASEOF_CUMULATIVE) {
419 for (i = 1; i <= rows; i++) {
420 if ((increaseOfFlags & INCREASEOF_RESET) && columnData[i] < columnData[iref])
422 if (i != rows && (columnData[i] - columnData[iref]) < increaseOfAmount)
431 if (increaseOfFlags & INCREASEOF_CUMULATIVE)
435 for (i = 1; i <= rows; i++) {
436 if (i != rows && (columnData[i] - columnData[i - 1]) < increaseOfAmount)
460 int64_t newStart = 0;
461 if (decreaseOfAmount <= 0) {
462 for (i = 1; i <= rows; i++) {
463 if (i != rows && columnData[i] >= columnData[i - 1])
474 if (decreaseOfFlags & DECREASEOF_CUMULATIVE) {
476 for (i = 1; i <= rows; i++) {
477 if ((decreaseOfFlags & DECREASEOF_RESET) && columnData[i] > columnData[iref])
479 if (i != rows && (columnData[iref] - columnData[i]) < decreaseOfAmount)
488 if (decreaseOfFlags & DECREASEOF_CUMULATIVE)
492 for (i = 1; i <= rows; i++) {
493 if (i != rows && (columnData[i - 1] - columnData[i]) < decreaseOfAmount)
526 int64_t newStart = 0;
528 for (i = 1; i <= rows; i++) {
530 ((dataType ==
SDDS_STRING && strcmp(stringData[i], stringData[i - 1]) == 0) ||
531 (dataType !=
SDDS_STRING && columnData[i] == columnData[i - 1])))
545 long region = 0, lastRegion = 0;
546 if (!(changeFlags & CHANGEOF_BASE) && rows >= 1)
547 changeBase = columnData[0];
549 lastRegion = (columnData[0] - changeBase) / changeAmount;
552 for (i = 1; i <= rows; i++) {
554 region = (columnData[i] - changeBase) / changeAmount;
555 if (i != rows && region == lastRegion)
585 int64_t newStart = 0;
587 for (i = 1; i <= rows; i++) {
588 if (i != rows && !breakNext) {
589 if (
wild_match(stringData[i], matchPattern)) {
590 if (matchPatternAfter) {
612 for (i = 0; i < rows; i += rowLimit - overlap) {
613 if ((j = i + rowLimit - 1) >= rows)
629 fprintf(stderr,
"Error: unknown break mode code seen---this can't happen\n");
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_CopyParameters(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode)
int32_t SDDS_CopyArrays(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_CopyRows(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, int64_t firstRow, int64_t lastRow)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_GetColumnInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Retrieves information about a specified column in the SDDS dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_FreeStringArray(char **string, int64_t strings)
Frees an array of strings by deallocating each individual string.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_DOUBLE
Identifier for the double data type.
#define SDDS_NUMERIC_TYPE(type)
Checks if the given type identifier corresponds to any numeric type.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
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.
long replaceFileAndBackUp(char *file, char *replacement)
Replaces a file with a replacement file and creates a backup of the original.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
long processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.
int wild_match(char *string, char *template)
Determine whether one string is a wildcard match for another.