-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriteEmptyArray.c
More file actions
106 lines (87 loc) · 3.79 KB
/
writeEmptyArray.c
File metadata and controls
106 lines (87 loc) · 3.79 KB
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
/*
* Copyright 2011 Emanuel Ey <[email protected]>
*
* This file is part of matOut.
*
* MatOut is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MatOut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with matOut. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdio.h>
#include <stdint.h>
#include "matOut.h"
/*
* Writes an empty mxArray to file.
* This is required when structures contain empty fields.
* The structures fields must always exist, even if empty.
*/
void writeEmptyArray(FILE* outfile);
void writeEmptyArray(FILE* outfile){
/*
* TODO: documentation
* TODO: verify successfull write
*/
uint32_t nBytes, tempUInt32, flags, mxClass;
size_t discard;
//char marker[8] = {'X','X','X','X','X','X','X','X'};
//fwrite(&marker, sizeof(uint8_t), 8, outfile);
/*
* Size required by an empty mxArray:
*/
/*
* nBytes += 8; //header: "miMatrix"[4B]; "size"[4B]
* nBytes += 16; //Array flags: "miUINT32"[4B]; "8"[4B]; "flags+mxCLASS"[4B]; "undefined"[4B]
* nBytes += 16; //2D dimensions (1x1)
* nBytes += 8; //empty array name "miINT8"[4B]; "0"[4B]
* nBytes += 8; //empty "double" data element "miDouble"[4B9; "0"[4B]
* NOTE: size written to header does not include the 8B required for the header itself;
*/
nBytes = 48;
//write header:
tempUInt32 = miMATRIX;
discard = fwrite(&tempUInt32, sizeof(uint32_t), 1, outfile); (void)discard;
discard = fwrite(&nBytes, sizeof(uint32_t), 1, outfile); (void)discard;
//write array flags:
tempUInt32 = miUINT32;
discard = fwrite(&tempUInt32, sizeof(uint32_t), 1, outfile); (void)discard;
tempUInt32 = 8;
discard = fwrite(&tempUInt32, sizeof(uint32_t), 1, outfile); (void)discard;
//NOTE: for the purpose of writing and empty array, we'll define it as "real" and "double"
flags = 0x00;
mxClass = mxDOUBLE_CLASS;
tempUInt32 = flags;
tempUInt32 <<= 8; //left shift the flags by one byte
tempUInt32 |= mxClass;
discard = fwrite(&tempUInt32, sizeof(uint32_t), 1, outfile); (void)discard;
tempUInt32 = 0x00;
discard = fwrite(&tempUInt32, sizeof(uint32_t), 1, outfile); (void)discard;
//write dimensions array (size is 1x1):
tempUInt32 = miINT32;
discard = fwrite(&tempUInt32, sizeof(uint32_t), 1, outfile); (void)discard;
tempUInt32 = 8;
discard = fwrite(&tempUInt32, sizeof(uint32_t), 1, outfile); (void)discard;
tempUInt32 = 1;
discard = fwrite(&tempUInt32, sizeof(uint32_t), 1, outfile); (void)discard;
discard = fwrite(&tempUInt32, sizeof(uint32_t), 1, outfile); (void)discard;
//write an empty name:
tempUInt32 = miINT8;
discard = fwrite(&tempUInt32, sizeof(uint32_t), 1, outfile); (void)discard;
tempUInt32 = 0;
discard = fwrite(&tempUInt32, sizeof(uint32_t), 1, outfile); (void)discard;
//write mxClass (double) and size (which is zero):
tempUInt32 = miDOUBLE;
discard = fwrite(&tempUInt32, sizeof(uint32_t), 1, outfile); (void)discard;
tempUInt32 = 0;
discard = fwrite(&tempUInt32, sizeof(uint32_t), 1, outfile); (void)discard;
//fwrite(&marker, sizeof(uint8_t), 8, outfile);
}