fm_plugin 0.1.17

Build plug-ins for use with FileMaker Pro and FileMaker Server.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use super::*;
use std::convert::TryFrom;
use std::fmt;
use std::mem::ManuallyDrop;

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct fmx_BinaryData {
    _address: u8,
}

#[cfg_attr(target_os = "macos", link(kind = "framework", name = "FMWrapper"))]
#[cfg_attr(target_os = "windows", link(kind = "static", name = "FMWrapper"))]
#[cfg_attr(target_os = "linux", link(kind = "dylib", name = "FMWrapper"))]
extern "C" {
    fn FM_BinaryData_Constructor1(_x: *mut fmx__fmxcpt) -> *mut fmx_BinaryData;
    fn FM_BinaryData_Constructor2(
        sourceData: *const fmx_BinaryData,
        _x: *mut fmx__fmxcpt,
    ) -> *mut fmx_BinaryData;

    fn FM_BinaryData_Constructor3(
        name: *const fmx_Text,
        amount: u32,
        buffer: *mut i8,
        _x: *mut fmx__fmxcpt,
    ) -> *mut fmx_BinaryData;

    #[allow(dead_code)]
    fn FM_BinaryData_Constructor4(
        name: *const fmx_Text,
        context: *mut u32,
        _x: *mut fmx__fmxcpt,
    ) -> *mut fmx_BinaryData;

    #[allow(dead_code)]
    fn FM_BinaryData_operatorAS(
        _self: *mut fmx_BinaryData,
        source: *const fmx_BinaryData,
        _x: *mut fmx__fmxcpt,
    ) -> *mut fmx_BinaryData;

    fn FM_BinaryData_operatorEQ(
        _self: *const fmx_BinaryData,
        compareData: *const fmx_BinaryData,
        _x: *mut fmx__fmxcpt,
    ) -> bool;

    fn FM_BinaryData_operatorNE(
        _self: *const fmx_BinaryData,
        compareData: *const fmx_BinaryData,
        _x: *mut fmx__fmxcpt,
    ) -> bool;

    fn FM_BinaryData_GetCount(_self: *const fmx_BinaryData, _x: *mut fmx__fmxcpt) -> i32;

    fn FM_BinaryData_GetIndex(
        _self: *const fmx_BinaryData,
        dataType: *const fmx_QuadChar,
        _x: *mut fmx__fmxcpt,
    ) -> i32;

    fn FM_BinaryData_GetTotalSize(_self: *const fmx_BinaryData, _x: *mut fmx__fmxcpt) -> u32;

    fn FM_BinaryData_GetType(
        _self: *const fmx_BinaryData,
        index: i32,
        dataType: *mut fmx_QuadChar,
        _x: *mut fmx__fmxcpt,
    );

    fn FM_BinaryData_GetSize(_self: *const fmx_BinaryData, index: i32, _x: *mut fmx__fmxcpt)
        -> u32;

    fn FM_BinaryData_GetData(
        _self: *const fmx_BinaryData,
        index: i32,
        offset: u32,
        amount: u32,
        buffer: *mut i8,
        _x: *mut fmx__fmxcpt,
    ) -> FMError;

    fn FM_BinaryData_Add(
        _self: *mut fmx_BinaryData,
        dataType: *const fmx_QuadChar,
        amount: u32,
        buffer: *mut i8,
        _x: *mut fmx__fmxcpt,
    ) -> FMError;

    fn FM_BinaryData_Remove(
        _self: *mut fmx_BinaryData,
        dataType: *const fmx_QuadChar,
        _x: *mut fmx__fmxcpt,
    ) -> bool;

    fn FM_BinaryData_RemoveAll(_self: *mut fmx_BinaryData, _x: *mut fmx__fmxcpt);
    fn FM_BinaryData_Delete(_self: *mut fmx_BinaryData, _x: *mut fmx__fmxcpt);

    fn FM_BinaryData_GetFNAMData(
        _self: *const fmx_BinaryData,
        filepathlist: *mut fmx_Text,
        _x: *mut fmx__fmxcpt,
    ) -> FMError;

    fn FM_BinaryData_AddFNAMData(
        _self: *mut fmx_BinaryData,
        filepathlist: *const fmx_Text,
        _x: *mut fmx__fmxcpt,
    ) -> FMError;

    fn FM_BinaryData_GetSIZEData(
        _self: *const fmx_BinaryData,
        width: *mut i16,
        height: *mut i16,
        _x: *mut fmx__fmxcpt,
    ) -> FMError;

    fn FM_BinaryData_AddSIZEData(
        _self: *mut fmx_BinaryData,
        width: i16,
        height: i16,
        _x: *mut fmx__fmxcpt,
    ) -> FMError;

    fn FM_BinaryData_AddBegin(
        _self: *mut fmx_BinaryData,
        dataType: *const fmx_QuadChar,
        context: *mut u32,
        _x: *mut fmx__fmxcpt,
    ) -> FMError;

    fn FM_BinaryData_AddAppend(
        _self: *mut fmx_BinaryData,
        context: u32,
        amount: u32,
        buffer: *mut i8,
        _x: *mut fmx__fmxcpt,
    ) -> FMError;

    fn FM_BinaryData_AddFinish(
        _self: *mut fmx_BinaryData,
        context: u32,
        _x: *mut fmx__fmxcpt,
    ) -> FMError;
}

