Skip to content

Commit

Permalink
EXODUS: Clean up bad merge
Browse files Browse the repository at this point in the history
  • Loading branch information
gsjaardema committed Sep 20, 2023
1 parent 3990119 commit 3116290
Showing 1 changed file with 115 additions and 129 deletions.
244 changes: 115 additions & 129 deletions packages/seacas/libraries/exodus/src/ex_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1099,20 +1099,20 @@ void ex__rm_stat_ptr(int exoid, struct ex__obj_stats **obj_ptr)
struct ex__obj_stats *tmp_ptr = *obj_ptr;
struct ex__obj_stats *last_head_list_ptr = *obj_ptr; /* save last head pointer */

while (tmp_ptr) /* Walk linked list of file ids/vals */
while (tmp_ptr) /* Walk linked list of file ids/vals */
{
if (exoid == tmp_ptr->exoid) /* linear search for exodus file id */
if (exoid == tmp_ptr->exoid) /* linear search for exodus file id */
{
if (tmp_ptr == *obj_ptr) { /* Are we at the head of the list? */
*obj_ptr = (*obj_ptr)->next; /* yes, reset ptr to head of list */
if (tmp_ptr == *obj_ptr) { /* Are we at the head of the list? */
*obj_ptr = (*obj_ptr)->next; /* yes, reset ptr to head of list */
}
else { /* no, remove this record from chain*/
else { /* no, remove this record from chain*/
last_head_list_ptr->next = tmp_ptr->next;
}
free(tmp_ptr->id_vals); /* free up memory */
free(tmp_ptr->stat_vals);
free(tmp_ptr);
break; /* Quit if found */
break; /* Quit if found */
}
last_head_list_ptr = tmp_ptr; /* save last head pointer */
tmp_ptr = tmp_ptr->next; /* Loop back if not */
Expand Down Expand Up @@ -1199,12 +1199,12 @@ struct ex__list_item **ex__get_counter_list(ex_entity_type obj_type)
int ex__inc_file_item(int exoid, /* file id */
struct ex__list_item **list_ptr) /* ptr to ptr to list_item */
{
struct ex__list_item *tlist_ptr = *list_ptr; /* use temp list ptr to walk linked list */
while (tlist_ptr) { /* Walk linked list of file ids/vals */
if (exoid == tlist_ptr->exo_id) { /* linear search for exodus file id */
break; /* Quit if found */
struct ex__list_item *tlist_ptr = *list_ptr; /* use temp list ptr to walk linked list */
while (tlist_ptr) { /* Walk linked list of file ids/vals */
if (exoid == tlist_ptr->exo_id) { /* linear search for exodus file id */
break; /* Quit if found */
}
tlist_ptr = tlist_ptr->next; /* Loop back if not */
tlist_ptr = tlist_ptr->next; /* Loop back if not */
}

if (!tlist_ptr) { /* ptr NULL? */
Expand Down Expand Up @@ -1257,7 +1257,7 @@ int ex__get_file_item(int exoid, /* file id */
if (exoid == tlist_ptr->exo_id) { /* linear search for exodus file id */
break; /* Quit if found */
}
tlist_ptr = tlist_ptr->next; /* Loop back if not */
tlist_ptr = tlist_ptr->next; /* Loop back if not */
}

if (!tlist_ptr) { /* ptr NULL? */
Expand Down Expand Up @@ -1304,11 +1304,11 @@ void ex__rm_file_item(int exoid, /* file id */
if (tlist_ptr == *list_ptr) { /* Are we at the head of the list? */
*list_ptr = (*list_ptr)->next; /* yes, reset ptr to head of list */
}
else { /* no, remove this record from chain*/
else { /* no, remove this record from chain*/
last_head_list_ptr->next = tlist_ptr->next;
}
free(tlist_ptr); /* free up memory */
break; /* Quit if found */
free(tlist_ptr); /* free up memory */
break; /* Quit if found */
}
last_head_list_ptr = tlist_ptr; /* save last head pointer */
tlist_ptr = tlist_ptr->next; /* Loop back if not */
Expand Down Expand Up @@ -2354,134 +2354,120 @@ int ex__get_varid(int exoid, ex_entity_type obj_type, ex_entity_id id)
if (status != 0) {
if (status == EX_NULLENTITY) { /* NULL object? */
return EX_NOERR;
}

switch (obj_type) {
case EX_NODE_SET: entryptr = VAR_NODE_NS(id_ndx); break;
case EX_EDGE_SET: entryptr = VAR_EDGE_ES(id_ndx); break;
case EX_FACE_SET: entryptr = VAR_FACE_FS(id_ndx); break;
case EX_SIDE_SET: entryptr = VAR_ELEM_SS(id_ndx); break;
case EX_ELEM_SET: entryptr = VAR_ELEM_ELS(id_ndx); break;
case EX_EDGE_BLOCK: entryptr = VAR_EBCONN(id_ndx); break;
case EX_FACE_BLOCK: entryptr = VAR_FBCONN(id_ndx); break;
case EX_ELEM_BLOCK: entryptr = VAR_CONN(id_ndx); break;
default:
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported in call to %s",
obj_type, __func__);
ex_err(__func__, errmsg, EX_BADPARAM);
return EX_FATAL;
}

if ((status = nc_inq_varid(exoid, entryptr, &varid)) != NC_NOERR) {
snprintf(errmsg, MAX_ERR_LENGTH,
"ERROR: failed to locate %s id %" PRId64 " in id array in file id %d",
"ERROR: failed to locate entity list array for %s %" PRId64 " in file id %d",
ex_name_of_object(obj_type), id, exoid);
ex_err_fn(exoid, __func__, errmsg, status);
return EX_FATAL;
}
}

switch (obj_type) {
case EX_NODE_SET: entryptr = VAR_NODE_NS(id_ndx); break;
case EX_EDGE_SET: entryptr = VAR_EDGE_ES(id_ndx); break;
case EX_FACE_SET: entryptr = VAR_FACE_FS(id_ndx); break;
case EX_SIDE_SET: entryptr = VAR_ELEM_SS(id_ndx); break;
case EX_ELEM_SET: entryptr = VAR_ELEM_ELS(id_ndx); break;
case EX_EDGE_BLOCK: entryptr = VAR_EBCONN(id_ndx); break;
case EX_FACE_BLOCK: entryptr = VAR_FBCONN(id_ndx); break;
case EX_ELEM_BLOCK: entryptr = VAR_CONN(id_ndx); break;
default:
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported in call to %s",
obj_type, __func__);
ex_err(__func__, errmsg, EX_BADPARAM);
return EX_FATAL;
}

if ((status = nc_inq_varid(exoid, entryptr, &varid)) != NC_NOERR) {
snprintf(errmsg, MAX_ERR_LENGTH,
"ERROR: failed to locate entity list array for %s %" PRId64 " in file id %d",
ex_name_of_object(obj_type), id, exoid);
ex_err_fn(exoid, __func__, errmsg, status);
return EX_FATAL;
}

if ((status = nc_inq_varid(exoid, entryptr, &varid)) != NC_NOERR) {
snprintf(errmsg, MAX_ERR_LENGTH,
"ERROR: failed to locate entity list array for %s %" PRId64 " in file id %d",
ex_name_of_object(obj_type), id, exoid);
ex_err_fn(exoid, __func__, errmsg, status);
return EX_FATAL;
}
return varid;
}
return varid;
}

/*
* Code from:
* https://stackoverflow.com/questions/11034002/how-to-get-absolute-path-of-file-or-directory-that-does-not-exist
*
* Return the input path in a canonical form. This is achieved by
* expanding all symbolic links, resolving references to "." and "..",
* and removing duplicate "/" characters.
*
* If the file exists, its path is canonicalized and returned. If the file,
* or parts of the containing directory, do not exist, path components are
* removed from the end until an existing path is found. The remainder of the
* path is then appended to the canonical form of the existing path,
* and returned. Consequently, the returned path may not exist. The portion
* of the path which exists, however, is represented in canonical form.
*
* If successful, this function returns a C-string, which needs to be freed by
* the caller using free().
*
* ARGUMENTS:
* file_path
* File path, whose canonical form to return.
*
* RETURNS:
* On success, returns the canonical path to the file, which needs to be freed
* by the caller.
*
* On failure, returns NULL.
*/
char *ex__canonicalize_filename(char const *file_path)
{
/*
* Code from:
* https://stackoverflow.com/questions/11034002/how-to-get-absolute-path-of-file-or-directory-that-does-not-exist
*
* Return the input path in a canonical form. This is achieved by
* expanding all symbolic links, resolving references to "." and "..",
* and removing duplicate "/" characters.
*
* If the file exists, its path is canonicalized and returned. If the file,
* or parts of the containing directory, do not exist, path components are
* removed from the end until an existing path is found. The remainder of the
* path is then appended to the canonical form of the existing path,
* and returned. Consequently, the returned path may not exist. The portion
* of the path which exists, however, is represented in canonical form.
*
* If successful, this function returns a C-string, which needs to be freed by
* the caller using free().
*
* ARGUMENTS:
* file_path
* File path, whose canonical form to return.
*
* RETURNS:
* On success, returns the canonical path to the file, which needs to be freed
* by the caller.
*
* On failure, returns NULL.
*/
char *ex__canonicalize_filename(char const *file_path)
{
#if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \
defined(__MINGW32__) || defined(_WIN64) || defined(__MINGW64__)
return _fullpath(NULL, file_path, _MAX_PATH);
return _fullpath(NULL, file_path, _MAX_PATH);
#else
char *canonical_file_path = NULL;
unsigned int file_path_len = strlen(file_path);

if (file_path_len > 0) {
canonical_file_path = realpath(file_path, NULL);
if (canonical_file_path == NULL && errno == ENOENT) {
// The file was not found. Back up to a segment which exists,
// and append the remainder of the path to it.
char *file_path_copy = NULL;
if (file_path[0] == '/' || (strncmp(file_path, "./", 2) == 0) ||
(strncmp(file_path, "../", 3) == 0)) {
// Absolute path, or path starts with "./" or "../"
file_path_copy = strdup(file_path);
}
else {
// Relative path
file_path_copy = (char *)malloc(strlen(file_path) + 3);
strcpy(file_path_copy, "./");
strcat(file_path_copy, file_path);
}
char *canonical_file_path = NULL;
unsigned int file_path_len = strlen(file_path);

if (file_path_len > 0) {
canonical_file_path = realpath(file_path, NULL);
if (canonical_file_path == NULL && errno == ENOENT) {
// The file was not found. Back up to a segment which exists,
// and append the remainder of the path to it.
char *file_path_copy = NULL;
if (file_path[0] == '/' || (strncmp(file_path, "./", 2) == 0) ||
(strncmp(file_path, "../", 3) == 0)) {
// Absolute path, or path starts with "./" or "../"
file_path_copy = strdup(file_path);
}
else {
// Relative path
file_path_copy = (char *)malloc(strlen(file_path) + 3);
strcpy(file_path_copy, "./");
strcat(file_path_copy, file_path);
}

// Remove path components from the end, until an existing path is found
for (int char_idx = strlen(file_path_copy) - 1; char_idx >= 0 && canonical_file_path == NULL;
--char_idx) {
if (file_path_copy[char_idx] == '/') {
// Remove the slash character
file_path_copy[char_idx] = '\0';

canonical_file_path = realpath(file_path_copy, NULL);
if (canonical_file_path != NULL) {
// An existing path was found. Append the remainder of the path
// to a canonical form of the existing path.
char *combined_file_path = (char *)malloc(strlen(canonical_file_path) +
strlen(file_path_copy + char_idx + 1) + 2);
strcpy(combined_file_path, canonical_file_path);
strcat(combined_file_path, "/");
strcat(combined_file_path, file_path_copy + char_idx + 1);
free(canonical_file_path);
canonical_file_path = combined_file_path;
}
else {
// The path segment does not exist. Replace the slash character
// and keep trying by removing the previous path component.
file_path_copy[char_idx] = '/';
// Remove path components from the end, until an existing path is found
for (int char_idx = strlen(file_path_copy) - 1;
char_idx >= 0 && canonical_file_path == NULL; --char_idx) {
if (file_path_copy[char_idx] == '/') {
// Remove the slash character
file_path_copy[char_idx] = '\0';

canonical_file_path = realpath(file_path_copy, NULL);
if (canonical_file_path != NULL) {
// An existing path was found. Append the remainder of the path
// to a canonical form of the existing path.
char *combined_file_path = (char *)malloc(strlen(canonical_file_path) +
strlen(file_path_copy + char_idx + 1) + 2);
strcpy(combined_file_path, canonical_file_path);
strcat(combined_file_path, "/");
strcat(combined_file_path, file_path_copy + char_idx + 1);
free(canonical_file_path);
canonical_file_path = combined_file_path;
}
else {
// The path segment does not exist. Replace the slash character
// and keep trying by removing the previous path component.
file_path_copy[char_idx] = '/';
}
}
}
}

free(file_path_copy);
free(file_path_copy);
}
}
}
return canonical_file_path;
return canonical_file_path;
#endif
}
}

0 comments on commit 3116290

Please sign in to comment.