Skip to content

stdlib_system: essential path functionality #999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove pre-processor parameters
  • Loading branch information
wassup05 committed Jul 6, 2025
commit 45824b52fcb193c4397b7e5a788be737f64f1127
6 changes: 0 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ if(CMAKE_Fortran_COMPILER_ID STREQUAL GNU AND CMAKE_Fortran_COMPILER_VERSION VER
message(FATAL_ERROR "GCC Version 9 or newer required")
endif()

# Convert CMAKE_SYSTEM_NAME to uppercase
string(TOUPPER "${CMAKE_SYSTEM_NAME}" SYSTEM_NAME_UPPER)

# Pass the uppercase system name as a macro
add_compile_options(-D${SYSTEM_NAME_UPPER})

# --- compiler feature checks
include(CheckFortranSourceCompiles)
include(CheckFortranSourceRuns)
Expand Down
2 changes: 0 additions & 2 deletions config/fypp_deployment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import platform
import fypp
import argparse
from joblib import Parallel, delayed
Expand Down Expand Up @@ -116,7 +115,6 @@ def fpm_build(args,unknown):
for idx, arg in enumerate(unknown):
if arg.startswith("--flag"):
flags= flags + unknown[idx+1]
flags = flags + "-D{}".format(platform.system().upper())
#==========================================
# build with fpm
subprocess.run("fpm build"+
Expand Down
4 changes: 2 additions & 2 deletions example/system/example_path_base_name.f90
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
! Usage of base_name
program example_path_base_name
use stdlib_system, only: base_name, ISWIN
use stdlib_system, only: base_name, OS_TYPE, OS_WINDOWS
character(len=:), allocatable :: p1

if( ISWIN ) then
if(OS_TYPE() == OS_WINDOWS) then
p1 = 'C:\Users'
else
p1 = '/home'
Expand Down
4 changes: 2 additions & 2 deletions example/system/example_path_dir_name.f90
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
! Usage of dir_name
program example_path_dir_name
use stdlib_system, only: dir_name, ISWIN
use stdlib_system, only: dir_name, OS_TYPE, OS_WINDOWS
character(len=:), allocatable :: p1, head, tail

if( ISWIN ) then
if(OS_TYPE() == OS_WINDOWS) then
p1 = 'C:\Users' ! C:\Users
else
p1 = '/home' ! /home
Expand Down
4 changes: 2 additions & 2 deletions example/system/example_path_join.f90
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
! Usage of join_path, operator(/)
program example_path_join
use stdlib_system, only: join_path, operator(/), ISWIN
use stdlib_system, only: join_path, operator(/), OS_TYPE, OS_WINDOWS
character(len=:), allocatable :: p1, p2, p3
character(len=20) :: parr(4)

if( ISWIN ) then
if(OS_TYPE() == OS_WINDOWS) then
p1 = 'C:'/'Users'/'User1'/'Desktop'
p2 = join_path('C:\Users\User1', 'Desktop')
parr = [character(len=20) :: 'C:', 'Users', 'User1', 'Desktop']
Expand Down
4 changes: 2 additions & 2 deletions example/system/example_path_split_path.f90
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
! Usage of split_path
program example_path_split_path
use stdlib_system, only: join_path, split_path, ISWIN
use stdlib_system, only: join_path, split_path, OS_TYPE, OS_WINDOWS
character(len=:), allocatable :: p1, head, tail

if( ISWIN ) then
if(OS_TYPE() == OS_WINDOWS) then
p1 = join_path('C:\Users\User1', 'Desktop') ! C:\Users\User1\Desktop
else
p1 = join_path('/home/User1', 'Desktop') ! /home/User1/Desktop
Expand Down
23 changes: 12 additions & 11 deletions src/stdlib_system.F90
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,7 @@ module stdlib_system
public :: is_windows

!! Public path related functions and interfaces
#ifdef WINDOWS
character(len=1), parameter, public :: pathsep = '\'
logical, parameter, public :: ISWIN = .true.
#else
character(len=1), parameter, public :: pathsep = '/'
logical, parameter, public :: ISWIN = .false.
#endif

public :: path_sep
public :: join_path
public :: operator(/)
public :: split_path
Expand Down Expand Up @@ -572,12 +565,12 @@ end function process_get_ID
!! join the paths provided according to the OS-specific path-separator
!! ([Specification](../page/specs/stdlib_system.html#join_path))
!!
module pure function join2(p1, p2) result(path)
module function join2(p1, p2) result(path)
character(:), allocatable :: path
character(*), intent(in) :: p1, p2
end function join2

module pure function joinarr(p) result(path)
module function joinarr(p) result(path)
character(:), allocatable :: path
character(*), intent(in) :: p(:)
end function joinarr
Expand All @@ -590,7 +583,7 @@ end function joinarr
!! A binary operator to join the paths provided according to the OS-specific path-separator
!! ([Specification](../page/specs/stdlib_system.html#operator(/)))
!!
module pure function join_op(p1, p2) result(path)
module function join_op(p1, p2) result(path)
character(:), allocatable :: path
character(*), intent(in) :: p1, p2
end function join_op
Expand Down Expand Up @@ -866,4 +859,12 @@ subroutine delete_file(path, err)
end if
end subroutine delete_file

character function path_sep()
if (OS_TYPE() == OS_WINDOWS) then
path_sep = '\'
else
path_sep = '/'
end if
end function path_sep

end module stdlib_system
24 changes: 13 additions & 11 deletions src/stdlib_system_path.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
use stdlib_ascii, only: reverse
use stdlib_strings, only: chomp, find, join
contains
module pure function join2(p1, p2) result(path)
module function join2(p1, p2) result(path)
character(:), allocatable :: path
character(*), intent(in) :: p1, p2

path = trim(p1) // pathsep // trim(p2)
path = trim(p1) // path_sep() // trim(p2)
end function join2

module pure function joinarr(p) result(path)
module function joinarr(p) result(path)
character(:), allocatable :: path
character(*), intent(in) :: p(:)

path = join(p, pathsep)
path = join(p, path_sep())
end function joinarr

module pure function join_op(p1, p2) result(path)
module function join_op(p1, p2) result(path)
character(:), allocatable :: path
character(*), intent(in) :: p1, p2

Expand All @@ -28,6 +28,8 @@ module subroutine split_path(p, head, tail)
character(:), allocatable, intent(out) :: head, tail
character(:), allocatable :: temp
integer :: i
character(len=1) :: sep
sep = path_sep()

! Empty string, return (.,'')
if (trim(p) == '') then
Expand All @@ -37,28 +39,28 @@ module subroutine split_path(p, head, tail)
end if

! Remove trailing path separators
temp = trim(chomp(trim(p), pathsep))
temp = trim(chomp(trim(p), sep))

if (temp == '') then
head = pathsep
head = sep
tail = ''
return
end if

i = find(reverse(temp), pathsep)
i = find(reverse(temp), sep)

! if no `pathsep`, then it probably was a root dir like `C:\`
if (i == 0) then
head = temp // pathsep
head = temp // sep
tail = ''
return
end if

head = temp(:len(temp)-i)

! child of a root directory
if (find(head, pathsep) == 0) then
head = head // pathsep
if (find(head, sep) == 0) then
head = head // sep
end if

tail = temp(len(temp)-i+2:)
Expand Down
12 changes: 6 additions & 6 deletions test/system/test_path.f90
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module test_path
use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test
use stdlib_system, only: join_path, operator(/), split_path, ISWIN
use stdlib_system, only: join_path, operator(/), split_path, OS_TYPE, OS_WINDOWS
implicit none
contains
!> Collect all exported unit tests
Expand Down Expand Up @@ -32,7 +32,7 @@ subroutine test_join_path(error)
character(len=:), allocatable :: path
character(len=20) :: paths(5)

if (ISWIN) then
if (OS_TYPE() == OS_WINDOWS) then
path = join_path('C:\Users', 'Alice')
call checkpath(error, 'join_path', 'C:\Users\Alice', path)
if (allocated(error)) return
Expand All @@ -43,8 +43,8 @@ subroutine test_join_path(error)
call checkpath(error, 'join_path', 'C:\Users\Bob\Pictures\2025', path)
if (allocated(error)) return

path = join_path('C:\Users\John Doe', 'Pictures\2025') ! path with spaces
call checkpath(error, 'join_path', 'C:\Users\John Doe\Pictures\2025', path)
path = join_path('"C:\Users\John Doe"', 'Pictures\2025') ! path with spaces
call checkpath(error, 'join_path', '"C:\Users\John Doe"\Pictures\2025', path)
if (allocated(error)) return
else
path = join_path('/home', 'Alice')
Expand All @@ -64,7 +64,7 @@ subroutine test_join_path_op(error)
type(error_type), allocatable, intent(out) :: error
character(len=:), allocatable :: path

if (ISWIN) then
if (OS_TYPE() == OS_WINDOWS) then
path = 'C:'/'Users'/'Alice'/'Desktop'
call checkpath(error, 'join_path operator', 'C:\Users\Alice\Desktop', path)
if (allocated(error)) return
Expand All @@ -85,7 +85,7 @@ subroutine test_split_path(error)
call checkpath(error, 'split_path-tail', '', tail)
if (allocated(error)) return

if (ISWIN) then
if (OS_TYPE() == OS_WINDOWS) then
call split_path('\\\\', head, tail)
call checkpath(error, 'split_path-head', '\', head)
if (allocated(error)) return
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy