Merge branch 'upstream'
[routino] / src / ways.c
1 /***************************************
2  $Header: /home/amb/routino/src/RCS/ways.c,v 1.46 2010/07/24 10:09:07 amb Exp $
3
4  Way data type functions.
5
6  Part of the Routino routing software.
7  ******************/ /******************
8  This file Copyright 2008-2010 Andrew M. Bishop
9
10  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  ***************************************/
23
24
25 #include <stdlib.h>
26
27 #include "ways.h"
28
29 #include "files.h"
30
31
32 /*++++++++++++++++++++++++++++++++++++++
33   Load in a way list from a file.
34
35   Ways* LoadWayList Returns the way list.
36
37   const char *filename The name of the file to load.
38   ++++++++++++++++++++++++++++++++++++++*/
39
40 Ways *LoadWayList(const char *filename)
41 {
42  Ways *ways;
43
44  ways=(Ways*)malloc(sizeof(Ways));
45
46 #if !SLIM
47
48  ways->data=MapFile(filename);
49
50  /* Copy the WaysFile structure from the loaded data */
51
52  ways->file=*((WaysFile*)ways->data);
53
54  /* Set the pointers in the Ways structure. */
55
56  ways->ways =(Way *)(ways->data+sizeof(WaysFile));
57  ways->names=(char*)(ways->data+sizeof(WaysFile)+ways->file.number*sizeof(Way));
58
59 #else
60
61  ways->fd=ReOpenFile(filename);
62
63  /* Copy the WaysFile header structure from the loaded data */
64
65  ReadFile(ways->fd,&ways->file,sizeof(WaysFile));
66
67  ways->namesoffset=sizeof(WaysFile)+ways->file.number*sizeof(Way);
68
69  ways->nincache=~0;
70  ways->ncached=NULL;
71
72 #endif
73
74  return(ways);
75 }
76
77
78 /*++++++++++++++++++++++++++++++++++++++
79   Return 0 if the two ways are the same (in respect of their types and limits),
80            otherwise return positive or negative to allow sorting.
81
82   int WaysCompare Returns a comparison.
83
84   Way *way1 The first way.
85
86   Way *way2 The second way.
87   ++++++++++++++++++++++++++++++++++++++*/
88
89 int WaysCompare(Way *way1,Way *way2)
90 {
91  if(way1==way2)
92     return(0);
93
94  if(way1->type!=way2->type)
95     return((int)way1->type - (int)way2->type);
96
97  if(way1->allow!=way2->allow)
98     return((int)way1->allow - (int)way2->allow);
99
100  if(way1->props!=way2->props)
101     return((int)way1->props - (int)way2->props);
102
103  if(way1->speed!=way2->speed)
104     return((int)way1->speed - (int)way2->speed);
105
106  if(way1->weight!=way2->weight)
107     return((int)way1->weight - (int)way2->weight);
108
109  if(way1->height!=way2->height)
110     return((int)way1->height - (int)way2->height);
111
112  if(way1->width!=way2->width)
113     return((int)way1->width - (int)way2->width);
114
115  if(way1->length!=way2->length)
116     return((int)way1->length - (int)way2->length);
117
118  return(0);
119 }