Fortran Coder

标题: F2018标准C descriptors初体验 [打印本页]

作者: weixing1531    时间: 4 天前
标题: F2018标准C descriptors初体验
本帖最后由 weixing1531 于 2024-6-30 11:09 编辑

F2018标准进一步加强了与C语言交互,搞出来C descriptors这个新玩意,主要以结构体CFI_cdesc_t为载体用于C语言操作Fortran中动态数组、数组切片,指针、递延字符串、访问派生类型成员变量、访问二维数组元素、optional、TYPE(*)、DIMENSION(..)等高级特性。

以下是C descritors操作递延字符串的一个例子,使用C语言strtok函数模拟Fortran2023标准子程序split来分裂字符串。
系统环境:Windows10
编译器:SimplyFortran3.35+gnu14.1.0
C语言代码:
[C] 纯文本查看 复制代码
#include<stdlib.h>
#include "iso_fortran_binding.h"
#include<stdio.h>
#include<string.h>
//*str:被搜索字符串 *ft:间隔符集合 *num:分裂个数 ans:C descriptors
void c_split(char *str,const char *ft,int *num,CFI_cdesc_t *ans){
    char *token=NULL;
    char out[255]; //提取的字符串 char *out; 会报错
    size_t width,len=0; //字符串总长度
   
    *num=0; //分裂个数
    token=strtok(str,ft); //C函数
    printf("++++++C函数内部计算过程开始++++++\n");

    while(token!=NULL){
        width=strlen(token); //每个子字符串长度
        strncpy(out+len,token,width); //out为字符串首地址
        strncpy(out+len+width,",",1); //逗号间隔
        len=len+width+1; //包含,的长度
        printf("[%d]:%s\n",*num,token); //打印中间成果
        *num=*num+1; //分裂个数
        token=strtok(NULL,ft); //C函数
    }
    len=len-1; //字符串长度
    strncpy(out+len,"\0",1); //将尾部,改为\0
    printf("++++++C函数内部计算过程结束++++++\n");
   
    if (ans->base_addr) { //已分配内存
        width = CFI_deallocate(ans); //释放内存
        if (width) return; //非0释放内存失败
    }

    if (out) {
        width = CFI_allocate(ans,(CFI_index_t*)0,(CFI_index_t*)0,len); //分配内存
        if (width) return; //非0分配内存失败
        memcpy(ans->base_addr,out,len); //字符串内存拷贝
    }
}


Fortran代码:
[Fortran] 纯文本查看 复制代码
program main
    use iso_c_binding
    implicit none

    interface
        !void c_split(char *str,const char *ft,int *num,CFI_cdesc_t *ans)
        subroutine c_split(str,ft,num,ans) bind(c) !封装c语言strtok子程序
            import::c_char,c_int
            character(c_char),intent(in)::str(*),ft(*)
            integer(c_int)::num !分裂个数
            character(:,c_char), allocatable, intent(out) :: ans !递延字符串
        end subroutine
    end interface

    integer(c_int)::r,g,b,num
    character(255)::temp_str !用于字符串占位
    character(:),allocatable::str,sep !被解析字符串 间隔符
    character(:,c_char),allocatable::ans !递延字符串

    str="SetColor(255,30,58)" !被解析字符串
    write(*,"('原始字符串:',a)")str
    sep=str(13:13) !间隔符,
    !分隔符集合"(,)"
    call c_split(str//c_null_char,"("//sep//")"//c_null_char,num,ans)
   
    write(*,"('分裂个数:',i0)")num
    write(*,"('提取字符串长度:',i0)")len(ans)
    write(*,"('提取字符串长度(验算):',i0)")storage_size(ans)/storage_size(ans(1:1))
    write(*,"('提取的字符串:',a)") ans !提取的字符串
    read(ans,*)temp_str,r,g,b
    write(*,"('rgb的数值分别为:',*(g0,:,','))")r,g,b

    deallocate(ans,str,sep)
    read(*,*)
end program











作者: fcode    时间: 4 天前
嚯,反向适配了这是。
以前 ISO_C_Binding 还是在 Fortran 领空内适配(迁就)C语言。
现在牛气了,干预到 C 语言领空去了。
作者: weixing1531    时间: 4 天前
本帖最后由 weixing1531 于 2024-6-26 11:18 编辑
fcode 发表于 2024-6-26 10:04
嚯,反向适配了这是。
以前 ISO_C_Binding 还是在 Fortran 领空内适配(迁就)C语言。
现在牛气了,干预到  ...

len(ans)报错
可能gfortran支持还不够好
Fortran傍上C这个大款后,准备抢班夺权了。






欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2