Skip to content

Commit

Permalink
Refactor cob_chk_file_mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeclerck committed Jun 1, 2024
1 parent 18626fb commit 05442df
Showing 1 changed file with 40 additions and 61 deletions.
101 changes: 40 additions & 61 deletions libcob/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,43 @@ do_acu_hypen_translation (char *src)
strncpy (file_open_name, file_open_buff, (size_t)COB_FILE_MAX);
}

/* apply COB_FILE_PATH if set (similar to ACUCOBOL's FILE-PREFIX)
MF and Fujistu simply don't have that - not set by default,
so no compatilibity issue here */
static void
apply_file_paths (void)
{
int k;
if (file_paths) {
for(k=0; file_paths[k] != NULL; k++) {
snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s",
file_paths[k], SLASH_CHAR, file_open_name);
file_open_buff[COB_FILE_MAX] = 0;
if (access (file_open_buff, F_OK) == 0) {
break;
}
#if defined(WITH_CISAM) || defined(WITH_DISAM) || defined(WITH_VBISAM) || defined(WITH_VISAM)
/* ISAM may append '.dat' to file name */
snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s.dat",
file_paths[k], SLASH_CHAR, file_open_name);
file_open_buff[COB_FILE_MAX] = 0;
if (access (file_open_buff, F_OK) == 0) {
snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s",
file_paths[k], SLASH_CHAR, file_open_name);
file_open_buff[COB_FILE_MAX] = 0;
break;
}
#endif
}
if (file_paths[k] == NULL) {
snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s",
file_paths[0], SLASH_CHAR, file_open_name);
file_open_buff[COB_FILE_MAX] = 0;
}
strncpy (file_open_name, file_open_buff, (size_t)COB_FILE_MAX);
}
}

void
cob_chk_file_mapping (cob_file *f, char *filename)
{
Expand All @@ -1642,7 +1679,7 @@ cob_chk_file_mapping (cob_file *f, char *filename)
char *saveptr;
char *orig;
unsigned int dollar, badchar;
int k, qt;
int qt;

if (f != NULL)
memset (f->file_status, '0', (size_t)2);
Expand Down Expand Up @@ -1717,37 +1754,7 @@ cob_chk_file_mapping (cob_file *f, char *filename)
return ;
}
}
/* apply COB_FILE_PATH if set (similar to ACUCOBOL's FILE-PREFIX)
MF and Fujistu simply don't have that - not set by default,
so no compatilibity issue here */
if (file_paths) {
for(k=0; file_paths[k] != NULL; k++) {
snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s",
file_paths[k], SLASH_CHAR, file_open_name);
file_open_buff[COB_FILE_MAX] = 0;
if (access (file_open_buff, F_OK) == 0) {
break;
}
#if defined(WITH_CISAM) || defined(WITH_DISAM) || defined(WITH_VBISAM) || defined(WITH_VISAM)
/* ISAM may append '.dat' to file name */
snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s.dat",
file_paths[k], SLASH_CHAR, file_open_name);
file_open_buff[COB_FILE_MAX] = 0;
if (access (file_open_buff, F_OK) == 0) {
snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s",
file_paths[k], SLASH_CHAR, file_open_name);
file_open_buff[COB_FILE_MAX] = 0;
break;
}
#endif
}
if (file_paths[k] == NULL) {
snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s",
file_paths[0], SLASH_CHAR, file_open_name);
file_open_buff[COB_FILE_MAX] = 0;
}
strncpy (file_open_name, file_open_buff, (size_t)COB_FILE_MAX);
}
apply_file_paths ();
return;
}

Expand Down Expand Up @@ -1832,36 +1839,8 @@ cob_chk_file_mapping (cob_file *f, char *filename)
if (looks_absolute (file_open_name)) {
return;
}
/* apply COB_FILE_PATH if set (similar to ACUCOBOL's FILE-PREFIX) */
if (file_paths) {
for(k=0; file_paths[k] != NULL; k++) {
snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s",
file_paths[k], SLASH_CHAR, file_open_name);
file_open_buff[COB_FILE_MAX] = 0;
if (access (file_open_buff, F_OK) == 0) {
break;
}
#if defined(WITH_CISAM) || defined(WITH_DISAM) || defined(WITH_VBISAM) || defined(WITH_VISAM)
/* ISAM may append '.dat' to file name */
snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s.dat",
file_paths[k], SLASH_CHAR, file_open_name);
file_open_buff[COB_FILE_MAX] = 0;
if (access (file_open_buff, F_OK) == 0) {
snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s",
file_paths[k], SLASH_CHAR, file_open_name);
file_open_buff[COB_FILE_MAX] = 0;
break;
}
#endif
}
if (file_paths[k] == NULL) {
snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s",
file_paths[0], SLASH_CHAR, file_open_name);
file_open_buff[COB_FILE_MAX] = 0;
}
strncpy (file_open_name, file_open_buff, (size_t)COB_FILE_MAX);
}

apply_file_paths ();
}

void
Expand Down

0 comments on commit 05442df

Please sign in to comment.