19#include "match_string.h"
25# if defined(__BORLANDC__)
26# define _setmode(handle, amode) setmode(handle, amode)
46 if (
sizeof(gzFile) !=
sizeof(
void *)) {
47 SDDS_SetError(
"gzFile is not the same size as void *, possible corruption of the SDDS_LAYOUT structure");
55 SDDS_SetError(
"Unable to copy layout--can't zero SDDS_DATASET structure (SDDS_InitializeCopy)");
58 if (strcmp(filemode,
"r") == 0) {
59 filemode = FOPEN_READ_MODE;
60 SDDS_target->mode = SDDS_READMODE;
61 }
else if (strcmp(filemode,
"w") == 0) {
62 filemode = FOPEN_WRITE_MODE;
63 SDDS_target->mode = SDDS_WRITEMODE;
65 SDDS_target->pagecount_offset = NULL;
66 if (!(strcmp(filemode,
"r") == 0 || strcmp(filemode,
"w") == 0 || strcmp(filemode,
"rb") == 0 || strcmp(filemode,
"wb") == 0 || strcmp(filemode,
"m") == 0)) {
67 SDDS_SetError(
"Programming error--invalid file mode (SDDS_InitializeCopy)");
71 SDDS_target->layout.popenUsed = 0;
72 SDDS_target->layout.gzipFile = 0;
73 SDDS_target->layout.lzmaFile = 0;
76 sprintf(s,
"unable to open file %s for copy--file is locked (SDDS_InitializeCopy)", filename);
81 if ((extension = strrchr(filename,
'.')) && ((strcmp(extension,
".xz") == 0) || (strcmp(extension,
".lzma") == 0))) {
82 SDDS_target->layout.lzmaFile = 1;
84 sprintf(s,
"Unable to open file %s (SDDS_InitializeCopy)", filename);
88 if (!(SDDS_target->layout.lzmafp = lzma_open(filename, filemode))) {
89 sprintf(s,
"Unable to open file %s for writing (SDDS_InitializeCopy)", filename);
93 SDDS_target->layout.fp = SDDS_target->layout.lzmafp->fp;
95 if (!filemode || !(SDDS_target->layout.fp = fopen(filename, filemode))) {
96 sprintf(s,
"Unable to open file %s (SDDS_InitializeCopy)", filename);
101 if ((strcmp(filemode,
"w") == 0 || strcmp(filemode,
"wb") == 0) && !
SDDS_LockFile(SDDS_target->layout.fp, filename,
"SDDS_InitializeCopy"))
104 SDDS_SetError(
"Memory allocation failure (SDDS_InitializeCopy)");
108 if ((extension = strrchr(filename,
'.')) && strcmp(extension,
".gz") == 0) {
109 SDDS_target->layout.gzipFile = 1;
110 if ((SDDS_target->layout.gzfp = gzdopen(fileno(SDDS_target->layout.fp), filemode)) == NULL) {
111 sprintf(s,
"Unable to open compressed file %s for writing (SDDS_InitializeCopy)", filename);
118 SDDS_target->layout.filename = NULL;
119 SDDS_target->layout.fp = NULL;
120 SDDS_target->mode = SDDS_MEMMODE;
122 if (strcmp(filemode,
"w") == 0 || strcmp(filemode,
"wb") == 0)
123 SDDS_target->layout.fp = stdout;
124 else if (strcmp(filemode,
"r") == 0 || strcmp(filemode,
"rb") == 0)
125 SDDS_target->layout.fp = stdin;
132 if (strcmp(filemode,
"m") != 0) {
133 if (_setmode(_fileno(SDDS_target->layout.fp), _O_BINARY) == -1) {
134 sprintf(s,
"unable to set stdout or stdin to binary mode");
142 SDDS_target->page_number = SDDS_target->page_started = 0;
166 source = &SDDS_source->layout;
169 for (i = 0; i < source->n_columns; i++)
172 source->column_definition[i].symbol, source->column_definition[i].units, source->column_definition[i].description, source->column_definition[i].format_string, source->column_definition[i].type, source->column_definition[i].field_length) < 0) {
174 SDDS_SetError(
"Unable to define column (SDDS_AppendLayout)");
178 for (i = 0; i < source->n_parameters; i++)
181 source->parameter_definition[i].symbol, source->parameter_definition[i].units, source->parameter_definition[i].description, source->parameter_definition[i].format_string, source->parameter_definition[i].type, source->parameter_definition[i].fixed_value) < 0) {
183 SDDS_SetError(
"Unable to define parameter (SDDS_AppendLayout)");
187 for (i = 0; i < source->n_associates; i++)
189 SDDS_DefineAssociate(SDDS_target, source->associate_definition[i].name, source->associate_definition[i].filename, source->associate_definition[i].path, source->associate_definition[i].description, source->associate_definition[i].contents, source->associate_definition[i].sdds) < 0) {
191 SDDS_SetError(
"Unable to define associate (SDDS_AppendLayout)");
195 for (i = 0; i < source->n_arrays; i++)
198 source->array_definition[i].symbol,
199 source->array_definition[i].units, source->array_definition[i].description,
200 source->array_definition[i].format_string, source->array_definition[i].type, source->array_definition[i].field_length, source->array_definition[i].dimensions, source->array_definition[i].group_name) < 0) {
230 target = &SDDS_target->layout;
231 source = &SDDS_source->layout;
232 target->version = source->version;
233 target->data_mode = source->data_mode;
234 target->data_mode.no_row_counts = 0;
235 target->data_mode.fixed_row_count = 0;
236 target->data_mode.column_memory_mode = DEFAULT_COLUMN_MEMORY_MODE;
237 target->layout_written = 0;
238 target->byteOrderDeclared = 0;
239 if (source->description)
241 if (source->contents)
244 for (i = 0; i < source->n_columns; i++)
245 if (
SDDS_DefineColumn(SDDS_target, source->column_definition[i].name, source->column_definition[i].symbol,
246 source->column_definition[i].units, source->column_definition[i].description, source->column_definition[i].format_string, source->column_definition[i].type, source->column_definition[i].field_length) < 0) {
250 for (i = 0; i < source->n_parameters; i++)
251 if (
SDDS_DefineParameter(SDDS_target, source->parameter_definition[i].name, source->parameter_definition[i].symbol,
252 source->parameter_definition[i].units, source->parameter_definition[i].description, source->parameter_definition[i].format_string, source->parameter_definition[i].type, source->parameter_definition[i].fixed_value) < 0) {
253 SDDS_SetError(
"Unable to define parameter (SDDS_CopyLayout)");
257 for (i = 0; i < source->n_associates; i++)
258 if (
SDDS_DefineAssociate(SDDS_target, source->associate_definition[i].name, source->associate_definition[i].filename, source->associate_definition[i].path, source->associate_definition[i].description, source->associate_definition[i].contents, source->associate_definition[i].sdds) < 0) {
259 SDDS_SetError(
"Unable to define associate (SDDS_CopyLayout)");
263 for (i = 0; i < source->n_arrays; i++)
264 if (
SDDS_DefineArray(SDDS_target, source->array_definition[i].name, source->array_definition[i].symbol,
265 source->array_definition[i].units, source->array_definition[i].description,
266 source->array_definition[i].format_string, source->array_definition[i].type, source->array_definition[i].field_length, source->array_definition[i].dimensions, source->array_definition[i].group_name) < 0) {
287 int32_t i, target_index;
289 char messageBuffer[1024];
291 if (!buffer && !(buffer =
SDDS_Malloc(
sizeof(
char) * 16))) {
301 for (i = 0; i < SDDS_source->layout.n_parameters; i++) {
302 if ((target_index =
SDDS_GetParameterIndex(SDDS_target, SDDS_source->layout.parameter_definition[i].name)) < 0)
304 if (SDDS_source->layout.parameter_definition[i].type != SDDS_target->layout.parameter_definition[target_index].type) {
306 sprintf(messageBuffer,
"Can't cast between nonnumeric types for parameters %s and %s (SDDS_CopyParameters)", SDDS_source->layout.parameter_definition[i].name, SDDS_target->layout.parameter_definition[target_index].name);
310 if (!
SDDS_SetParameters(SDDS_target, SDDS_SET_BY_INDEX | SDDS_PASS_BY_REFERENCE, target_index,
SDDS_CastValue(SDDS_source->parameter[i], 0, SDDS_source->layout.parameter_definition[i].type, SDDS_target->layout.parameter_definition[target_index].type, buffer), -1)) {
311 sprintf(messageBuffer,
"Error setting parameter with cast value for parameters %s and %s (SDDS_CopyParameters)", SDDS_source->layout.parameter_definition[i].name, SDDS_target->layout.parameter_definition[target_index].name);
315 }
else if (!
SDDS_SetParameters(SDDS_target, SDDS_SET_BY_INDEX | SDDS_PASS_BY_REFERENCE, target_index, SDDS_source->parameter[i], -1)) {
316 sprintf(messageBuffer,
"Unable to copy parameters for parameters %s and %s (SDDS_CopyParameters)", SDDS_source->layout.parameter_definition[i].name, SDDS_target->layout.parameter_definition[target_index].name);
335 int32_t i, j, target_index;
336 char messageBuffer[1024];
338 for (i = 0; i < SDDS_source->layout.n_arrays; i++) {
339 if ((target_index =
SDDS_GetArrayIndex(SDDS_target, SDDS_source->layout.array_definition[i].name)) < 0)
341 SDDS_target->array[target_index].definition = SDDS_target->layout.array_definition + target_index;
342 SDDS_target->array[target_index].elements = SDDS_source->array[i].elements;
343 if (!(SDDS_target->array[target_index].dimension = (int32_t *)
SDDS_Malloc(
sizeof(*SDDS_target->array[i].dimension) * SDDS_target->array[target_index].definition->dimensions)) ||
344 !(SDDS_target->array[target_index].data =
SDDS_Realloc(SDDS_target->array[target_index].data,
SDDS_type_size[SDDS_target->array[target_index].definition->type - 1] * SDDS_target->array[target_index].elements))) {
345 SDDS_SetError(
"Unable to copy arrays--allocation failure (SDDS_CopyArrays)");
349 for (j = 0; j < SDDS_target->array[target_index].definition->dimensions; j++)
350 SDDS_target->array[target_index].dimension[j] = SDDS_source->array[i].dimension[j];
351 if (!SDDS_source->array[i].data) {
352 SDDS_target->array[target_index].data = NULL;
355 if (SDDS_source->layout.array_definition[i].type != SDDS_target->layout.array_definition[target_index].type) {
357 sprintf(messageBuffer,
"Can't cast between nonnumeric types for parameters %s and %s (SDDS_CopyArrays)", SDDS_source->layout.array_definition[i].name, SDDS_target->layout.array_definition[target_index].name);
361 for (j = 0; j < SDDS_source->array[i].elements; j++) {
362 if (!
SDDS_CastValue(SDDS_source->array[i].data, j, SDDS_source->layout.array_definition[i].type, SDDS_target->layout.array_definition[target_index].type, (
char *)(SDDS_target->array[target_index].data) + j *
SDDS_type_size[SDDS_target->layout.array_definition[target_index].type - 1])) {
368 if (SDDS_target->array[target_index].definition->type !=
SDDS_STRING)
369 memcpy(SDDS_target->array[target_index].data, SDDS_source->array[i].data,
SDDS_type_size[SDDS_target->array[target_index].definition->type - 1] * SDDS_target->array[target_index].elements);
370 else if (!
SDDS_CopyStringArray(SDDS_target->array[target_index].data, SDDS_source->array[i].data, SDDS_target->array[target_index].elements)) {
389 int32_t target_index;
390 SDDS_target->n_rows = 0;
391 if (SDDS_target->layout.n_columns && SDDS_target->n_rows_allocated < SDDS_source->n_rows) {
392 SDDS_SetError(
"Unable to copy columns--insufficient memory allocated to target table");
395 if (!SDDS_target->layout.n_columns)
397 for (i = 0; i < SDDS_source->layout.n_columns; i++) {
398 if ((target_index =
SDDS_GetColumnIndex(SDDS_target, SDDS_source->layout.column_definition[i].name)) < 0)
400 if (SDDS_source->layout.column_definition[i].type !=
SDDS_STRING) {
401 if (SDDS_source->layout.column_definition[i].type == SDDS_target->layout.column_definition[target_index].type)
402 memcpy(SDDS_target->data[target_index], SDDS_source->data[i],
SDDS_type_size[SDDS_source->layout.column_definition[i].type - 1] * SDDS_source->n_rows);
406 SDDS_SetError(
"Can't cast between nonnumeric types (SDDS_CopyColumns)");
409 for (j = 0; j < SDDS_source->n_rows; j++) {
410 if (!
SDDS_CastValue(SDDS_source->data[i], j, SDDS_source->layout.column_definition[i].type, SDDS_target->layout.column_definition[target_index].type, (
char *)(SDDS_target->data[target_index]) + j *
SDDS_type_size[SDDS_target->layout.column_definition[target_index].type - 1])) {
416 }
else if (!
SDDS_CopyStringArray(SDDS_target->data[target_index], SDDS_source->data[i], SDDS_source->n_rows)) {
420 SDDS_target->column_flag[target_index] = 1;
421 SDDS_target->column_order[target_index] = target_index;
423 SDDS_target->n_rows = SDDS_source->n_rows;
424 if (SDDS_target->row_flag)
425 for (i = 0; i < SDDS_target->n_rows; i++)
426 SDDS_target->row_flag[i] = 1;
441 int32_t size, target_index;
444 int64_t *rowList, roi;
447 if (!SDDS_target->layout.n_columns)
450 if (roi > SDDS_target->n_rows_allocated) {
451 SDDS_SetError(
"Unable to copy rows of interest--insufficient memory allocated to target page (SDDS_CopyRowsOfInterest)");
455 rowList = malloc(
sizeof(*rowList) * roi);
457 for (j = 0; j < SDDS_source->n_rows; j++) {
458 if (SDDS_source->row_flag[j]) {
464 for (i = 0; i < SDDS_source->layout.n_columns; i++) {
465 if ((target_index =
SDDS_GetColumnIndex(SDDS_target, SDDS_source->layout.column_definition[i].name)) < 0)
467 if (SDDS_source->layout.column_definition[i].type !=
SDDS_STRING) {
468 if (SDDS_source->layout.column_definition[i].type == SDDS_target->layout.column_definition[target_index].type) {
469 size =
SDDS_type_size[SDDS_source->layout.column_definition[i].type - 1];
470 for (k = 0; k < roi; k++) {
471 memcpy((
char *)SDDS_target->data[target_index] + k * size, (
char *)SDDS_source->data[i] + rowList[k] * size,
SDDS_type_size[SDDS_source->layout.column_definition[i].type - 1]);
474 for (k = 0; k < roi; k++) {
476 SDDS_source->layout.column_definition[i].type, SDDS_target->layout.column_definition[target_index].type, (
char *)(SDDS_target->data[target_index]) + k *
SDDS_type_size[SDDS_target->layout.column_definition[target_index].type - 1])) {
477 sprintf(buffer,
"Problem with cast for column %s (SDDS_CopyRowsOfInterest)", SDDS_source->layout.column_definition[i].name);
484 if (SDDS_source->layout.column_definition[i].type != SDDS_target->layout.column_definition[target_index].type) {
485 sprintf(buffer,
"Unable to copy columns---inconsistent data types for %s (SDDS_CopyRowsOfInterest)", SDDS_source->layout.column_definition[i].name);
489 for (k = 0; k < roi; k++) {
490 if (((
char **)SDDS_target->data[target_index])[k])
491 free(((
char **)SDDS_target->data[target_index])[k]);
492 if (!
SDDS_CopyString(&((
char **)SDDS_target->data[target_index])[k], ((
char **)SDDS_source->data[i])[rowList[k]])) {
493 SDDS_SetError(
"Unable to copy rows (SDDS_CopyRowsOfInterest)");
498 SDDS_target->column_flag[target_index] = 1;
499 SDDS_target->column_order[target_index] = target_index;
502 SDDS_target->n_rows = roi;
503 if (SDDS_target->row_flag)
504 for (i = 0; i < SDDS_target->n_rows; i++)
505 SDDS_target->row_flag[i] = 1;
521 int32_t size, target_index;
524 if (SDDS_target->n_rows_allocated < (sum = SDDS_target->n_rows + SDDS_source->n_rows) && !
SDDS_LengthenTable(SDDS_target, sum - SDDS_target->n_rows_allocated)) {
525 SDDS_SetError(
"Unable to copy additional rows (SDDS_CopyAdditionalRows)");
528 if (SDDS_target->layout.n_columns == 0)
530 for (i = 0; i < SDDS_source->layout.n_columns; i++) {
531 if ((target_index =
SDDS_GetColumnIndex(SDDS_target, SDDS_source->layout.column_definition[i].name)) < 0)
534 if (SDDS_source->layout.column_definition[i].type !=
SDDS_STRING) {
535 if (SDDS_source->layout.column_definition[i].type == SDDS_target->layout.column_definition[target_index].type) {
536 memcpy((
char *)SDDS_target->data[target_index] + size * SDDS_target->n_rows, SDDS_source->data[i],
SDDS_type_size[SDDS_source->layout.column_definition[i].type - 1] * SDDS_source->n_rows);
538 for (j = 0; j < SDDS_source->n_rows; j++) {
540 SDDS_source->layout.column_definition[i].type, SDDS_target->layout.column_definition[target_index].type, (
char *)(SDDS_target->data[target_index]) + (j + SDDS_target->n_rows) *
SDDS_type_size[SDDS_target->layout.column_definition[target_index].type - 1])) {
541 sprintf(buffer,
"Problem with cast for column %s (SDDS_CopyAdditionalRows)", SDDS_source->layout.column_definition[i].name);
548 if (SDDS_source->layout.column_definition[i].type != SDDS_target->layout.column_definition[target_index].type) {
549 sprintf(buffer,
"Unable to copy columns---inconsistent data types for %s (SDDS_CopyAdditionalRows)", SDDS_source->layout.column_definition[i].name);
553 if (!
SDDS_CopyStringArray((
char **)((
char *)SDDS_target->data[target_index] + size * SDDS_target->n_rows), SDDS_source->data[i], SDDS_source->n_rows)) {
554 SDDS_SetError(
"Unable to copy columns (SDDS_CopyAdditionalRows)");
558 SDDS_target->column_flag[target_index] = 1;
559 SDDS_target->column_order[target_index] = target_index;
561 SDDS_target->n_rows += SDDS_source->n_rows;
562 if (SDDS_target->row_flag)
563 for (i = 0; i < SDDS_target->n_rows; i++)
564 SDDS_target->row_flag[i] = 1;
584 if (!
SDDS_StartPage(SDDS_target, SDDS_target->layout.n_columns ? SDDS_source->n_rows : 0)) {
604 SDDS_dataset->deferSavingLayout = mode;
618 if (SDDS_dataset->deferSavingLayout)
624 if ((source = &SDDS_dataset->layout) == (target = &SDDS_dataset->original_layout)) {
625 SDDS_SetError(
"\"original\" and working page layouts share memory!");
630 if (source->n_columns) {
632 SDDS_SetError(
"Unable to save layout--allocation failure (SDDS_SaveLayout)");
635 memcpy((
char *)target->column_definition, (
char *)source->column_definition,
sizeof(
COLUMN_DEFINITION) * source->n_columns);
636 memcpy((
char *)target->column_index, (
char *)source->column_index,
sizeof(
SORTED_INDEX *) * source->n_columns);
638 if (source->n_parameters) {
639 if (!(target->parameter_definition =
642 SDDS_SetError(
"Unable to save layout--allocation failure (SDDS_SaveLayout)");
645 memcpy((
char *)target->parameter_definition, (
char *)source->parameter_definition,
sizeof(
PARAMETER_DEFINITION) * source->n_parameters);
646 memcpy((
char *)target->parameter_index, (
char *)source->parameter_index,
sizeof(
SORTED_INDEX *) * source->n_parameters);
648 if (source->n_arrays) {
650 SDDS_SetError(
"Unable to save layout--allocation failure (SDDS_SaveLayout)");
653 memcpy((
char *)target->array_definition, (
char *)source->array_definition,
sizeof(
ARRAY_DEFINITION) * source->n_arrays);
654 memcpy((
char *)target->array_index, (
char *)source->array_index,
sizeof(
SORTED_INDEX *) * source->n_arrays);
656 if (source->n_associates) {
658 SDDS_SetError(
"Unable to save layout--allocation failure (SDDS_SaveLayout)");
661 memcpy((
char *)target->associate_definition, (
char *)source->associate_definition,
sizeof(
ASSOCIATE_DEFINITION) * source->n_associates);
664 target->n_columns = source->n_columns;
665 target->n_parameters = source->n_parameters;
666 target->n_associates = source->n_associates;
667 target->n_arrays = source->n_arrays;
668 target->description = source->description;
669 target->contents = source->contents;
670 target->version = source->version;
671 target->data_mode = source->data_mode;
672 target->filename = source->filename;
673 target->fp = source->fp;
674 target->popenUsed = source->popenUsed;
676 if (SDDS_dataset->layout.n_columns) {
677 if (!(SDDS_dataset->column_track_memory = (
short *)
SDDS_Realloc(SDDS_dataset->column_track_memory,
sizeof(
short) * SDDS_dataset->layout.n_columns))) {
678 SDDS_SetError(
"memory allocation failure (SDDS_SaveLayout)");
681 if (!
SDDS_SetMemory(SDDS_dataset->column_track_memory, SDDS_dataset->layout.n_columns,
SDDS_SHORT, (
short)1, (
short)0)) {
682 SDDS_SetError(
"Unable to initialize memory (SDDS_SaveLayout)");
703 source = &SDDS_dataset->original_layout;
704 target = &SDDS_dataset->layout;
707 if (source->n_columns) {
708 if (target->column_definition == source->column_definition) {
709 SDDS_SetError(
"Unable to restore layout--column definition pointers are the same (SDDS_RestoreLayout)");
713 SDDS_SetError(
"Unable to restore layout--allocation failure (SDDS_RestoreLayout)");
716 memcpy((
char *)target->column_definition, (
char *)source->column_definition,
sizeof(
COLUMN_DEFINITION) * source->n_columns);
718 if (source->n_parameters) {
719 if (target->parameter_definition == source->parameter_definition) {
720 SDDS_SetError(
"Unable to restore layout--parameter definition pointers are the same (SDDS_RestoreLayout)");
724 SDDS_SetError(
"Unable to restore layout--allocation failure (SDDS_RestoreLayout)");
727 memcpy((
char *)target->parameter_definition, (
char *)source->parameter_definition,
sizeof(
PARAMETER_DEFINITION) * source->n_parameters);
729 if (source->n_arrays) {
730 if (target->array_definition == source->array_definition) {
731 SDDS_SetError(
"Unable to restore layout--array definition pointers are the same (SDDS_RestoreLayout)");
735 SDDS_SetError(
"Unable to restore layout--allocation failure (SDDS_RestoreLayout)");
738 memcpy((
char *)target->array_definition, (
char *)source->array_definition,
sizeof(
ARRAY_DEFINITION) * source->n_arrays);
740 if (source->n_associates) {
741 if (target->associate_definition == source->associate_definition) {
742 SDDS_SetError(
"Unable to restore layout--associate definition pointers are the same (SDDS_RestoreLayout)");
746 SDDS_SetError(
"Unable to restore layout--allocation failure (SDDS_RestoreLayout)");
749 memcpy((
char *)target->associate_definition, (
char *)source->associate_definition,
sizeof(
ASSOCIATE_DEFINITION) * source->n_associates);
752 target->n_columns = source->n_columns;
753 target->n_parameters = source->n_parameters;
754 target->n_associates = source->n_associates;
755 target->n_arrays = source->n_arrays;
756 target->description = source->description;
757 target->contents = source->contents;
758 target->version = source->version;
759 target->data_mode = source->data_mode;
760 target->filename = source->filename;
761 target->fp = source->fp;
779 int64_t i, j, source_row;
787 if (target_row >= SDDS_target->n_rows_allocated) {
788 SDDS_SetError(
"Unable to copy row--target page not large enough");
791 if (SDDS_target->n_rows <= target_row)
792 SDDS_target->n_rows = target_row + 1;
795 for (i = j = 0; i < SDDS_source->n_rows; i++)
796 if (SDDS_source->row_flag[i] && j++ == source_srow) {
801 if (source_row == -1) {
802 SDDS_SetError(
"Unable to copy row--source selected-row does not exist");
806 for (i = 0; i < SDDS_target->layout.n_columns; i++) {
807 if ((j =
SDDS_GetColumnIndex(SDDS_source, SDDS_target->layout.column_definition[i].name)) < 0 || !SDDS_source->column_flag[j])
810 if (!
SDDS_CopyString(((
char ***)SDDS_target->data)[i] + target_row, ((
char ***)SDDS_source->data)[j][source_row])) {
811 SDDS_SetError(
"Unable to copy row--string copy failed (SDDS_CopyRow)");
816 memcpy((
char *)SDDS_target->data[i] + size * target_row, (
char *)SDDS_source->data[j] + size * source_row, size);
818 SDDS_target->row_flag[target_row] = 1;
843 if (target_row >= SDDS_target->n_rows_allocated) {
844 SDDS_SetError(
"Unable to copy row--target page not large enough");
847 if (SDDS_target->n_rows <= target_row)
848 SDDS_target->n_rows = target_row + 1;
849 if (source_row >= SDDS_source->n_rows_allocated) {
850 SDDS_SetError(
"Unable to copy row--source row non-existent");
854 for (i = 0; i < SDDS_target->layout.n_columns; i++) {
855 if ((j =
SDDS_GetColumnIndex(SDDS_source, SDDS_target->layout.column_definition[i].name)) < 0 || !SDDS_source->column_flag[j])
858 if (!
SDDS_CopyString(((
char ***)SDDS_target->data)[i] + target_row, ((
char ***)SDDS_source->data)[j][source_row])) {
859 SDDS_SetError(
"Unable to copy row--string copy failed (SDDS_CopyRow)");
864 memcpy((
char *)SDDS_target->data[i] + size * target_row, (
char *)SDDS_source->data[j] + size * source_row, size);
866 SDDS_target->row_flag[target_row] = 1;
884 int32_t size, target_index;
886 int64_t *rowList, roi;
889 if (!SDDS_target->layout.n_columns)
891 roi = lastRow - firstRow + 1;
892 if (roi > SDDS_target->n_rows_allocated) {
893 SDDS_SetError(
"Unable to copy rows of interest--insufficient memory allocated to target page (SDDS_CopyRows)");
896 rowList = malloc(
sizeof(*rowList) * roi);
899 for (j = firstRow; j <= lastRow; j++) {
904 for (i = 0; i < SDDS_source->layout.n_columns; i++) {
905 if ((target_index =
SDDS_GetColumnIndex(SDDS_target, SDDS_source->layout.column_definition[i].name)) < 0)
907 if (SDDS_source->layout.column_definition[i].type !=
SDDS_STRING) {
908 if (SDDS_source->layout.column_definition[i].type == SDDS_target->layout.column_definition[target_index].type) {
909 size =
SDDS_type_size[SDDS_source->layout.column_definition[i].type - 1];
910 for (k = 0; k < roi; k++) {
912 memcpy((
char *)SDDS_target->data[target_index] + k * size, (
char *)SDDS_source->data[i] + rowList[k] * size,
SDDS_type_size[SDDS_source->layout.column_definition[i].type - 1]);
915 for (k = 0; k < roi; k++) {
917 SDDS_source->layout.column_definition[i].type, SDDS_target->layout.column_definition[target_index].type, (
char *)(SDDS_target->data[target_index]) + k *
SDDS_type_size[SDDS_target->layout.column_definition[target_index].type - 1])) {
918 sprintf(buffer,
"Problem with cast for column %s (SDDS_CopyRows)", SDDS_source->layout.column_definition[i].name);
925 if (SDDS_source->layout.column_definition[i].type != SDDS_target->layout.column_definition[target_index].type) {
926 sprintf(buffer,
"Unable to copy columns---inconsistent data types for %s (SDDS_CopyRows)", SDDS_source->layout.column_definition[i].name);
930 for (k = 0; k < roi; k++) {
931 if (((
char **)SDDS_target->data[target_index])[k])
932 free(((
char **)SDDS_target->data[target_index])[k]);
933 if (!
SDDS_CopyString(&((
char **)SDDS_target->data[target_index])[k], ((
char **)SDDS_source->data[i])[rowList[k]])) {
939 SDDS_target->column_flag[target_index] = 1;
940 SDDS_target->column_order[target_index] = target_index;
945 SDDS_target->n_rows = roi;
946 if (SDDS_target->row_flag) {
947 for (i = 0; i < roi; i++) {
948 SDDS_target->row_flag[i] = 1;
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
void SDDS_DeferSavingLayout(SDDS_DATASET *SDDS_dataset, int32_t mode)
int32_t SDDS_CopyLayout(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_CopyColumns(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_CopyParameters(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_SaveLayout(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_AppendLayout(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, uint32_t mode)
int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode)
int32_t SDDS_CopyAdditionalRows(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_CopyRowDirect(SDDS_DATASET *SDDS_target, int64_t target_row, SDDS_DATASET *SDDS_source, int64_t source_row)
int32_t SDDS_CopyPage(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_CopyArrays(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_CopyRow(SDDS_DATASET *SDDS_target, int64_t target_row, SDDS_DATASET *SDDS_source, int64_t source_srow)
int32_t SDDS_CopyRows(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, int64_t firstRow, int64_t lastRow)
int32_t SDDS_CopyRowsOfInterest(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_RestoreLayout(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_type_size[SDDS_NUM_TYPES]
Array of sizes for each supported data type.
int32_t SDDS_LengthenTable(SDDS_DATASET *SDDS_dataset, int64_t n_additional_rows)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetParameters(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
Internal definitions and function declarations for SDDS with LZMA support.
int32_t SDDS_DefineAssociate(SDDS_DATASET *SDDS_dataset, const char *name, const char *filename, const char *path, const char *description, const char *contents, int32_t sdds)
Defines an associate for the SDDS dataset.
int32_t SDDS_DefineArray(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, int32_t field_length, int32_t dimensions, const char *group_name)
Defines a data array within the SDDS dataset.
int32_t SDDS_DefineColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, int32_t field_length)
Defines a data column within the SDDS dataset.
int32_t SDDS_DefineParameter(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, char *fixed_value)
Defines a data parameter with a fixed string value.
int32_t SDDS_FileIsLocked(const char *filename)
Determines if a specified file is locked.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
int32_t SDDS_ZeroMemory(void *mem, int64_t n_bytes)
Sets a block of memory to zero.
int32_t SDDS_GetArrayIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named array 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.
void * SDDS_CastValue(void *data, int64_t index, int32_t data_type, int32_t desired_type, void *memory)
Casts a value from one SDDS data type to another.
int32_t SDDS_SetMemory(void *mem, int64_t n_elements, int32_t data_type,...)
Initializes a memory block with a sequence of values based on a specified data type.
int32_t SDDS_CheckDataset(SDDS_DATASET *SDDS_dataset, const char *caller)
Validates the SDDS dataset pointer.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
int32_t SDDS_CopyStringArray(char **target, char **source, int64_t n_strings)
Copies an array of strings from source to target.
void * SDDS_Malloc(size_t size)
Allocates memory of a specified size.
int32_t SDDS_GetTypeSize(int32_t type)
Retrieves the size in bytes of a specified SDDS data type.
int32_t SDDS_LockFile(FILE *fp, const char *filename, const char *caller)
Attempts to lock a specified file.
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.
int32_t SDDS_GetAssociateIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named associate in the SDDS dataset.
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.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_SHORT
Identifier for the signed short integer data type.
#define SDDS_NUMERIC_TYPE(type)
Checks if the given type identifier corresponds to any numeric type.