3

Context

From Swift, I am trying to call a specific function of the libxlsxwriter C library. The documentation is here: https://libxlsxwriter.github.io/worksheet_8h.html#a62bf44845ce9dcc505bf228999db5afa

The function assembles a "rich string" (the equivalent of an AttributedString, where different formats/styles apply to different ranges) and writes it to a specific cell in the Excel worksheet. In C, the function works like this:

lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
 
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
 
lxw_rich_string_tuple fragment11 = {.format = NULL,   .string = "This is "     };
lxw_rich_string_tuple fragment12 = {.format = bold,   .string = "bold"         };
lxw_rich_string_tuple fragment13 = {.format = NULL,   .string = " and this is "};
lxw_rich_string_tuple fragment14 = {.format = italic, .string = "italic"       };
 
lxw_rich_string_tuple *rich_string1[] = {&fragment11, &fragment12,
                                         &fragment13, &fragment14, NULL};
 
worksheet_write_rich_string(worksheet, CELL("A1"), rich_string1, NULL);

The Problem

I have an array of lxw_rich_string_tuple structs, but I'm unclear how to convert this into the array of pointers that worksheet_write_rich_string() accepts:


// The worksheet object and `lxw_format` objects are already existent. 
// This array contains multiple well-formed `lxw_rich_string_tuple` structs, which I can see in the debugger.
//
var rawTuples: [lxw_rich_string_tuple] =  ...


// Parameters: 
//    - the worksheet on which to write this string
//    - the row of the cell in which to write
//    - the column of the cell in which to write
//    - a null-terminated array of pointers to `lxw_rich_string_tuple` structs
//    - an optional format object to use, null in this case.
//
worksheet_write_rich_string(worksheet, 0, 1, ?, NULL);

The trouble is the ?. I've tried all sorts of withUnsafeBytes and withUnsafeMutableBytes and UnsafeMutableRawPointer().bindMemory(to:capacity:) and I cannot figure out the magic Swift gibberish to do what is such a SIMPLE thing in C. Thanks.

My Attempt

This gives no compiler errors, but crashes with a Bad Access exception:

let argsSize: Int = rawTuples.count

rawTuples.withUnsafeMutableBufferPointer { rawTuplesPointer in

    let ptr = UnsafeMutableRawPointer(rawTuplesPointer.baseAddress!).bindMemory(to: lxw_rich_string_tuple.self, capacity: argsSize)
    var tuplePointers: [UnsafeMutablePointer<lxw_rich_string_tuple>?] = []

    for i in 0 ..< argsSize
    {
        let tp: UnsafeMutablePointer<lxw_rich_string_tuple>? = ptr + (i * MemoryLayout<lxw_rich_string_tuple>.stride)           
        tuplePointers.append(tp)
    }
    tuplePointers.append(nil)

    tuplePointers.withUnsafeBufferPointer { tpPointer in

        let m = UnsafeMutablePointer(mutating: tpPointer.baseAddress)
        worksheet_write_rich_string(lxw_worksheet, cell.row, cell.col, m, nil)

    }
}

1 Answer 1

2

Try this:

rawTuples.withUnsafeMutableBufferPointer { p in
    guard let arrBaseAddress = p.baseAddress else { return }

    var pointersToEachArrayElement: [UnsafeMutablePointer<_>?] = 
        Array(arrBaseAddress ..< arrBaseAddress.advanced(by: p.count))
    pointersToEachArrayElement.append(nil)
    pointersToEachArrayElement.withUnsafeMutableBufferPointer { q in
        // use q.baseAddress in the call to worksheet_write_rich_string
    }
}

The idea is similar to your attempt - to create an array of pointers to each of the array elements. But unlike your attempt, I avoided calling the initialisers of the pointer types (which I don't think you are supposed to do), and instead tried to use the withXXX functions as much as I could.

You should also consider just writing Objective-C wrappers around the C function and lxw_rich_string_tuple. Sometimes C functions are just not bridged into Swift in a very convenient way, and this is not the first time I've experienced something like this, unfortunately :(

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.