Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Annotation of /trunk/src/profiles.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 367 - (hide annotations) (download) (as text)
Tue Apr 13 17:20:09 2010 UTC (14 years, 11 months ago) by amb
File MIME type: text/x-csrc
File size: 29098 byte(s)
Name the tag variables and functions after the XSD data type and not the tag
name that uses it.

1 amb 83 /***************************************
2 amb 367 $Header: /home/amb/CVS/routino/src/profiles.c,v 1.39 2010-04-13 17:20:03 amb Exp $
3 amb 83
4 amb 363 Load the profiles from a file and the functions for handling them.
5 amb 151
6     Part of the Routino routing software.
7 amb 83 ******************/ /******************
8 amb 320 This file Copyright 2008-2010 Andrew M. Bishop
9 amb 83
10 amb 151 This program is free software: you can redistribute it and/or modify
11     it under the terms of the GNU Affero General Public License as published by
12     the Free Software Foundation, either version 3 of the License, or
13     (at your option) any later version.
14    
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18     GNU Affero General Public License for more details.
19    
20     You should have received a copy of the GNU Affero General Public License
21     along with this program. If not, see <http://www.gnu.org/licenses/>.
22 amb 83 ***************************************/
23    
24    
25     #include <stdio.h>
26 amb 336 #include <string.h>
27 amb 341 #include <stdlib.h>
28 amb 83
29     #include "profiles.h"
30 amb 97 #include "types.h"
31 amb 83 #include "ways.h"
32 amb 341 #include "xmlparse.h"
33 amb 348 #include "functions.h"
34 amb 83
35    
36 amb 341 /*+ The profiles that have been loaded from file. +*/
37     static Profile **loaded_profiles=NULL;
38 amb 83
39 amb 341 /*+ The number of profiles that have been loaded from file. +*/
40     static int nloaded_profiles=0;
41 amb 83
42 amb 294
43 amb 341 /* The XML tag processing function prototypes */
44 amb 294
45 amb 367 //static int xmlDeclaration_function(int _type_,const char *version,const char *encoding);
46     //static int RoutinoProfilesType_function(int _type_);
47     static int profileType_function(int _type_,const char *name,const char *transport);
48     //static int restrictionsType_function(int _type_);
49     static int lengthType_function(int _type_,const char *limit);
50     static int widthType_function(int _type_,const char *limit);
51     static int heightType_function(int _type_,const char *limit);
52     static int weightType_function(int _type_,const char *limit);
53     //static int propertiesType_function(int _type_);
54     static int onewayType_function(int _type_,const char *obey);
55     static int propertyType_function(int _type_,const char *type,const char *percent);
56     //static int preferencesType_function(int _type_);
57     static int preferenceType_function(int _type_,const char *highway,const char *percent);
58     //static int speedsType_function(int _type_);
59     static int speedType_function(int _type_,const char *highway,const char *kph);
60 amb 314
61    
62 amb 341 /* The XML tag definitions */
63 amb 83
64 amb 341 /*+ The speedType type tag. +*/
65 amb 367 static xmltag speedType_tag=
66 amb 341 {"speed",
67 amb 355 2, {"highway","kph"},
68 amb 367 speedType_function,
69 amb 341 {NULL}};
70 amb 83
71 amb 341 /*+ The speedsType type tag. +*/
72 amb 367 static xmltag speedsType_tag=
73 amb 341 {"speeds",
74 amb 355 0, {NULL},
75 amb 341 NULL,
76 amb 367 {&speedType_tag,NULL}};
77 amb 296
78 amb 341 /*+ The preferenceType type tag. +*/
79 amb 367 static xmltag preferenceType_tag=
80 amb 341 {"preference",
81 amb 355 2, {"highway","percent"},
82 amb 367 preferenceType_function,
83 amb 341 {NULL}};
84 amb 296
85 amb 341 /*+ The preferencesType type tag. +*/
86 amb 367 static xmltag preferencesType_tag=
87 amb 341 {"preferences",
88 amb 355 0, {NULL},
89 amb 341 NULL,
90 amb 367 {&preferenceType_tag,NULL}};
91 amb 83
92 amb 341 /*+ The propertyType type tag. +*/
93 amb 367 static xmltag propertyType_tag=
94 amb 341 {"property",
95 amb 355 2, {"type","percent"},
96 amb 367 propertyType_function,
97 amb 341 {NULL}};
98 amb 83
99 amb 341 /*+ The onewayType type tag. +*/
100 amb 367 static xmltag onewayType_tag=
101 amb 341 {"oneway",
102 amb 355 1, {"obey"},
103 amb 367 onewayType_function,
104 amb 341 {NULL}};
105 amb 83
106 amb 341 /*+ The propertiesType type tag. +*/
107 amb 367 static xmltag propertiesType_tag=
108 amb 341 {"properties",
109 amb 355 0, {NULL},
110 amb 341 NULL,
111 amb 367 {&propertyType_tag,NULL}};
112 amb 83
113 amb 341 /*+ The weightType type tag. +*/
114 amb 367 static xmltag weightType_tag=
115 amb 341 {"weight",
116 amb 355 1, {"limit"},
117 amb 367 weightType_function,
118 amb 341 {NULL}};
119 amb 83
120 amb 341 /*+ The heightType type tag. +*/
121 amb 367 static xmltag heightType_tag=
122 amb 341 {"height",
123 amb 355 1, {"limit"},
124 amb 367 heightType_function,
125 amb 341 {NULL}};
126 amb 83
127 amb 341 /*+ The widthType type tag. +*/
128 amb 367 static xmltag widthType_tag=
129 amb 341 {"width",
130 amb 355 1, {"limit"},
131 amb 367 widthType_function,
132 amb 341 {NULL}};
133 amb 83
134 amb 341 /*+ The lengthType type tag. +*/
135 amb 367 static xmltag lengthType_tag=
136 amb 341 {"length",
137 amb 355 1, {"limit"},
138 amb 367 lengthType_function,
139 amb 341 {NULL}};
140 amb 83
141 amb 341 /*+ The restrictionsType type tag. +*/
142 amb 367 static xmltag restrictionsType_tag=
143 amb 341 {"restrictions",
144 amb 355 0, {NULL},
145 amb 341 NULL,
146 amb 367 {&onewayType_tag,&weightType_tag,&heightType_tag,&widthType_tag,&lengthType_tag,NULL}};
147 amb 83
148 amb 341 /*+ The profileType type tag. +*/
149 amb 367 static xmltag profileType_tag=
150 amb 341 {"profile",
151 amb 355 2, {"name","transport"},
152 amb 367 profileType_function,
153     {&speedsType_tag,&preferencesType_tag,&propertiesType_tag,&restrictionsType_tag,NULL}};
154 amb 83
155 amb 341 /*+ The RoutinoProfilesType type tag. +*/
156 amb 367 static xmltag RoutinoProfilesType_tag=
157 amb 341 {"routino-profiles",
158 amb 355 0, {NULL},
159 amb 341 NULL,
160 amb 367 {&profileType_tag,NULL}};
161 amb 83
162 amb 367 /*+ The xmlDeclaration type tag. +*/
163     static xmltag xmlDeclaration_tag=
164 amb 348 {"xml",
165 amb 355 2, {"version","encoding"},
166 amb 341 NULL,
167     {NULL}};
168    
169    
170     /*+ The complete set of tags at the top level. +*/
171 amb 367 static xmltag *xml_toplevel_tags[]={&xmlDeclaration_tag,&RoutinoProfilesType_tag,NULL};
172 amb 341
173    
174     /* The XML tag processing functions */
175    
176    
177 amb 83 /*++++++++++++++++++++++++++++++++++++++
178 amb 344 The function that is called when the speedType XSD type is seen
179 amb 341
180 amb 367 int speedType_function Returns 0 if no error occured or something else otherwise.
181 amb 363
182 amb 344 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
183    
184 amb 355 const char *highway The contents of the 'highway' attribute (or NULL if not defined).
185 amb 341
186 amb 355 const char *kph The contents of the 'kph' attribute (or NULL if not defined).
187 amb 341 ++++++++++++++++++++++++++++++++++++++*/
188    
189 amb 367 static int speedType_function(int _type_,const char *highway,const char *kph)
190 amb 341 {
191 amb 344 if(_type_&XMLPARSE_TAG_START)
192     {
193 amb 363 double speed;
194 amb 348 Highway highwaytype;
195 amb 341
196 amb 363 XMLPARSE_ASSERT_STRING("speed",highway);
197 amb 348
198     highwaytype=HighwayType(highway);
199    
200     if(highwaytype==Way_Count)
201 amb 363 XMLPARSE_INVALID("speed",highway);
202 amb 348
203 amb 363 XMLPARSE_ASSERT_FLOATING("speed",kph,speed);
204 amb 348
205 amb 363 loaded_profiles[nloaded_profiles-1]->speed[highwaytype]=kph_to_speed(speed);
206 amb 344 }
207 amb 363
208     return(0);
209 amb 341 }
210    
211    
212     /*++++++++++++++++++++++++++++++++++++++
213 amb 363 The function that is called when the speedsType XSD type is seen
214    
215 amb 367 int speedsType_function Returns 0 if no error occured or something else otherwise.
216 amb 363
217     int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
218     ++++++++++++++++++++++++++++++++++++++*/
219    
220 amb 367 //static int speedsType_function(int _type_)
221 amb 363 //{
222     // return(0);
223     //}
224    
225    
226     /*++++++++++++++++++++++++++++++++++++++
227 amb 344 The function that is called when the preferenceType XSD type is seen
228 amb 341
229 amb 367 int preferenceType_function Returns 0 if no error occured or something else otherwise.
230 amb 363
231 amb 344 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
232    
233 amb 355 const char *highway The contents of the 'highway' attribute (or NULL if not defined).
234 amb 341
235 amb 355 const char *percent The contents of the 'percent' attribute (or NULL if not defined).
236 amb 341 ++++++++++++++++++++++++++++++++++++++*/
237    
238 amb 367 static int preferenceType_function(int _type_,const char *highway,const char *percent)
239 amb 341 {
240 amb 344 if(_type_&XMLPARSE_TAG_START)
241     {
242 amb 348 Highway highwaytype;
243 amb 363 double p;
244 amb 341
245 amb 363 XMLPARSE_ASSERT_STRING("preference",highway);
246 amb 348
247     highwaytype=HighwayType(highway);
248    
249     if(highwaytype==Way_Count)
250 amb 363 XMLPARSE_INVALID("preference",highway);
251 amb 348
252 amb 363 XMLPARSE_ASSERT_FLOATING("preference",percent,p);
253 amb 348
254 amb 363 loaded_profiles[nloaded_profiles-1]->highway[highwaytype]=p;
255 amb 344 }
256 amb 363
257     return(0);
258 amb 341 }
259    
260    
261     /*++++++++++++++++++++++++++++++++++++++
262 amb 363 The function that is called when the preferencesType XSD type is seen
263    
264 amb 367 int preferencesType_function Returns 0 if no error occured or something else otherwise.
265 amb 363
266     int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
267     ++++++++++++++++++++++++++++++++++++++*/
268    
269 amb 367 //static int preferencesType_function(int _type_)
270 amb 363 //{
271     // return(0);
272     //}
273    
274    
275     /*++++++++++++++++++++++++++++++++++++++
276 amb 344 The function that is called when the propertyType XSD type is seen
277 amb 341
278 amb 367 int propertyType_function Returns 0 if no error occured or something else otherwise.
279 amb 363
280 amb 344 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
281    
282 amb 355 const char *type The contents of the 'type' attribute (or NULL if not defined).
283 amb 341
284 amb 355 const char *percent The contents of the 'percent' attribute (or NULL if not defined).
285 amb 341 ++++++++++++++++++++++++++++++++++++++*/
286    
287 amb 367 static int propertyType_function(int _type_,const char *type,const char *percent)
288 amb 341 {
289 amb 344 if(_type_&XMLPARSE_TAG_START)
290     {
291 amb 348 Property property;
292 amb 363 double p;
293 amb 341
294 amb 363 XMLPARSE_ASSERT_STRING("property",type);
295 amb 348
296     property=PropertyType(type);
297    
298     if(property==Property_Count)
299 amb 363 XMLPARSE_INVALID("property",type);
300 amb 348
301 amb 363 XMLPARSE_ASSERT_FLOATING("property",percent,p);
302 amb 348
303 amb 363 loaded_profiles[nloaded_profiles-1]->props_yes[property]=p;
304 amb 344 }
305 amb 363
306     return(0);
307 amb 341 }
308    
309    
310     /*++++++++++++++++++++++++++++++++++++++
311 amb 344 The function that is called when the onewayType XSD type is seen
312 amb 341
313 amb 367 int onewayType_function Returns 0 if no error occured or something else otherwise.
314 amb 363
315 amb 344 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
316    
317 amb 355 const char *obey The contents of the 'obey' attribute (or NULL if not defined).
318 amb 341 ++++++++++++++++++++++++++++++++++++++*/
319    
320 amb 367 static int onewayType_function(int _type_,const char *obey)
321 amb 341 {
322 amb 344 if(_type_&XMLPARSE_TAG_START)
323 amb 348 {
324 amb 363 int o;
325 amb 348
326 amb 363 XMLPARSE_ASSERT_INTEGER("oneway",obey,o);
327 amb 348
328 amb 363 loaded_profiles[nloaded_profiles-1]->oneway=!!o;
329 amb 348 }
330 amb 363
331     return(0);
332 amb 341 }
333    
334    
335     /*++++++++++++++++++++++++++++++++++++++
336 amb 363 The function that is called when the propertiesType XSD type is seen
337    
338 amb 367 int propertiesType_function Returns 0 if no error occured or something else otherwise.
339 amb 363
340     int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
341     ++++++++++++++++++++++++++++++++++++++*/
342    
343 amb 367 //static int propertiesType_function(int _type_)
344 amb 363 //{
345     // return(0);
346     //}
347    
348    
349     /*++++++++++++++++++++++++++++++++++++++
350 amb 344 The function that is called when the weightType XSD type is seen
351 amb 341
352 amb 367 int weightType_function Returns 0 if no error occured or something else otherwise.
353 amb 363
354 amb 344 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
355    
356 amb 355 const char *limit The contents of the 'limit' attribute (or NULL if not defined).
357 amb 341 ++++++++++++++++++++++++++++++++++++++*/
358    
359 amb 367 static int weightType_function(int _type_,const char *limit)
360 amb 341 {
361 amb 344 if(_type_&XMLPARSE_TAG_START)
362 amb 348 {
363 amb 363 double l;
364 amb 348
365 amb 363 XMLPARSE_ASSERT_FLOATING("weight",limit,l);
366 amb 348
367 amb 363 loaded_profiles[nloaded_profiles-1]->weight=tonnes_to_weight(l);
368 amb 348 }
369 amb 363
370     return(0);
371 amb 341 }
372    
373    
374     /*++++++++++++++++++++++++++++++++++++++
375 amb 344 The function that is called when the heightType XSD type is seen
376 amb 341
377 amb 367 int heightType_function Returns 0 if no error occured or something else otherwise.
378 amb 363
379 amb 344 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
380    
381 amb 355 const char *limit The contents of the 'limit' attribute (or NULL if not defined).
382 amb 341 ++++++++++++++++++++++++++++++++++++++*/
383    
384 amb 367 static int heightType_function(int _type_,const char *limit)
385 amb 341 {
386 amb 344 if(_type_&XMLPARSE_TAG_START)
387 amb 348 {
388 amb 363 double l;
389 amb 348
390 amb 363 XMLPARSE_ASSERT_FLOATING("height",limit,l);
391 amb 348
392 amb 363 loaded_profiles[nloaded_profiles-1]->height=metres_to_height(l);
393 amb 348 }
394 amb 363
395     return(0);
396 amb 341 }
397    
398    
399     /*++++++++++++++++++++++++++++++++++++++
400 amb 344 The function that is called when the widthType XSD type is seen
401 amb 341
402 amb 367 int widthType_function Returns 0 if no error occured or something else otherwise.
403 amb 363
404 amb 344 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
405    
406 amb 355 const char *limit The contents of the 'limit' attribute (or NULL if not defined).
407 amb 341 ++++++++++++++++++++++++++++++++++++++*/
408    
409 amb 367 static int widthType_function(int _type_,const char *limit)
410 amb 341 {
411 amb 344 if(_type_&XMLPARSE_TAG_START)
412 amb 348 {
413 amb 363 double l;
414 amb 348
415 amb 363 XMLPARSE_ASSERT_FLOATING("width",limit,l);
416 amb 348
417 amb 363 loaded_profiles[nloaded_profiles-1]->width=metres_to_width(l);
418 amb 348 }
419 amb 363
420     return(0);
421 amb 341 }
422    
423    
424     /*++++++++++++++++++++++++++++++++++++++
425 amb 344 The function that is called when the lengthType XSD type is seen
426 amb 341
427 amb 367 int lengthType_function Returns 0 if no error occured or something else otherwise.
428 amb 363
429 amb 344 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
430    
431 amb 355 const char *limit The contents of the 'limit' attribute (or NULL if not defined).
432 amb 341 ++++++++++++++++++++++++++++++++++++++*/
433    
434 amb 367 static int lengthType_function(int _type_,const char *limit)
435 amb 341 {
436 amb 344 if(_type_&XMLPARSE_TAG_START)
437 amb 348 {
438 amb 363 double l;
439 amb 348
440 amb 363 XMLPARSE_ASSERT_FLOATING("length",limit,l);
441 amb 348
442 amb 363 loaded_profiles[nloaded_profiles-1]->length=metres_to_length(l);
443 amb 348 }
444 amb 363
445     return(0);
446 amb 341 }
447    
448    
449     /*++++++++++++++++++++++++++++++++++++++
450 amb 363 The function that is called when the restrictionsType XSD type is seen
451    
452 amb 367 int restrictionsType_function Returns 0 if no error occured or something else otherwise.
453 amb 363
454     int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
455     ++++++++++++++++++++++++++++++++++++++*/
456    
457 amb 367 //static int restrictionsType_function(int _type_)
458 amb 363 //{
459     // return(0);
460     //}
461    
462    
463     /*++++++++++++++++++++++++++++++++++++++
464 amb 344 The function that is called when the profileType XSD type is seen
465 amb 341
466 amb 367 int profileType_function Returns 0 if no error occured or something else otherwise.
467 amb 363
468 amb 344 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
469    
470 amb 355 const char *name The contents of the 'name' attribute (or NULL if not defined).
471 amb 341
472 amb 355 const char *transport The contents of the 'transport' attribute (or NULL if not defined).
473 amb 341 ++++++++++++++++++++++++++++++++++++++*/
474    
475 amb 367 static int profileType_function(int _type_,const char *name,const char *transport)
476 amb 341 {
477 amb 344 if(_type_&XMLPARSE_TAG_START)
478     {
479 amb 348 Transport transporttype;
480     int i;
481    
482 amb 363 XMLPARSE_ASSERT_STRING("profile",name);
483     XMLPARSE_ASSERT_STRING("profile",transport);
484 amb 348
485     for(i=0;i<nloaded_profiles;i++)
486     if(!strcmp(name,loaded_profiles[i]->name))
487 amb 363 XMLPARSE_MESSAGE("profile","profile name must be unique");
488 amb 348
489     transporttype=TransportType(transport);
490    
491     if(transporttype==Transport_None)
492 amb 363 XMLPARSE_INVALID("profile",transport);
493 amb 348
494 amb 344 if((nloaded_profiles%16)==0)
495     loaded_profiles=(Profile**)realloc((void*)loaded_profiles,(nloaded_profiles+16)*sizeof(Profile*));
496 amb 341
497 amb 344 nloaded_profiles++;
498 amb 341
499 amb 344 loaded_profiles[nloaded_profiles-1]=(Profile*)calloc(1,sizeof(Profile));
500 amb 341
501 amb 344 loaded_profiles[nloaded_profiles-1]->name=strcpy(malloc(strlen(name)+1),name);
502 amb 348 loaded_profiles[nloaded_profiles-1]->transport=transporttype;
503 amb 344 }
504 amb 363
505     return(0);
506 amb 341 }
507    
508    
509     /*++++++++++++++++++++++++++++++++++++++
510 amb 363 The function that is called when the RoutinoProfilesType XSD type is seen
511    
512 amb 367 int RoutinoProfilesType_function Returns 0 if no error occured or something else otherwise.
513 amb 363
514     int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
515     ++++++++++++++++++++++++++++++++++++++*/
516    
517 amb 367 //static int RoutinoProfilesType_function(int _type_)
518 amb 363 //{
519     // return(0);
520     //}
521    
522    
523     /*++++++++++++++++++++++++++++++++++++++
524 amb 367 The function that is called when the XML declaration is seen
525 amb 363
526 amb 367 int xmlDeclaration_function Returns 0 if no error occured or something else otherwise.
527 amb 363
528     int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
529    
530     const char *version The contents of the 'version' attribute (or NULL if not defined).
531    
532     const char *encoding The contents of the 'encoding' attribute (or NULL if not defined).
533     ++++++++++++++++++++++++++++++++++++++*/
534    
535 amb 367 //static int xmlDeclaration_function(int _type_,const char *version,const char *encoding)
536 amb 363 //{
537     // return(0);
538     //}
539    
540    
541     /*++++++++++++++++++++++++++++++++++++++
542 amb 341 The XML profile parser.
543    
544     int ParseXMLProfiles Returns 0 if OK or something else in case of an error.
545    
546     const char *filename The name of the file to read.
547     ++++++++++++++++++++++++++++++++++++++*/
548    
549     int ParseXMLProfiles(const char *filename)
550     {
551 amb 348 int retval;
552    
553     if(!ExistsFile(filename))
554     {
555     fprintf(stderr,"Error: Specified profiles file '%s' does not exist.\n",filename);
556     return(1);
557     }
558    
559 amb 341 FILE *file=fopen(filename,"r");
560    
561     if(!file)
562 amb 348 {
563     fprintf(stderr,"Error: Cannot open profiles file '%s' for reading.\n",filename);
564 amb 341 return(1);
565 amb 348 }
566 amb 341
567 amb 366 retval=ParseXML(file,xml_toplevel_tags,XMLPARSE_UNKNOWN_ATTR_ERRNONAME);
568 amb 348
569 amb 341 fclose(file);
570    
571 amb 363 if(retval)
572 amb 348 {
573     int i;
574    
575     for(i=0;i<nloaded_profiles;i++)
576     free(loaded_profiles[i]);
577     free(loaded_profiles);
578    
579     nloaded_profiles=0;
580    
581     return(1);
582     }
583    
584 amb 341 return(0);
585     }
586    
587    
588     /*++++++++++++++++++++++++++++++++++++++
589 amb 83 Get the profile for a type of transport.
590    
591     Profile *GetProfile Returns a pointer to the profile.
592    
593 amb 341 const char *name The name of the profile.
594 amb 83 ++++++++++++++++++++++++++++++++++++++*/
595    
596 amb 341 Profile *GetProfile(const char *name)
597 amb 83 {
598 amb 341 int i;
599    
600     for(i=0;i<nloaded_profiles;i++)
601     if(!strcmp(loaded_profiles[i]->name,name))
602     return(loaded_profiles[i]);
603    
604     return(NULL);
605 amb 83 }
606    
607    
608     /*++++++++++++++++++++++++++++++++++++++
609 amb 166 Update a profile with highway preference scaling factor.
610    
611     Profile *profile The profile to be updated.
612     ++++++++++++++++++++++++++++++++++++++*/
613    
614     void UpdateProfile(Profile *profile)
615     {
616 amb 168 score_t hmax=0;
617 amb 166 int i;
618    
619 amb 341 profile->allow=ALLOWED(profile->transport);
620    
621 amb 173 /* Normalise the highway preferences into the range 0 -> 1 */
622    
623 amb 300 for(i=1;i<Way_Count;i++)
624 amb 298 {
625     if(profile->highway[i]<0)
626     profile->highway[i]=0;
627    
628 amb 166 if(profile->highway[i]>hmax)
629     hmax=profile->highway[i];
630 amb 298 }
631 amb 166
632 amb 300 for(i=1;i<Way_Count;i++)
633 amb 298 profile->highway[i]/=hmax;
634 amb 173
635 amb 298 /* Normalise the attribute preferences into the range 0 -> 1 */
636    
637     for(i=1;i<Property_Count;i++)
638     {
639     if(profile->props_yes[i]<0)
640     profile->props_yes[i]=0;
641    
642     if(profile->props_yes[i]>100)
643     profile->props_yes[i]=100;
644    
645     profile->props_yes[i]/=100;
646     profile->props_no [i] =1-profile->props_yes[i];
647     }
648    
649 amb 173 /* Find the fastest and most preferred highway type */
650    
651     profile->max_speed=0;
652    
653 amb 300 for(i=1;i<Way_Count;i++)
654 amb 173 if(profile->speed[i]>profile->max_speed)
655     profile->max_speed=profile->speed[i];
656    
657 amb 298 profile->max_pref=1; /* since highway prefs were normalised to 1 */
658 amb 173
659 amb 298 for(i=1;i<Property_Count;i++)
660     if(profile->props_yes[i]>profile->props_no[i])
661     profile->max_pref*=profile->props_yes[i];
662     else if(profile->props_no[i]>profile->props_yes[i])
663     profile->max_pref*=profile->props_no[i];
664 amb 166 }
665    
666    
667     /*++++++++++++++++++++++++++++++++++++++
668 amb 83 Print out a profile.
669    
670     const Profile *profile The profile to print.
671     ++++++++++++++++++++++++++++++++++++++*/
672    
673     void PrintProfile(const Profile *profile)
674     {
675 amb 214 unsigned int i;
676 amb 83
677     printf("Profile\n=======\n");
678    
679     printf("\n");
680    
681     printf("Transport: %s\n",TransportName(profile->transport));
682    
683     printf("\n");
684    
685 amb 300 for(i=1;i<Way_Count;i++)
686 amb 166 printf("Highway %-12s: %3d%%\n",HighwayName(i),(int)profile->highway[i]);
687 amb 83
688     printf("\n");
689    
690 amb 300 for(i=1;i<Way_Count;i++)
691 amb 166 if(profile->highway[i])
692 amb 85 printf("Speed on %-12s: %3d km/h / %2.0f mph\n",HighwayName(i),profile->speed[i],(double)profile->speed[i]/1.6);
693 amb 83
694     printf("\n");
695    
696 amb 298 for(i=1;i<Property_Count;i++)
697     printf("Highway property %-12s: %3d%%\n",PropertyName(i),(int)profile->props_yes[i]);
698    
699     printf("\n");
700    
701 amb 138 printf("Obey one-way : %s\n",profile->oneway?"yes":"no");
702     printf("Minimum weight: %.1f tonnes\n",weight_to_tonnes(profile->weight));
703     printf("Minimum height: %.1f metres\n",height_to_metres(profile->height));
704     printf("Minimum width : %.1f metres\n",width_to_metres(profile->width));
705     printf("Minimum length: %.1f metres\n",length_to_metres(profile->length));
706 amb 83 }
707 amb 129
708    
709     /*++++++++++++++++++++++++++++++++++++++
710 amb 336 Print out the profiles as XML for use as program input.
711     ++++++++++++++++++++++++++++++++++++++*/
712    
713     void PrintProfilesXML(void)
714     {
715     unsigned int i,j;
716     char *padding=" ";
717    
718     printf("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
719     printf("\n");
720    
721     printf("<routino-profiles xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"routino-profiles.xsd\">\n");
722     printf("\n");
723    
724 amb 341 for(j=0;j<nloaded_profiles;j++)
725 amb 336 {
726 amb 341 printf(" <profile name=\"%s\" transport=\"%s\">\n",loaded_profiles[j]->name,TransportName(loaded_profiles[j]->transport));
727 amb 336
728     printf(" <speeds>\n");
729     for(i=1;i<Way_Count;i++)
730 amb 341 printf(" <speed highway=\"%s\"%s kph=\"%d\" />\n",HighwayName(i),padding+3+strlen(HighwayName(i)),loaded_profiles[j]->speed[i]);
731 amb 336 printf(" </speeds>\n");
732    
733     printf(" <preferences>\n");
734     for(i=1;i<Way_Count;i++)
735 amb 341 printf(" <preference highway=\"%s\"%s percent=\"%.0f\" />\n",HighwayName(i),padding+3+strlen(HighwayName(i)),loaded_profiles[j]->highway[i]);
736 amb 336 printf(" </preferences>\n");
737    
738     printf(" <properties>\n");
739     for(i=1;i<Property_Count;i++)
740 amb 341 printf(" <property type=\"%s\"%s percent=\"%.0f\" />\n",PropertyName(i),padding+6+strlen(PropertyName(i)),loaded_profiles[j]->props_yes[i]);
741 amb 336 printf(" </properties>\n");
742    
743     printf(" <restrictions>\n");
744 amb 341 printf(" <oneway obey=\"%d\" /> \n",loaded_profiles[j]->oneway);
745     printf(" <weight limit=\"%.1f\" />\n",weight_to_tonnes(loaded_profiles[j]->weight));
746     printf(" <height limit=\"%.1f\" />\n",height_to_metres(loaded_profiles[j]->height));
747     printf(" <width limit=\"%.1f\" />\n",width_to_metres(loaded_profiles[j]->width));
748     printf(" <length limit=\"%.1f\" />\n",length_to_metres(loaded_profiles[j]->length));
749 amb 336 printf(" </restrictions>\n");
750    
751     printf(" </profile>\n");
752     printf("\n");
753     }
754    
755     printf("</routino-profiles>\n");
756     }
757    
758    
759     /*++++++++++++++++++++++++++++++++++++++
760 amb 343 Print out the profiles as JavaScript Object Notation for use in a web form.
761 amb 129 ++++++++++++++++++++++++++++++++++++++*/
762    
763 amb 343 void PrintProfilesJSON(void)
764 amb 129 {
765 amb 214 unsigned int i,j;
766 amb 129
767 amb 343 printf("var routino={ // contains all default Routino options (generated using \"--help-profile-json\").\n");
768 amb 336 printf("\n");
769 amb 320
770     printf(" // Default transport type\n");
771     printf(" transport: 'motorcar',\n");
772     printf("\n");
773    
774     printf(" // Transport types\n");
775     printf(" transports: {");
776 amb 341 for(j=0;j<nloaded_profiles;j++)
777     printf("%s%s: %d",j==1?"":", ",TransportName(loaded_profiles[j]->transport),j);
778 amb 320 printf("},\n");
779 amb 129 printf("\n");
780    
781 amb 320 printf(" // Highway types\n");
782     printf(" highways: {");
783 amb 300 for(i=1;i<Way_Count;i++)
784 amb 292 printf("%s%s: %d",i==1?"":", ",HighwayName(i),i);
785 amb 320 printf("},\n");
786 amb 129 printf("\n");
787    
788 amb 320 printf(" // Property types\n");
789     printf(" properties: {");
790 amb 298 for(i=1;i<Property_Count;i++)
791     printf("%s%s: %d",i==1?"":", ",PropertyName(i),i);
792 amb 320 printf("},\n");
793 amb 298 printf("\n");
794    
795 amb 320 printf(" // Restriction types\n");
796     printf(" restrictions: {oneway: 1, weight: 2, height: 3, width: 4, length: 5},\n");
797 amb 138 printf("\n");
798    
799 amb 320 printf(" // Allowed highways\n");
800     printf(" profile_highway: {\n");
801 amb 300 for(i=1;i<Way_Count;i++)
802 amb 129 {
803 amb 320 printf(" %12s: {",HighwayName(i));
804 amb 341 for(j=0;j<nloaded_profiles;j++)
805     printf("%s%s: %3d",j==1?"":", ",TransportName(loaded_profiles[j]->transport),(int)loaded_profiles[j]->highway[i]);
806 amb 300 printf("}%s\n",i==(Way_Count-1)?"":",");
807 amb 129 }
808 amb 320 printf(" },\n");
809 amb 129 printf("\n");
810    
811 amb 320 printf(" // Speed limits\n");
812     printf(" profile_speed: {\n");
813 amb 300 for(i=1;i<Way_Count;i++)
814 amb 129 {
815 amb 320 printf(" %12s: {",HighwayName(i));
816 amb 341 for(j=0;j<nloaded_profiles;j++)
817     printf("%s%s: %3d",j==1?"":", ",TransportName(loaded_profiles[j]->transport),loaded_profiles[j]->speed[i]);
818 amb 300 printf("}%s\n",i==(Way_Count-1)?"":",");
819 amb 129 }
820 amb 320 printf(" },\n");
821 amb 129 printf("\n");
822    
823 amb 320 printf(" // Highway properties\n");
824     printf(" profile_property: {\n");
825 amb 298 for(i=1;i<Property_Count;i++)
826     {
827 amb 320 printf(" %12s: {",PropertyName(i));
828 amb 341 for(j=0;j<nloaded_profiles;j++)
829     printf("%s%s: %3d",j==1?"":", ",TransportName(loaded_profiles[j]->transport),(int)loaded_profiles[j]->props_yes[i]);
830 amb 298 printf("}%s\n",i==(Property_Count-1)?"":",");
831     }
832 amb 320 printf(" },\n");
833 amb 298 printf("\n");
834    
835 amb 320 printf(" // Restrictions\n");
836     printf(" profile_restrictions: {\n");
837     printf(" %12s: {","oneway");
838 amb 341 for(j=0;j<nloaded_profiles;j++)
839     printf("%s%s: %4d",j==1?"":", ",TransportName(loaded_profiles[j]->transport),loaded_profiles[j]->oneway);
840 amb 138 printf("},\n");
841 amb 320 printf(" %12s: {","weight");
842 amb 341 for(j=0;j<nloaded_profiles;j++)
843     printf("%s%s: %4.1f",j==1?"":", ",TransportName(loaded_profiles[j]->transport),weight_to_tonnes(loaded_profiles[j]->weight));
844 amb 138 printf("},\n");
845 amb 320 printf(" %12s: {","height");
846 amb 341 for(j=0;j<nloaded_profiles;j++)
847     printf("%s%s: %4.1f",j==1?"":", ",TransportName(loaded_profiles[j]->transport),height_to_metres(loaded_profiles[j]->height));
848 amb 138 printf("},\n");
849 amb 320 printf(" %12s: {","width");
850 amb 341 for(j=0;j<nloaded_profiles;j++)
851     printf("%s%s: %4.1f",j==1?"":", ",TransportName(loaded_profiles[j]->transport),width_to_metres(loaded_profiles[j]->width));
852 amb 138 printf("},\n");
853 amb 320 printf(" %12s: {","length");
854 amb 341 for(j=0;j<nloaded_profiles;j++)
855     printf("%s%s: %4.1f",j==1?"":", ",TransportName(loaded_profiles[j]->transport),length_to_metres(loaded_profiles[j]->length));
856 amb 138 printf("}\n");
857 amb 320 printf(" }\n");
858 amb 129 printf("\n");
859 amb 320
860     printf("}; // end of routino variable\n");
861 amb 129 }
862 amb 145
863    
864     /*++++++++++++++++++++++++++++++++++++++
865     Print out the profiles as Perl for use in a web CGI.
866     ++++++++++++++++++++++++++++++++++++++*/
867    
868     void PrintProfilesPerl(void)
869     {
870 amb 214 unsigned int i,j;
871 amb 145
872 amb 343 printf("$routino={ # contains all default Routino options (generated using \"--help-profile-perl\").\n");
873 amb 336 printf("\n");
874 amb 320
875     printf(" # Default transport type\n");
876     printf(" transport => 'motorcar',\n");
877     printf("\n");
878    
879     printf(" # Transport types\n");
880     printf(" transports => {");
881 amb 341 for(j=0;j<nloaded_profiles;j++)
882     printf("%s%s => %d",j==1?"":", ",TransportName(loaded_profiles[j]->transport),j);
883 amb 320 printf("},\n");
884 amb 145 printf("\n");
885    
886 amb 320 printf(" # Highway types\n");
887     printf(" highways => {");
888 amb 300 for(i=1;i<Way_Count;i++)
889 amb 320 printf("%s%s => %d",i==1?"":", ",HighwayName(i),i);
890     printf("},\n");
891 amb 145 printf("\n");
892    
893 amb 320 printf(" # Property types\n");
894     printf(" properties => {");
895 amb 298 for(i=1;i<Property_Count;i++)
896 amb 320 printf("%s%s => %d",i==1?"":", ",PropertyName(i),i);
897     printf("},\n");
898 amb 298 printf("\n");
899    
900 amb 320 printf(" # Restriction types\n");
901     printf(" restrictions => {oneway => 1, weight => 2, height => 3, width => 4, length => 5},\n");
902 amb 145 printf("\n");
903    
904 amb 320 printf(" # Allowed highways\n");
905     printf(" profile_highway => {\n");
906 amb 300 for(i=1;i<Way_Count;i++)
907 amb 145 {
908 amb 148 printf(" %12s => {",HighwayName(i));
909 amb 341 for(j=0;j<nloaded_profiles;j++)
910     printf("%s %s => %3d",j==1?"":", ",TransportName(loaded_profiles[j]->transport),(int)loaded_profiles[j]->highway[i]);
911 amb 300 printf("}%s\n",i==(Way_Count-1)?"":",");
912 amb 145 }
913 amb 320 printf(" },\n");
914 amb 145 printf("\n");
915    
916 amb 320 printf(" # Speed limits\n");
917     printf(" profile_speed => {\n");
918 amb 300 for(i=1;i<Way_Count;i++)
919 amb 145 {
920 amb 148 printf(" %12s => {",HighwayName(i));
921 amb 341 for(j=0;j<nloaded_profiles;j++)
922     printf("%s %s => %3d",j==1?"":", ",TransportName(loaded_profiles[j]->transport),loaded_profiles[j]->speed[i]);
923 amb 300 printf("}%s\n",i==(Way_Count-1)?"":",");
924 amb 145 }
925 amb 320 printf(" },\n");
926 amb 145 printf("\n");
927    
928 amb 320 printf(" # Highway properties\n");
929     printf(" profile_property => {\n");
930 amb 298 for(i=1;i<Property_Count;i++)
931     {
932     printf(" %12s => {",PropertyName(i));
933 amb 341 for(j=0;j<nloaded_profiles;j++)
934     printf("%s %s => %3d",j==1?"":", ",TransportName(loaded_profiles[j]->transport),(int)loaded_profiles[j]->props_yes[i]);
935 amb 298 printf("}%s\n",i==(Property_Count-1)?"":",");
936     }
937 amb 320 printf(" },\n");
938 amb 298 printf("\n");
939    
940 amb 320 printf(" # Restrictions\n");
941     printf(" profile_restrictions => {\n");
942     printf(" %12s => {","oneway");
943 amb 341 for(j=0;j<nloaded_profiles;j++)
944     printf("%s %s => %4d",j==1?"":", ",TransportName(loaded_profiles[j]->transport),loaded_profiles[j]->oneway);
945 amb 145 printf("},\n");
946 amb 320 printf(" %12s => {","weight");
947 amb 341 for(j=0;j<nloaded_profiles;j++)
948     printf("%s %s => %4.1f",j==1?"":", ",TransportName(loaded_profiles[j]->transport),weight_to_tonnes(loaded_profiles[j]->weight));
949 amb 145 printf("},\n");
950 amb 320 printf(" %12s => {","height");
951 amb 341 for(j=0;j<nloaded_profiles;j++)
952     printf("%s %s => %4.1f",j==1?"":", ",TransportName(loaded_profiles[j]->transport),height_to_metres(loaded_profiles[j]->height));
953 amb 145 printf("},\n");
954 amb 320 printf(" %12s => {","width");
955 amb 341 for(j=0;j<nloaded_profiles;j++)
956     printf("%s %s => %4.1f",j==1?"":", ",TransportName(loaded_profiles[j]->transport),width_to_metres(loaded_profiles[j]->width));
957 amb 145 printf("},\n");
958 amb 320 printf(" %12s => {","length");
959 amb 341 for(j=0;j<nloaded_profiles;j++)
960     printf("%s %s => %4.1f",j==1?"":", ",TransportName(loaded_profiles[j]->transport),length_to_metres(loaded_profiles[j]->length));
961 amb 145 printf("}\n");
962 amb 320 printf(" },\n");
963 amb 145 printf("\n");
964 amb 320
965     printf("}; # end of routino variable\n");
966 amb 145 }

Properties

Name Value
cvs:description Definition of built-in profiles and other functions.