#[derive(Eq)]
pub struct BinaryData {
    pub(crate) ptr: *mut fmx_BinaryData,
    drop: bool,
}

impl BinaryData {
    pub fn new() -> Self {
        let mut _x = fmx__fmxcpt::new();
        let ptr = unsafe { FM_BinaryData_Constructor1(&mut _x) };
        _x.check();
        Self { ptr, drop: true }
    }

    pub fn to_owned(&self) -> Self {
        let mut _x = fmx__fmxcpt::new();
        let ptr = unsafe { FM_BinaryData_Constructor2(self.ptr, &mut _x) };
        _x.check();
        Self { ptr, drop: true }
    }

    pub fn from_buffer<T: ToText>(name: T, buffer: Vec<u8>) -> Self {
        let mut _x = fmx__fmxcpt::new();
        let name = name.to_text();
        let buffer_size = buffer.len() as u32;
        let mut buffer = ManuallyDrop::new(buffer);
        let buffer_ptr = buffer.as_mut_ptr();

        let ptr = unsafe {
            FM_BinaryData_Constructor3(name.ptr, buffer_size as u32, buffer_ptr as *mut i8, &mut _x)
        };
        unsafe { ManuallyDrop::drop(&mut buffer) };
        _x.check();
        Self { ptr, drop: true }
    }

    pub fn from_ptr(ptr: *const fmx_BinaryData) -> Self {
        Self {
            ptr: ptr as *mut fmx_BinaryData,
            drop: false,
        }
    }

    pub fn get_stream_count(&self) -> i32 {
        let mut _x = fmx__fmxcpt::new();
        let count = unsafe { FM_BinaryData_GetCount(self.ptr, &mut _x) };
        _x.check();
        count
    }

    pub fn get_stream_index(&self, stream_type: BinaryStreamType) -> i32 {
        let mut _x = fmx__fmxcpt::new();
        let quad = QuadChar::from(stream_type);
        let index = unsafe { FM_BinaryData_GetIndex(self.ptr, quad.ptr, &mut _x) };
        _x.check();
        index
    }

    pub fn total_size(&self) -> u32 {
        let mut _x = fmx__fmxcpt::new();
        let size = unsafe { FM_BinaryData_GetTotalSize(self.ptr, &mut _x) };
        _x.check();
        size
    }

    pub fn get_size(&self, index: i32) -> u32 {
        let mut _x = fmx__fmxcpt::new();
        let size = unsafe { FM_BinaryData_GetSize(self.ptr, index, &mut _x) };
        _x.check();
        size
    }

