114char *option[N_OPTIONS] = {
138 "sddsprintout [<SDDSinput>] [<outputfile>]\n"
139 " [-pipe=[input][,output]] \n"
140 " [-columns[=<name-list>[,format={<string>|@<columnName>}][,label=<string>][,editLabel=<command>][,useDefaultFormat][,endsline][,blankLines=<number>]][,factor=<value>][,nounits]] \n"
141 " [-parameters[=<name-list>[,format={<string>|@<parameterName>}][,label=<string>][,editLabel=<command>][,useDefaultFormat][,endsline][,blankLines=<number]][,factor=<value>]] \n"
142 " [-array[=<name-list>[,format=<string>]] \n"
143 " [-fromPage=<number>] \n"
144 " [-toPage=<number>] \n"
145 " [-formatDefaults=<SDDStype>=<format-string>[,...]]\n"
146 " [-width=<integer>] \n"
148 " [-paginate[=lines=<number>][,notitle][,nolabels]]\n"
150 " [-title=<string>] \n"
152 " [-postPageLines=<number>]\n"
153 " [-spreadsheet[=delimiter=<string>][,quotemark=<string>][,nolabels][,csv][,schfile=<filename>]]\n"
154 " [-latexFormat[=longtable][,booktable][,sideways][,label={<string>|@<parameterName>}][,caption={<string>|@<parameterName>}][,group=<columnName>][,translate=<filename>][,justify=<codeList>][,complete][,comment=<string>]]\n"
155 " [-htmlFormat[=caption=<string>][,translate=<filename>]]\n"
159 "-spreadsheet=csv is the simple way of -spreadsheet=nolabels,quote=\",delimiter=\\, -notitle \n"
160 "Translation file for LaTeX mode has columns OldName and NewName, and can be used to translate symbols and units.\n\n"
163 "Program by Michael Borland. (\"" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
165#define PAGINATION_ON 0x0001U
166#define PAGINATION_NOTITLE 0x0002U
167#define PAGINATION_NOLABELS 0x0004U
169#define LATEX_FORMAT 0x0001UL
170#define LATEX_LONGTABLE 0x0002UL
171#define LATEX_BOOKTABLE 0x0004UL
172#define LATEX_LABEL 0x0008UL
173#define LATEX_CAPTION 0x0010UL
174#define LATEX_GROUP 0x0020UL
175#define LATEX_TRANSLATE 0x0040UL
176#define LATEX_SIDEWAYS 0x0080UL
177#define LATEX_JUSTIFY 0x0100UL
178#define LATEX_COMPLETE 0x0200UL
179#define LATEX_COMMENT 0x0400UL
180#define LATEX_LABEL_PARAM 0x0800UL
181#define LATEX_CAPTION_PARAM 0x1000UL
183#define HTML_FORMAT 0x0001U
184#define HTML_CAPTION 0x0002U
185#define HTML_TRANSLATE 0x0004U
187#define MARKDOWN_FORMAT 0x0001U
196#define ENDSLINE 0x0001U
197#define USEDEFAULTFORMAT 0x0002U
198#define LABEL_GIVEN 0x0004U
199#define EDITLABEL_GIVEN 0x0008U
200#define FACTOR_GIVEN 0x0010U
201#define NO_UNITS 0x0020U
205 char *name, *format, **header, *label, *editLabel;
206 long fieldWidth, index, headers, type, endsLine, blankLines, useDefaultFormat, noUnits;
213 char *name, *format, *label, *editLabel;
214 long fieldWidth, index, type, endsLine, blankLines, useDefaultFormat;
221 char *name, *format, *label;
226char *makeTexSafeString(
char *source);
227char *makeTexExponentialString(
char *text);
228void copyAndPad(
char *target,
char *source,
long sourceWidth,
long targetWidth);
229long makeColumnHeaders(
char ***header,
long *fieldWidth,
char *name,
char *editLabel,
char *units,
double factor,
230 char **format,
unsigned long spreadsheetFlags,
unsigned long latexFormat, htab *translationTable,
unsigned long htmlFormat);
231char *makeParameterLabel(
long *fieldWidth,
char *name,
char *editLabel,
char *units,
double factor,
char *format);
232long changeDefaultFormats(
char **argv,
long argc,
long noWarnings);
233void setDefaultFormats(
void);
235 unsigned long spreadsheetFlags,
long csv,
unsigned long latexformat, htab *translationTable,
unsigned long htmlformat);
236void printColumnHeaders(FILE *fpOut,
PRINT_COLUMN *printColumn,
long printColumns,
long width,
PAGINATION *pagination,
237 long latexFormat,
char *latexTitle,
long htmlFormat,
char *htmlTitle,
long markdownFormat);
239 long noWarnings,
long noLabels,
long csv);
241long getFormatFieldLength(
char *format,
long *extraChars);
242char **makeListOfNames(
char *
string,
long *names);
244 FILE *fpOut,
unsigned long spreadsheetFlags,
char *spreadsheetDelimiter,
245 char *spreadsheetQuoteMark,
PAGINATION *pagination,
char *title,
long noLabels);
246long characterCount(
char *
string,
char c);
247long printPageTitle(FILE *fpOut,
char *title);
249 long width, FILE *fpOut,
unsigned long spreadsheetFlags,
char *spreadsheetDelimiter,
char *spreadsheetQuoteMark,
250 long latexFormat,
char *latexTitle,
char *latexLabel,
char *latexGroupColumn,
251 long htmlFormat,
char *htmlTitle,
long markdownFormat,
252 PAGINATION *pagination,
char *title,
long noLabels);
253long checkPagination(FILE *fpOut,
PAGINATION *pagination,
char *title);
254void replaceFormatWidth(
char *buffer,
char *format,
long width);
255void CreateSCHFile(
char *output,
char *input,
unsigned long flags,
char *delimiter,
char *quote,
257htab *readTranslationTable(
char *TranslationFile);
258char *findTranslation(htab *ht,
char *key);
259char *modifyUnitsWithFactor(
char *units0,
double factor,
unsigned long latexFormat);
260char *makeMarkdownSafeString(
char *source);
262static char **defaultFormat = NULL;
263static char **csvFormat = NULL;
264static char *latexJustify = NULL;
266#define SPREADSHEET_ON 0x0001U
267#define SPREADSHEET_DELIMITER 0x0002U
268#define SPREADSHEET_QUOTEMARK 0x0004U
269#define SPREADSHEET_NOLABELS 0x0008U
270#define SPREADSHEET_CSV 0x0010UL
272int main(
int argc,
char **argv) {
274 long i, i_arg, pageNumber, width;
276 char *input, *output, **name, *format, *title, *label, *editLabel, *schFile;
280 long printColumns, printParameters, printArrays, firstPage, bufferLines;
281 long fromPage, toPage, names, pageAdvance, noTitle, noLabels, noWarnings;
283 unsigned long latexFormat, htmlFormat, markdownFormat;
285 unsigned long flags, pipeFlags, spreadsheetFlags, dummyFlags, postPageLines, CSV = 0;
286 char *spreadsheetDelimiter, *spreadsheetQuoteMark;
288 char **formatDefaultArg;
289 long formatDefaultArgs;
290 static char formatbuffer[100], formatbuffer2[100];
291 char *latexLabel, *latexCaption = NULL, *latexGroup, *latexTranslationFile, *latexComment = NULL;
292 char *latexCaptionBuffer = NULL, *latexLabelBuffer = NULL;
293 htab *TranslationTable;
294 char *htmlCaption = NULL, *htmlTranslationFile;
298 argc =
scanargs(&s_arg, argc, argv);
302 input = output = NULL;
303 fromPage = toPage = 0;
305 printParameter = NULL;
309 printColumns = printParameters = printArrays = pipeFlags = flags = 0;
310 postPageLines = pageAdvance = noTitle = noLabels = 0;
312 spreadsheetFlags = 0;
313 pagination.flags = pagination.currentLine = 0;
315 formatDefaultArgs = 0;
317 formatDefaultArg = NULL;
320 TranslationTable = NULL;
324 for (i_arg = 1; i_arg < argc; i_arg++) {
325 if (s_arg[i_arg].arg_type == OPTION) {
326 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
328 if (s_arg[i_arg].n_items < 2) {
329 s_arg[i_arg].n_items = 2;
330 s_arg[i_arg].list =
SDDS_Realloc(s_arg[i_arg].list,
sizeof(*s_arg[i_arg].list) * 2);
333 name = makeListOfNames(s_arg[i_arg].list[1], &names);
334 printColumn =
SDDS_Realloc(printColumn,
sizeof(*printColumn) * (names + printColumns));
335 s_arg[i_arg].n_items -= 2;
339 if (!
scanItemList(&flags, s_arg[i_arg].list + 2, &s_arg[i_arg].n_items, 0,
341 "endsline", -1, NULL, 0, ENDSLINE,
342 "usedefaultformat", -1, NULL, 0, USEDEFAULTFORMAT,
343 "blanklines",
SDDS_LONG, &blankLines, 1, 0,
344 "editlabel",
SDDS_STRING, &editLabel, 1, EDITLABEL_GIVEN,
346 "nounits", -1, NULL, 0, NO_UNITS,
347 "factor",
SDDS_DOUBLE, &factor, 1, FACTOR_GIVEN, NULL))
349 for (i = 0; i < names; i++) {
351 printColumn[i + printColumns].name = name[i];
354 replaceString(formatbuffer2, formatbuffer,
"lu", PRIu32, -1, 0);
357 if (flags & ENDSLINE && (i == names - 1))
358 printColumn[i + printColumns].endsLine = 1;
359 if (flags & USEDEFAULTFORMAT)
360 printColumn[i + printColumns].useDefaultFormat = 1;
361 printColumn[i + printColumns].label = printColumn[i + printColumns].editLabel = NULL;
362 if (flags & LABEL_GIVEN)
363 printColumn[i + printColumns].label = label;
364 if (flags & EDITLABEL_GIVEN)
365 printColumn[i + printColumns].editLabel = editLabel;
366 printColumn[i + printColumns].noUnits = flags & NO_UNITS ? 1 : 0;
367 printColumn[i + printColumns].factor = factor;
368 printColumn[i + printColumns].blankLines = blankLines;
371 printColumns += names;
374 if (s_arg[i_arg].n_items < 2) {
375 s_arg[i_arg].n_items = 2;
376 s_arg[i_arg].list =
SDDS_Realloc(s_arg[i_arg].list,
sizeof(*s_arg[i_arg].list) * 2);
379 name = makeListOfNames(s_arg[i_arg].list[1], &names);
380 printParameter =
SDDS_Realloc(printParameter,
sizeof(*printParameter) * (names + printParameters));
381 s_arg[i_arg].n_items -= 2;
385 if (!
scanItemList(&flags, s_arg[i_arg].list + 2, &s_arg[i_arg].n_items, 0,
387 "endsline", -1, NULL, 0, ENDSLINE,
388 "usedefaultformat", -1, NULL, 0, USEDEFAULTFORMAT,
389 "blanklines",
SDDS_LONG, &blankLines, 1, 0,
390 "editlabel",
SDDS_STRING, &editLabel, 1, EDITLABEL_GIVEN,
392 "factor",
SDDS_DOUBLE, &factor, 1, FACTOR_GIVEN, NULL))
394 for (i = 0; i < names; i++) {
395 SDDS_ZeroMemory(printParameter + i + printParameters,
sizeof(*printParameter));
396 printParameter[i + printParameters].name = name[i];
399 replaceString(formatbuffer2, formatbuffer,
"lu", PRIu32, -1, 0);
400 SDDS_CopyString(&printParameter[i + printParameters].format, formatbuffer2);
402 if (flags & ENDSLINE && (i == names - 1))
403 printParameter[i + printParameters].endsLine = 1;
404 if (flags & USEDEFAULTFORMAT)
405 printParameter[i + printParameters].useDefaultFormat = 1;
406 printParameter[i + printParameters].label = printParameter[i + printParameters].editLabel = NULL;
407 if (flags & LABEL_GIVEN)
408 printParameter[i + printParameters].label = label;
409 if (flags & EDITLABEL_GIVEN)
410 printParameter[i + printParameters].editLabel = editLabel;
411 printParameter[i + printParameters].factor = factor;
412 printParameter[i + printParameters].blankLines = blankLines;
415 printParameters += names;
418 if (s_arg[i_arg].n_items < 2) {
419 s_arg[i_arg].n_items = 2;
420 s_arg[i_arg].list =
SDDS_Realloc(s_arg[i_arg].list,
sizeof(*s_arg[i_arg].list) * 2);
423 name = makeListOfNames(s_arg[i_arg].list[1], &names);
424 printArray =
SDDS_Realloc(printArray,
sizeof(*printArray) * (names + printArrays));
425 s_arg[i_arg].n_items -= 2;
427 if (!
scanItemList(&flags, s_arg[i_arg].list + 2, &s_arg[i_arg].n_items, 0,
430 for (i = 0; i < names; i++) {
431 printArray[i + printArrays].name = name[i];
434 replaceString(formatbuffer2, formatbuffer,
"lu", PRIu32, -1, 0);
439 printArrays += names;
442 if (s_arg[i_arg].n_items < 2)
445 SDDS_Bomb(
"invalid syntax: specify -fromPage once only");
446 if (sscanf(s_arg[i_arg].list[1],
"%ld", &fromPage) != 1 || fromPage <= 0)
447 SDDS_Bomb(
"invalid -fromPage syntax or value");
450 if (s_arg[i_arg].n_items < 2)
453 SDDS_Bomb(
"invalid syntax: specify -toPage once only");
454 if (sscanf(s_arg[i_arg].list[1],
"%ld", &toPage) != 1 || toPage <= 0)
455 SDDS_Bomb(
"invalid -toPage syntax or value");
457 case SET_FORMATDEFAULTS:
458 formatDefaultArg = s_arg[i_arg].list + 1;
459 formatDefaultArgs = s_arg[i_arg].n_items - 1;
462 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1],
"%ld", &width) != 1 || (width < 40 && width))
463 SDDS_Bomb(
"invalid -width syntax or value");
466 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
469 case SET_PAGEADVANCE:
476 if (s_arg[i_arg].n_items != 2)
478 title = s_arg[i_arg].list[1];
480 case SET_SPREADSHEET:
481 s_arg[i_arg].n_items--;
482 spreadsheetDelimiter = NULL;
483 spreadsheetQuoteMark =
"";
484 if (!
scanItemList(&spreadsheetFlags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
485 "delimiter",
SDDS_STRING, &spreadsheetDelimiter, 1, SPREADSHEET_DELIMITER,
486 "quotemark",
SDDS_STRING, &spreadsheetQuoteMark, 1, SPREADSHEET_QUOTEMARK,
487 "nolabels", -1, NULL, 0, SPREADSHEET_NOLABELS,
"csv", -1, NULL, 0, SPREADSHEET_CSV,
489 SDDS_Bomb(
"invalid -spreadsheet syntax");
490 if (!spreadsheetDelimiter || !(spreadsheetFlags & SPREADSHEET_DELIMITER))
491 spreadsheetDelimiter =
"\t";
492 if (spreadsheetFlags & SPREADSHEET_CSV) {
493 spreadsheetDelimiter =
",";
494 spreadsheetFlags |= SPREADSHEET_DELIMITER;
495 spreadsheetFlags |= SPREADSHEET_QUOTEMARK;
496 spreadsheetQuoteMark =
"\"";
500 spreadsheetFlags |= SPREADSHEET_ON;
504 s_arg[i_arg].n_items--;
505 pagination.lines = 66;
506 if (!
scanItemList(&dummyFlags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
507 "lines",
SDDS_LONG, &pagination.lines, 1, 0,
508 "notitle", -1, NULL, 0, PAGINATION_NOTITLE,
509 "nolabels", -1, NULL, 0, PAGINATION_NOLABELS, NULL) ||
510 pagination.lines <= 3)
511 SDDS_Bomb(
"invalid -paginate syntax/values");
512 pagination.flags |= PAGINATION_ON;
517 case SET_POSTPAGELINES:
518 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1],
"%ld", &postPageLines) != 1)
519 SDDS_Bomb(
"invalid -postPageLines syntax/values");
524 case SET_BUFFERLINES:
525 if (s_arg[i_arg].n_items < 2)
526 SDDS_Bomb(
"invalid -bufferLines syntax");
527 if (sscanf(s_arg[i_arg].list[1],
"%ld", &bufferLines) != 1 || bufferLines < 0)
528 SDDS_Bomb(
"invalid -bufferLines syntax or value");
530 case SET_LATEXFORMAT:
531 s_arg[i_arg].n_items--;
535 if (!
scanItemList(&latexFormat, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
536 "longtable", -1, NULL, 0, LATEX_LONGTABLE,
537 "booktable", -1, NULL, 0, LATEX_BOOKTABLE,
538 "sideways", -1, NULL, 0, LATEX_SIDEWAYS,
540 "caption",
SDDS_STRING, &latexCaption, 1, LATEX_CAPTION,
541 "comment",
SDDS_STRING, &latexComment, 1, LATEX_COMMENT,
543 "translate",
SDDS_STRING, &latexTranslationFile, 1, LATEX_TRANSLATE,
544 "justify",
SDDS_STRING, &latexJustify, 1, LATEX_JUSTIFY,
545 "complete", -1, NULL, 0, LATEX_COMPLETE, NULL))
546 SDDS_Bomb(
"invalid -latexFormat syntax/values");
547 latexFormat |= LATEX_FORMAT;
548 if (latexFormat & LATEX_LONGTABLE && latexFormat & LATEX_SIDEWAYS)
549 SDDS_Bomb(
"invalid -latexFormat syntax/values: give only one of longtable and sideways");
552 s_arg[i_arg].n_items--;
554 if (!
scanItemList(&htmlFormat, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
555 "caption",
SDDS_STRING, &htmlCaption, 1, HTML_CAPTION,
556 "translate",
SDDS_STRING, &htmlTranslationFile, 1, HTML_TRANSLATE, NULL))
557 SDDS_Bomb(
"invalid -htmlFormat syntax/values");
558 htmlFormat |= HTML_FORMAT;
561 if (s_arg[i_arg].n_items != 1)
563 markdownFormat |= MARKDOWN_FORMAT;
566 fprintf(stderr,
"error: unknown switch: %s\n", s_arg[i_arg].list[0]);
572 input = s_arg[i_arg].list[0];
574 output = s_arg[i_arg].list[0];
580 pipeFlags |= DEFAULT_STDOUT;
581 processFilenames(
"sddsprintout", &input, &output, pipeFlags, noWarnings, NULL);
583 if (formatDefaultArgs)
584 changeDefaultFormats(formatDefaultArg, formatDefaultArgs, noWarnings);
588 SDDS_Bomb(
"-pageAdvance and -postPageLines are incompatible");
589 if (pagination.flags & PAGINATION_ON)
590 SDDS_Bomb(
"-pageAdvance and -paginate are incompatible");
592 if (pagination.flags & PAGINATION_ON && postPageLines)
593 SDDS_Bomb(
"-postPageLines and -paginate are incompatible");
595 if (!printColumns && !printParameters && !printArrays)
596 SDDS_Bomb(
"you must specify at least one of -columns, -parameters, or -arrays");
597 if (fromPage && toPage && fromPage > toPage)
598 SDDS_Bomb(
"invalid -fromPage and -toPage");
599 if ((latexFormat ? 1 : 0) + (htmlFormat ? 1 : 0) + (markdownFormat ? 1 : 0) > 1)
600 SDDS_Bomb(
"-latexFormat, -htmlFormat, and -markdown are mutually exclusive");
602 if (latexFormat & LATEX_TRANSLATE)
603 TranslationTable = readTranslationTable(latexTranslationFile);
604 if (htmlFormat & HTML_TRANSLATE)
605 TranslationTable = readTranslationTable(htmlTranslationFile);
613 if (output && !(fpOut = fopen(output,
"w")))
616 printColumns = processPrintColumns(&printColumn, printColumns, &SDDS_dataset, noWarnings,
617 spreadsheetFlags, CSV, latexFormat, TranslationTable, htmlFormat);
618 printParameters = processPrintParameters(&printParameter, printParameters, &SDDS_dataset, noWarnings, noLabels, CSV);
624 pagination.flags |= noTitle ? PAGINATION_NOTITLE : 0;
626 title =
tmalloc(
sizeof(*title) * ((input ? strlen(input) : 10) + 100));
628 sprintf(title,
"Printout for SDDS file %s", input ? input :
"stdin");
629 else if (markdownFormat)
630 sprintf(title,
"Printout for SDDS file %s", input ? input :
"stdin");
632 sprintf(title,
"Printout for SDDS file %s%s", input ? input :
"stdin", latexFormat ?
"" :
"\n");
636 if (printArrays || printParameters || !printColumns)
637 SDDS_Bomb(
"Can't create schFile except for pure column printout.");
638 CreateSCHFile(schFile, input, spreadsheetFlags, spreadsheetDelimiter, spreadsheetQuoteMark, printColumn, printColumns);
642 if (latexFormat & LATEX_LABEL && latexLabel[0] ==
'@') {
643 memmove(latexLabel, latexLabel + 1, strlen(latexLabel));
645 fprintf(stderr,
"sddsprintout: error: parameter %s not found in input file\n", latexLabel);
648 latexFormat |= LATEX_LABEL_PARAM;
650 if (latexFormat & LATEX_CAPTION && latexCaption[0] ==
'@') {
651 memmove(latexCaption, latexCaption + 1, strlen(latexCaption));
653 fprintf(stderr,
"sddsprintout: error: parameter %s not found in input file\n", latexCaption);
656 latexFormat |= LATEX_CAPTION_PARAM;
658 if (latexFormat & LATEX_COMPLETE) {
659 fprintf(fpOut,
"\\documentclass{report}\n\\pagestyle{empty}\n");
660 if (latexFormat & LATEX_BOOKTABLE)
661 fprintf(fpOut,
"\\usepackage{booktabs}\n");
662 if (latexFormat & LATEX_SIDEWAYS)
663 fprintf(fpOut,
"\\usepackage{rotating}\n");
664 fprintf(fpOut,
"\\begin{document}\n");
667 fprintf(fpOut,
"%% %s\n", latexComment);
672 if (markdownFormat) {
676 while ((pageNumber =
SDDS_ReadPageSparse(&SDDS_dataset, 0, (printColumns || SDDS_dataset.layout.data_mode.column_major) ? 1 : 1000000, 0, 0)) > 0) {
677 if ((fromPage && pageNumber < fromPage) || (toPage && pageNumber > toPage))
679 if (pagination.flags & PAGINATION_ON) {
681 fputc(
'\014', fpOut);
682 pagination.currentLine = 1;
685 pagination.currentLine += printPageTitle(fpOut, title);
689 printPageTitle(fpOut, title);
690 }
else if (pageAdvance)
691 fputc(
'\014', fpOut);
692 else if (postPageLines > 0) {
693 long line = postPageLines;
698 if (!latexFormat && !htmlFormat && !markdownFormat)
699 doPrintParameters(&SDDS_dataset, printParameter, printParameters, width, fpOut, spreadsheetFlags,
700 spreadsheetDelimiter, spreadsheetQuoteMark, &pagination, title, noLabels);
702 latexCaptionBuffer = latexLabelBuffer = NULL;
703 if (latexFormat & LATEX_CAPTION_PARAM && !(latexCaptionBuffer =
SDDS_GetParameterAsString(&SDDS_dataset, latexCaption, NULL))) {
705 snprintf(buffer, 1024,
"Error: can't read latex caption parameter %s\n", latexCaption);
708 if (latexFormat & LATEX_LABEL_PARAM && !(latexLabelBuffer =
SDDS_GetParameterAsString(&SDDS_dataset, latexLabel, NULL))) {
710 snprintf(buffer, 1024,
"Error: can't read latex label parameter %s\n", latexLabel);
713 doPrintColumns(&SDDS_dataset, printColumn, printColumns, width, fpOut, spreadsheetFlags, spreadsheetDelimiter,
714 spreadsheetQuoteMark, latexFormat,
715 latexFormat & LATEX_CAPTION_PARAM ? latexCaptionBuffer : latexCaption,
716 latexFormat & LATEX_LABEL_PARAM ? latexLabelBuffer : latexLabel,
717 latexGroup, htmlFormat, htmlCaption, markdownFormat,
718 &pagination, title, noLabels);
719 if (latexFormat & LATEX_CAPTION_PARAM && latexCaptionBuffer)
720 free(latexCaptionBuffer);
721 if (latexFormat & LATEX_LABEL_PARAM && latexLabelBuffer)
722 free(latexLabelBuffer);
724 for (i = 0; i < bufferLines; i++)
732 for (i = 0; i < printColumns; i++) {
733 if (printColumn[i].headers) {
734 for (
long j = 0; j < printColumn[i].headers; j++) {
735 free(printColumn[i].header[j]);
737 free(printColumn[i].header);
740 if (latexFormat & LATEX_COMPLETE)
741 fprintf(fpOut,
"\\end{document}\n");
749 unsigned long spreadsheetFlags,
long csv,
unsigned long latexFormat, htab *TranslationTable,
unsigned long htmlFormat) {
751 long i, irequest, iname, printColumns, names, *columnUsed, columnLimit;
753 char **name, *units, *format;
755 if (printRequests < 1 || !(printRequest = *printRequestPtr))
759 fprintf(stderr,
"warning: no column data in input file\n");
762 columnUsed =
tmalloc(
sizeof(*columnUsed) * columnLimit);
763 for (i = 0; i < columnLimit; i++)
765 printColumn =
tmalloc(
sizeof(*printColumn) * columnLimit);
769 for (irequest = 0; irequest < printRequests; irequest++) {
770 if ((names =
SDDS_MatchColumns(inTable, &name, SDDS_MATCH_STRING, FIND_ANY_TYPE, printRequest[irequest].name, SDDS_0_PREVIOUS | SDDS_OR)) > 0) {
771 for (iname = 0; iname < names; iname++) {
774 if (!columnUsed[index]) {
778 printColumn[printColumns].index = index;
779 if (iname == names - 1)
780 printColumn[printColumns].endsLine = printRequest[irequest].endsLine;
782 printColumn[printColumns].endsLine = 0;
784 printColumn[printColumns].blankLines = printRequest[irequest].blankLines;
789 SDDS_CopyString(&printColumn[printColumns].format, csvFormat[printColumn[printColumns].type - 1]);
791 if (printRequest[irequest].format) {
792 if (printRequest[irequest].format[0] !=
'@') {
794 fprintf(stderr,
"error: given format (\"%s\") for column %s is invalid\n",
795 printRequest[irequest].format, name[iname]);
802 fprintf(stderr,
"error: given format column (\"%s\") for column %s is absent or not string type\n",
803 printRequest[irequest].format + 1, name[iname]);
808 SDDS_CopyString(&printColumn[printColumns].format, printRequest[irequest].format);
810 SDDS_CopyString(&printColumn[printColumns].format, defaultFormat[printColumn[printColumns].type - 1]);
814 printColumn[printColumns].headers =
815 makeColumnHeaders(&printColumn[printColumns].header, &printColumn[printColumns].fieldWidth,
816 printRequest[irequest].label ? printRequest[irequest].label : name[iname],
817 printRequest[irequest].editLabel,
818 printRequest[irequest].noUnits ? NULL : units, printRequest[irequest].factor,
819 &printColumn[printColumns].format, spreadsheetFlags, latexFormat, TranslationTable, htmlFormat);
821 fprintf(stderr,
"%s has format >%s<, name %s, units %s\n",
822 printRequest[irequest].label ? printRequest[irequest].label : name[iname], printColumn[printColumns].format,
823 name[iname], units?units:
"NULL");
825 printColumn[printColumns].data = NULL;
826 printColumn[printColumns].factor = printRequest[irequest].factor;
828 columnUsed[index] = 1;
835 }
else if (names < 0)
837 else if (!noWarnings)
838 fprintf(stderr,
"warning: no column matches %s\n", printRequest[irequest].name);
839 free(printRequest[irequest].name);
840 if (printRequest[irequest].format)
841 free(printRequest[irequest].format);
844 *printRequestPtr = printColumn;
849 long noWarnings,
long noLabels,
long csv) {
851 long i, irequest, iname, printParameters, names, *parameterUsed, parameterLimit, maxFieldWidth, index;
852 char **name, *units, *format;
854 if (printRequests < 1 || !(printRequest = *printRequestPtr))
858 fprintf(stderr,
"warning: no parameter data in input file\n");
861 parameterUsed =
tmalloc(
sizeof(*parameterUsed) * parameterLimit);
862 for (i = 0; i < parameterLimit; i++)
863 parameterUsed[i] = 0;
864 printParameter =
tmalloc(
sizeof(*printParameter) * parameterLimit);
868 for (irequest = 0; irequest < printRequests; irequest++) {
869 if ((names =
SDDS_MatchParameters(inTable, &name, SDDS_MATCH_STRING, FIND_ANY_TYPE, printRequest[irequest].name, SDDS_0_PREVIOUS | SDDS_OR)) > 0) {
870 for (iname = 0; iname < names; iname++) {
873 if (!parameterUsed[index]) {
877 printParameter[printParameters].index = index;
879 if (iname == names - 1)
880 printParameter[printParameters].endsLine = printRequest[irequest].endsLine;
881 printParameter[printParameters].blankLines = printRequest[irequest].blankLines;
886 SDDS_CopyString(&printParameter[printParameters].format, csvFormat[printParameter[printParameters].type - 1]);
888 if (printRequest[irequest].format) {
890 fprintf(stderr,
"error: given format (\"%s\") for parameter %s is invalid\n", printRequest[irequest].format, name[iname]);
893 SDDS_CopyString(&printParameter[printParameters].format, printRequest[irequest].format);
895 SDDS_CopyString(&printParameter[printParameters].format, defaultFormat[printParameter[printParameters].type - 1]);
901 printParameter[printParameters].label =
902 makeParameterLabel(&printParameter[printParameters].fieldWidth, printRequest[irequest].label ? printRequest[irequest].label : name[iname], printRequest[irequest].editLabel, units, printRequest[irequest].factor, printParameter[printParameters].format);
904 printParameter[printParameters].label =
"";
906 if (printParameter[printParameters].fieldWidth > maxFieldWidth)
907 maxFieldWidth = printParameter[printParameters].fieldWidth;
908 printParameter[printParameters].data = NULL;
909 printParameter[printParameters].factor = printRequest[irequest].factor;
911 parameterUsed[index] = 1;
918 }
else if (names < 0)
920 else if (!noWarnings)
921 fprintf(stderr,
"warning: no parameter matches %s\n", printRequest[irequest].name);
922 free(printRequest[irequest].name);
923 if (printRequest[irequest].format)
924 free(printRequest[irequest].format);
926 for (i = 0; i < printParameters; i++) {
928 if (printParameter[i].fieldWidth < maxFieldWidth) {
929 ptr =
tmalloc(
sizeof(*ptr) * (maxFieldWidth + 1));
930 strcpy(ptr, printParameter[i].label);
932 free(printParameter[i].label);
933 printParameter[i].label = ptr;
934 printParameter[i].fieldWidth = maxFieldWidth;
938 *printRequestPtr = printParameter;
939 return printParameters;
942#if SDDS_NUM_TYPES != 11
943# error "number of SDDS types is not 11 as expected"
946void setDefaultFormats(
void) {
973long changeDefaultFormats(
char **argv,
long argc,
long noWarnings) {
976 static char formatbuffer[100], formatbuffer2[100];
978 for (i = 0; i < argc; i++) {
979 if (!(format = strchr(argv[i],
'='))) {
980 fprintf(stderr,
"-formatDefault syntax error with keyword \"%s\"\n", argv[i]);
985 fprintf(stderr,
"-formatDefault error: unknown type \"%s\"\n", argv[i]);
989 fprintf(stderr,
"-formatDefault error: invalid format string \"%s\" for type \"%s\"\n", format, argv[i]);
992 free(defaultFormat[type - 1]);
996 replaceString(formatbuffer2, formatbuffer,
"lu", PRIu32, -1, 0);
1001 if (!getFormatFieldLength(format, NULL) && !noWarnings)
1002 fprintf(stderr,
"warning: no field length for default format \"%s\"---this will produce poor results\n", defaultFormat[type - 1]);
1007long makeColumnHeaders(
char ***header,
long *fieldWidth,
char *name,
char *editLabel,
char *units,
double factor,
1008 char **format,
unsigned long spreadsheetHeaders,
unsigned long latexFormat, htab *TranslationTable,
unsigned long htmlFormat) {
1009 long formatWidth, unitsWidth, nameWidth, headers, i, formatPadding, formatExtraChars;
1010 static char buffer[1024], buffer2[1026];
1011 static char nameBuffer[1024];
1015 fprintf(stderr,
"makeColumnHeaders(name=%s, units=%s,...)\n",
1016 name, units?units:
"NULL");
1018 if (latexFormat & LATEX_TRANSLATE) {
1019 name = findTranslation(TranslationTable, name);
1020 units = findTranslation(TranslationTable, units);
1022 if (htmlFormat & HTML_TRANSLATE) {
1023 name = findTranslation(TranslationTable, name);
1024 units = findTranslation(TranslationTable, units);
1027 units = modifyUnitsWithFactor(units, 1. / factor, latexFormat);
1030 strcpy(nameBuffer, name);
1032 SDDS_Bomb(
"Problem editing column label");
1037 fprintf(stderr,
"name: >%s< format: >%s<\n", name, *format ? *format :
"NULL");
1041 unitsWidth = units ? strlen(units) : 0;
1042 nameWidth = strlen(name);
1043 *fieldWidth = unitsWidth > nameWidth ? unitsWidth : nameWidth;
1045 if (!(spreadsheetHeaders & SPREADSHEET_ON)) {
1050 if (!*format || (*format && (*format)[0] !=
'@')) {
1052 formatWidth = getFormatFieldLength(*format, &formatExtraChars);
1053 if ((formatWidth + formatExtraChars) > *fieldWidth)
1054 *fieldWidth = formatWidth + formatExtraChars;
1057 if (*fieldWidth < 2)
1060 fprintf(stderr,
"fieldWidth = %ld, formatWidth = %ld, formatExtra = %ld\n", *fieldWidth, formatWidth, formatExtraChars);
1062 if (*format && (formatPadding = (*fieldWidth - 2 - formatExtraChars) - formatWidth) > 0) {
1067 formatWidth = *fieldWidth - 2 - formatExtraChars;
1068 replaceFormatWidth(buffer, *format, formatWidth);
1070 fprintf(stderr,
"Adjusted format width to %ld: >%s<\n", formatWidth, buffer);
1073 sprintf(buffer2,
" %s ", buffer);
1076 sprintf(buffer,
" %s ", *format);
1083 *header =
tmalloc(
sizeof(**header) * (headers = 2));
1084 for (i = 0; i < headers; i++)
1085 (*header)[i] =
tmalloc(
sizeof(***header) * (*fieldWidth + 1));
1088 copyAndPad((*header)[0], name, nameWidth, *fieldWidth);
1090 copyAndPad((*header)[1], units, unitsWidth, *fieldWidth);
1092 fprintf(stderr,
"header0 padded to %ld: >%s<\n", *fieldWidth, (*header)[0]);
1093 (*header)[0][0] =
'+';
1094 (*header)[0][strlen((*header)[0]) - 1] =
'-';
1095 fprintf(stderr,
"header1 padded to %ld: >%s<\n", *fieldWidth, (*header)[1]);
1096 (*header)[1][0] =
'+';
1097 (*header)[1][strlen((*header)[1]) - 1] =
'-';
1103void copyAndPad(
char *target,
char *source,
long sourceWidth,
long targetWidth) {
1104 long i, head, tail, excess;
1105 if ((excess = targetWidth - sourceWidth) < 0)
1106 SDDS_Bomb(
"negative excess in copyAndPad()---programming error");
1107 head = tail = excess / 2;
1108 if ((i = excess - head - tail))
1110 for (i = 0; i < head; i++)
1114 strcat(target, source);
1115 for (i = strlen(target); i < targetWidth; i++)
1142char *makeParameterLabel(
long *fieldWidth,
char *name,
char *editLabel,
char *units,
double factor,
char *format) {
1143 long formatWidth, labelWidth, extraFormatChars;
1145 static char buffer[1024];
1148 strcpy(buffer, name);
1150 SDDS_Bomb(
"Problem editing parameter label");
1154 *fieldWidth = (labelWidth = strlen(name) + 4 + (units && !
SDDS_StringIsBlank(units) ? strlen(units) + 3 : 0)) + (formatWidth = getFormatFieldLength(format, &extraFormatChars));
1155 *fieldWidth += extraFormatChars;
1156 if (*fieldWidth < 2)
1158 label =
tmalloc(
sizeof(*label) * (labelWidth + 1));
1160 units = modifyUnitsWithFactor(units, 1. / factor, 0);
1162 sprintf(label,
"%s (%s) = ", name, units);
1164 sprintf(label,
"%s = ", name);
1168long getFormatFieldLength(
char *format,
long *extraChars) {
1175 while (*format && *format !=
'%')
1177 if (*format !=
'%') {
1178 fprintf(stderr,
"Bad format string: %s\n", format0);
1182 *extraChars = format - format0;
1183 while (*format && !isdigit(*format))
1188 if (!*format || !isdigit(*format))
1191 width = atoi(format);
1193 while (isdigit(*format) || *format ==
'.')
1195 if (*format ==
'l' || *format ==
'h')
1200 *extraChars += strlen(format);
1206 FILE *fpOut,
unsigned long spreadsheetFlags,
char *spreadsheetDelimiter,
1207 char *spreadsheetQuoteMark,
PAGINATION *pagination,
char *title,
long noLabels) {
1208 long parameter, outputRow, length;
1209 char printBuffer[2 * SDDS_MAXLINE];
1210 static void **dataBuffer = NULL;
1213 if (!printParameters)
1215 if (!dataBuffer && !(dataBuffer = malloc(
sizeof(
void *) * 4)))
1216 SDDS_Bomb(
"Allocation failure in doPrintParameters");
1219 for (parameter = 0; parameter < printParameters; parameter++) {
1221 if (printParameter[parameter].format && printParameter[parameter].format[0] ==
'@' &&
1223 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1224 if (!
SDDS_GetParameter(inTable, printParameter[parameter].name, &dataBuffer[0]))
1225 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1227 format ? format : printParameter[parameter].format, printBuffer,
1228 SDDS_PRINT_NOQUOTES, printParameter[parameter].factor))
1229 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1230 if (spreadsheetFlags & SPREADSHEET_ON) {
1231 if (!(spreadsheetFlags & SPREADSHEET_NOLABELS))
1232 fprintf(fpOut,
"%s%s", printParameter[parameter].name, spreadsheetDelimiter);
1233 fprintf(fpOut,
"%s%s%s\n", spreadsheetQuoteMark, printBuffer, spreadsheetQuoteMark);
1236 length = strlen(printParameter[parameter].label) + strlen(printBuffer) + (parameter ? 2 : 0);
1237 if ((parameter && printParameter[parameter - 1].endsLine) || (outputRow && (outputRow + length) > width)) {
1239 checkPagination(fpOut, pagination, title);
1240 outputRow = length - 2;
1241 if (parameter && printParameter[parameter - 1].blankLines && printParameter[parameter - 1].endsLine) {
1243 for (i = 1; i < printParameter[parameter - 1].blankLines; i++) {
1244 checkPagination(fpOut, pagination, title);
1251 outputRow += length;
1253 fputs(printParameter[parameter].label, fpOut);
1254 fputs(printBuffer, fpOut);
1255 if (printParameter[parameter].type ==
SDDS_STRING && dataBuffer[0])
1256 free((
char *)dataBuffer[0]);
1258 if (!(spreadsheetFlags & SPREADSHEET_ON)) {
1259 checkPagination(fpOut, pagination, title);
1261 checkPagination(fpOut, pagination, title);
1266char **makeListOfNames(
char *
string,
long *names) {
1270 *names = characterCount(
string,
',') + 1;
1271 name =
tmalloc(
sizeof(*name) * *names);
1272 for (iName = 0;
string && iName < *names; iName++) {
1273 if ((ptr = strchr(
string,
',')))
1278 if (iName != *names)
1279 SDDS_Bomb(
"problem occurred scanning list of names");
1283long characterCount(
char *
string,
char c) {
1286 while ((ptr = strchr(
string, c))) {
1293long printPageTitle(FILE *fpOut,
char *title) {
1297 fprintf(fpOut,
"%s\n", title);
1300 while ((ptr = strchr(ptr,
'\n'))) {
1307long checkPagination(FILE *fpOut,
PAGINATION *pagination,
char *title) {
1308 if (!(pagination->flags & PAGINATION_ON))
1310 if ((pagination->currentLine += 1) >= pagination->lines) {
1311 fputc(
'\014', fpOut);
1312 pagination->currentLine = 0;
1313 if (!(pagination->flags & PAGINATION_NOTITLE))
1314 pagination->currentLine += printPageTitle(fpOut, title);
1320void printColumnHeaders(FILE *fpOut,
PRINT_COLUMN *printColumn,
long printColumns,
long width,
1321 PAGINATION *pagination,
long latexFormat,
char *latexTitle,
long htmlFormat,
char *htmlTitle,
1322 long markdownFormat) {
1324 long row, column, header, outputRow, maxOutputRow, length, noUnitsLine;
1326 if (markdownFormat) {
1328 for (column = 0; column < printColumns; column++) {
1329 const char *name =
trim_spaces(printColumn[column].header[0]);
1330 const char *units =
trim_spaces(printColumn[column].header[1]);
1333 printf(
"markdown: name %s, units %s\n", name, units);
1335 fprintf(fpOut,
" %s (", makeMarkdownSafeString((
char *)name));
1336 fprintf(fpOut,
"%s) |", makeMarkdownSafeString((
char *)units));
1338 fprintf(fpOut,
" %s |", makeMarkdownSafeString((
char *)name));
1344 for (column = 0; column < printColumns; column++)
1345 fputs(
" --- |", fpOut);
1347 pagination->currentLine += 2;
1352 if (latexFormat & LATEX_LONGTABLE) {
1353 fprintf(fpOut,
"\\begin{longtable}{");
1354 if (latexFormat & LATEX_JUSTIFY) {
1355 fprintf(fpOut,
"%s", latexJustify);
1357 for (column = 0; column < printColumns; column++)
1358 fprintf(fpOut,
"%s", latexFormat & LATEX_BOOKTABLE ?
"c" : (column == 0 ?
"|c|" :
"c|"));
1360 fprintf(fpOut,
"}\n");
1361 if (latexTitle && strlen(latexTitle)) {
1362 fprintf(fpOut,
"\\caption{%s}\\\\\n", latexTitle ? makeTexSafeString(latexTitle) :
"No caption");
1365 fprintf(fpOut,
"\\begin{%s}[htb]", latexFormat & LATEX_SIDEWAYS ?
"sidewaystable" :
"table");
1366 if (latexTitle && strlen(latexTitle)) {
1367 fprintf(fpOut,
"\\caption{%s}\n", latexTitle ? makeTexSafeString(latexTitle) :
"No caption");
1369 fprintf(fpOut,
"\n");
1370 fprintf(fpOut,
"\\begin{center}\n");
1371 fprintf(fpOut,
"\\begin{tabular}{");
1372 if (latexFormat & LATEX_JUSTIFY) {
1373 fprintf(fpOut,
"%s", latexJustify);
1375 for (column = 0; column < printColumns; column++)
1376 fprintf(fpOut,
"%s", latexFormat & LATEX_BOOKTABLE ?
"c" : (column == 0 ?
"|c|" :
"c|"));
1378 fprintf(fpOut,
"}\n");
1381 if (latexFormat & LATEX_BOOKTABLE)
1382 fprintf(fpOut,
"\\toprule\n");
1384 fprintf(fpOut,
"\\hline\n");
1387 fprintf(fpOut,
"<table style=\"width:100%%\">\n");
1388 if (htmlTitle && strlen(htmlTitle)) {
1389 fprintf(fpOut,
" <caption>%s</caption>\n", htmlTitle);
1394 for (column = 0; column < printColumns; column++) {
1398 if (column != printColumns)
1402 for (header = 0; header < printColumn[0].headers; header++) {
1404 if (noUnitsLine && header == 1)
1407 fprintf(fpOut,
" <tr>\n");
1409 for (column = 0; column < printColumns; column++) {
1410 label = printColumn[column].header[header];
1411 length = strlen(label);
1412 if (!latexFormat && !htmlFormat && ((column && printColumn[column - 1].endsLine) || (width && outputRow && (outputRow + length) > width))) {
1413 printColumn[column - 1].endsLine = 1;
1415 pagination->currentLine++;
1419 if (latexFormat & LATEX_TRANSLATE && strchr(label,
'$'))
1420 fputs(label, fpOut);
1421 else if (strpbrk(label,
"_^{}\\"))
1422 fprintf(fpOut,
"$%s$", label);
1424 fputs(makeTexSafeString(printColumn[column].header[header]), fpOut);
1425 fputs(column == printColumns - 1 ? (latexFormat & LATEX_BOOKTABLE ?
" \\\\ \n" :
" \\\\ \\hline") :
" & ", fpOut);
1426 }
else if (htmlFormat) {
1427 fprintf(fpOut,
" <th>%s</th>\n",
trim_spaces(label));
1429 fputs(label, fpOut);
1430 outputRow += length;
1431 if (outputRow > maxOutputRow)
1432 maxOutputRow = outputRow;
1436 fprintf(fpOut,
" </tr>\n");
1439 pagination->currentLine++;
1441 if (!latexFormat && !htmlFormat) {
1442 for (row = 0; row < maxOutputRow; row++)
1445 }
else if (latexFormat & LATEX_BOOKTABLE) {
1446 fputs(
"\\midrule\n", fpOut);
1448 pagination->currentLine++;
1452 long width, FILE *fpOut,
unsigned long spreadsheetFlags,
char *spreadsheetDelimiter,
char *spreadsheetQuoteMark,
1453 long latexFormat,
char *latexTitle,
char *latexLabel,
char *latexGroupColumn,
1454 long htmlFormat,
char *htmlTitle,
long markdownFormat,
1455 PAGINATION *pagination,
char *title,
long noLabels) {
1457 int64_t row, rows, nGroups, maxGroupLength, groupLength;
1459 char **groupData = NULL;
1460 char ***format = NULL;
1461 char printBuffer[16 * SDDS_MAXLINE];
1467 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1471 if ((latexTitle != NULL) && (latexTitle[0] ==
'@')) {
1472 latexTitle = latexTitle + 1;
1476 if ((htmlTitle != NULL) && (htmlTitle[0] ==
'@')) {
1477 htmlTitle = htmlTitle + 1;
1481 if (!(spreadsheetFlags & SPREADSHEET_ON) && !noLabels)
1482 printColumnHeaders(fpOut, printColumn, printColumns, width, pagination,
1483 latexFormat, latexTitle ? latexTitle : title,
1484 htmlFormat, htmlTitle ? htmlTitle : title, markdownFormat);
1485 else if (!(spreadsheetFlags & SPREADSHEET_NOLABELS) && !noLabels) {
1486 for (column = 0; column < printColumns; column++)
1487 fprintf(fpOut,
"%s%s", printColumn[column].name, column != printColumns - 1 ? spreadsheetDelimiter :
"\n");
1491 maxGroupLength = groupLength = 1;
1492 if (latexFormat & LATEX_GROUP) {
1494 fprintf(stderr,
"Error: unable to get data for column %s\n", latexGroupColumn);
1495 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1497 for (row = 1; row < rows; row++) {
1498 if (strcmp(groupData[row], groupData[row - 1])) {
1499 if (groupLength > maxGroupLength)
1500 maxGroupLength = groupLength;
1507 if (groupLength > maxGroupLength)
1508 maxGroupLength = groupLength;
1510 fprintf(fpOut,
"%% nGroups = %" PRId64
", maxGroupLength = %" PRId64
"\n", nGroups, maxGroupLength);
1513 data =
tmalloc(
sizeof(*data) * printColumns);
1514 format =
tmalloc(
sizeof(*format) * printColumns);
1515 for (column = 0; column < printColumns; column++) {
1516 if (!(data[column] =
SDDS_GetColumn(inTable, printColumn[column].name))) {
1517 fprintf(stderr,
"Error: unable to get data for column %s\n", printColumn[column].name);
1518 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1520 format[column] = NULL;
1521 if (printColumn[column].format && printColumn[column].format[0] ==
'@') {
1522 if (!(format[column] =
SDDS_GetColumn(inTable, printColumn[column].format + 1))) {
1523 fprintf(stderr,
"Error: unable to get format data for column %s from %s\n",
1524 printColumn[column].name, printColumn[column].format + 1);
1525 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1529 for (row = 0; row < rows; row++) {
1532 fprintf(fpOut,
" <tr bgcolor=\"#ddd\">\n");
1534 fprintf(fpOut,
" <tr>\n");
1536 if (latexFormat && nGroups > 1) {
1537 if (row == 0 || strcmp(groupData[row], groupData[row - 1])) {
1538 if (row != 0 && latexFormat & LATEX_BOOKTABLE)
1539 fprintf(fpOut,
"\\midrule\n");
1540 fprintf(fpOut,
"\\multicolumn{%ld}{l}{\\bf %s} \\\\ \n", printColumns, groupData[row]);
1543 for (column = 0; column < printColumns; column++) {
1545 fprintf(fpOut,
" <td style=\"text-align:center\">");
1548 sprintf(printBuffer, format[column] ? format[column][row] : printColumn[column].format,
" ");
1551 format[column] ? format[column][row] : printColumn[column].format,
1552 printBuffer, SDDS_PRINT_NOQUOTES,
1553 printColumn[column].factor)) {
1554 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1557 if (spreadsheetFlags & SPREADSHEET_ON) {
1558 fprintf(fpOut,
"%s%s%s%s", spreadsheetQuoteMark, printBuffer, spreadsheetQuoteMark, column != printColumns - 1 ? spreadsheetDelimiter :
"\n");
1563 fputs(makeTexExponentialString(printBuffer), fpOut);
1565 fputs(makeTexSafeString(printBuffer), fpOut);
1566 fputs(column != printColumns - 1 ?
" & " : (latexFormat & LATEX_BOOKTABLE ?
" \\\\ " :
" \\\\ \\hline"), fpOut);
1569 if (markdownFormat) {
1572 fprintf(fpOut,
" %s |", makeMarkdownSafeString(
trim_spaces(printBuffer)));
1577 fprintf(fpOut,
"</td>\n");
1580 fputs(printBuffer, fpOut);
1581 if (printColumn[column].endsLine) {
1584 if (checkPagination(fpOut, pagination, title))
1585 printColumnHeaders(fpOut, printColumn, printColumns, width, pagination, latexFormat, latexTitle ? latexTitle : title, htmlFormat, htmlTitle ? htmlTitle : title, markdownFormat);
1586 for (i = 0; i < printColumn[column].blankLines; i++) {
1588 if (checkPagination(fpOut, pagination, title))
1589 printColumnHeaders(fpOut, printColumn, printColumns, width, pagination, latexFormat, latexTitle ? latexTitle : title, htmlFormat, htmlTitle ? htmlTitle : title, markdownFormat);
1594 fprintf(fpOut,
" </tr>\n");
1595 }
else if (markdownFormat) {
1598 if (!(spreadsheetFlags & SPREADSHEET_ON) && !printColumn[column - 1].endsLine) {
1600 if (!latexFormat && !htmlFormat && checkPagination(fpOut, pagination, title))
1601 printColumnHeaders(fpOut, printColumn, printColumns, width, pagination, latexFormat, latexTitle ? latexTitle : title, htmlFormat, htmlTitle ? htmlTitle : title, markdownFormat);
1606 if (latexFormat & LATEX_BOOKTABLE)
1607 fputs(
"\\bottomrule\n", fpOut);
1608 if (latexFormat & LATEX_LONGTABLE) {
1610 fprintf(fpOut,
"\\label{%s}\n", latexLabel);
1612 fprintf(fpOut,
"%%\\label{%s}\n",
"tab:labelHere");
1613 fprintf(fpOut,
"\\end{longtable}\n");
1615 fprintf(fpOut,
"\\end{tabular}\n");
1616 fprintf(fpOut,
"\\end{center}\n");
1618 fprintf(fpOut,
"\\label{%s}\n", latexLabel);
1620 fprintf(fpOut,
"%%\\label{%s}\n",
"tab:labelHere");
1621 fprintf(fpOut,
"\\end{%s}\n", latexFormat & LATEX_SIDEWAYS ?
"sidewaystable" :
"table");
1625 fprintf(fpOut,
"</table><br>\n");
1627 for (column = 0; column < printColumns; column++) {
1630 free(format[column]);
1636void replaceFormatWidth(
char *buffer,
char *format,
long width) {
1640 if (*ptr ==
'%' && *(ptr + 1) !=
'%')
1646 if (*(ptr + 1) ==
'-') {
1647 sprintf(buffer,
"%s%%-%ld", format, width);
1650 sprintf(buffer,
"%s%%%ld", format, width);
1652 while (*ptr && isdigit(*ptr))
1654 strcat(buffer, ptr);
1656 strcpy(buffer, format);
1660void CreateSCHFile(
char *output,
char *input,
unsigned long flags,
char *delimiter,
1661 char *quote,
PRINT_COLUMN *printColumn,
long printColumns) {
1665 if (!(fp = fopen(output,
"w")))
1666 SDDS_Bomb(
"Couldn't open SCHFile for writing.");
1667 fprintf(fp,
"[%s]\nFiletype=Delimited\nDelimiter=%s\nSeparator=%s\nCharSet=ascii\n", input ? input :
"NULL", quote, delimiter);
1668 for (i = 0; i < printColumns; i++) {
1669 printColumn[i].useDefaultFormat = 1;
1670 fprintf(fp,
"Field%ld=%s,%s,00,00,00\n\n", i + 1, printColumn[i].name,
SDDS_NUMERIC_TYPE(printColumn[i].type) ?
"Float" :
"Char");
1674char *makeTexSafeString(
char *source) {
1675 static char *buffer = NULL;
1676 static long buflen = 0;
1677 long index = 0, length, inMath = 0;
1680 length = strlen(source);
1681 if (length > (buflen - 2)) {
1682 buflen = length * 2 + 2;
1683 if (!(buffer =
SDDS_Realloc(buffer,
sizeof(*buffer) * buflen)))
1688 if (*source ==
'_' || *source ==
'^' || *source ==
'{' || *source ==
'}' || *source ==
'%' || *source ==
'#') {
1690 buffer[index++] =
'\\';
1691 buffer[index++] = *source++;
1692 }
else if (*source ==
'<' || *source ==
'>' || *source ==
'|') {
1694 buffer[index++] =
'$';
1695 buffer[index++] = *source++;
1697 buffer[index++] =
'$';
1701 buffer[index++] = *source++;
1703 if (index >= (buflen - 1)) {
1705 if (!(buffer =
SDDS_Realloc(buffer,
sizeof(*buffer) * buflen)))
1713char *makeTexExponentialString(
char *text) {
1717 if ((ptr1 = ptr2 = strchr(text,
'e')) || (ptr1 = ptr2 = strchr(text,
'E'))) {
1719 while (*ptr2 && !(isdigit(*ptr2) || *ptr2 ==
'-'))
1723 if (strlen(ptr2) >= 100)
1724 SDDS_Bomb(
"buffer overflow in makeTexExponentialString");
1725 sscanf(ptr2,
"%ld", &exponent);
1727 if (exponent != 0) {
1728 sprintf(buffer,
"%ld", exponent);
1729 sprintf(ptr1,
"$\\times 10^{%s}$", buffer);
1735htab *readTranslationTable(
char *TranslationFile) {
1737 fprintf(stderr,
"The latex and html options in sddsprintout are not available on Windows\n");
1742 char **oldName, **newName;
1747 (rows = SDDS_RowCount(&SDDSin)) <= 0 ||
1750 SDDS_Bomb(
"Problem with translation file");
1753 hadd(ht, (ub1 *)oldName[rows], (ub4)strlen(oldName[rows]), newName[rows]);
1759char *findTranslation(htab *ht,
char *key) {
1761 SDDS_Bomb(
"The latex and html options in sddsprintout are not available on Windows");
1763 if (key && ht &&
hfind(ht, (ub1 *)key, (ub4)strlen(key)) == TRUE)
1764 return (
char *)hstuff(ht);
1769char *modifyUnitsWithFactor(
char *units0,
double factor,
unsigned long latexFormat) {
1770 char *units, buffer[100];
1772 units =
tmalloc(
sizeof(*units) * ((units0 ? strlen(units0) : 0) + 100));
1775 sprintf(buffer,
"%.2g", factor);
1776 sprintf(units,
"%s%s", makeTexExponentialString(buffer), units0 ? units0 :
"");
1778 sprintf(units,
"%.2g%s", factor, units0 ? units0 :
"");
1783char *makeMarkdownSafeString(
char *source) {
1784 static char *buffer = NULL;
1785 static long buflen = 0;
1791 length = strlen(source);
1792 if (length * 2 + 1 > buflen) {
1793 buflen = length * 2 + 1;
1794 buffer =
SDDS_Realloc(buffer,
sizeof(*buffer) * buflen);
1797 for (i = 0, j = 0; source[i]; i++) {
1798 if (source[i] ==
'|' || source[i] ==
'\\')
1800 buffer[j++] = source[i];
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_GetParameterInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Retrieves information about a specified parameter in the SDDS dataset.
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_GetParameterType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a parameter in the SDDS dataset by its index.
int32_t SDDS_ZeroMemory(void *mem, int64_t n_bytes)
Sets a block of memory to zero.
int32_t SDDS_VerifyPrintfFormat(const char *string, int32_t type)
Verifies that a printf format string is compatible with a specified data type.
int32_t SDDS_ParameterCount(SDDS_DATASET *page)
Retrieves the number of parameters in the SDDS dataset.
int32_t SDDS_GetParameterIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named parameter in the SDDS dataset.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
int32_t SDDS_MatchParameters(SDDS_DATASET *SDDS_dataset, char ***nameReturn, int32_t matchMode, int32_t typeMode,...)
Matches and retrieves parameter names from an SDDS dataset based on specified criteria.
int32_t SDDS_ColumnCount(SDDS_DATASET *page)
Retrieves the number of columns in the SDDS dataset.
int32_t SDDS_SprintTypedValueFactor(void *data, int64_t index, int32_t type, const char *format, char *buffer, uint32_t mode, double factor)
Reallocates memory to a new size and zero-initializes the additional space.
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.
int32_t SDDS_IdentifyType(char *typeName)
Identifies the SDDS data type based on its string name.
int32_t SDDS_StringIsBlank(char *s)
Checks if a string is blank (contains only whitespace characters).
int32_t SDDS_MatchColumns(SDDS_DATASET *SDDS_dataset, char ***nameReturn, int32_t matchMode, int32_t typeMode,...)
Matches and retrieves column names from an SDDS dataset based on specified criteria.
int32_t SDDS_GetColumnType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a column in the SDDS dataset by its index.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
int32_t SDDS_CheckParameter(SDDS_DATASET *SDDS_dataset, char *name, char *units, int32_t type, FILE *fp_message)
Checks if a parameter exists in the SDDS dataset with the specified name, units, and type.
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
long edit_string(char *text, char *edit)
Edits the provided text based on the specified edit commands.
#define SDDS_NUM_TYPES
Total number of defined SDDS data types.
#define SDDS_ULONG
Identifier for the unsigned 32-bit integer data type.
#define SDDS_FLOAT
Identifier for the float data type.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_ULONG64
Identifier for the unsigned 64-bit integer data type.
#define SDDS_FLOATING_TYPE(type)
Checks if the given type identifier corresponds to a floating-point type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_SHORT
Identifier for the signed short integer data type.
#define SDDS_CHARACTER
Identifier for the character data type.
#define SDDS_USHORT
Identifier for the unsigned short 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.
#define SDDS_LONGDOUBLE
Identifier for the long double data type.
#define SDDS_LONG64
Identifier for the signed 64-bit integer data type.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
htab * hcreate(word logsize)
Create a hash table.
word hadd(htab *t, ub1 *key, ub4 keyl, void *stuff)
Add an item to the hash table.
word hfind(htab *t, ub1 *key, ub4 keyl)
Find an item with a given key in the hash table.
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.
char * pad_with_spaces(char *s, int n)
Adds a specified number of spaces to the end of a string.
int replaceString(char *t, char *s, char *orig, char *repl, long count_limit, long here)
Replace occurrences of one string with another string with additional options.
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)
void free_scanargs(SCANNED_ARG **scanned, int argc)
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.
char * trim_spaces(char *s)
Trims leading and trailing spaces from a string.