70bb2f89cbd5c47650853a85203c61033699536c
15 #define BUFFER_SIZE (32*1024*1024)
17 static char data
[BUFFER_SIZE
];
21 /*********************************++
23 * Read input data into a buffer
26 static void read_file(int fd
){
29 while ((res
=read(fd
,data
+data_size
,BUFFER_SIZE
-1-data_size
))) data_size
+=res
;
31 DBG("Read %i bytes.\n",data_left
);
34 static int comp(char ** pptr
, char * b
){
36 if (strncmp(ptr
,b
,strlen(b
))==0){
43 static struct point
* get_point(char ** pptr
){
45 struct point
*result
= (struct point
*)malloc(sizeof(struct point
));
47 result
->x
=strtold(*pptr
,&ptr2
);
48 if (ptr2
==*pptr
) result
->valid
=0;
51 *pptr
=index(*pptr
,',')+1;
52 result
->y
=strtold(*pptr
,&ptr2
);
53 if (ptr2
==*pptr
) result
->valid
=0;
57 result
->previous_in_path
=NULL
;
60 (*pptr
)++; /* Step over comma between point specs! */
64 static void output (struct path
* first_path
){
65 struct point
* last_point
=NULL
;
66 struct path
* current_path
;
68 for (current_path
=first_path
; current_path
!= NULL
;
69 current_path
=current_path
->next
){
71 struct point
* current_point
;
73 /* Initial move to start of plot */
74 current_point
=current_path
->plot_start_point
;
75 if (!equal(current_point
,last_point
)) {
76 printf("PU, %.2f,%.2f\n",current_point
->x
,current_point
->y
);
79 if (current_path
->plot_start_point
80 == current_path
->first_point
){ /* Forward */
82 for (current_point
=current_point
->next
; current_point
!= NULL
;
83 current_point
=current_point
->next
) {
84 if (!equal(current_point
,last_point
)) {
85 printf("PD, %.2f,%.2f\n",current_point
->x
,current_point
->y
);
87 last_point
=current_point
;
89 } else { /* Going backwards */
90 for (current_point
=current_point
->previous_in_path
; current_point
!= NULL
;
91 current_point
=current_point
->previous_in_path
) {
92 if (!equal(current_point
,last_point
)) {
93 printf("PD, %.2f,%.2f\n",current_point
->x
,current_point
->y
);
95 last_point
=current_point
;
101 /*******************************************************************************
105 *******************************************************************************
107 int main(int argc
, char ** args
){
109 int fd
=0; /* File descriptor for data input, defaults to standard input */
111 char * ptr
=data
; /* Input buffer */
112 enum plot_mode_t plot_mode
=absolute
; /* Coordinate modus */
113 enum pen_state_t pen_state
=up
;
115 struct point pen_pos
={0,0,1};
117 double character_angle
=0;
118 double character_size
=40;
120 struct path
* first_path
=NULL
;
121 struct path
* current_path
=NULL
;
125 enum hpgl_cmd_t command
; /* Current command being processed */
129 /* Open input file to read if specified. */
131 fd
=open(args
[1],O_RDONLY
);
133 ERR("Could not open file: \"%s\"\n",args
[1]);
137 /* fd=open("haus.hpl",O_RDONLY); */
144 while(data_left
-3 && isspace(ptr
[0])) ptr
++;
146 /* Determine command */
148 if (comp(&ptr
, "SC")) command
=CMD_SC
;
149 if (comp(&ptr
, "PU")) command
=CMD_PU
;
150 if (comp(&ptr
, "PD")) command
=CMD_PD
;
151 if (comp(&ptr
, "LB")) command
=CMD_LB
;
152 if (comp(&ptr
, "DI")) command
=CMD_DI
;
153 if (comp(&ptr
, "SP")) command
=CMD_SP
;
154 if (comp(&ptr
, "PA")) command
=CMD_PA
;
155 if (comp(&ptr
, "PR")) command
=CMD_PR
;
156 if (comp(&ptr
, "IN")) command
=CMD_IN
;
157 if (command
==0) {data_left
-- ; ptr
++;}
158 else DBG("Command: %i %c%c\n",command
,ptr
[-2],ptr
[-1]);
160 int process_points
=0;
165 case CMD_PU
: /* Pen up - not much to do here. */
173 if (!in_path
){ /* Create and store a new path object with first point @pen_pos. */
175 struct point
* new_point
=(struct point
*)malloc(sizeof(struct point
));
176 new_point
->x
=pen_pos
.x
;
177 new_point
->y
=pen_pos
.y
;
178 new_point
->next
=NULL
;
179 new_point
->previous_in_path
=NULL
;
182 struct path
* new_path
=(struct path
*)malloc(sizeof(struct path
));
183 new_path
->next
=NULL
; /* This is the last in the chain. */
184 new_path
->first_point
=new_point
;
185 new_path
->last_point
=new_point
;
186 new_path
->plot_start_point
=new_path
->first_point
;
188 if (first_path
==NULL
){ /* This is the first path */
191 current_path
->next
=new_path
; /* Append to linked list */
193 current_path
=new_path
; /* Update pointer to current path. */
194 in_path
=1; /* We're in a path now, fine! */
214 case CMD_DI
: /* Character direction */
216 case CMD_SI
: /* Character size */
218 case CMD_SP
: /* Select pen */
230 if (process_points
) { /* Process point arguments */
231 struct point
* new_point
;
233 new_point
=get_point(&ptr
);
235 if (new_point
->valid
) { /* The following only works if we got a good point! */
237 /* Scale: Plot in cm!! */
238 new_point
->x
= new_point
->x
/ 500;
239 new_point
->y
= new_point
->y
/ 500;
241 DBG("Found point: %6.2f,%6.2f\n",new_point
->x
,new_point
->y
);
243 /* Convert relative coordinates to absolute coordinates if needed */
244 if (plot_mode
==relative
){
245 DBG("Relative -> absolute coordinate calculation!\n");
246 new_point
->x
+= pen_pos
.x
;
247 new_point
->y
+= pen_pos
.y
;
250 if (pen_state
==down
) { /* We're in a path, so add the point to current path */
251 current_path
->last_point
->next
=new_point
; /* Add to the end of list */
252 new_point
->previous_in_path
=current_path
->last_point
; /* Backwards reference */
253 current_path
->last_point
=new_point
; /* Fix the point */
255 pen_pos
.x
=new_point
->x
;
256 pen_pos
.y
=new_point
->y
;
259 } while (new_point
->valid
);
262 } /* while(data_left) */
264 struct path
* output_paths
;
266 round_path(first_path
);
268 /* output_paths = optimize(first_path); */
269 output_paths
=optimize(split_paths(first_path
));
270 output(output_paths
);
271 DBG("Programmende\n");