1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
/*******************************************************************************
*
* McXtrace, x-ray tracing package
* Copyright, All rights reserved
* DTU Physics, Kgs. Lyngby, Denmark
* Synchrotron SOLEIL, Saint-Aubin, France
*
* Component: File
*
* %I
*
* Written by: Greg Tucker
* Date: 2024
* Origin: ESS
*
* File.comp - allows to generate instrument/component input-files
* from METADATA blocks
*
* %D
* File.comp - allows to generate instrument/component input-files
* from METADATA blocks - see test_File.instr for an example.
*
* %P
* Input parameters:
* filename: [string] Filename for output-file generated from metadata block
* metadatakey: [string] METADATA-key for looking up file content (may belong to File instance or another comp)
* keep: [1] Flag to indicate if file should be kept post-simulation
*
* %E
*******************************************************************************/
DEFINE COMPONENT File
SETTING PARAMETERS (string filename=0, string metadatakey, int keep=0)
SHARE
%{
%}
DECLARE
%{
char * key;
char * name;
%}
INITIALIZE
%{
// Sort-out the key that we are searching for
if (metadatakey == NULL || metadatakey[0] == '\0'){
key = malloc((strlen(NAME_CURRENT_COMP) + 1) * sizeof(char));
strcpy(key, NAME_CURRENT_COMP);
} else {
key = malloc((strlen(metadatakey) + 1) * sizeof(char));
strcpy(key, metadatakey);
}
int matches = metadata_table_defined(num_metadata, metadata_table, key);
if (matches != 1) {
// 0 would mean _no_ matches; maybe they metadata name without the component was given?
if (matches == 0 && strcmp(key, NAME_CURRENT_COMP)) {
free(key);
key = malloc((strlen(NAME_CURRENT_COMP) + strlen(metadatakey) + 2) * sizeof(char));
sprintf(key, "%s:%s", NAME_CURRENT_COMP, metadatakey);
matches = metadata_table_defined(num_metadata, metadata_table, key);
}
// there's a bug in metadata_table_defined that returns num_metadata if key == NULL
// But since key _isn't_ null, any other result _should_ mean key == A_COMPONENT_NAME
// _and_ that component defines _multiple_ METADATA entries.
else {
printf("%s: There are %d METADATA entries that match %s; please select only one of:\n", NAME_CURRENT_COMP, matches, key);
metadata_table_print_component_keys(num_metadata, metadata_table, key);
exit(1);
}
}
if (metadata_table_defined(num_metadata, metadata_table, key) != 1) {
fprintf(stderr, "%s: No unique metadata defined with key %s\n", NAME_CURRENT_COMP, key);
exit(1);
}
char * name_part = metadata_table_key_literal(key);
if (name_part == NULL) {
#if defined(__MCCODE_VERSION__) && __MCCODE_VERSION__ >= 305000L
// We already restrict that only one key matches.
name_part = metadata_table_name(num_metadata, metadata_table, key);
// is key NAME_CURRENT_COMP or something provided by the user? We don't know
// so we _must_ cycle the key allocation to make this work.
char * new_key = malloc((strlen(key) + strlen(name_part) + 2) * sizeof(char));
sprintf(new_key, "%s:%s", key, name_part);
free(key);
key = new_key;
#else
printf("Can not update the key automatically prior to McCode v3.5.0, please fix %s usage or upgrade\n", NAME_CURRENT_COMP);
exit(1);
#endif
}
if (filename == NULL || filename[0] == '\0'){
char * comp_part = metadata_table_key_component(key);
name = malloc((strlen(comp_part) + strlen(name_part) + 2) * sizeof(char));
sprintf(name, "%s_%s", comp_part, name_part);
free(comp_part);
} else{
name = malloc((strlen(filename) + 1) * sizeof(char));
strcpy(name, filename);
}
free(name_part);
// Read the file contents and write them to file now.
FILE * file_ptr = fopen(name, "w");
fprintf(file_ptr, "%s", metadata_table_literal(num_metadata, metadata_table, key));
fclose(file_ptr);
%}
TRACE
%{
// Do nothing
%}
FINALLY
%{
// (Optionally) Remove the file now that the runtime is done
if (!keep && !remove(name)) {
fprintf(stderr, "%s: Could not remove file %s\n", NAME_CURRENT_COMP, name);
}
if (key) free(key);
if (name) free(name);
%}
MCDISPLAY
%{
%}
END
|