Fortran Coder

查看: 36|回复: 2
打印 上一主题 下一主题

[混编] F2018标准C descriptors初体验

[复制链接]

140

帖子

41

主题

1

精华

大师

F 币
1219 元
贡献
621 点
跳转到指定楼层
楼主
发表于 前天 02:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 weixing1531 于 2024-6-27 13:04 编辑

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

以下是C descritors操作递延字符串的一个例子,使用C语言strtok函数模拟Fortran2023标准子程序split来分裂字符串。
系统环境:Windows7(Windows10会报错,原因是C语言第18行代码会莫名其妙地改变ans->base_addr的地址)
编译器: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; //提取的字符串
    int width;
    size_t 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) 
    !len(ans)报错 Error: Array index at (1) must be of INTEGER type, found CHARACTER
    write(*,"('分裂个数:',i0)")num
    write(*,"('提取字符串长度:',i0)")len_trim(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









分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

1988

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1445 元
贡献
622 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

沙发
发表于 前天 10:04 | 只看该作者
嚯,反向适配了这是。
以前 ISO_C_Binding 还是在 Fortran 领空内适配(迁就)C语言。
现在牛气了,干预到 C 语言领空去了。

140

帖子

41

主题

1

精华

大师

F 币
1219 元
贡献
621 点
板凳
 楼主| 发表于 前天 10:57 | 只看该作者
本帖最后由 weixing1531 于 2024-6-26 11:18 编辑
fcode 发表于 2024-6-26 10:04
嚯,反向适配了这是。
以前 ISO_C_Binding 还是在 Fortran 领空内适配(迁就)C语言。
现在牛气了,干预到  ...

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

您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-6-28 05:14

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表