快速访问C Struct

编程入门 行业动态 更新时间:2024-10-27 01:24:25
本文介绍了快速访问C Struct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个OS X Swift应用程序,用于解析cvs文件.它在Objective-C中成功运行.然后我改用Swift,为了提高性能,我用C开发了解析/导入引擎.它的速度是Swift或Objective-C的5倍-很好.但是我很难在C和Swift之间交换数据-尤其是使用Struct:

I am developing a OS X Swift app for parsing cvs files. It runs successfully in Objective-C. Then I changed to Swift and for performance improvements I developed the parse/import engine in C. It is 5 times faster as in Swift or Objective-C - nice. But I have trouble to exchange the data between C and Swift - especially with Struct:

BridgingHeader:

#include "ToolBoxC.h"

ToolBoxC.h:

void loadFile(const char *fileName, const char *delimiters, const char *xRegex, int xRegexColumn, int xColumn, int yColumn, int xRow, int yRowShift, bool collectStrings); typedef struct { char **headerArray; int numberHeaderRows; char **dateArray; int numberDateRows; int **valueArray; char ***stringArray; int numberValueRows; int numberValueColums; } FileStruct; typedef struct { FileStruct fileContent[10000]; } FilesStruct; struct FilesStruct filesContent;

ToolBoxC.c:

struct FileStruct { char **headerArray; int numberHeaderRows; char **dateArray; int numberDateRows; int **valueArray; char ***stringArray; int numberValueRows; int numberValueColums; }; struct FilesStruct { struct FileStruct fileContent[10000]; }; void loadFile(const char *fileName, const char *delimiters, const char *xRegex, int xRegexColumn, int xColumn, int yColumn, int xRow, int yRowShift, bool collectStrings) { // some stuff struct FileStruct fileContent; fileContent.headerArray = headerArray; fileContent.numberHeaderRows = numberHeaderRows; fileContent.dateArray = dateArray; fileContent.numberDateRows = numberDateRows; fileContent.valueArray = valueArray; fileContent.stringArray = stringArray; fileContent.numberValueRows = numberValueRows; fileContent.numberValueColums = numberValueColumns; filesContent.fileContent[numberFiles] = fileContent; return; }

所有解析的数据都存储在 struct FilesStruct filesContent 中.通过使用Swift的参数调用函数loadFile()来开始解析.很好解析也可以.但是如何从Swift访问 struct FilesStruct filesContent 中的数据?

All the parsed data are stored in struct FilesStruct filesContent. The parsing is started by calling the function loadFile() with parameters from Swift. That works fine. Also the parsing is OK. But how can I access to the data in struct FilesStruct filesContent from Swift?

谢谢,马蒂亚斯.

推荐答案

尝试一下:

ToolBoxC.h

#include <stdbool.h> struct FileStruct { char **headerArray; int numberHeaderRows; char **dateArray; int numberDateRows; int **valueArray; char ***stringArray; int numberValueRows; int numberValueColums; }; extern struct FileStruct **loadedFiles; void loadFile(const char *fileName, const char *delimiters, const char *xRegex, int xRegexColumn, int xColumn, int yColumn, int xRow, int yRowShift, bool collectStrings);

ToolBoxC.c

#include <stdlib.h> #include "ToolBoxC.h" #define MaxFiles 10000 struct FileStruct **loadedFiles; void loadFile(const char *fileName, const char *delimiters, const char *xRegex, int xRegexColumn, int xColumn, int yColumn, int xRow, int yRowShift, bool collectStrings) { static int nextIndex = 0; if (loadedFiles == 0) loadedFiles = malloc(MaxFiles * sizeof(*loadedFiles)); struct FileStruct *file = malloc(sizeof(struct FileStruct)); file->numberDateRows = xRow; loadedFiles[nextIndex++] = file; }

快速测试方法

func loadFilesTest() -> Void { for var i:Int32 = 0; i < 10; ++i { loadFile("", "", "", 0, 0, 0, i, 0, true) } for var j = 0; j < 10; ++j { let pointer = UnsafePointer<FileStruct>(loadedFiles[j]) print("Number of date rows = \(pointer.memory.numberDateRows)") } }

更多推荐

快速访问C Struct

本文发布于:2023-11-26 04:41:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632665.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快速   Struct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!