Skip to content

Base: Add C array parsing with 2D shape support to LIBXML2 Fortran interface#6

Merged
hansec merged 8 commits intolibxml2from
copilot/add-libxml2-fortran-interface
Mar 12, 2026
Merged

Base: Add C array parsing with 2D shape support to LIBXML2 Fortran interface#6
hansec merged 8 commits intolibxml2from
copilot/add-libxml2-fortran-interface

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 6, 2026

Adds C-backed array parsing functions for int/real/logical types supporting comma-and-newline-delimited 2D data, returning a flat array plus a 2-component shape [nrows, ncols]. Also syncs with updated base branch that allows FoX and LibXml2 to coexist.

Primary changes

src/base/oft_xml_c.c — new unconditional (no HAVE_LIBXML2 guard) C functions:

  • oft_xml_parse_int_array_c — comma+newline → flat int32_t[] + shape[2]
  • oft_xml_parse_real_array_c — comma+newline → flat double[] + shape[2]
  • oft_xml_parse_logical_array_c — comma+newline → flat int32_t[] (0/1) + shape[2]
  • Commas delimit columns within a row; newlines delimit rows; whitespace-only lines are skipped; inconsistent column counts return an error

src/base/oft_xml.F90 — updated Fortran module:

  • BIND(C) interface declarations for the three new C functions (unconditional)
  • oft_xml_parse_int_array, oft_xml_parse_real_array, oft_xml_parse_logical_array rewritten as C-backed wrappers; now return shape(2) (nrows, ncols) instead of scalar n

Typical usage:

REAL(r8) :: vals(64)
INTEGER(i4) :: arr_shape(2), ierr

CALL oft_xml_parse_real_array("1.0,2.0\n3.0,4.0", vals, arr_shape, ierr)
! arr_shape == [2, 2], vals == [1.0, 2.0, 3.0, 4.0]

Secondary changes

  • src/CMakeLists.txt: synced with base branch — FoX is now searched unconditionally alongside LibXml2 (both may be present simultaneously)
  • .gitignore: added entries to exclude build environment artifacts (/libs/, /oft_venv/, etc.)

API changes

oft_xml_parse_int_array, oft_xml_parse_real_array, and oft_xml_parse_logical_array replace the n (count) output argument with shape(2) (nrows, ncols). These routines had no callers outside oft_xml.F90 itself at the time of this change.

Original prompt

Create a c file and a corresponding Fortran file containing a module with interface definitions for the c functions, both in the 'src/base' subdirectory that provides a limited interface between LIBXML2 and Fortran. The files should provide functions callable from Fortran via iso_c_bindings that supports the following interfaces:

  1. Parse an XML file from a given file path, returning a pointer to the XML doc
  2. Retrieve a pointer to the I-th xml node contained within another node by name
  3. Retrieve pointers to all xml nodes with a given name contained within a specified node
  4. Extract string content from a given xml node
  5. Extract the string value of a given attribute on a given xml node

Additionally functions should be provided to parse string output into logical, integer or float values including support for arrays via comma delimited strings.

The interface should exclusively use DOM style XMl access.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits March 6, 2026 07:36
Co-authored-by: hansec <821577+hansec@users.noreply.github.com>
…tent ADJUSTL usage

Co-authored-by: hansec <821577+hansec@users.noreply.github.com>
Copilot AI changed the title [WIP] Add limited interface between LIBXML2 and Fortran Base: Add LIBXML2 Fortran DOM interface Mar 6, 2026
add_definitions( -DHAVE_XML )
endif()
# LibXml2
find_package( OFT_LibXml2 )
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modify so that either FoX or LIBXML2 is found but not both, with LIBXML2 as the default.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b410a6f. LibXml2 is now tried first via CMake's built-in find_package(LibXml2); FoX is only searched if LibXml2 is not found.

!---------------------------------------------------------------------------------
!> Parse an XML file from a given file path.
!!
!! @param[in] filepath Path to the XML file
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throughout this module, only provide documentation of function/subroutine arguments inline with the variable declaration, not in the comment header using '@param'

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b410a6f. All @param lines have been removed from comment headers throughout the module; argument documentation is now inline with variable declarations only.

END IF
END SUBROUTINE oft_xml_free
#endif
!===============================================================================
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comments indicating subroutine/functions groupings.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b410a6f. Removed the !===... grouping comment blocks.

@@ -0,0 +1,25 @@
# FindOFT_LibXml2.cmake
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove OFT-specific find script for LIBXML2 and utilize builtin find_lbixml2 module in CMAKE

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b410a6f. Removed FindOFT_LibXml2.cmake and replaced with a direct find_package(LibXml2) call in CMakeLists.txt.

…dOFT_LibXml2 script, clean up doc comments

Co-authored-by: hansec <821577+hansec@users.noreply.github.com>
@hansec hansec changed the base branch from main to libxml2 March 11, 2026 14:15
Copy link
Copy Markdown
Owner

@hansec hansec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add subroutines in C and corresponding Fortran interfaces to parse 32-bit integer, 64-bit floating point, or boolean values into corresponding arrays. Values in strings should be assumed to be comma separated and optionally newline delimited as well to indicate 2 dimensional array data. Data should always be read into a flat array, but a 2-component shape value should be returned indicating the size and shape of the data.

Copilot AI and others added 2 commits March 12, 2026 11:58
Co-authored-by: hansec <821577+hansec@users.noreply.github.com>
…n wrappers

Co-authored-by: hansec <821577+hansec@users.noreply.github.com>
Copilot AI changed the title Base: Add LIBXML2 Fortran DOM interface Base: Add C array parsing with 2D shape support to LIBXML2 Fortran interface Mar 12, 2026
@hansec hansec marked this pull request as ready for review March 12, 2026 16:50
@hansec hansec merged commit 5b2f2b2 into libxml2 Mar 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants