forked from Embroidermodder/libembroidery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-csv.c
309 lines (287 loc) · 10.8 KB
/
format-csv.c
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
#define CSV_EXPECT_NULL 0
#define CSV_EXPECT_QUOTE1 1
#define CSV_EXPECT_QUOTE2 2
#define CSV_EXPECT_COMMA 3
#define CSV_MODE_NULL 0
#define CSV_MODE_COMMENT 1
#define CSV_MODE_VARIABLE 2
#define CSV_MODE_THREAD 3
#define CSV_MODE_STITCH 4
static char* csvStitchFlagToStr(int flags)
{
switch (flags) {
case NORMAL:
return "STITCH";
break;
case JUMP:
return "JUMP";
break;
case TRIM:
return "TRIM";
break;
case STOP:
return "COLOR";
break;
case END:
return "END";
break;
default:
return "UNKNOWN";
break;
}
}
static int csvStrToStitchFlag(const char* str)
{
if (!str) {
embLog("ERROR: format-csv.c csvStrToStitchFlag(), str argument is null\n");
return -1;
}
if (!strcmp(str, "STITCH"))
return NORMAL;
else if (!strcmp(str, "JUMP"))
return JUMP;
else if (!strcmp(str, "TRIM"))
return TRIM;
else if (!strcmp(str, "COLOR"))
return STOP;
else if (!strcmp(str, "END"))
return END;
else if (!strcmp(str, "UNKNOWN"))
return -1;
return -1;
}
/*! Reads a file with the given \a fileName and loads the data into \a pattern.
* Returns \c true if successful, otherwise returns \c false. */
static int readCsv(EmbPattern* pattern, EmbFile* file, const char* fileName)
{
int numColorChanges = 0;
int size = 1024;
int pos = 0;
int c = 0;
int cellNum = 0;
int process = 0;
int csvMode = CSV_MODE_NULL;
int expect = CSV_EXPECT_QUOTE1;
int flags = 0;
double xx = 0.0;
double yy = 0.0;
unsigned char r = 0, g = 0, b = 0;
char* buff = 0;
buff = (char*)malloc(size);
if (!buff) {
embLog("ERROR: format-csv.c readCsv(), unable to allocate memory for buff\n");
return 0;
}
pos = 0;
do {
c = embFile_getc(file);
switch (c) {
case '"':
if (expect == CSV_EXPECT_QUOTE1) {
expect = CSV_EXPECT_QUOTE2;
} else if (expect == CSV_EXPECT_QUOTE2)
expect = CSV_EXPECT_COMMA;
break;
case ',':
if (expect == CSV_EXPECT_COMMA) {
process = 1;
}
break;
case '\n':
if (expect == CSV_EXPECT_COMMA) {
process = 1;
} else if (expect == CSV_EXPECT_QUOTE1) {
/* Do Nothing. We encountered a blank line. */
} else {
embLog("ERROR: format-csv.c readCsv(), premature newline\n");
return 0;
}
break;
}
if (pos >= size - 1) {
size *= 2;
buff = (char*)realloc(buff, size);
if (!buff) {
embLog("ERROR: format-csv.c readCsv(), cannot re-allocate memory for buff\n");
return 0;
}
}
if (process) {
buff[pos] = 0;
pos = 0;
process = 0;
cellNum++;
expect = CSV_EXPECT_QUOTE1;
if (csvMode == CSV_MODE_NULL) {
if (!strcmp(buff, "#")) {
csvMode = CSV_MODE_COMMENT;
} else if (!strcmp(buff, ">")) {
csvMode = CSV_MODE_VARIABLE;
} else if (!strcmp(buff, "$")) {
csvMode = CSV_MODE_THREAD;
} else if (!strcmp(buff, "*")) {
csvMode = CSV_MODE_STITCH;
} else { /* TODO: error */
return 0;
}
} else if (csvMode == CSV_MODE_COMMENT) {
/* Do Nothing */
} else if (csvMode == CSV_MODE_VARIABLE) {
/* Do Nothing */
} else if (csvMode == CSV_MODE_THREAD) {
if (cellNum == 2) {
/* Do Nothing. Ignore Thread Number */
} else if (cellNum == 3)
r = (unsigned char)atoi(buff);
else if (cellNum == 4)
g = (unsigned char)atoi(buff);
else if (cellNum == 5)
b = (unsigned char)atoi(buff);
else if (cellNum == 6) {
/* TODO: Thread Description */
} else if (cellNum == 7) {
/* TODO: Thread Catalog Number */
EmbThread t;
t.color.r = r;
t.color.g = g;
t.color.b = b;
t.description = "TODO:DESCRIPTION";
t.catalogNumber = "TODO:CATALOG_NUMBER";
embPattern_addThread(pattern, t);
csvMode = CSV_MODE_NULL;
cellNum = 0;
} else {
/* TODO: error */
return 0;
}
} else if (csvMode == CSV_MODE_STITCH) {
if (cellNum == 2) {
flags = csvStrToStitchFlag(buff);
if (flags == STOP)
numColorChanges++;
} else if (cellNum == 3)
xx = atof(buff);
else if (cellNum == 4) {
yy = atof(buff);
embPattern_addStitchAbs(pattern, xx, yy, flags, 1);
csvMode = CSV_MODE_NULL;
cellNum = 0;
} else {
/* TODO: error */
return 0;
}
}
if (c == '\n') {
csvMode = CSV_MODE_NULL;
cellNum = 0;
}
} else {
if (expect == CSV_EXPECT_QUOTE2 && c != '"')
buff[pos++] = (char)c;
}
} while (c != EOF);
/* if not enough colors defined, fill in random colors */
while (pattern->threads->count < numColorChanges) {
embPattern_addThread(pattern, embThread_getRandom());
}
free(buff);
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. */
static int writeCsv(EmbPattern* pattern, EmbFile* file, const char* fileName)
{
EmbStitch st;
EmbRect boundingRect;
EmbThread thr;
int i;
int stitchCount = 0;
int threadCount = 0;
stitchCount = pattern->stitchList->count;
threadCount = pattern->threads->count;
boundingRect = embPattern_calcBoundingBox(pattern);
if (!stitchCount) {
embLog("ERROR: format-csv.c writeCsv(), pattern contains no stitches\n");
return 0;
}
embPattern_end(pattern);
/* write header */
embFile_print(file, "\"#\",\"Embroidermodder 2 CSV Embroidery File\"\n");
embFile_print(file, "\"#\",\"http://embroidermodder.github.io\"\n");
embFile_print(file, "\n");
embFile_print(file, "\"#\",\"General Notes:\"\n");
embFile_print(file, "\"#\",\"This file can be read by Excel or LibreOffice as CSV (Comma Separated Value) or with a text editor.\"\n");
embFile_print(file, "\"#\",\"Lines beginning with # are comments.\"\n");
embFile_print(file, "\"#\",\"Lines beginning with > are variables: [VAR_NAME], [VAR_VALUE]\"\n");
embFile_print(file, "\"#\",\"Lines beginning with $ are threads: [THREAD_NUMBER], [RED], [GREEN], [BLUE], [DESCRIPTION], [CATALOG_NUMBER]\"\n");
embFile_print(file, "\"#\",\"Lines beginning with * are stitch entries: [STITCH_TYPE], [X], [Y]\"\n");
embFile_print(file, "\n");
embFile_print(file, "\"#\",\"Stitch Entry Notes:\"\n");
embFile_print(file, "\"#\",\"STITCH instructs the machine to move to the position [X][Y] and then make a stitch.\"\n");
embFile_print(file, "\"#\",\"JUMP instructs the machine to move to the position [X][Y] without making a stitch.\"\n");
embFile_print(file, "\"#\",\"TRIM instructs the machine to cut the thread before moving to the position [X][Y] without making a stitch.\"\n");
embFile_print(file, "\"#\",\"COLOR instructs the machine to stop temporarily so that the user can change to a different color thread before resuming.\"\n");
embFile_print(file, "\"#\",\"END instructs the machine that the design is completed and there are no further instructions.\"\n");
embFile_print(file, "\"#\",\"UNKNOWN encompasses instructions that may not be supported currently.\"\n");
embFile_print(file, "\"#\",\"[X] and [Y] are absolute coordinates in millimeters (mm).\"\n");
embFile_print(file, "\n");
/* write variables */
embFile_print(file, "\"#\",\"[VAR_NAME]\",\"[VAR_VALUE]\"\n");
embFile_print(file, "\">\",\"STITCH_COUNT:\",\"");
writeInt(file, stitchCount, 6);
embFile_print(file, "\"\n");
embFile_print(file, "\">\",\"THREAD_COUNT:\",\"");
writeInt(file, threadCount, 6);
embFile_print(file, "\"\n");
embFile_print(file, "\">\",\"EXTENTS_LEFT:\",\"");
writeFloat(file, boundingRect.left);
embFile_print(file, "\"\n");
embFile_print(file, "\">\",\"EXTENTS_TOP:\",\"");
writeFloat(file, boundingRect.top);
embFile_print(file, "\"\n");
embFile_print(file, "\">\",\"EXTENTS_RIGHT:\",\"");
writeFloat(file, boundingRect.right);
embFile_print(file, "\"\n");
embFile_print(file, "\">\",\"EXTENTS_BOTTOM:\",\"");
writeFloat(file, boundingRect.bottom);
embFile_print(file, "\"\n");
embFile_print(file, "\">\",\"EXTENTS_WIDTH:\",\"");
writeFloat(file, embRect_width(boundingRect));
embFile_print(file, "\"\n");
embFile_print(file, "\">\",\"EXTENTS_HEIGHT:\",\"");
writeFloat(file, embRect_height(boundingRect));
embFile_print(file, "\"\n\n");
/* write colors */
embFile_print(file, "\"#\",\"[THREAD_NUMBER]\",\"[RED]\",\"[GREEN]\",\"[BLUE]\",\"[DESCRIPTION]\",\"[CATALOG_NUMBER]\"\n");
for (i = 0; i < threadCount; i++) {
thr = pattern->threads->thread[i];
/* TODO: fix segfault that backtraces here when libembroidery-convert from dst to csv. */
embFile_print(file, "\"$\",\"");
writeInt(file, i + 1, 3);
embFile_print(file, "\",\"");
writeInt(file, (int)thr.color.r, 4);
embFile_print(file, "\",\"");
writeInt(file, (int)thr.color.g, 4);
embFile_print(file, "\",\"");
writeInt(file, (int)thr.color.b, 4);
embFile_print(file, "\",\"");
embFile_print(file, thr.description);
embFile_print(file, "\",\"");
embFile_print(file, thr.catalogNumber);
embFile_print(file, "\"\n");
}
embFile_print(file, "\n");
/* write stitches */
embFile_print(file, "\"#\",\"[STITCH_TYPE]\",\"[X]\",\"[Y]\"\n");
for (i = 0; i < pattern->stitchList->count; i++) {
st = pattern->stitchList->stitch[i];
embFile_print(file, "\"*\",\"");
embFile_print(file, csvStitchFlagToStr(st.flags));
embFile_print(file, "\",\"");
writeFloat(file, st.x);
embFile_print(file, "\",\"");
writeFloat(file, st.y);
embFile_print(file, "\"\n");
}
return 1;
}