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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
#define COMMAND_QUIT "quit"
#define COMMAND_IMAGE "image"
#define COMMAND_TEXT "text"
#define COMMAND_MOVE "move"
#define COMMAND_LIST "list"
#define COMMAND_POLY "poly"
#define COMMAND_SAVE "save"
#define ERR_CONFIG_FILE -3
#define ERR_CMD_ARGS -1
#define ERR_MEM -2
#define ERR_CORRUPT_CONFIG -4
#define CMDARG_CONFIG_FILE "-c="
//forward declarations,global vars, structs--------------------------------------
//forward declarations here
int run();
int getcommandlineargs(int argc, char *argv[]);
int read_config(char *filename_config);
int command_poly();
int command_text();
int command_image();
int command_move();
int command_list();
int command_save();
void command_quit();
int write_svg_tag(char* file_name);
int close_svg_tag(char* file_name);
int draw_text();
int draw_poly();
int draw_image();
void error_messages();
int gatherdata(int poly, int image, int text);
int fGetStr(char *string, int max_characters, FILE * stream);
int getlistobj();
int addtolist();
//end of forwardeclarations
//global variables
int run_ = TRUE;
int ret_val_ = 0;
char * filename_config;
//end of global variables
//structs comming here
//config struct
typedef struct
{
char magic_number[5];
char prompt[4];
int def_width;
int def_height;
} Config;
Config config_;
//Struct for a 2D point(espezially for polys)
typedef struct
{
int x;
int y;
} Point;
//Struct for all kind of objects
typedef struct
{
Point *points;
char * text_string;
int point_count;
char *fill;
char *stroke_fill;
int stroke_width;
char *shortname;
char* url;
int width;
int height;
int font_size;
} DataObject;
//------------------------------------------------------------------------------
//Main--------------------------------------------------------------------------
int main (int argc, char *argv[])
{
getcommandlineargs(argc,argv);
if(ret_val_ != 0)
{
error_messages();
return ret_val_;
}
// read_config(filename_config); //load config before anything else...
if(ret_val_ != 0)
{
error_messages();
return ret_val_;
}
run();
return ret_val_;
}
//------------------------------------------------------------------------------
int getcommandlineargs(int argc, char *argv[])
{
if (argc != 2) //arg one is programm itself
{
ret_val_ = ERR_CMD_ARGS;
return ret_val_;
}
if (strncmp(argv[1], CMDARG_CONFIG_FILE, 2) == 0) //check -c=, if not error
filename_config = argv[1]+strlen(CMDARG_CONFIG_FILE);
else
{
ret_val_ = ERR_CMD_ARGS;
return ret_val_;
}
return TRUE;
}
//Run---------------------------------------------------------------------------
int run()
{
char input_command[100];
while(run_ == TRUE)
{
printf("esp> ");
scanf("%s",input_command);
//go though list of commands and take the correct one
if(strcmp(input_command, COMMAND_QUIT) == 0)
{
command_quit();
}
else if(strcmp(input_command, COMMAND_SAVE) == 0)
{
command_save();
}
else if(strcmp(input_command, COMMAND_LIST) == 0)
{
command_list();
}
else if(strcmp(input_command, COMMAND_IMAGE) == 0)
{
command_image();
}
else if(strcmp(input_command, COMMAND_TEXT) == 0)
{
command_text();
}
else if(strcmp(input_command, COMMAND_POLY) == 0)
{
command_poly();
}
else if(strcmp(input_command, COMMAND_MOVE) == 0)
{
command_move();
}
else
printf("unkown command\n");
}
return TRUE;
}
//------------------------------------------------------------------------------
int command_poly()
{
//DataPoly * polygone;
return TRUE;
}
//------------------------------------------------------------------------------
int command_text()
{
char buffer_txt[1000];
char buffer_fill[100];
DataObject * text;
text = (DataObject*)malloc(sizeof(DataObject));
if (text == NULL)
return ERR_MEM;
//setting shortname here...
text->shortname = "tx";
printf("text? ");
//scanf("%s", buffer_txt);
fGetStr(buffer_txt, 1000, stdin);
text->text_string = (char*)malloc(strlen(buffer_txt)+1);
if (text->text_string == NULL)
return ERR_MEM;
strcpy(text->text_string, buffer_txt);
printf("x? ");
scanf("%d", &text->points->x);
printf("y? ");
scanf("%d", &text->points->y);
printf("font-size? ");
scanf("%d", &text->font_size);
printf("fill? ");
scanf("%s", text->fill);
text->fill = (char*)malloc(strlen(buffer_fill)+1);
if (text->fill == NULL)
return ERR_MEM;
strcpy(text->fill, buffer_fill);
//printf("group? ");
//ka wie i des mit da group löse..
return TRUE;
}
//------------------------------------------------------------------------------
int command_image()
{
DataObject * image;
char buffer_url[1000];
image = (DataObject*)malloc(sizeof(DataObject));
if (image == NULL)
return ERR_MEM;
//setting shortname here...
image->shortname = "im";
printf("image-url? ");
//fGetStr(buffer_url, 1000, stdin);
scanf("%s", buffer_url);
image->url = (char*)malloc(strlen(buffer_url)+1);
if (image->url == NULL)
return ERR_MEM;
strcpy(image->url, buffer_url);
printf("x? ");
scanf("%d", &image->points->x);
printf("y? ");
scanf("%d", &image->points->y);
printf("width? ");
scanf("%d", &image->width);
printf("height? ");
scanf("%d", &image->height);
printf("group? ");
//ka wie i des mit da group löse..
return TRUE;
}
//------------------------------------------------------------------------------
int command_move()
{
printf("not implemented yet\n");
return TRUE;
}
//------------------------------------------------------------------------------
int command_list()
{
printf("not implemented yet\n");
return TRUE;
}
//------------------------------------------------------------------------------
int command_save()
{
printf("not implemented yet\n");
return TRUE;
}
//------------------------------------------------------------------------------
void command_quit()
{
run_ = FALSE;
}
//------------------------------------------------------------------------------
int write_svg_tag(char* file_name)
{
FILE *file_ptr;
file_ptr = fopen(file_name, "w");
fprintf(file_ptr, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
fprintf(file_ptr, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" ");
fprintf(file_ptr, "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
fprintf(file_ptr, "<svg width=\"%d\" height=\"%d\" ", config_.def_width,
config_.def_height);
fprintf(file_ptr, "xmlns=\"http://www.w3.org/2000/svg\">\n");
fclose(file_ptr);
return TRUE;
}
//------------------------------------------------------------------------------
int close_svg_tag(char* file_name)
{
FILE *file_ptr;
file_ptr = fopen(file_name, "a");
fprintf(file_ptr, "</svg>");
fclose(file_ptr);
return TRUE;
}
//------------------------------------------------------------------------------
int draw_text()
{
printf("not implemented yet\n");
return TRUE;
}
//------------------------------------------------------------------------------
int draw_poly()
{
printf("not implemented yet\n");
return TRUE;
}
//------------------------------------------------------------------------------
int draw_image()
{
printf("not implemented yet\n");
return TRUE;
}
//------------------------------------------------------------------------------
int read_config(char *filename_config)
{
FILE *file_config;
size_t fileop_ret_val;
if ((file_config = fopen(filename_config, "r")) == NULL)
{
ret_val_ = ERR_CONFIG_FILE;
return ret_val_;
}
fileop_ret_val = fread(&config_, 1, sizeof(config_), file_config);
if (fileop_ret_val != sizeof(config_))
{
ret_val_ = ERR_CONFIG_FILE;
return ret_val_;
}
fclose(file_config);
return TRUE;
}
//------------------------------------------------------------------------------
void error_messages()
{
switch(ret_val_)
{
case ERR_CONFIG_FILE:
{
printf("error: cannot read configfile %s\n", filename_config);
}break;
case ERR_CMD_ARGS:
{
printf("usage: ex2 -c=configfile\n");
}break;
case ERR_MEM:
{
printf("error: out of memory\n");
}break;
case ERR_CORRUPT_CONFIG:
{
printf("error: configfile %s corrupt\n" , filename_config);
}break;
}
}
//------------------------------------------------------------------------------
int gatherdata(int poly, int image, int text)
{
return TRUE;
}
//------------------------------------------------------------------------------
int fGetStr(char *string, int max_characters, FILE * stream)
{
fgets(string, max_characters, stream);
if(string[strlen(string) - 1] == '\n') string[strlen(string) - 1] = 0;
return TRUE;
}
//------------------------------------------------------------------------------
|