-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
143 lines (115 loc) · 4.81 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include "objectMove.h"
uint8_t handleInput(int argc, char* argv[], objectSettings_t** objects, char** outFileName, uint8_t* forceFile);
void printHelp(void);
void printObjects(objectSettings_t* objects, uint16_t objectCount);
int main(int argc, char *argv[]) {
setbuf(stdout, NULL);
// Check for correct number of arguments
if(argc < 4) {
printHelp();
}
char* outFileName = NULL;
objectSettings_t* objects = 0;
uint8_t forceFile = 0;
uint16_t objectCount = handleInput(argc, argv, &objects, &outFileName, &forceFile);
if(objectCount == 0) return 1;
if(moveObjects(argv[1], outFileName, forceFile, objects, objectCount) == 0)
printf("Done :)\n");
return 0;
}
uint8_t handleInput(int argc, char* argv[], objectSettings_t** objects, char** outFileName, uint8_t* forceFile) {
// Count amount of objects and allocate the array
uint16_t objectCount = 0;
for(uint16_t i = 1; i < argc; i++) {
if(argv[i][0] == '-' && argv[i][1] == 'N')
objectCount++;
}
*objects = (objectSettings_t*) calloc(objectCount, sizeof(objectSettings_t));
// Go through the arguments and save them
objectSettings_t* currentObject = *objects - 1;
// -1 because the N option goes to the next object before doing anything.
// Not the prettiest but it works
for(uint16_t i = 2; i < argc; i++) {
if(argv[i][0] == '-') {
switch(argv[i][1]) {
case 'N':
if(i + 1 >= argc) {
printf("Please specify the object's name when using -N\n");
return 0;
}
currentObject++;
char* storeName = (char*)malloc(2 + strlen(argv[i + 1]));
sprintf(storeName, "%s ", argv[i + 1]);
currentObject->name = storeName;
i++;
break;
case 'a':
if(!isalpha(argv[i][2])) {
printf("Please specify an axis when using -a\n");
return 0;
}
if(i + 1 >= argc) {
printf("Please specify the offset amount when using -a\n");
return 0;
}
currentObject->axes[currentObject->axisCount].identifier = argv[i][2];
currentObject->axes[currentObject->axisCount].offset = atoi(argv[i + 1]);
currentObject->axisCount++;
i++;
break;
case 'c':
currentObject->noColorChange = 1;
break;
case 'O':
*outFileName = argv[i + 1];
i++;
break;
case 'F':
*forceFile = 1;
break;
case 'H':
case 'h':
printHelp();
break;
default:
printf("%s is not a valid option.\n", argv[i]);
return 0;
}
}
else {
printf("%s is not a valid option.\n", argv[i]);
printHelp();
return 0;
}
}
if(objectCount == 0) printf("No objects were specified.\n");
return objectCount;
}
void printHelp(void) {
printf("\nUsage:\n");
printf("The first argument should be the input file name. A better more detailed explanation can be found on git:\n");
printf("https://github.com/Kwekker/PrusaSlicer-gcode-object-mover\n");
printf("-H\t\tShow help\n");
printf("-O <filename>\tWrite an output file with name <filename> instead of overwriting the input file.\n");
printf("-F\t\tOverwrite the current file without asking for confirmation.\n");
printf("-N <name>\tAll consecutive lowercase flags will be about the object named <name>.\n");
printf("-a<I> <offset>\tSet the offset of an axis. <I> is the axis name (like 'X'), <offset> is an offset integer.\n");
printf("-c\t\tNo filament change will be inserted before the object (so just don't use this flag if you want a filament change).\n");
printf("\n");
}
void printObjects(objectSettings_t* objects, uint16_t objectCount) {
for(uint8_t i = 0; i < objectCount; i++) {
objectSettings_t* o = objects + i;
printf("name: %s\nno color c: %d\naxis count: %d\n", o->name, o->noColorChange, o->axisCount);
for(uint8_t j = 0; j < o->axisCount; j++) {
printf("\tchar: %c\n\toffset: %d\n", o->axes[j].identifier, o->axes[j].offset);
}
printf("\n");
}
}