[C] syntaxhighlighter_viewsource syntaxhighlighter_copycode
#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) { //已分配内存
        if (CFI_deallocate(ans)) return; //非0释放内存失败
    }
    if (out) {
        if (CFI_allocate(ans,(CFI_index_t*)0,(CFI_index_t*)0,len)) return; //非0分配内存失败
        memcpy(ans->base_addr,out,len); //字符串内存拷贝
    }
}
[Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode
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