    pub fn get_data(&self, index: i32, offset: u32, amount: usize) -> Vec<u8> {
        let buffer = Vec::with_capacity(amount);
        let mut buffer = ManuallyDrop::new(buffer);
        let ptr = buffer.as_mut_ptr();

        let mut _x = fmx__fmxcpt::new();
        let error =
            unsafe { FM_BinaryData_GetData(self.ptr, index, offset, amount as u32, ptr, &mut _x) };
        _x.check();
        if error != FMError::NoError {
            panic!();
        }
        unsafe { Vec::from_raw_parts(ptr as *mut u8, amount, amount) }
    }

    pub fn get_type(&self, index: i32) -> BinaryStreamType {
        let mut _x = fmx__fmxcpt::new();
        let quad = QuadChar::empty();
        unsafe { FM_BinaryData_GetType(self.ptr, index, quad.ptr, &mut _x) };
        _x.check();
        BinaryStreamType::from(quad)
    }

    pub fn add_stream(&self, stream_type: BinaryStreamType, buffer: &mut Vec<u8>) {
        let mut _x = fmx__fmxcpt::new();
        let quad = QuadChar::from(stream_type);
        let size = buffer.len() as u32;
        let mut buffer = ManuallyDrop::new(buffer);
        let buffer_ptr = buffer.as_mut_ptr();
        let error =
            unsafe { FM_BinaryData_Add(self.ptr, quad.ptr, size, buffer_ptr as *mut i8, &mut _x) };
        unsafe { ManuallyDrop::drop(&mut buffer) };
        _x.check();
        if error != FMError::NoError {
            panic!();
        }
    }

    pub fn remove_stream(&self, stream_type: BinaryStreamType) {
        let mut _x = fmx__fmxcpt::new();
        let quad = QuadChar::from(stream_type);
        let success = unsafe { FM_BinaryData_Remove(self.ptr, quad.ptr, &mut _x) };
        _x.check();
        if !success {
            panic!();
        }
    }

    pub fn remove_all(&self) {
        let mut _x = fmx__fmxcpt::new();
        unsafe { FM_BinaryData_RemoveAll(self.ptr, &mut _x) };
        _x.check();
    }

    pub fn start_stream(&self, stream_type: BinaryStreamType) -> u32 {
        let mut _x = fmx__fmxcpt::new();
        let quad = QuadChar::from(stream_type);
        let mut context = 0;
        let error = unsafe { FM_BinaryData_AddBegin(self.ptr, quad.ptr, &mut context, &mut _x) };
        _x.check();
        if error != FMError::NoError {
            panic!();
        }
        context
    }

    pub fn append_stream(&self, stream_id: u32, buffer: &mut Vec<u8>) {
        let mut _x = fmx__fmxcpt::new();
        let size = buffer.len() as u32;
        let mut buffer = ManuallyDrop::new(buffer);
        let buffer_ptr = buffer.as_mut_ptr();
        let error = unsafe {
            FM_BinaryData_AddAppend(self.ptr, stream_id, size, buffer_ptr as *mut i8, &mut _x)
        };
        unsafe { ManuallyDrop::drop(&mut buffer) };
        _x.check();
        if error != FMError::NoError {
            panic!();
        }
    }

    pub fn end_stream(&self, stream_id: u32) {
        let mut _x = fmx__fmxcpt::new();
        let error = unsafe { FM_BinaryData_AddFinish(self.ptr, stream_id, &mut _x) };
        _x.check();
        if error != FMError::NoError {
            panic!();
        }
    }

    pub fn get_file_names(&self) -> Text {
        let mut _x = fmx__fmxcpt::new();
        let file_paths = Text::new();
        let error = unsafe { FM_BinaryData_GetFNAMData(self.ptr, file_paths.ptr, &mut _x) };
        _x.check();
        if error != FMError::NoError {
            panic!();
        }
        file_paths
    }

    pub fn add_file_paths<T: ToText>(&self, file_paths: T) {
        let mut _x = fmx__fmxcpt::new();
        let f_paths = file_paths.to_text();
        let error = unsafe { FM_BinaryData_AddFNAMData(self.ptr, f_paths.ptr, &mut _x) };
        _x.check();
        if error != FMError::NoError {
            panic!();
        }
    }

