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/waysx.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 151 - (hide annotations) (download) (as text)
Wed Apr 8 16:54:34 2009 UTC (15 years, 11 months ago) by amb
File MIME type: text/x-csrc
File size: 6036 byte(s)
Changed the license to Affero GPLv3.

1 amb 110 /***************************************
2 amb 151 $Header: /home/amb/CVS/routino/src/waysx.c,v 1.4 2009-04-08 16:54:34 amb Exp $
3 amb 110
4     Extended Way data type functions.
5 amb 151
6     Part of the Routino routing software.
7 amb 110 ******************/ /******************
8 amb 151 This file Copyright 2008,2009 Andrew M. Bishop
9 amb 110
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 110 ***************************************/
23    
24    
25     #include <assert.h>
26     #include <stdlib.h>
27     #include <string.h>
28    
29     #include "functions.h"
30     #include "waysx.h"
31    
32    
33     /* Constants */
34    
35     /*+ The array size increment for ways - expect ~1,000,000 ways. +*/
36     #define INCREMENT_WAYS 256*1024
37    
38    
39     /* Functions */
40    
41     static int sort_by_name(WayX **a,WayX **b);
42    
43    
44     /*++++++++++++++++++++++++++++++++++++++
45     Allocate a new way list.
46    
47     WaysX *NewWayList Returns the way list.
48     ++++++++++++++++++++++++++++++++++++++*/
49    
50     WaysX *NewWayList(void)
51     {
52     WaysX *waysx;
53    
54     waysx=(WaysX*)malloc(sizeof(WaysX));
55    
56     waysx->sorted=0;
57     waysx->alloced=INCREMENT_WAYS;
58     waysx->number=0;
59     waysx->length=0;
60    
61     waysx->idata=(WayX*)malloc(waysx->alloced*sizeof(WayX));
62     waysx->ndata=NULL;
63    
64     return(waysx);
65     }
66    
67    
68     /*++++++++++++++++++++++++++++++++++++++
69     Save the way list to a file.
70    
71     WaysX* waysx The set of ways to save.
72    
73     const char *filename The name of the file to save.
74     ++++++++++++++++++++++++++++++++++++++*/
75    
76     void SaveWayList(WaysX* waysx,const char *filename)
77     {
78     int i;
79     int fd;
80     Ways *ways=calloc(1,sizeof(Ways));
81    
82     assert(waysx->sorted); /* Must be sorted */
83    
84     /* Fill in a Ways structure with the offset of the real data in the file after
85     the Way structure itself. */
86    
87     ways->number=waysx->number;
88     ways->data=NULL;
89     ways->ways=(void*)sizeof(Ways);
90     ways->names=(void*)sizeof(Ways)+ways->number*sizeof(Way);
91    
92     /* Write out the Ways structure and then the real data. */
93    
94     fd=OpenFile(filename);
95    
96     WriteFile(fd,ways,sizeof(Ways));
97    
98     for(i=0;i<waysx->number;i++)
99 amb 132 {
100 amb 110 WriteFile(fd,&waysx->idata[i].way,sizeof(Way));
101    
102 amb 132 if(!((i+1)%10000))
103     {
104     printf("\rWriting Ways: Ways=%d",i+1);
105     fflush(stdout);
106     }
107     }
108    
109     printf("\rWrote Ways: Ways=%d \n",waysx->number);
110     fflush(stdout);
111    
112 amb 110 WriteFile(fd,waysx->names,waysx->length);
113    
114     CloseFile(fd);
115    
116     /* Free the fake Ways */
117    
118     free(ways);
119     }
120    
121    
122     /*++++++++++++++++++++++++++++++++++++++
123     Append a way to a way list.
124    
125     Way *AppendWay Returns the newly appended way.
126    
127     WaysX* waysx The set of ways to process.
128    
129     const char *name The name or reference of the way.
130     ++++++++++++++++++++++++++++++++++++++*/
131    
132     Way *AppendWay(WaysX* waysx,const char *name)
133     {
134     assert(!waysx->sorted); /* Must not be sorted */
135    
136     waysx->sorted=0;
137    
138     /* Check that the array has enough space. */
139    
140     if(waysx->number==waysx->alloced)
141     {
142     waysx->alloced+=INCREMENT_WAYS;
143    
144     waysx->idata=(WayX*)realloc((void*)waysx->idata,waysx->alloced*sizeof(WayX));
145     }
146    
147     /* Insert the way */
148    
149     waysx->idata[waysx->number].name=strcpy((char*)malloc(strlen(name)+1),name);
150    
151     memset(&waysx->idata[waysx->number].way,0,sizeof(Way));
152    
153     waysx->number++;
154    
155     return(&waysx->idata[waysx->number-1].way);
156     }
157    
158    
159     /*++++++++++++++++++++++++++++++++++++++
160     Sort the list of ways and fix the names.
161    
162     WaysX* waysx The set of ways to process.
163     ++++++++++++++++++++++++++++++++++++++*/
164    
165     void SortWayList(WaysX* waysx)
166     {
167     int i;
168    
169     assert(!waysx->sorted); /* Must not be sorted */
170    
171     waysx->sorted=1;
172    
173 amb 132 printf("Sorting Ways"); fflush(stdout);
174    
175 amb 110 /* Sort the ways by name */
176    
177     waysx->ndata=malloc(waysx->number*sizeof(WayX*));
178    
179     for(i=0;i<waysx->number;i++)
180     waysx->ndata[i]=&waysx->idata[i];
181    
182     qsort(waysx->ndata,waysx->number,sizeof(WayX*),(int (*)(const void*,const void*))sort_by_name);
183    
184     /* Allocate the new data */
185    
186     waysx->names=(char*)malloc(waysx->alloced*sizeof(char));
187    
188     /* Setup the offsets for the names in the way array */
189    
190     for(i=0;i<waysx->number;i++)
191     {
192     if(i && !strcmp(waysx->ndata[i]->name,waysx->ndata[i-1]->name)) /* Same name */
193     waysx->ndata[i]->way.name=waysx->ndata[i-1]->way.name;
194     else /* Different name */
195     {
196     if((waysx->length+strlen(waysx->ndata[i]->name)+1)>=waysx->alloced)
197     {
198     waysx->alloced+=INCREMENT_WAYS;
199    
200     waysx->names=(char*)realloc((void*)waysx->names,waysx->alloced*sizeof(char));
201     }
202    
203     strcpy(&waysx->names[waysx->length],waysx->ndata[i]->name);
204    
205     waysx->ndata[i]->way.name=waysx->length;
206    
207     waysx->length+=strlen(waysx->ndata[i]->name)+1;
208     }
209     }
210 amb 132
211     printf("\rSorted Ways \n"); fflush(stdout);
212 amb 110 }
213    
214    
215     /*++++++++++++++++++++++++++++++++++++++
216     Sort the ways into name order.
217    
218     int sort_by_name Returns the comparison of the name fields.
219    
220     Way **a The first Way.
221    
222     Way **b The second Way.
223     ++++++++++++++++++++++++++++++++++++++*/
224    
225     static int sort_by_name(WayX **a,WayX **b)
226     {
227     char *a_name=(*a)->name;
228     char *b_name=(*b)->name;
229    
230     return(strcmp(a_name,b_name));
231     }
232 amb 134
233    
234     /*++++++++++++++++++++++++++++++++++++++
235     Return 1 if the two ways are the same (in respect of their types and limits).
236    
237     int WaysSame Returns a comparison.
238    
239     Way *way1 The first way.
240    
241     Way *way2 The second way.
242     ++++++++++++++++++++++++++++++++++++++*/
243    
244     int WaysSame(Way *way1,Way *way2)
245     {
246     if(way1->type ==way2->type &&
247     way1->allow ==way2->allow &&
248     way1->speed ==way2->speed &&
249     way1->weight==way2->weight &&
250     way1->height==way2->height &&
251     way1->width ==way2->width &&
252     way1->length==way2->length)
253     return(1);
254     else
255     return(0);
256     }

Properties

Name Value
cvs:description Extended ways functions.