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
|
#include "format-inf.h"
#include "emb-file.h"
#include "emb-logging.h"
#include "helpers-binary.h"
#include <stdlib.h>
#include <string.h>
/*! Reads a file with the given \a fileName and loads the data into \a pattern.
* Returns \c true if successful, otherwise returns \c false. */
int readInf(EmbPattern* pattern, const char* fileName)
{
int numberOfColors;
int i;
EmbFile* file = 0;
if(!pattern) { embLog_error("format-inf.c readInf(), pattern argument is null\n"); return 0; }
if(!fileName) { embLog_error("format-inf.c readInf(), fileName argument is null\n"); return 0; }
file = embFile_open(fileName, "rb");
if(!file)
{
/* NOTE: The .inf format is an optional color file. Do not log an error if the file does not exist */
return 0;
}
binaryReadUInt32BE(file);
binaryReadUInt32BE(file);
binaryReadUInt32BE(file);
numberOfColors = binaryReadUInt32BE(file);
embThreadList_free(pattern->threadList);
pattern->threadList = 0;
pattern->lastThread = 0;
for(i = 0; i < numberOfColors; i++)
{
char colorType[50];
char colorDescription[50];
EmbThread t;
binaryReadUInt16(file);
binaryReadUInt16(file);
t.color.r = binaryReadByte(file);
t.color.g = binaryReadByte(file);
t.color.b = binaryReadByte(file);
t.catalogNumber = "";
t.description = "";
embPattern_addThread(pattern, t);
binaryReadUInt16(file);
binaryReadString(file, colorType, 50);
binaryReadString(file, colorDescription, 50);
}
embFile_close(file);
return 1;
}
/*! Writes the data from \a pattern to a file with the given \a fileName.
* Returns \c true if successful, otherwise returns \c false. */
int writeInf(EmbPattern* pattern, const char* fileName)
{
EmbThreadList* pointer = 0;
int i = 1, bytesRemaining;
EmbFile* file = 0;
if(!pattern) { embLog_error("format-inf.c writeInf(), pattern argument is null\n"); return 0; }
if(!fileName) { embLog_error("format-inf.c writeInf(), fileName argument is null\n"); return 0; }
file = embFile_open(fileName, "wb");
if(!file)
{
embLog_error("format-inf.c writeInf(), cannot open %s for writing\n", fileName);
return 0;
}
binaryWriteUIntBE(file, 0x01);
binaryWriteUIntBE(file, 0x08);
/* write place holder offset */
binaryWriteUIntBE(file, 0x00);
binaryWriteUIntBE(file, embThreadList_count(pattern->threadList));
pointer = pattern->threadList;
while(pointer)
{
char buffer[50];
EmbColor c;
c = pointer->thread.color;
sprintf(buffer, "RGB(%d,%d,%d)", (int)c.r, (int)c.g, (int)c.b);
binaryWriteUShortBE(file, (unsigned short)(14 + strlen(buffer))); /* record length */
binaryWriteUShortBE(file, (unsigned short)i); /* record number */
binaryWriteByte(file, c.r);
binaryWriteByte(file, c.g);
binaryWriteByte(file, c.b);
binaryWriteUShortBE(file, (unsigned short)i); /* needle number */
binaryWriteBytes(file, "RGB\0", 4);
embFile_printf(file, buffer);
binaryWriteByte(file, 0);
pointer = pointer->next;
i++;
}
embFile_seek(file, -8, SEEK_END);
bytesRemaining = embFile_tell(file);
embFile_seek(file, 8, SEEK_SET);
binaryWriteUIntBE(file, bytesRemaining);
embFile_close(file);
return 1;
}
/* kate: bom off; indent-mode cstyle; indent-width 4; replace-trailing-space-save on; */
|