    pub fn get_dimensions(&self) -> (i16, i16) {
        let mut _x = fmx__fmxcpt::new();
        let mut height = 0;
        let mut width = 0;
        let error =
            unsafe { FM_BinaryData_GetSIZEData(self.ptr, &mut width, &mut height, &mut _x) };
        _x.check();
        if error != FMError::NoError {
            panic!();
        }
        (width, height)
    }

    pub fn set_dimensions(&self, height: i16, width: i16) {
        let mut _x = fmx__fmxcpt::new();
        let error = unsafe { FM_BinaryData_AddSIZEData(self.ptr, width, height, &mut _x) };
        _x.check();
        if error != FMError::NoError {
            panic!();
        }
    }
}

impl Drop for BinaryData {
    fn drop(&mut self) {
        if self.drop {
            let mut _x = fmx__fmxcpt::new();
            unsafe { FM_BinaryData_Delete(self.ptr, &mut _x) };
            _x.check();
        }
    }
}

impl Default for BinaryData {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug, PartialEq)]
pub enum BinaryStreamType {
    FNAM,
    JPEG,
    GIF,
    EPS,
    META,
    PNG,
    BMP,
    PDF,
    FILE,
    ZLIB,
    FORK,
    SND,
    MAIN,
    SIZE,
    Other(String),
}

impl From<BinaryStreamType> for QuadChar {
    fn from(stream_type: BinaryStreamType) -> QuadChar {
        use BinaryStreamType::*;
        match stream_type {
            FNAM => QuadChar::new(b"FNAM"),
            JPEG => QuadChar::new(b"JPEG"),
            GIF => QuadChar::new(b"GIFf"),
            EPS => QuadChar::new(b"EPSf"),
            META => QuadChar::new(b"META"),
            PNG => QuadChar::new(b"PNGf"),
            BMP => QuadChar::new(b"BMPf"),
            PDF => QuadChar::new(b"PDF "),
            SIZE => QuadChar::new(b"SIZE"),
            FILE => QuadChar::new(b"FILE"),
            ZLIB => QuadChar::new(b"ZLIB"),
            FORK => QuadChar::new(b"FORK"),
            SND => QuadChar::new(b"snd "),
            MAIN => QuadChar::new(b"MAIN"),
            Other(txt) => {
                let slice = txt.as_bytes();
                let bytes = <&[u8; 4]>::try_from(&slice[..4]).unwrap();
                QuadChar::new(bytes)
            }
        }
    }
}

impl From<QuadChar> for BinaryStreamType {
    fn from(quad: QuadChar) -> BinaryStreamType {
        let txt = quad.to_string();
        use BinaryStreamType::*;
        match txt.as_ref() {
            "FNAM" => FNAM,
            "JPEG" => JPEG,
            "GIFf" => GIF,
            "EPSf" => EPS,
            "META" => META,
            "PNGf" => PNG,
            "BMPf" => BMP,
            "PDF " => PDF,
            "SIZE" => SIZE,
            "FILE" => FILE,
            "ZLIB" => ZLIB,
            "FORK" => FORK,
            "snd " => SND,
            "MAIN" => MAIN,
            t => Other(t.to_string()),
        }
    }
}

impl PartialEq for BinaryData {
    fn eq(&self, other: &BinaryData) -> bool {
        let mut _x = fmx__fmxcpt::new();
        let result = unsafe { FM_BinaryData_operatorEQ(self.ptr, other.ptr, &mut _x) };
        _x.check();
        result
    }

    #[allow(clippy::partialeq_ne_impl)]
    fn ne(&self, other: &BinaryData) -> bool {
        let mut _x = fmx__fmxcpt::new();
        let result = unsafe { FM_BinaryData_operatorNE(self.ptr, other.ptr, &mut _x) };
        _x.check();
        result
    }
}

impl fmt::Display for BinaryStreamType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl ToText for BinaryStreamType {
    fn to_text(self) -> Text {
        self.to_string().to_text()
    }
}