SDDSlib
Loading...
Searching...
No Matches
SDDS_copy.c
Go to the documentation of this file.
1/**
2 * @file SDDS_copy.c
3 * @brief This file contains the functions related to copying SDDS data.
4 *
5 * The SDDS_copy.c file provides functions for copying data from SDDS files.
6 *
7 * @copyright
8 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
9 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
10 *
11 * @license
12 * This file is distributed under the terms of the Software License Agreement
13 * found in the file LICENSE included with this distribution.
14 *
15 * @author M. Borland, C. Saunders, R. Soliday, H. Shang
16 */
17
18#include "mdb.h"
19#include "match_string.h"
20#include "SDDS.h"
21#include "SDDS_internal.h"
22#if defined(_WIN32)
23# include <fcntl.h>
24# include <io.h>
25# if defined(__BORLANDC__)
26# define _setmode(handle, amode) setmode(handle, amode)
27# endif
28#endif
29
30/**
31 * Initializes an SDDS_DATASET structure in preparation for copying a data table from another SDDS_DATASET structure.
32 *
33 * @param SDDS_target Address of SDDS_DATASET structure into which to copy data.
34 * @param SDDS_source Address of SDDS_DATASET structure from which to copy data.
35 * @param filename A NULL-terminated character string giving a filename to be associated with the new SDDS_DATASET. Typically, the name of a file to which the copied data will be written after modification. Ignored if NULL.
36 * @param filemode A NULL-terminated character string giving the fopen file mode to be used to open the file named by filename. Ignored if filename is NULL.
37 *
38 * @return 1 on success. On failure, returns 0 and records an error message.
39 */
40int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode) {
41 char s[SDDS_MAXLINE];
42#if defined(zLib)
43 char *extension;
44#endif
45
46 if (sizeof(gzFile) != sizeof(void *)) {
47 SDDS_SetError("gzFile is not the same size as void *, possible corruption of the SDDS_LAYOUT structure");
48 return (0);
49 }
50 if (!SDDS_CheckDataset(SDDS_source, "SDDS_InitializeCopy"))
51 return (0);
52 if (!SDDS_CheckDataset(SDDS_target, "SDDS_InitializeCopy"))
53 return (0);
54 if (!SDDS_ZeroMemory((void *)SDDS_target, sizeof(SDDS_DATASET))) {
55 SDDS_SetError("Unable to copy layout--can't zero SDDS_DATASET structure (SDDS_InitializeCopy)");
56 return (0);
57 }
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;
64 }
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)");
68 return (0);
69 }
70
71 SDDS_target->layout.popenUsed = 0;
72 SDDS_target->layout.gzipFile = 0;
73 SDDS_target->layout.lzmaFile = 0;
74 if (filename) {
75 if (SDDS_FileIsLocked(filename)) {
76 sprintf(s, "unable to open file %s for copy--file is locked (SDDS_InitializeCopy)", filename);
78 return 0;
79 }
80
81 if ((extension = strrchr(filename, '.')) && ((strcmp(extension, ".xz") == 0) || (strcmp(extension, ".lzma") == 0))) {
82 SDDS_target->layout.lzmaFile = 1;
83 if (!filemode) {
84 sprintf(s, "Unable to open file %s (SDDS_InitializeCopy)", filename);
86 return (0);
87 }
88 if (!(SDDS_target->layout.lzmafp = lzma_open(filename, filemode))) {
89 sprintf(s, "Unable to open file %s for writing (SDDS_InitializeCopy)", filename);
91 return 0;
92 }
93 SDDS_target->layout.fp = SDDS_target->layout.lzmafp->fp;
94 } else {
95 if (!filemode || !(SDDS_target->layout.fp = fopen(filename, filemode))) {
96 sprintf(s, "Unable to open file %s (SDDS_InitializeCopy)", filename);
98 return (0);
99 }
100 }
101 if ((strcmp(filemode, "w") == 0 || strcmp(filemode, "wb") == 0) && !SDDS_LockFile(SDDS_target->layout.fp, filename, "SDDS_InitializeCopy"))
102 return 0;
103 if (!SDDS_CopyString(&SDDS_target->layout.filename, filename)) {
104 SDDS_SetError("Memory allocation failure (SDDS_InitializeCopy)");
105 return (0);
106 }
107#if defined(zLib)
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);
112 SDDS_SetError(s);
113 return 0;
114 }
115 }
116#endif
117 } else {
118 SDDS_target->layout.filename = NULL;
119 SDDS_target->layout.fp = NULL;
120 SDDS_target->mode = SDDS_MEMMODE;
121 if (filemode) {
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;
126
127 /* else if (strcmp(filemode, "m")!=0) {
128 SDDS_SetError("Unknown filemode (SDDS_InitializeCopy)");
129 return(0);
130 } */
131#if defined(_WIN32)
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");
135 SDDS_SetError(s);
136 return 0;
137 }
138 }
139#endif
140 }
141 }
142 SDDS_target->page_number = SDDS_target->page_started = 0;
143 if (!SDDS_CopyLayout(SDDS_target, SDDS_source))
144 return (0);
145 return (1);
146}
147
148/**
149 * Appends layout definitions (columns, parameters, associates, arrays) from one SDDS_DATASET to another.
150 * Only definitions that do not already exist in the target dataset are added.
151 *
152 * @param SDDS_target Address of the SDDS_DATASET structure to which layout definitions will be appended.
153 * @param SDDS_source Address of the SDDS_DATASET structure from which layout definitions will be taken.
154 * @param mode Mode flag (currently unused; can be set to 0).
155 *
156 * @return Returns 1 on success; 0 on failure, with an error message recorded.
157 */
158int32_t SDDS_AppendLayout(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, uint32_t mode) {
159 SDDS_LAYOUT *source;
160 int64_t i;
161
162 if (!SDDS_CheckDataset(SDDS_target, "SDDS_AppendLayout"))
163 return (0);
164 if (!SDDS_CheckDataset(SDDS_source, "SDDS_AppendLayout"))
165 return (0);
166 source = &SDDS_source->layout;
167 SDDS_DeferSavingLayout(SDDS_target, 1);
168
169 for (i = 0; i < source->n_columns; i++)
170 if (SDDS_GetColumnIndex(SDDS_target, source->column_definition[i].name) < 0 &&
171 SDDS_DefineColumn(SDDS_target, source->column_definition[i].name,
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) {
173 SDDS_DeferSavingLayout(SDDS_target, 0);
174 SDDS_SetError("Unable to define column (SDDS_AppendLayout)");
175 return (0);
176 }
177
178 for (i = 0; i < source->n_parameters; i++)
179 if (SDDS_GetParameterIndex(SDDS_target, source->parameter_definition[i].name) < 0 &&
180 SDDS_DefineParameter(SDDS_target, source->parameter_definition[i].name,
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) {
182 SDDS_DeferSavingLayout(SDDS_target, 0);
183 SDDS_SetError("Unable to define parameter (SDDS_AppendLayout)");
184 return (0);
185 }
186
187 for (i = 0; i < source->n_associates; i++)
188 if (SDDS_GetAssociateIndex(SDDS_target, source->associate_definition[i].name) < 0 &&
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) {
190 SDDS_DeferSavingLayout(SDDS_target, 0);
191 SDDS_SetError("Unable to define associate (SDDS_AppendLayout)");
192 return (0);
193 }
194
195 for (i = 0; i < source->n_arrays; i++)
196 if (SDDS_GetArrayIndex(SDDS_target, source->array_definition[i].name) < 0 &&
197 SDDS_DefineArray(SDDS_target, source->array_definition[i].name,
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) {
201 SDDS_DeferSavingLayout(SDDS_target, 0);
202 SDDS_SetError("Unable to define array (SDDS_AppendLayout)");
203 return (0);
204 }
205 SDDS_DeferSavingLayout(SDDS_target, 0);
206 if (!SDDS_SaveLayout(SDDS_target)) {
207 SDDS_SetError("Unable to save layout (SDDS_AppendLayout)");
208 return (0);
209 }
210 return (1);
211}
212
213/**
214 * Copies the entire layout (including version, data mode, description, contents, columns, parameters, associates, and arrays) from one SDDS_DATASET to another.
215 * The target dataset's existing layout will be replaced.
216 *
217 * @param SDDS_target Address of the SDDS_DATASET structure into which the layout will be copied.
218 * @param SDDS_source Address of the SDDS_DATASET structure from which the layout will be copied.
219 *
220 * @return Returns 1 on success; 0 on failure, with an error message recorded.
221 */
222int32_t SDDS_CopyLayout(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source) {
223 SDDS_LAYOUT *target, *source;
224 int64_t i;
225
226 if (!SDDS_CheckDataset(SDDS_target, "SDDS_CopyLayout"))
227 return (0);
228 if (!SDDS_CheckDataset(SDDS_source, "SDDS_CopyLayout"))
229 return (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)
240 SDDS_CopyString(&target->description, source->description);
241 if (source->contents)
242 SDDS_CopyString(&target->contents, source->contents);
243 SDDS_DeferSavingLayout(SDDS_target, 1);
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) {
247 SDDS_SetError("Unable to define column (SDDS_CopyLayout)");
248 return (0);
249 }
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)");
254 return (0);
255 }
256
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)");
260 return (0);
261 }
262
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) {
267 SDDS_SetError("Unable to define array (SDDS_CopyLayout)");
268 return (0);
269 }
270 SDDS_DeferSavingLayout(SDDS_target, 0);
271 if (!SDDS_SaveLayout(SDDS_target)) {
272 SDDS_SetError("Unable to save layout (SDDS_CopyLayout)");
273 return (0);
274 }
275 return (1);
276}
277
278/**
279 * Copies parameter values from one SDDS_DATASET structure into another for parameters with matching names.
280 *
281 * @param SDDS_target Address of the SDDS_DATASET structure into which parameter values will be copied.
282 * @param SDDS_source Address of the SDDS_DATASET structure from which parameter values will be copied.
283 *
284 * @return Returns 1 on success; 0 on failure, with an error message recorded.
285 */
286int32_t SDDS_CopyParameters(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source) {
287 int32_t i, target_index;
288 char *buffer = NULL; /* will be sized to hold any SDDS data type with room to spare */
289 char messageBuffer[1024];
290
291 if (!buffer && !(buffer = SDDS_Malloc(sizeof(char) * 16))) {
292 SDDS_SetError("Allocation failure (SDDS_CopyParameters)");
293 return 0;
294 }
295
296 if (!SDDS_CheckDataset(SDDS_target, "SDDS_CopyParameters"))
297 return (0);
298 if (!SDDS_CheckDataset(SDDS_source, "SDDS_CopyParameters"))
299 return (0);
300
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)
303 continue;
304 if (SDDS_source->layout.parameter_definition[i].type != SDDS_target->layout.parameter_definition[target_index].type) {
305 if (!SDDS_NUMERIC_TYPE(SDDS_source->layout.parameter_definition[i].type) || !SDDS_NUMERIC_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);
307 SDDS_SetError(messageBuffer);
308 return 0;
309 }
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);
312 SDDS_SetError(messageBuffer);
313 return 0;
314 }
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);
317 SDDS_SetError(messageBuffer);
318 return (0);
319 }
320 }
321 if (buffer)
322 free(buffer);
323 return (1);
324}
325
326/**
327 * Copies array data from one SDDS_DATASET structure into another for arrays with matching names.
328 *
329 * @param SDDS_target Address of the SDDS_DATASET structure into which array data will be copied.
330 * @param SDDS_source Address of the SDDS_DATASET structure from which array data will be copied.
331 *
332 * @return Returns 1 on success; 0 on failure, with an error message recorded.
333 */
334int32_t SDDS_CopyArrays(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source) {
335 int32_t i, j, target_index;
336 char messageBuffer[1024];
337
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)
340 continue;
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)");
346 return (0);
347 }
348
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;
353 continue;
354 }
355 if (SDDS_source->layout.array_definition[i].type != SDDS_target->layout.array_definition[target_index].type) {
356 if (!SDDS_NUMERIC_TYPE(SDDS_source->layout.array_definition[i].type) || !SDDS_NUMERIC_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);
358 SDDS_SetError(messageBuffer);
359 return 0;
360 }
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])) {
363 SDDS_SetError("Problem with cast (SDDS_CopyArrays)");
364 return 0;
365 }
366 }
367 } else {
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)) {
371 SDDS_SetError("Unable to copy arrays (SDDS_CopyArrays)");
372 return (0);
373 }
374 }
375 }
376 return (1);
377}
378
379/**
380 * Copies column data from one SDDS_DATASET structure into another for columns with matching names.
381 *
382 * @param SDDS_target Address of the SDDS_DATASET structure into which column data will be copied.
383 * @param SDDS_source Address of the SDDS_DATASET structure from which column data will be copied.
384 *
385 * @return Returns 1 on success; 0 on failure, with an error message recorded.
386 */
387int32_t SDDS_CopyColumns(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source) {
388 int64_t i, j;
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");
393 return (0);
394 }
395 if (!SDDS_target->layout.n_columns)
396 return 1;
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)
399 continue;
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);
403 else {
404 /* Do a cast between the source and target types, if they are both numeric */
405 if (!SDDS_NUMERIC_TYPE(SDDS_source->layout.column_definition[i].type) || !SDDS_NUMERIC_TYPE(SDDS_target->layout.column_definition[target_index].type)) {
406 SDDS_SetError("Can't cast between nonnumeric types (SDDS_CopyColumns)");
407 return 0;
408 }
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])) {
411 SDDS_SetError("Problem with cast (SDDS_CopyColumns)");
412 return 0;
413 }
414 }
415 }
416 } else if (!SDDS_CopyStringArray(SDDS_target->data[target_index], SDDS_source->data[i], SDDS_source->n_rows)) {
417 SDDS_SetError("Unable to copy columns (SDDS_CopyColumns)");
418 return (0);
419 }
420 SDDS_target->column_flag[target_index] = 1;
421 SDDS_target->column_order[target_index] = target_index;
422 }
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;
427 return (1);
428}
429
430/**
431 * Copies rows of interest from the source SDDS_DATASET to the target SDDS_DATASET for columns with matching names.
432 * Rows of interest are those that have their row flags set in the source dataset.
433 *
434 * @param SDDS_target Address of the SDDS_DATASET structure into which rows will be copied.
435 * @param SDDS_source Address of the SDDS_DATASET structure from which rows will be copied.
436 *
437 * @return Returns 1 on success; 0 on failure, with an error message recorded.
438 */
439int32_t SDDS_CopyRowsOfInterest(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source) {
440 int64_t i, j, k;
441 int32_t size, target_index;
442 /* int32_t rows; */
443 char buffer[1024];
444 int64_t *rowList, roi;
445 k = 0;
446
447 if (!SDDS_target->layout.n_columns)
448 return 1;
449 roi = SDDS_CountRowsOfInterest(SDDS_source);
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)");
452 return 0;
453 }
454
455 rowList = malloc(sizeof(*rowList) * roi);
456 k = 0;
457 for (j = 0; j < SDDS_source->n_rows; j++) {
458 if (SDDS_source->row_flag[j]) {
459 rowList[k] = j;
460 k++;
461 }
462 }
463
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)
466 continue;
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]);
472 }
473 } else {
474 for (k = 0; k < roi; k++) {
475 if (!SDDS_CastValue(SDDS_source->data[i], rowList[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);
478 SDDS_SetError(buffer);
479 return 0;
480 }
481 }
482 }
483 } else {
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);
486 SDDS_SetError(buffer);
487 return (0);
488 }
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)");
494 return (0);
495 }
496 }
497 }
498 SDDS_target->column_flag[target_index] = 1;
499 SDDS_target->column_order[target_index] = target_index;
500 }
501 free(rowList);
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;
506
507 return (1);
508}
509
510/**
511 * Copies additional rows from one SDDS_DATASET to another.
512 * The rows from SDDS_source are appended to the existing rows in SDDS_target.
513 *
514 * @param SDDS_target Pointer to the SDDS_DATASET structure where rows will be appended.
515 * @param SDDS_source Pointer to the SDDS_DATASET structure from which rows will be copied.
516 *
517 * @return Returns 1 on success; 0 on failure, with an error message recorded.
518 */
519int32_t SDDS_CopyAdditionalRows(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source) {
520 int64_t i, j, sum;
521 int32_t size, target_index;
522 char buffer[1024];
523
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)");
526 return (0);
527 }
528 if (SDDS_target->layout.n_columns == 0)
529 return 1;
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)
532 continue;
533 size = SDDS_GetTypeSize(SDDS_source->layout.column_definition[i].type);
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);
537 } else {
538 for (j = 0; j < SDDS_source->n_rows; j++) {
539 if (!SDDS_CastValue(SDDS_source->data[i], 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);
542 SDDS_SetError(buffer);
543 return 0;
544 }
545 }
546 }
547 } else {
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);
550 SDDS_SetError(buffer);
551 return (0);
552 }
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)");
555 return (0);
556 }
557 }
558 SDDS_target->column_flag[target_index] = 1;
559 SDDS_target->column_order[target_index] = target_index;
560 }
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;
565
566 return (1);
567}
568
569/**
570 * Copies the data from one SDDS_DATASET structure to another.
571 * This includes parameters, arrays, and columns.
572 *
573 * @param SDDS_target Pointer to the SDDS_DATASET structure where data will be copied to.
574 * @param SDDS_source Pointer to the SDDS_DATASET structure from which data will be copied.
575 *
576 * @return Returns 1 on success; 0 on failure, with an error message recorded.
577 */
578int32_t SDDS_CopyPage(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source) {
579 if (!SDDS_CheckDataset(SDDS_target, "SDDS_CopyPage"))
580 return (0);
581 if (!SDDS_CheckDataset(SDDS_source, "SDDS_CopyPage"))
582 return (0);
583
584 if (!SDDS_StartPage(SDDS_target, SDDS_target->layout.n_columns ? SDDS_source->n_rows : 0)) {
585 SDDS_SetError("Unable to copy page (SDDS_CopyPage)");
586 return (0);
587 }
588 if (!SDDS_CopyParameters(SDDS_target, SDDS_source))
589 return (0);
590 if (!SDDS_CopyArrays(SDDS_target, SDDS_source))
591 return (0);
592 if (!SDDS_CopyColumns(SDDS_target, SDDS_source))
593 return (0);
594 return (1);
595}
596
597/**
598 * Sets the flag to defer or resume saving the layout of an SDDS_DATASET.
599 *
600 * @param SDDS_dataset Pointer to the SDDS_DATASET structure.
601 * @param mode Non-zero value to defer saving the layout; zero to resume saving.
602 */
603void SDDS_DeferSavingLayout(SDDS_DATASET *SDDS_dataset, int32_t mode) {
604 SDDS_dataset->deferSavingLayout = mode;
605}
606
607/**
608 * Saves the current layout of the SDDS_DATASET.
609 * The layout is stored internally for future restoration if needed.
610 *
611 * @param SDDS_dataset Pointer to the SDDS_DATASET structure whose layout is to be saved.
612 *
613 * @return Returns 1 on success; 0 on failure, with an error message recorded.
614 */
615int32_t SDDS_SaveLayout(SDDS_DATASET *SDDS_dataset) {
616 SDDS_LAYOUT *source, *target;
617
618 if (SDDS_dataset->deferSavingLayout)
619 return 1;
620
621 if (!SDDS_CheckDataset(SDDS_dataset, "SDDS_SaveLayout"))
622 return (0);
623
624 if ((source = &SDDS_dataset->layout) == (target = &SDDS_dataset->original_layout)) {
625 SDDS_SetError("\"original\" and working page layouts share memory!");
626 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
627 }
628
629 /* copy pointer elements of structure into new memory */
630 if (source->n_columns) {
631 if (!(target->column_definition = (COLUMN_DEFINITION *)SDDS_Realloc((void *)target->column_definition, sizeof(COLUMN_DEFINITION) * source->n_columns)) || !(target->column_index = (SORTED_INDEX **)SDDS_Realloc((void *)target->column_index, sizeof(SORTED_INDEX *) * source->n_columns))) {
632 SDDS_SetError("Unable to save layout--allocation failure (SDDS_SaveLayout)");
633 return (0);
634 }
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);
637 }
638 if (source->n_parameters) {
639 if (!(target->parameter_definition =
640 (PARAMETER_DEFINITION *)SDDS_Realloc((void *)target->parameter_definition, sizeof(PARAMETER_DEFINITION) * source->n_parameters)) ||
641 !(target->parameter_index = (SORTED_INDEX **)SDDS_Realloc((void *)target->parameter_index, sizeof(SORTED_INDEX *) * source->n_parameters))) {
642 SDDS_SetError("Unable to save layout--allocation failure (SDDS_SaveLayout)");
643 return (0);
644 }
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);
647 }
648 if (source->n_arrays) {
649 if (!(target->array_definition = (ARRAY_DEFINITION *)SDDS_Realloc((void *)target->array_definition, sizeof(ARRAY_DEFINITION) * source->n_arrays)) || !(target->array_index = (SORTED_INDEX **)SDDS_Realloc((void *)target->array_index, sizeof(SORTED_INDEX *) * source->n_arrays))) {
650 SDDS_SetError("Unable to save layout--allocation failure (SDDS_SaveLayout)");
651 return (0);
652 }
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);
655 }
656 if (source->n_associates) {
657 if (!(target->associate_definition = (ASSOCIATE_DEFINITION *)SDDS_Realloc((void *)target->associate_definition, sizeof(ASSOCIATE_DEFINITION) * source->n_associates))) {
658 SDDS_SetError("Unable to save layout--allocation failure (SDDS_SaveLayout)");
659 return (0);
660 }
661 memcpy((char *)target->associate_definition, (char *)source->associate_definition, sizeof(ASSOCIATE_DEFINITION) * source->n_associates);
662 }
663
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;
675
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)");
679 return(0);
680 }
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)");
683 return (0);
684 }
685 }
686
687 return (1);
688}
689
690/**
691 * Restores a previously saved layout of the SDDS_DATASET.
692 *
693 * @param SDDS_dataset Pointer to the SDDS_DATASET structure whose layout is to be restored.
694 *
695 * @return Returns 1 on success; 0 on failure, with an error message recorded.
696 */
697int32_t SDDS_RestoreLayout(SDDS_DATASET *SDDS_dataset) {
698 SDDS_LAYOUT *source, *target;
699
700 if (!SDDS_CheckDataset(SDDS_dataset, "SDDS_RestoreLayout"))
701 return (0);
702
703 source = &SDDS_dataset->original_layout;
704 target = &SDDS_dataset->layout;
705
706 /* copy pointer elements of structure into new memory */
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)");
710 return (0);
711 }
712 if (!(target->column_definition = (COLUMN_DEFINITION *)SDDS_Realloc((void *)target->column_definition, sizeof(COLUMN_DEFINITION) * source->n_columns))) {
713 SDDS_SetError("Unable to restore layout--allocation failure (SDDS_RestoreLayout)");
714 return (0);
715 }
716 memcpy((char *)target->column_definition, (char *)source->column_definition, sizeof(COLUMN_DEFINITION) * source->n_columns);
717 }
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)");
721 return (0);
722 }
723 if (!(target->parameter_definition = (PARAMETER_DEFINITION *)SDDS_Realloc((void *)target->parameter_definition, sizeof(PARAMETER_DEFINITION) * source->n_parameters))) {
724 SDDS_SetError("Unable to restore layout--allocation failure (SDDS_RestoreLayout)");
725 return (0);
726 }
727 memcpy((char *)target->parameter_definition, (char *)source->parameter_definition, sizeof(PARAMETER_DEFINITION) * source->n_parameters);
728 }
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)");
732 return (0);
733 }
734 if (!(target->array_definition = (ARRAY_DEFINITION *)SDDS_Realloc((void *)target->array_definition, sizeof(ARRAY_DEFINITION) * source->n_arrays))) {
735 SDDS_SetError("Unable to restore layout--allocation failure (SDDS_RestoreLayout)");
736 return (0);
737 }
738 memcpy((char *)target->array_definition, (char *)source->array_definition, sizeof(ARRAY_DEFINITION) * source->n_arrays);
739 }
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)");
743 return (0);
744 }
745 if (!(target->associate_definition = (ASSOCIATE_DEFINITION *)SDDS_Realloc((void *)target->associate_definition, sizeof(ASSOCIATE_DEFINITION) * source->n_associates))) {
746 SDDS_SetError("Unable to restore layout--allocation failure (SDDS_RestoreLayout)");
747 return (0);
748 }
749 memcpy((char *)target->associate_definition, (char *)source->associate_definition, sizeof(ASSOCIATE_DEFINITION) * source->n_associates);
750 }
751
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;
762
763 return (1);
764}
765
766/**
767 * Copies a row from the source SDDS_DATASET to the target SDDS_DATASET.
768 * Only columns that exist in both datasets are copied.
769 * The source row is determined by its position among the selected rows.
770 *
771 * @param SDDS_target Pointer to the SDDS_DATASET structure where the row will be copied to.
772 * @param target_row Index of the row in the target dataset where data will be placed.
773 * @param SDDS_source Pointer to the SDDS_DATASET structure from which the row will be copied.
774 * @param source_srow Index of the selected row (among rows of interest) in the source dataset.
775 *
776 * @return Returns 1 on success; 0 on failure, with an error message recorded.
777 */
778int32_t SDDS_CopyRow(SDDS_DATASET *SDDS_target, int64_t target_row, SDDS_DATASET *SDDS_source, int64_t source_srow) {
779 int64_t i, j, source_row;
780 int32_t size, type;
781
782 if (!SDDS_CheckDataset(SDDS_target, "SDDS_CopyRow"))
783 return (0);
784 if (!SDDS_CheckDataset(SDDS_source, "SDDS_CopyRow"))
785 return (0);
786
787 if (target_row >= SDDS_target->n_rows_allocated) {
788 SDDS_SetError("Unable to copy row--target page not large enough");
789 return (0);
790 }
791 if (SDDS_target->n_rows <= target_row)
792 SDDS_target->n_rows = target_row + 1;
793
794 source_row = -1;
795 for (i = j = 0; i < SDDS_source->n_rows; i++)
796 if (SDDS_source->row_flag[i] && j++ == source_srow) {
797 source_row = i;
798 break;
799 }
800
801 if (source_row == -1) {
802 SDDS_SetError("Unable to copy row--source selected-row does not exist");
803 return (0);
804 }
805
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])
808 continue;
809 if ((type = SDDS_GetColumnType(SDDS_target, i)) == SDDS_STRING) {
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)");
812 return (0);
813 }
814 } else {
815 size = SDDS_type_size[type - 1];
816 memcpy((char *)SDDS_target->data[i] + size * target_row, (char *)SDDS_source->data[j] + size * source_row, size);
817 }
818 SDDS_target->row_flag[target_row] = 1;
819 }
820 return (1);
821}
822
823/**
824 * Copies a specific row from the source SDDS_DATASET to the target SDDS_DATASET.
825 * Only columns that exist in both datasets are copied.
826 *
827 * @param SDDS_target Pointer to the SDDS_DATASET structure where the row will be copied to.
828 * @param target_row Index of the row in the target dataset where data will be placed.
829 * @param SDDS_source Pointer to the SDDS_DATASET structure from which the row will be copied.
830 * @param source_row Index of the row in the source dataset to be copied.
831 *
832 * @return Returns 1 on success; 0 on failure, with an error message recorded.
833 */
834int32_t SDDS_CopyRowDirect(SDDS_DATASET *SDDS_target, int64_t target_row, SDDS_DATASET *SDDS_source, int64_t source_row) {
835 int64_t i, j;
836 int32_t size, type;
837
838 if (!SDDS_CheckDataset(SDDS_target, "SDDS_CopyRow"))
839 return (0);
840 if (!SDDS_CheckDataset(SDDS_source, "SDDS_CopyRow"))
841 return (0);
842
843 if (target_row >= SDDS_target->n_rows_allocated) {
844 SDDS_SetError("Unable to copy row--target page not large enough");
845 return (0);
846 }
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");
851 return (0);
852 }
853
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])
856 continue;
857 if ((type = SDDS_GetColumnType(SDDS_target, i)) == SDDS_STRING) {
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)");
860 return (0);
861 }
862 } else {
863 size = SDDS_type_size[type - 1];
864 memcpy((char *)SDDS_target->data[i] + size * target_row, (char *)SDDS_source->data[j] + size * source_row, size);
865 }
866 SDDS_target->row_flag[target_row] = 1;
867 }
868 return (1);
869}
870
871/**
872 * Copies a range of rows from the source SDDS_DATASET to the target SDDS_DATASET.
873 * Only columns that exist in both datasets are copied.
874 *
875 * @param SDDS_target Pointer to the SDDS_DATASET structure where rows will be copied to.
876 * @param SDDS_source Pointer to the SDDS_DATASET structure from which rows will be copied.
877 * @param firstRow Index of the first row to copy from the source dataset.
878 * @param lastRow Index of the last row to copy from the source dataset.
879 *
880 * @return Returns 1 on success; 0 on failure, with an error message recorded.
881 */
882int32_t SDDS_CopyRows(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, int64_t firstRow, int64_t lastRow) {
883 int64_t i, j, k;
884 int32_t size, target_index;
885 char buffer[1024];
886 int64_t *rowList, roi;
887 k = 0;
888
889 if (!SDDS_target->layout.n_columns)
890 return 1;
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)");
894 return 0;
895 }
896 rowList = malloc(sizeof(*rowList) * roi);
897 k = 0;
898
899 for (j = firstRow; j <= lastRow; j++) {
900 rowList[k] = j;
901 k++;
902 }
903
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)
906 continue;
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++) {
911
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]);
913 }
914 } else {
915 for (k = 0; k < roi; k++) {
916 if (!SDDS_CastValue(SDDS_source->data[i], rowList[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);
919 SDDS_SetError(buffer);
920 return 0;
921 }
922 }
923 }
924 } else {
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);
927 SDDS_SetError(buffer);
928 return (0);
929 }
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]])) {
934 SDDS_SetError("Unable to copy rows (SDDS_CopyRows)");
935 return (0);
936 }
937 }
938 }
939 SDDS_target->column_flag[target_index] = 1;
940 SDDS_target->column_order[target_index] = target_index;
941 }
942
943 free(rowList);
944
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;
949 }
950 }
951
952 return (1);
953}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
void SDDS_DeferSavingLayout(SDDS_DATASET *SDDS_dataset, int32_t mode)
Definition SDDS_copy.c:603
int32_t SDDS_CopyLayout(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
Definition SDDS_copy.c:222
int32_t SDDS_CopyColumns(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
Definition SDDS_copy.c:387
int32_t SDDS_CopyParameters(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
Definition SDDS_copy.c:286
int32_t SDDS_SaveLayout(SDDS_DATASET *SDDS_dataset)
Definition SDDS_copy.c:615
int32_t SDDS_AppendLayout(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, uint32_t mode)
Definition SDDS_copy.c:158
int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode)
Definition SDDS_copy.c:40
int32_t SDDS_CopyAdditionalRows(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
Definition SDDS_copy.c:519
int32_t SDDS_CopyRowDirect(SDDS_DATASET *SDDS_target, int64_t target_row, SDDS_DATASET *SDDS_source, int64_t source_row)
Definition SDDS_copy.c:834
int32_t SDDS_CopyPage(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
Definition SDDS_copy.c:578
int32_t SDDS_CopyArrays(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
Definition SDDS_copy.c:334
int32_t SDDS_CopyRow(SDDS_DATASET *SDDS_target, int64_t target_row, SDDS_DATASET *SDDS_source, int64_t source_srow)
Definition SDDS_copy.c:778
int32_t SDDS_CopyRows(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, int64_t firstRow, int64_t lastRow)
Definition SDDS_copy.c:882
int32_t SDDS_CopyRowsOfInterest(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
Definition SDDS_copy.c:439
int32_t SDDS_RestoreLayout(SDDS_DATASET *SDDS_dataset)
Definition SDDS_copy.c:697
int32_t SDDS_type_size[SDDS_NUM_TYPES]
Array of sizes for each supported data type.
Definition SDDS_data.c:62
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,...)
int64_t SDDS_CountRowsOfInterest(SDDS_DATASET *SDDS_dataset)
Counts the number of rows marked as "of interest" in the current data table.
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.
Definition SDDS_utils.c:379
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.
Definition SDDS_utils.c:552
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
Definition SDDS_utils.c:432
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.
Definition SDDS_utils.c:639
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.
Definition SDDS_utils.c:856
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
Definition SDDS_utils.c:677
#define SDDS_STRING
Identifier for the string data type.
Definition SDDStypes.h:85
#define SDDS_SHORT
Identifier for the signed short integer data type.
Definition SDDStypes.h:73
#define SDDS_NUMERIC_TYPE(type)
Checks if the given type identifier corresponds to any numeric type.
Definition SDDStypes.h:138