Skip to content

Commit dd52b96

Browse files
authored
Merge pull request #2321 from mccode-dev/format-misc
Add comp-formatter to McStas misc
2 parents 650ec7c + 9c2c391 commit dd52b96

13 files changed

Lines changed: 951 additions & 988 deletions

mcstas-comps/misc/Annulus.comp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ TRACE %{
6262

6363
MCDISPLAY
6464
%{
65-
mcdis_annulus(0,0,0,outer_radius, inner_radius, 0,1,0);
65+
mcdis_annulus (0, 0, 0, outer_radius, inner_radius, 0, 1, 0);
6666
%}
6767

6868
END

mcstas-comps/misc/Circle.comp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ TRACE %{
6060

6161
MCDISPLAY
6262
%{
63-
mcdis_new_circle(0,0,0,radius, 0,1,0);
63+
mcdis_new_circle (0, 0, 0, radius, 0, 1, 0);
6464
%}
6565

6666
END

mcstas-comps/misc/Cone.comp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ TRACE %{
5757

5858
MCDISPLAY
5959
%{
60-
mcdis_cone(0,0,0,radius,yheight, 0,1,0);
60+
mcdis_cone (0, 0, 0, radius, yheight, 0, 1, 0);
6161
%}
6262

6363
END

mcstas-comps/misc/Disc.comp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ TRACE %{
6363

6464
MCDISPLAY
6565
%{
66-
mcdis_disc(0,0,0,radius, nx,ny,nz);
66+
mcdis_disc (0, 0, 0, radius, nx, ny, nz);
6767
%}
6868

6969
END

mcstas-comps/misc/File.comp

Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -36,88 +36,90 @@ SHARE
3636
%}
3737
DECLARE
3838
%{
39-
char * key;
40-
char * name;
39+
char* key;
40+
char* name;
4141
%}
4242
INITIALIZE
4343
%{
44-
// Sort-out the key that we are searching for
45-
if (metadatakey == NULL || metadatakey[0] == '\0'){
46-
key = malloc((strlen(NAME_CURRENT_COMP) + 1) * sizeof(char));
47-
strcpy(key, NAME_CURRENT_COMP);
48-
} else {
49-
key = malloc((strlen(metadatakey) + 1) * sizeof(char));
50-
strcpy(key, metadatakey);
51-
}
52-
int matches = metadata_table_defined(num_metadata, metadata_table, key);
53-
if (matches != 1) {
54-
// 0 would mean _no_ matches; maybe they metadata name without the component was given?
55-
if (matches == 0 && strcmp(key, NAME_CURRENT_COMP)) {
56-
free(key);
57-
key = malloc((strlen(NAME_CURRENT_COMP) + strlen(metadatakey) + 2) * sizeof(char));
58-
sprintf(key, "%s:%s", NAME_CURRENT_COMP, metadatakey);
59-
matches = metadata_table_defined(num_metadata, metadata_table, key);
44+
// Sort-out the key that we are searching for
45+
if (metadatakey == NULL || metadatakey[0] == '\0') {
46+
key = malloc ((strlen (NAME_CURRENT_COMP) + 1) * sizeof (char));
47+
strcpy (key, NAME_CURRENT_COMP);
48+
} else {
49+
key = malloc ((strlen (metadatakey) + 1) * sizeof (char));
50+
strcpy (key, metadatakey);
6051
}
61-
// there's a bug in metadata_table_defined that returns num_metadata if key == NULL
62-
// But since key _isn't_ null, any other result _should_ mean key == A_COMPONENT_NAME
63-
// _and_ that component defines _multiple_ METADATA entries.
64-
else {
65-
printf("%s: There are %d METADATA entries that match %s; please select only one of:\n", NAME_CURRENT_COMP, matches, key);
66-
metadata_table_print_component_keys(num_metadata, metadata_table, key);
67-
exit(1);
52+
int matches = metadata_table_defined (num_metadata, metadata_table, key);
53+
if (matches != 1) {
54+
// 0 would mean _no_ matches; maybe they metadata name without the component was given?
55+
if (matches == 0 && strcmp (key, NAME_CURRENT_COMP)) {
56+
free (key);
57+
key = malloc ((strlen (NAME_CURRENT_COMP) + strlen (metadatakey) + 2) * sizeof (char));
58+
sprintf (key, "%s:%s", NAME_CURRENT_COMP, metadatakey);
59+
matches = metadata_table_defined (num_metadata, metadata_table, key);
60+
}
61+
// there's a bug in metadata_table_defined that returns num_metadata if key == NULL
62+
// But since key _isn't_ null, any other result _should_ mean key == A_COMPONENT_NAME
63+
// _and_ that component defines _multiple_ METADATA entries.
64+
else {
65+
printf ("%s: There are %d METADATA entries that match %s; please select only one of:\n", NAME_CURRENT_COMP, matches, key);
66+
metadata_table_print_component_keys (num_metadata, metadata_table, key);
67+
exit (1);
68+
}
69+
}
70+
if (metadata_table_defined (num_metadata, metadata_table, key) != 1) {
71+
fprintf (stderr, "%s: No unique metadata defined with key %s\n", NAME_CURRENT_COMP, key);
72+
exit (1);
73+
}
74+
75+
char* name_part = metadata_table_key_literal (key);
76+
if (name_part == NULL) {
77+
#if defined(__MCCODE_VERSION__) && __MCCODE_VERSION__ >= 305000L
78+
// We already restrict that only one key matches.
79+
name_part = metadata_table_name (num_metadata, metadata_table, key);
80+
// is key NAME_CURRENT_COMP or something provided by the user? We don't know
81+
// so we _must_ cycle the key allocation to make this work.
82+
char* new_key = malloc ((strlen (key) + strlen (name_part) + 2) * sizeof (char));
83+
sprintf (new_key, "%s:%s", key, name_part);
84+
free (key);
85+
key = new_key;
86+
#else
87+
printf ("Can not update the key automatically prior to McCode v3.5.0, please fix %s usage or upgrade\n", NAME_CURRENT_COMP);
88+
exit (1);
89+
#endif
6890
}
69-
}
70-
if (metadata_table_defined(num_metadata, metadata_table, key) != 1) {
71-
fprintf(stderr, "%s: No unique metadata defined with key %s\n", NAME_CURRENT_COMP, key);
72-
exit(1);
73-
}
74-
75-
char * name_part = metadata_table_key_literal(key);
76-
if (name_part == NULL) {
77-
#if defined(__MCCODE_VERSION__) && __MCCODE_VERSION__ >= 305000L
78-
// We already restrict that only one key matches.
79-
name_part = metadata_table_name(num_metadata, metadata_table, key);
80-
// is key NAME_CURRENT_COMP or something provided by the user? We don't know
81-
// so we _must_ cycle the key allocation to make this work.
82-
char * new_key = malloc((strlen(key) + strlen(name_part) + 2) * sizeof(char));
83-
sprintf(new_key, "%s:%s", key, name_part);
84-
free(key);
85-
key = new_key;
86-
#else
87-
printf("Can not update the key automatically prior to McCode v3.5.0, please fix %s usage or upgrade\n", NAME_CURRENT_COMP);
88-
exit(1);
89-
#endif
90-
}
9191

92-
if (filename == NULL || filename[0] == '\0'){
93-
char * comp_part = metadata_table_key_component(key);
94-
name = malloc((strlen(comp_part) + strlen(name_part) + 2) * sizeof(char));
95-
sprintf(name, "%s_%s", comp_part, name_part);
96-
free(comp_part);
97-
} else{
98-
name = malloc((strlen(filename) + 1) * sizeof(char));
99-
strcpy(name, filename);
100-
}
101-
102-
free(name_part);
92+
if (filename == NULL || filename[0] == '\0') {
93+
char* comp_part = metadata_table_key_component (key);
94+
name = malloc ((strlen (comp_part) + strlen (name_part) + 2) * sizeof (char));
95+
sprintf (name, "%s_%s", comp_part, name_part);
96+
free (comp_part);
97+
} else {
98+
name = malloc ((strlen (filename) + 1) * sizeof (char));
99+
strcpy (name, filename);
100+
}
101+
102+
free (name_part);
103103

104-
// Read the file contents and write them to file now.
105-
FILE * file_ptr = fopen(name, "w");
106-
fprintf(file_ptr, "%s", metadata_table_literal(num_metadata, metadata_table, key));
107-
fclose(file_ptr);
104+
// Read the file contents and write them to file now.
105+
FILE* file_ptr = fopen (name, "w");
106+
fprintf (file_ptr, "%s", metadata_table_literal (num_metadata, metadata_table, key));
107+
fclose (file_ptr);
108108
%}
109109
TRACE
110110
%{
111-
// Do nothing
111+
// Do nothing
112112
%}
113113
FINALLY
114114
%{
115-
// (Optionally) Remove the file now that the runtime is done
116-
if (!keep && !remove(name)) {
117-
fprintf(stderr, "%s: Could not remove file %s\n", NAME_CURRENT_COMP, name);
118-
}
119-
if (key) free(key);
120-
if (name) free(name);
115+
// (Optionally) Remove the file now that the runtime is done
116+
if (!keep && !remove (name)) {
117+
fprintf (stderr, "%s: Could not remove file %s\n", NAME_CURRENT_COMP, name);
118+
}
119+
if (key)
120+
free (key);
121+
if (name)
122+
free (name);
121123
%}
122124
MCDISPLAY
123125
%{

mcstas-comps/misc/Legacy_circle.comp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ TRACE
8282

8383
MCDISPLAY
8484
%{
85-
circle("xy",0,0,0,radius);
85+
circle ("xy", 0, 0, 0, radius);
8686
%}
8787

8888
END

0 commit comments

Comments
 (0)