Ejemplo n.º 1
0
int32_t SDDS_TransferAllArrayDefinitions(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source,
                                      uint32_t mode)
{
  SDDS_LAYOUT *target, *source;
  int32_t i;

  if (!SDDS_CheckDataset(SDDS_target, "SDDS_TransferAllArrayDefinitions"))
    return(0);
  if (!SDDS_CheckDataset(SDDS_source, "SDDS_TransferAllArrayDefinitions"))
    return(0);
  if (mode) {
    /* haven't done this one yet */
    SDDS_SetError("Nonzero mode not supported for arrays (SDDS_TransferAllArrayDefinitions)");
    return 0;
  }
  target = &SDDS_target->layout;
  source = &SDDS_source->layout;
  SDDS_DeferSavingLayout(1);
  for (i=0; i<source->n_arrays; i++)
    if (SDDS_DefineArray(SDDS_target, source->array_definition[i].name, source->array_definition[i].symbol,
                         source->array_definition[i].units, source->array_definition[i].description,
                         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) {
      SDDS_SetError("Unable to define parameter (SDDS_TransferAllArrayDefinitions)");
      SDDS_DeferSavingLayout(0);
      return 0;
    }
  SDDS_DeferSavingLayout(0);
  return 1;
}
Ejemplo n.º 2
0
int32_t SDDS_CopyParameters(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
{
  int32_t i, target_index;
  static char *buffer = NULL; /* will be sized to hold any SDDS data type with room to spare */
  char messageBuffer[1024];
  
  if (!buffer && !(buffer=SDDS_Malloc(sizeof(char)*16))) {
    SDDS_SetError("Allocation failure (SDDS_CopyParameters)");
    return 0;
  }

  if (!SDDS_CheckDataset(SDDS_target, "SDDS_CopyParameters"))
    return(0);
  if (!SDDS_CheckDataset(SDDS_source, "SDDS_CopyParameters"))
    return(0);

  for (i=0; i<SDDS_source->layout.n_parameters; i++) {
    if ((target_index = SDDS_GetParameterIndex(SDDS_target, SDDS_source->layout.parameter_definition[i].name))<0)
      continue;
    if (SDDS_source->layout.parameter_definition[i].type!=
        SDDS_target->layout.parameter_definition[target_index].type) {
      if (!SDDS_NUMERIC_TYPE(SDDS_source->layout.parameter_definition[i].type) ||
          !SDDS_NUMERIC_TYPE(SDDS_target->layout.parameter_definition[target_index].type)) {
        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);
        SDDS_SetError(messageBuffer);
        return 0;
      }
      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)) {
        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);
        SDDS_SetError(messageBuffer);
        return 0;
      }
    }
    else if (!SDDS_SetParameters(SDDS_target, SDDS_SET_BY_INDEX|SDDS_PASS_BY_REFERENCE, target_index,
                                 SDDS_source->parameter[i], -1)) {
      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);
      SDDS_SetError(messageBuffer);
      return(0);
    }
  }
  return(1);
}
Ejemplo n.º 3
0
int32_t SDDS_CopyRow(SDDS_DATASET *SDDS_target, int32_t target_row, SDDS_DATASET *SDDS_source, int32_t source_srow)
{
  int32_t i, j, source_row, size, type;

  if (!SDDS_CheckDataset(SDDS_target, "SDDS_CopyRow"))
    return(0);
  if (!SDDS_CheckDataset(SDDS_source, "SDDS_CopyRow"))
    return(0);
  
  if (target_row>=SDDS_target->n_rows_allocated) {
    SDDS_SetError("Unable to copy row--target page not large enough");
    return(0);
  }
  if (SDDS_target->n_rows<=target_row)
    SDDS_target->n_rows = target_row+1;

  source_row = -1;
  for (i=j=0; i<SDDS_source->n_rows; i++)
    if (SDDS_source->row_flag[i] && j++==source_srow) {
      source_row = i;
      break;
    }

  if (source_row==-1) {
    SDDS_SetError("Unable to copy row--source selected-row does not exist");
    return(0);
  }

  for (i=0; i<SDDS_target->layout.n_columns; i++) {
    if ((j=SDDS_GetColumnIndex(SDDS_source, SDDS_target->layout.column_definition[i].name))<0 ||
        !SDDS_source->column_flag[j])
      continue;
    if ((type=SDDS_GetColumnType(SDDS_target, i))==SDDS_STRING) {
      if (!SDDS_CopyString(((char***)SDDS_target->data)[i]+target_row, ((char***)SDDS_source->data)[j][source_row])) {
        SDDS_SetError("Unable to copy row--string copy failed (SDDS_CopyRow)");
        return(0);
      }
    }
    else {
      size = SDDS_type_size[type-1];
      memcpy((char*)SDDS_target->data[i]+size*target_row, (char*)SDDS_source->data[j]+size*source_row, size);
    }
    SDDS_target->row_flag[target_row] = 1;
  }
  return(1);
}
Ejemplo n.º 4
0
int32_t SDDS_CopyPage(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
{
  /*  int32_t i, index, column_count, j;*/
  /*  SDDS_LAYOUT target_layout, source_layout;*/
  if (!SDDS_CheckDataset(SDDS_target, "SDDS_CopyPage"))
    return(0);
  if (!SDDS_CheckDataset(SDDS_source, "SDDS_CopyPage"))
    return(0);

  if (!SDDS_StartPage(SDDS_target, SDDS_target->layout.n_columns?SDDS_source->n_rows:0)) {
    SDDS_SetError("Unable to copy page (SDDS_CopyPage)");
    return(0);
  }
  if (!SDDS_CopyParameters(SDDS_target, SDDS_source))
    return(0);
  if (!SDDS_CopyArrays(SDDS_target, SDDS_source))
    return(0);
  if (!SDDS_CopyColumns(SDDS_target, SDDS_source))
    return(0);
  return(1);
}
Ejemplo n.º 5
0
int32_t SDDS_TransferAllColumnDefinitions(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source,
                                       uint32_t mode)
{
  SDDS_LAYOUT *target, *source;
  int32_t i, index;
  char messBuffer[1024];
  
  if (!SDDS_CheckDataset(SDDS_target, "SDDS_TransferAllColumnDefinitions"))
    return(0);
  if (!SDDS_CheckDataset(SDDS_source, "SDDS_TransferAllColumnDefinitions"))
    return(0);
  if (mode&SDDS_TRANSFER_KEEPOLD && mode&SDDS_TRANSFER_OVERWRITE) {
    SDDS_SetError("Inconsistent mode flags (SDDS_TransferAllColumnDefinitions)");
    return 0;
  }
  target = &SDDS_target->layout;
  source = &SDDS_source->layout;
  SDDS_DeferSavingLayout(1);
  for (i=0; i<source->n_columns; i++)
    if ((index=SDDS_GetParameterIndex(SDDS_target, source->parameter_definition[i].name))>=0) {
      /* already exists */
      if (mode&SDDS_TRANSFER_KEEPOLD) 
        continue;
      if (!(mode&SDDS_TRANSFER_OVERWRITE)) {
        sprintf(messBuffer,
                "Unable to define parameter %s---already exists (SDDS_TransferAllParameterDefinitions)",
                source->parameter_definition[i].name);
        SDDS_SetError(messBuffer);
        SDDS_DeferSavingLayout(0);
        return 0;
      }
      if (source->column_definition[i].type!=target->column_definition[index].type 
          && SDDS_target->n_rows_allocated) {
        sprintf(messBuffer, 
                "Unable to define parameter %s---type mismatch and table already allocated (SDDS_TransferAllParameterDefinitions)",
                source->parameter_definition[i].name);
        SDDS_SetError(messBuffer);
        SDDS_DeferSavingLayout(0);
        return 0;
      }
      if (!SDDS_ChangeColumnInformation(SDDS_target, "symbol", 
                                        &source->column_definition[i].symbol,
                                        SDDS_BY_INDEX, index) ||
          !SDDS_ChangeColumnInformation(SDDS_target, "units", 
                                        &source->column_definition[i].units,
                                        SDDS_BY_INDEX, index) ||
          !SDDS_ChangeColumnInformation(SDDS_target, "description", 
                                        &source->column_definition[i].description,
                                        SDDS_BY_INDEX, index) ||
          !SDDS_ChangeColumnInformation(SDDS_target, "format_string", 
                                        &source->column_definition[i].format_string,
                                        SDDS_BY_INDEX, index) ||
          !SDDS_ChangeColumnInformation(SDDS_target, "type", 
                                        &source->column_definition[i].type,
                                        SDDS_BY_INDEX, index) ||
          !SDDS_ChangeColumnInformation(SDDS_target, "field_length", 
                                        &source->column_definition[i].field_length,
                                        SDDS_BY_INDEX, index)) {
        SDDS_SetError("Unable to define column---problem with overwrite (SDDS_TransferAllColumnDefinitions)");
        SDDS_DeferSavingLayout(0);
        return 0;
      }
      target->column_definition[index].definition_mode = 
        source->column_definition[index].definition_mode;
      if (target->column_definition[index].type==SDDS_STRING)
        target->column_definition[index].memory_number =
          SDDS_CreateRpnMemory(source->column_definition[i].name, 1);
      else
        target->column_definition[index].memory_number =
          SDDS_CreateRpnMemory(source->column_definition[i].name, 0);
    } else {
      if (SDDS_DefineColumn(SDDS_target, source->column_definition[i].name, 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) {
        SDDS_SetError("Unable to define column (SDDS_TransferAllColumnDefinitions)");
        SDDS_DeferSavingLayout(0);
        return 0;
      }
    }
  SDDS_DeferSavingLayout(0);
  return 1;
}
Ejemplo n.º 6
0
int32_t SDDS_RestoreLayout(SDDS_DATASET *SDDS_dataset)
{
  SDDS_LAYOUT *source, *target;

  if (!SDDS_CheckDataset(SDDS_dataset, "SDDS_RestoreLayout"))
    return(0);

  source = &SDDS_dataset->original_layout;
  target = &SDDS_dataset->layout;

  /* copy pointer elements of structure into new memory */
  if (source->n_columns) {
    if (target->column_definition==source->column_definition) {
      SDDS_SetError("Unable to restore layout--column definition pointers are the same  (SDDS_RestoreLayout)");
      return(0);
    }
    if (!(target->column_definition =
          (COLUMN_DEFINITION*)SDDS_Realloc((void*)target->column_definition, 
                                           sizeof(COLUMN_DEFINITION)*source->n_columns))) {
      SDDS_SetError("Unable to restore layout--allocation failure (SDDS_RestoreLayout)");
      return(0);
    }
    memcpy((char*)target->column_definition, (char*)source->column_definition, sizeof(COLUMN_DEFINITION)*source->n_columns);
  }
  if (source->n_parameters) {
    if (target->parameter_definition==source->parameter_definition) {
      SDDS_SetError("Unable to restore layout--parameter definition pointers are the same  (SDDS_RestoreLayout)");
      return(0);
    }
    if (!(target->parameter_definition =
          (PARAMETER_DEFINITION*)SDDS_Realloc((void*)target->parameter_definition, 
                                              sizeof(PARAMETER_DEFINITION)*source->n_parameters))) {
      SDDS_SetError("Unable to restore layout--allocation failure (SDDS_RestoreLayout)");
      return(0);
    }
    memcpy((char*)target->parameter_definition, (char*)source->parameter_definition, 
           sizeof(PARAMETER_DEFINITION)*source->n_parameters);
  }
  if (source->n_arrays) {
    if (target->array_definition==source->array_definition) {
      SDDS_SetError("Unable to restore layout--array definition pointers are the same  (SDDS_RestoreLayout)");
      return(0);
    }
    if (!(target->array_definition =
          (ARRAY_DEFINITION*)SDDS_Realloc((void*)target->array_definition, 
                                          sizeof(ARRAY_DEFINITION)*source->n_arrays))) {
      SDDS_SetError("Unable to restore layout--allocation failure (SDDS_RestoreLayout)");
      return(0);
    }
    memcpy((char*)target->array_definition, (char*)source->array_definition, sizeof(ARRAY_DEFINITION)*source->n_arrays);
  }
  if (source->n_associates) {
    if (target->associate_definition==source->associate_definition) {
      SDDS_SetError("Unable to restore layout--associate definition pointers are the same  (SDDS_RestoreLayout)");
      return(0);
    }
    if (!(target->associate_definition =
          (ASSOCIATE_DEFINITION*)SDDS_Realloc((void*)target->associate_definition, 
                                              sizeof(ASSOCIATE_DEFINITION)*source->n_associates))) {
      SDDS_SetError("Unable to restore layout--allocation failure (SDDS_RestoreLayout)");
      return(0);
    }
    memcpy((char*)target->associate_definition, (char*)source->associate_definition, 
           sizeof(ASSOCIATE_DEFINITION)*source->n_associates);
  }

  target->n_columns = source->n_columns;
  target->n_parameters = source->n_parameters;
  target->n_associates = source->n_associates;
  target->n_arrays = source->n_arrays;
  target->description = source->description;
  target->contents = source->contents;
  target->version = source->version;
  target->data_mode = source->data_mode;
  target->filename = source->filename;
  target->fp = source->fp;
  
  return(1);
}
Ejemplo n.º 7
0
int32_t SDDS_SaveLayout(SDDS_DATASET *SDDS_dataset)
{
  SDDS_LAYOUT *source, *target;

  if (deferSavingLayout)
    return 1;

  if (!SDDS_CheckDataset(SDDS_dataset, "SDDS_SaveLayout"))
    return(0);

  if ((source = &SDDS_dataset->layout)==(target = &SDDS_dataset->original_layout)) {
    SDDS_SetError("\"original\" and working page layouts share memory!");
    SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors);
  }

  /* copy pointer elements of structure into new memory */
  if (source->n_columns) {
    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))) {
      SDDS_SetError("Unable to save layout--allocation failure (SDDS_SaveLayout)");
      return(0);
    }
    memcpy((char*)target->column_definition, (char*)source->column_definition, 
           sizeof(COLUMN_DEFINITION)*source->n_columns);
    memcpy((char*)target->column_index, (char*)source->column_index, 
           sizeof(SORTED_INDEX*)*source->n_columns);
  }
  if (source->n_parameters) {
    if (!(target->parameter_definition =
          (PARAMETER_DEFINITION*)SDDS_Realloc((void*)target->parameter_definition, 
                                              sizeof(PARAMETER_DEFINITION)*source->n_parameters)) || 
        !(target->parameter_index = 
          (SORTED_INDEX**)SDDS_Realloc((void*)target->parameter_index,
                                      sizeof(SORTED_INDEX*)*source->n_parameters))) {
      SDDS_SetError("Unable to save layout--allocation failure (SDDS_SaveLayout)");
      return(0);
    }
    memcpy((char*)target->parameter_definition, (char*)source->parameter_definition, 
           sizeof(PARAMETER_DEFINITION)*source->n_parameters);
    memcpy((char*)target->parameter_index, (char*)source->parameter_index, 
           sizeof(SORTED_INDEX*)*source->n_parameters);
  }
  if (source->n_arrays) {
    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))) {
      SDDS_SetError("Unable to save layout--allocation failure (SDDS_SaveLayout)");
      return(0);
    }
    memcpy((char*)target->array_definition, (char*)source->array_definition, sizeof(ARRAY_DEFINITION)*source->n_arrays);
    memcpy((char*)target->array_index, (char*)source->array_index, sizeof(SORTED_INDEX*)*source->n_arrays);
  }
  if (source->n_associates) {
    if (!(target->associate_definition =
          (ASSOCIATE_DEFINITION*)SDDS_Realloc((void*)target->associate_definition, 
                                              sizeof(ASSOCIATE_DEFINITION)*source->n_associates))) {
      SDDS_SetError("Unable to save layout--allocation failure (SDDS_SaveLayout)");
      return(0);
    }
    memcpy((char*)target->associate_definition, (char*)source->associate_definition, 
           sizeof(ASSOCIATE_DEFINITION)*source->n_associates);
  }

  target->n_columns = source->n_columns;
  target->n_parameters = source->n_parameters;
  target->n_associates = source->n_associates;
  target->n_arrays = source->n_arrays;
  target->description = source->description;
  target->contents = source->contents;
  target->version = source->version;
  target->data_mode = source->data_mode;
  target->filename = source->filename;
  target->fp = source->fp;
  target->popenUsed = source->popenUsed;

  return(1);
}
Ejemplo n.º 8
0
int32_t SDDS_CopyLayout(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
{
  SDDS_LAYOUT *target, *source;
  int32_t i;

  if (!SDDS_CheckDataset(SDDS_target, "SDDS_CopyLayout"))
    return(0);
  if (!SDDS_CheckDataset(SDDS_source, "SDDS_CopyLayout"))
    return(0);
  target = &SDDS_target->layout;
  source = &SDDS_source->layout;
  target->version = source->version;
  target->data_mode = source->data_mode;
  target->data_mode.no_row_counts = 0;
  target->data_mode.fixed_row_count = 0;
  target->layout_written = 0;
  target->byteOrderDeclared = 0;
  if (source->description)
    SDDS_CopyString(&target->description, source->description);
  if (source->contents)
    SDDS_CopyString(&target->contents, source->contents);        
  SDDS_DeferSavingLayout(1);
  for (i=0; i<source->n_columns; i++)
    if (SDDS_DefineColumn(SDDS_target, source->column_definition[i].name, 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) {
      SDDS_SetError("Unable to define column (SDDS_CopyLayout)");
      return(0);
    }
  for (i=0; i<source->n_parameters; i++)
    if (SDDS_DefineParameter(SDDS_target, source->parameter_definition[i].name, 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) {
      SDDS_SetError("Unable to define parameter (SDDS_CopyLayout)");
      return(0);
    }
  
  for (i=0; i<source->n_associates; i++) 
    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) {
      SDDS_SetError("Unable to define associate (SDDS_CopyLayout)");
      return(0);
    }
  
  for (i=0; i<source->n_arrays; i++)
    if (SDDS_DefineArray(SDDS_target, source->array_definition[i].name, source->array_definition[i].symbol,
                         source->array_definition[i].units, source->array_definition[i].description,
                         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) {
      SDDS_SetError("Unable to define array (SDDS_CopyLayout)");
      return(0);
    }
  SDDS_DeferSavingLayout(0);
  if (!SDDS_SaveLayout(SDDS_target))  {
    SDDS_SetError("Unable to save layout (SDDS_CopyLayout)");
    return(0);
  }
  return(1);
}
Ejemplo n.º 9
0
int32_t SDDS_AppendLayout(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, uint32_t mode)
{
  SDDS_LAYOUT *source;
  int32_t i;

  if (!SDDS_CheckDataset(SDDS_target, "SDDS_AppendLayout"))
    return(0);
  if (!SDDS_CheckDataset(SDDS_source, "SDDS_AppendLayout"))
    return(0);
  source = &SDDS_source->layout;
  SDDS_DeferSavingLayout(1);

  for (i=0; i<source->n_columns; i++)
    if (SDDS_GetColumnIndex(SDDS_target, source->column_definition[i].name)<0 &&
        SDDS_DefineColumn(SDDS_target, source->column_definition[i].name, 
                          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) {
      SDDS_DeferSavingLayout(0);
      SDDS_SetError("Unable to define column (SDDS_AppendLayout)");
      return(0);
    }

  for (i=0; i<source->n_parameters; i++)
    if (SDDS_GetParameterIndex(SDDS_target, source->parameter_definition[i].name)<0 &&
        SDDS_DefineParameter(SDDS_target, source->parameter_definition[i].name, 
                             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) {
      SDDS_DeferSavingLayout(0);
      SDDS_SetError("Unable to define parameter (SDDS_AppendLayout)");
      return(0);
    }
  
  for (i=0; i<source->n_associates; i++) 
    if (SDDS_GetAssociateIndex(SDDS_target,source->associate_definition[i].name)<0 &&
        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) {
      SDDS_DeferSavingLayout(0);
      SDDS_SetError("Unable to define associate (SDDS_AppendLayout)");
      return(0);
    }
  
  for (i=0; i<source->n_arrays; i++)
    if (SDDS_GetArrayIndex(SDDS_target, source->array_definition[i].name)<0 &&
        SDDS_DefineArray(SDDS_target, source->array_definition[i].name,
                         source->array_definition[i].symbol,
                         source->array_definition[i].units, source->array_definition[i].description,
                         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) {
      SDDS_DeferSavingLayout(0);
      SDDS_SetError("Unable to define array (SDDS_AppendLayout)");
      return(0);
    }
  SDDS_DeferSavingLayout(0);
  if (!SDDS_SaveLayout(SDDS_target))  {
    SDDS_SetError("Unable to save layout (SDDS_AppendLayout)");
    return(0);
  }
  return(1);
}
Ejemplo n.º 10
0
int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode)
{
  char s[SDDS_MAXLINE];
#if defined(zLib)
  char *extension;
#endif

  if (sizeof(gzFile) != sizeof(void*)) {
    SDDS_SetError("gzFile is not the same size as void *, possible corruption of the SDDS_LAYOUT structure");
    return(0);
  }
  if (!SDDS_CheckDataset(SDDS_source, "SDDS_InitializeCopy"))
    return(0);
  if (!SDDS_CheckDataset(SDDS_target, "SDDS_InitializeCopy"))
    return(0);
  if (!SDDS_ZeroMemory((void *)SDDS_target, sizeof(SDDS_DATASET))) {
    SDDS_SetError("Unable to copy layout--can't zero SDDS_DATASET structure (SDDS_InitializeCopy)");
    return(0);
  }
  if (strcmp(filemode, "r")==0) {
    filemode = FOPEN_READ_MODE;
    SDDS_target->mode=SDDS_READMODE;
  } else if (strcmp(filemode, "w")==0) {
    filemode = FOPEN_WRITE_MODE;
    SDDS_target->mode=SDDS_WRITEMODE;
  }
  SDDS_target->pagecount_offset=NULL;
  if (!(strcmp(filemode, "r")==0 || strcmp(filemode, "w")==0 ||
	strcmp(filemode, "rb")==0 || strcmp(filemode, "wb")==0 ||
	strcmp(filemode, "m")==0)) {
    SDDS_SetError("Programming error--invalid file mode (SDDS_InitializeCopy)");
    return(0);
  }

  SDDS_target->layout.popenUsed = 0;
  SDDS_target->layout.gzipFile = 0;
  SDDS_target->layout.lzmaFile = 0;
  if (filename) {
    if (SDDS_FileIsLocked(filename)) {
      sprintf(s, "unable to open file %s for copy--file is locked (SDDS_InitializeCopy)", filename);
      SDDS_SetError(s);
      return 0;
    }

    if ((extension = strrchr(filename, '.')) && ((strcmp(extension, ".xz")==0) || (strcmp(extension, ".lzma")==0))) {
      SDDS_target->layout.lzmaFile = 1;
      if (!filemode) {
        sprintf(s, "Unable to open file %s (SDDS_InitializeCopy)", filename);
        SDDS_SetError(s);
        return(0);
      }
      if (!(SDDS_target->layout.lzmafp=lzma_open(filename, filemode))) {
	sprintf(s, "Unable to open file %s for writing (SDDS_InitializeCopy)", filename);
	SDDS_SetError(s);
	return 0;
      }
      SDDS_target->layout.fp = SDDS_target->layout.lzmafp->fp;
    } else {
      if (!filemode || !(SDDS_target->layout.fp=fopen(filename, filemode))) {
        sprintf(s, "Unable to open file %s (SDDS_InitializeCopy)", filename);
        SDDS_SetError(s);
        return(0);
      }
    }
    if ((strcmp(filemode, "w")==0 || strcmp(filemode, "wb")==0) && 
	!SDDS_LockFile(SDDS_target->layout.fp, filename, "SDDS_InitializeCopy"))
      return 0;
    if (!SDDS_CopyString(&SDDS_target->layout.filename, filename)) {
      SDDS_SetError("Memory allocation failure (SDDS_InitializeCopy)");
      return(0);
    }
#if defined(zLib)
    if ((extension = strrchr(filename, '.')) && strcmp(extension, ".gz")==0) {
      SDDS_target->layout.gzipFile = 1;
      if ((SDDS_target->layout.gzfp=gzdopen(fileno(SDDS_target->layout.fp), filemode)) == NULL) {
	sprintf(s, "Unable to open compressed file %s for writing (SDDS_InitializeCopy)", filename);
	SDDS_SetError(s);
	return 0;
      }
    }
#endif
  }
  else {
    SDDS_target->layout.filename = NULL;
    SDDS_target->layout.fp = NULL;
    SDDS_target->mode=SDDS_MEMMODE;
    if (filemode) {
      if (strcmp(filemode, "w")==0 || strcmp(filemode, "wb")==0) 
        SDDS_target->layout.fp = stdout;
      else if (strcmp(filemode, "r")==0 || strcmp(filemode, "rb")==0)
        SDDS_target->layout.fp = stdin;
      
      /*      else if (strcmp(filemode, "m")!=0) {
        SDDS_SetError("Unknown filemode (SDDS_InitializeCopy)");
        return(0);
	}*/
#if defined(_WIN32)
      if (strcmp(filemode, "m")!=0) {
	if (_setmode(_fileno(SDDS_target->layout.fp), _O_BINARY) == -1) {
	  sprintf(s, "unable to set stdout or stdin to binary mode");
	  SDDS_SetError(s);
	  return 0;
	}      
      }
#endif

    }
  }
  SDDS_target->page_number =SDDS_target->page_started= 0;
  if (!SDDS_CopyLayout(SDDS_target, SDDS_source))
    return(0);
  return(1);
}